赞
踩
目录
2、为esp8266NodeMcu搭建Arduino开发环境
上一篇文章微信小程序+esp8266NodeMcu(cp2102)+onenet物联平台(一)介绍了onenet平台注册及设备连接,接下来介绍,如何使用Arduino IDE编写代码,控制esp8266,使用MQTT协议与onenet平台进行数据交互。关于MQTT协议,可参考太极创客的教程,非常详细。零基础入门学用物联网 – MQTT基础篇 – 目录 – 太极创客
下载地址:CP210x USB 至 UART 桥 VCP 驱动器 - 芯科科技
解压缩后安装驱动,根据自己电脑系统选择安装文件,我安装的是64位驱动。
安装后,使用usb数据线连接8266,然后打开设备管理器,查看端口(我的端口为com5)。
Arduino IDE下载地址:国外官网 或者 太极创客百度网盘 请输入提取码0ig4
打开“文件”-“首选项”
在Arduino IDE的“首选项”对话框中找到“附加开发板管理网址”,复制网址:
http://arduino.esp8266.com/stable/package_esp8266com_index.json |
然后点击“好”
打开Arduino IDE的“开发板管理器”
安装时间比较长,也可以取消安装,然后去网盘下载: https://pan.baidu.com/s/1b0vs8SG5pCAji7MrsE3Aiw?pwd=ag4s 提取码: ag4s
安装完毕后,在Arduino IDE的开发板菜单中选中“NodeMCU开发板”
设置Arduino IDE的NodeMCU开发板端口
PubSubClient库是一个MQTT库,进行主题的发布与订阅,
打开Arduino,按下图安装,或前往1-6 ESP8266发布MQTT消息 – 太极创客下载。
15年前学过c语言,不过,早都忘光了,调试过程中废了很大劲,勉强可以用。
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <Ticker.h>
-
- // 设置wifi接入信息(请根据您的WiFi信息进行修改)
- const char* ssid = "wifi名称";//wifi名称
- const char* password = "wifi密码";//wifi密码
- //MQTT连接设置
- const char* mqttServer = "mqtts.heclouds.com";//onenet 的 域名
- //const char *mqtt_server = "183.230.40.96"; //onenet 的 IP地址
- const int mqtt_port = 1883;//onenet 的 端口
- #define mqtt_devName "d1"//设备名
- #define mqtt_pubid "558037"//产品ID
- //鉴权信息,即onenet生成的设备连接token
- #define mqtt_authorization "version=2018-10-31&res=products%2F558....."
-
- WiFiClient wifiClient;//创建一个WIFI连接客户端
- PubSubClient mqttClient(wifiClient);// 创建一个PubSub客户端, 传入创建的WIFI客户端
- Ticker tim1; //定时器,用来循环上传数据
-
- void setup() {
- // put your setup code here, to run once:
- pinMode(LED_BUILTIN, OUTPUT); // 设置板上LED引脚为输出模式
- digitalWrite(LED_BUILTIN, HIGH); // 启动后关闭板上LED
- Serial.begin(9600);
-
- //设置ESP8266工作模式为无线终端模式
- WiFi.mode(WIFI_STA);
-
- // 连接WiFi
- connectWifi();
-
- // 设置MQTT服务器和端口号
- mqttClient.setServer(mqttServer, mqtt_port);
- // 设置MQTT订阅回调函数
- mqttClient.setCallback(receiveCallback);
- // 连接MQTT服务器
- connectMQTTServer();
- //定时 每隔10秒发送一次温度数据点
- tim1.attach(10, sendDpData);
- }
-
- void loop() {
- // put your main code here, to run repeatedly:
- if (mqttClient.connected()) { // 如果开发板成功连接服务器
- // 保持心跳
- mqttClient.loop();
- } else { // 如果开发板未能成功连接服务器
- connectMQTTServer(); // 则尝试连接服务器
- }
- }
连接wifi
- // ESP8266连接wifi
- void connectWifi() {
- WiFi.begin(ssid, password);
- //等待WiFi连接,成功连接后输出成功信息
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi Connected!");
- Serial.println("");
- }
连接MQTT服务器
- //连接MQTT
- void connectMQTTServer() {
- if (mqttClient.connect(mqtt_devName, mqtt_pubid, mqtt_authorization)) {
- Serial.println("MQTT Server Connected.");
- Serial.println("Server Address: ");
- Serial.println(mqttServer);
- //订阅主题
- subscribeTopic();
- } else {
- Serial.print("MQTT Server Connect Failed. Client State:");
- Serial.println(mqttClient.state());
- delay(3000);
- }
- }
发布数据,向数据流temperatrue发送数据
- //发送数据 向主题发送数据流,即设备数据点上传
- void sendDpData() {
- // payload,需要发送的数据格式
- // {
- // "id": 123,
- // "dp": {
- // "temperatrue": [{
- // "v": 30,
- // }]
- // }
- // }
- //将payload转为字符串,传递的参数格式化为浮点数,保留2位小数
- char dataTemplate[] = "{\"id\":123,\"dp\":{\"temperatrue\":[{\"v\":%.2f,}]}}";
- //生成1-100的随机数,作为温度值
- srand((unsigned int)time(NULL));
- int temp = rand() % 100 + 1;
- // Serial.println("随机数");
- // Serial.println(temp);
- //发送信息缓冲区
- char msgJson[56];
- //将temp放入dataTemplate,复制给msgJson
- snprintf(msgJson, 56, dataTemplate, (float)temp);
-
- //设置发布主题
- String topicString = "$sys/558037/d1/dp/post/json";
- char publishTopic[topicString.length() + 1];
- strcpy(publishTopic, topicString.c_str());//将字符串转为字符数组
-
- // 实现ESP8266向主题发布信息
- if (mqttClient.publish(publishTopic, msgJson)) {
- Serial.println("发布数据点主题:"); Serial.println(publishTopic);
- Serial.println("发布信息:"); Serial.println(msgJson);
- } else {
- Serial.println("Message Publish Failed.");
- }
- }
订阅用户发送命令主题
- //订阅命令主题
- void subscribeTopic() {
- // 建立主题
- String topicString = "$sys/558037/d1/cmd/request/+";
- char subTopic[topicString.length() + 1];
- strcpy(subTopic, topicString.c_str());
-
- // 通过串口监视器输出是否成功订阅主题以及订阅的主题名称
- if (mqttClient.subscribe(subTopic)) {
- Serial.println("订阅的主题为:");
- Serial.println(subTopic);
- } else {
- Serial.print("Subscribe Fail...");
- }
- }
MQTT订阅回调函数,根据用户发送的命令,执行开关灯,及调节灯的亮度
- /*
- 订阅回调函数,参数固定
- 当用户端发送cmd命令后,esp8266进行的回调处理
- */
- void receiveCallback(char* topic, byte* payload, unsigned int length) {
- Serial.print("收到的cmd主题 [");
- Serial.print(topic);
- Serial.println("] ");
- //获取主题topic//$sys/558037/d1/cmd/request/b790bdca-5528-4469-8639-7439046bf39a 中的 cmdid 90977789-5c61-4bb9-9387-ba9a676ff9f5
- char cmdStr[strlen(topic) - 27];//声明cmdStr,保存cmdid
- strcpy(cmdStr, topic + 27);//从topic字符串第27个位置开始向后取值
-
- //输出payload内容
- Serial.print("收到的数据:");
- char payload_char[length + 1];
- memset(payload_char, 0, sizeof(payload_char));
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- payload_char[i] = (char)payload[i];
- }
- Serial.println();
- Serial.println("--------------");
-
- /*
- 判断收到的命令类型
- led 开关灯,led:0关灯,led:1开灯
- bright 调节灯亮度,bright:数字,数字为PWD调节灯的亮度,范围0-1023
- */
- char *dataPtr = NULL;//传递的数据
- char numBuf[10];
- int num = 0;
- dataPtr = strchr((char *)payload_char, ':');//查找冒号
- if (dataPtr != NULL) //如果找到了
- {
- dataPtr++;//后移一位,指向数据
- while (*dataPtr >= '0' && *dataPtr <= '9') //循环取出数据
- {
- numBuf[num++] = *dataPtr++;
- }
- //numBuf[num] = 0;
- num = atoi((const char *)numBuf); //转为数值
- if (strstr((char *)payload_char, "led")) //搜索"led"
- {
- Serial.println("控制类型led");
- Serial.print("数值:");
- Serial.println(num);
- if (num == 1) //控制数据如果为1,代表开
- {
- digitalWrite(BUILTIN_LED, LOW); // 则点亮LED。
- Serial.println("LED ON");
- }
- else if (num == 0) //控制数据如果为0,代表关
- {
- digitalWrite(BUILTIN_LED, HIGH); // 否则熄灭LED。
- Serial.println("LED OFF");
- }
- }
- if (strstr((char *)payload_char, "bright")) //搜索"bright"
- {
- Serial.println("控制类型bright");
- Serial.print("数值:");
- Serial.println(num);
- analogWrite(4, num); //GPIO4接Led灯,pwm输出
- }
- }
-
- //向服务器返回信息,即已收到命令
- sendCmdResponse(cmdStr);
-
- }
向服务器发送确认信息,通知服务器已收到cmd命令信息。
- //接受到cmd命令后,返回的数据信息
- void sendCmdResponse(char* cmdStr) {
- //返回OK
- char responseData[] = "ok";
- //设置主题的模板字符串
- char topicString[] = "$sys/558037/d1/cmd/response/%s";
- //计算发布主题publicTopic需要的字符空间,topicString长度 + cmdStr长度
- int slength = strlen(topicString) + strlen(cmdStr);
- //定义发布主题
- char publicTopic[slength];
- //将topicString和cmdStr进行拼接
- snprintf(publicTopic, slength, topicString, cmdStr);
-
- // 实现ESP8266向主题发布信息
- if (mqttClient.publish(publicTopic, responseData)) {
- Serial.println("发布命令回复主题:"); Serial.println(publicTopic);
- Serial.println("主题信息:"); Serial.println(responseData);
- } else {
- Serial.println("Message Publish Failed.");
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。