目标:开发esp32通过onenet平台远程控制LED、继电器等其它设备,并利用onenet可视化功能开发出一个简单的控制页面。
原以为能够快速完成,没想到接入mqtt协议、数据流上传、可视化按键都不同程度遇到了问题,还好经过一番查找和修改,终于初步完成了前两个功能,可视化按键是因为我需要远程控制继电器,刚开始时,一直无数据流可选择。没想到需要通过数据流来进行,所以需要在代码创建一个数据3laig流,虽然我在onenet平台也创建数据流,但在可视化界面一直不显示。明天在尝试一下新的方法。
发现有许多网友其实也不太清楚怎么连接onenet,我也是站在前人的基础上摸索了好久。
这几天一直在上班,没有太多来深入,一直利用下班时间来完成,续更中。。。。。。
1、参考链接:ESP32+MQTT+OneNet上报数据点+平台命令控制LED - Arduino专区 - OneNET设备云论坛 (10086.cn)
2、参考代码:
#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
//#include <ArduinoJson.h>
#include "DHT.h"
#define DHTPIN 5 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
#define BUILTIN_LED 21
// Update these with values suitable for your network.
const char *ssid = "Oppo-Xu"; //wifi名
const char *password = "123456ABC"; //wifi密码
const char *mqtt_server ="183.230.40.96"; //onenet 的 IP地址
const char *clientid = "smartlight";//OneNet注册设备的“设备名称”
const char *userid = "ze3xOCB42t";//OneNet 注册的MQTT产品的“产品ID”
const char *mqtt_password ="version=2018-10-31&res=products%2Fze3xOCB42t%2Fdevices%2Fsmartlight&et=1923199463&method=md5&sign=uG4snxEt7s%2Bqi55uPtCtxA%3D%3D";//OneNet 注册的MQTT产品的“产品ID”
char outTopic[] = "$sys/ze3xOCB42t/smartlight/dp/post/json";//数据上传的Topic,格式为“$sys/userid/clientid/dp/post/json”
char inTopic[] = "$sys/ze3xOCB42t/smartlight/dp/post/json/+";//数据返回的Topic,格式为“$sys/userid/clientid/dp/post/json/+”
char cmdTopic[] = "$sys/ze3xOCB42t/smartlight/cmd/#";//接收平台命令的TOPIC,格式为“$sys/userid/clientid/cmd/#”
int switch0 = 0;//开关状态
DHT dht(DHTPIN, DHTTYPE);
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
//数据上报格式模板,按网上例程的不成功,只能按下列格式,有Temperatrue、Humidity、switch0三个数据点
char dataTemplete[] = "{\"id\":1,\"dp\":{\"Temperatrue\":[{\"v\":%5.1f}],\"Humidity\":[{\"v\":%d}],\"switch0\":[{\"v\":%d}]}}";
char msgJson[100];//上传的数据
int h = 10;
void setup_wifi()
{
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// digitalWrite(BUILTIN_LED, HIGH);
}
//收到订阅Topic信息的回调处理
void callback(char *topic, byte *payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
// 平台发来命令“1”时开灯
if ((char)payload[0] == '1')
{
switch0 = 1;
//digitalWrite(BUILTIN_LED, HIGH);
}
else
{
switch0 = 0;
//digitalWrite(BUILTIN_LED, LOW);
}
digitalWrite(BUILTIN_LED, switch0);
}
void reconnect()
{
//循环连接
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
if (client.connect(clientid, userid, mqtt_password))
{
Serial.println("connected");
//订阅inTop的消息,上报数据后平台自动发来的
//client.unsubscribe (inTopic);
client.subscribe(cmdTopic); //订阅平台发来命令消息
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output, where do it esp32 get gpio21 as led
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883); //按OneNet开发文档设置端口为1883
client.setCallback(callback);
//dht.begin();
}
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000)
{
float t = 15.5;
lastMsg = now;
sprintf(msgJson, dataTemplete, t, h, switch0);//按模板格式化上传数据到msgJson
Serial.print("Publish message: ");
Serial.println(msgJson);
client.publish(outTopic, msgJson);//上传
}
h=h+2;
}