中国移动onenet平台
前言
最近在弄onenet平台,用arduino结合esp8266,就是不知道怎么回事,一直连不上wifi,然后就用esp8266试了一下,没想到一下就在线了,先看一下图。
一、onenet平台
你就创建一个mqtt协议接入的设备就行了
二、ESP8266
怎么配置环境的就不讲了,直接附上源码,这个代码可以直接连
1.完整代码
const char *Device_ID = ""; //设备ID
const char *Product_ID = ""; //产品ID
const char *Api_KEY = "MQ"; //鉴权信息
const char *WiFi_SSID = "QCX"; //WiFi名称
const char *WiFi_Password = "88888888"; //WiFi密码
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
float Current = 0;
uint8_t Upload_Package[100];
uint32_t Last_Updata_Time = 0;
WiFiClient espClient;
PubSubClient client(espClient);
void WiFi_Init(void);
void MQTT_Init(void);
void MQTT_Reconnection(void);
void MQTT_Callback(char *MQTT_Topic, uint8_t *MQTT_Payload, uint16_t MQTT_Payload_Len);
void setup()
{
Serial.begin(115200);
WiFi_Init();
MQTT_Init();
}
void loop()
{
if (!client.connected())
MQTT_Reconnection();
if (millis() - Last_Updata_Time >= 3000)
{
Last_Updata_Time = millis();
float ADC_Value = analogRead(A0);
Current = (ADC_Value - 512) * 0.033783784;
Serial.print("Current: ");
Serial.print(Current);
Serial.println(" A\r\n");
memset(Upload_Package, 0, 100);
String Json_Buffer = ",;Current,";
Json_Buffer += String(Current);
Json_Buffer += ";";
Upload_Package[0] = 5;
Upload_Package[1] = highByte(Json_Buffer.length());
Upload_Package[2] = lowByte(Json_Buffer.length());
for (int i = 0; i < Json_Buffer.length(); i++)
Upload_Package[i + 3] = Json_Buffer.c_str()[i];
client.publish("$dp", Upload_Package, Json_Buffer.length() + 3);
}
client.loop();
}
void WiFi_Init(void)
{
Serial.print("\r\n\r\nConnecting to ");
Serial.print(WiFi_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WiFi_SSID, WiFi_Password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.print("\r\nWiFi Connected.\r\nIP address: ");
Serial.println(WiFi.localIP());
}
void MQTT_Init(void)
{
client.setServer("183.230.40.39", 6002);
client.setCallback(MQTT_Callback);
}
void MQTT_Callback(char *MQTT_Topic, uint8_t *MQTT_Payload, uint16_t MQTT_Payload_Len)
{
Serial.printf("Topic: %s[%d]:\r\n", MQTT_Topic, MQTT_Payload_Len);
for (uint16_t i = 0; i < MQTT_Payload_Len; i++)
{
Serial.print(MQTT_Payload[i] < 0x10 ? " 0x0" : " 0x");
Serial.print(MQTT_Payload[i]);
}
Serial.println();
}
void MQTT_Reconnection(void)
{
while (!client.connected())
{
Serial.println("MQTT Reconnection.");
if (client.connect(Device_ID, Product_ID, Api_KEY))
Serial.println("MQTT Connected");
else
{
Serial.print("Connect Failed, Error Code = ");
Serial.print(client.state());
Serial.println(", Try again in 5 seconds.");
delay(5000);
}
}
}
将A0口读取的模拟值上传到onenet,将接收到onenet下发信息时在串口打印
2.联网代码
下面是联网的代码,固件下载AT 4M固件
//首先要有WiFi参数
const char *WiFi_SSID = "*******"; //WiFi名称
const char *WiFi_Password = "***********"; //WiFi密码
//其次要有WiFi驱动库
#include <ESP8266WiFi.h>
//声明下函数
void WiFi_Init(void);
void setup()//setup只执行一次,联网放这里面就行
{
//串口要初始化下,方便查看过程
Serial.begin(115200);
//开始WiFi初始化
WiFi_Init();
}
void loop()
{
}
void WiFi_Init(void)
{
//打印下提示语
Serial.print("\r\n\r\nConnecting to ");
Serial.print(WiFi_SSID);
//STA模式,传入wifi参数
WiFi.mode(WIFI_STA);
WiFi.begin(WiFi_SSID, WiFi_Password);
Serial.print("\r\nWiFi Connected.\r\nIP address: ");
Serial.println(WiFi.localIP());
}
3.连云代码
这个代码是连接onenet平台的
//首先要有参数
const char *Device_ID = ""; //设备ID
const char *Product_ID = ""; //产品ID
const char *Api_KEY = ""; //鉴权信息
//提供socket协议(MQTT基于TCP)
#include <ESP8266WiFi.h>
//提供MQTT协议
#include <PubSubClient.h>
//创建对象
WiFiClient espClient;
PubSubClient client(espClient);
//声明下函数
void MQTT_Init(void);
void MQTT_Reconnection(void);
void setup()
{
Serial.begin(115200);
MQTT_Init();//初始化MQTT
}
void loop()//loop相当于其他单片机的while(1)
{
//判断MQTT连接了没,没的话重新连接
if (!client.connected())
MQTT_Reconnection();
//MQTT运行
client.loop();
}
void MQTT_Init(void)
{
client.setServer("183.230.40.39", 6002);//设置服务器IP地址和端口
client.setCallback(MQTT_Callback); //设置接收到信息的回调函数
}
//MQTT重连函数
void MQTT_Reconnection(void)
{
while (!client.connected())//循环到MQTT已连接
{
Serial.println("MQTT Reconnection.");
if (client.connect(Device_ID, Product_ID, Api_KEY))//调用MQTT连接,传入对应的参数,返回1则连接成功
Serial.println("MQTT Connected");
else//否则就打印错误消息
{
Serial.print("Connect Failed, Error Code = ");
Serial.print(client.state());
Serial.println(", Try again in 5 seconds.");
delay(5000);
}
}
}
4.数据处理
//在初始化MQTT时候,我们已经设置了回调函数
client.setCallback(MQTT_Callback);
//所以,当收到信息时,会调用这个函数,传入参数含义如下
//MQTT_Topic 收到的消息的主题
//MQTT_Payload 收到的消息的内容
//MQTT_Payload_Len 收到的消息的内容的长度
void MQTT_Callback(char *MQTT_Topic, uint8_t *MQTT_Payload, uint16_t MQTT_Payload_Len)
{
//此处只进行了打印,没有其余处理操作
Serial.printf("Topic: %s[%d]:\r\n", MQTT_Topic, MQTT_Payload_Len);
for (uint16_t i = 0; i < MQTT_Payload_Len; i++)
{
Serial.print(MQTT_Payload[i] < 0x10 ? " 0x0" : " 0x");
Serial.print(MQTT_Payload[i]);
}
Serial.println();
}
//若要上传数据,则使用发布函数发布
//$dp ONENT上传数据固定主题
//Upload_Package 上传的数据数组
//Json_Buffer.length() + 3 上传的数据长度,因为包含0,使用不能使用strlen计算长度
client.publish("$dp", Upload_Package, Json_Buffer.length() + 3);
Upload_Package[0]为上传格式,可以参照ONENET文档,此处用的是5
数据示例:,;数据名称,数据值;
Upload_Package[1] * 256 + Upload_Package[2] 为 3~数据结束的长度
总结
ONENET的上传数据格式有点特殊,前3字节为固定格式,其后才是真正数据,所以我后面写了一个+3
ONENET的端口号是6002不是传统的1883
格式5即使是字符也不需要双引号(不同于JSON)