当前位置:网站首页>ESP8266使用arduino连接阿里云物联网
ESP8266使用arduino连接阿里云物联网
2022-07-06 09:16:00 【一入极客深似海】
这里使用 AliyunIoTSDK库进行快速接入
需要的进行前期准备
安装
库文件【AliyunIoTSDK】
-【ArduinoJson】
-【Crypto】
-【PubSubClient】 这个是mqtt的支持包 图片中所展示的1和2都可以,任选其一。我这里因为一些其他的项目就都安装了。不影响后面的开发
阿里云设备创建 (图片讲解太麻烦了,直接上视频吧)视频链接
// 引入 wifi 模块,并实例化,
#include <ESP8266WiFi.h>
static WiFiClient espClient;
// 引入阿里云 IoT SDK
#include <AliyunIoTSDK.h>
// 设置产品和设备的信息,从阿里云设备信息里查看
#define PRODUCT_KEY "a1rCF0cxG0w"
#define DEVICE_NAME "ceshi"
#define DEVICE_SECRET "32f735fc37b68a86b6eed749f1ba2bf8"
#define REGION_ID "cn-shanghai"
// 设置 wifi 信息
#define WIFI_SSID "xxxx"
#define WIFI_PASSWD "12345678"
#define led D4
void setup()
{
Serial.begin(115200);
// 初始化 wifi
wifiInit(WIFI_SSID, WIFI_PASSWD);
// 初始化 iot,需传入 wifi 的 client,和设备产品信息
AliyunIoTSDK::begin(espClient, PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET, REGION_ID);
// 绑定一个设备属性回调,当远程修改此属性,会触发 powerCallback
// PowerSwitch 是在设备产品中定义的物联网模型的 id
AliyunIoTSDK::bindData("PowerSwitch_1", powerCallback);
}
void loop()
{
AliyunIoTSDK::send("CurrentTemperature", ((float)random(1000,9999))/100);
AliyunIoTSDK::send("CurrentHumidity", (int)random(0,100));
AliyunIoTSDK::send("PowerSwitch_1", !(int)digitalRead(led));
delay(2000);
AliyunIoTSDK::loop();
}
// 初始化 wifi 连接
void wifiInit(const char *ssid, const char *passphrase)
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, passphrase);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("WiFi not Connect");
}
Serial.println("Connected to AP");
pinMode(led,OUTPUT);
}
// 电源属性修改的回调函数
void powerCallback(JsonVariant p)
{
int PowerSwitch = p["PowerSwitch_1"];
if (PowerSwitch == 1)
{
digitalWrite(led,0);
}
if (PowerSwitch == 0)
{
digitalWrite(led,1);
}
}```
边栏推荐
- C语言函数之可变参数原理:va_start、va_arg及va_end
- PyTorch四种常用优化器测试
- FTP file upload file implementation, regularly scan folders to upload files in the specified format to the server, C language to realize FTP file upload details and code case implementation
- Gallery之图片浏览、组件学习
- 【flink】flink学习
- 数据分析之缺失值填充(重点讲解多重插值法Miceforest)
- Small L's test paper
- Dependency in dependencymanagement cannot be downloaded and red is reported
- TypeScript
- Linux Yum install MySQL
猜你喜欢
Vs2019 desktop app quick start
C语言回调函数【C语言】
Mysql的索引实现之B树和B+树
Pytoch Foundation
R & D thinking 01 ----- classic of embedded intelligent product development process
Machine learning -- decision tree (sklearn)
Stage 4 MySQL database
uCOS-III 的特点、任务状态、启动
Principle and implementation of MySQL master-slave replication
5G工作原理详解(解释&图解)
随机推荐
[Bluebridge cup 2020 preliminary] horizontal segmentation
[BSidesCF_2020]Had_a_bad_day
【flink】flink学习
Machine learning -- linear regression (sklearn)
MongoDB
{一周总结}带你走进js知识的海洋
E-commerce data analysis -- User Behavior Analysis
Funny cartoon: Programmer's logic
Implementation scheme of distributed transaction
RT-Thread 线程的时间片轮询调度
搞笑漫画:程序员的逻辑
Matlab learning and actual combat notes
Codeforces Round #753 (Div. 3)
JS array + array method reconstruction
【Flink】CDH/CDP Flink on Yarn 日志配置
Password free login of distributed nodes
Linux yum安装MySQL
互聯網協議詳解
[CDH] cdh5.16 configuring the setting of yarn task centralized allocation does not take effect
Using LinkedHashMap to realize the caching of an LRU algorithm