“Mqtt mode”的版本间的差异
502748957@qq.com(讨论 | 贡献) |
502748957@qq.com(讨论 | 贡献) |
||
第31行: | 第31行: | ||
#include <aJSON.h> | #include <aJSON.h> | ||
− | #define WIFI_SSID " | + | #define WIFI_SSID "ssid" //路由器名称 |
− | #define WIFI_PWD " | + | #define WIFI_PWD "password" //路由器密码 |
#define MQTTSERVER "mqtt://mCotton.microduino.cn" | #define MQTTSERVER "mqtt://mCotton.microduino.cn" | ||
− | #define ID "5bf768cd38dd330017b61492" | + | #define ID "5bf768cd38dd330017b61492" //mCotton上的设备ID,建立设备后可以找到 |
− | #define USER "5bf768cd38dd330017b61492" | + | #define USER "5bf768cd38dd330017b61492" //同上 |
− | #define PASS "tAP5CjYYKgX1" | + | #define PASS "tAP5CjYYKgX1" //此处为Secure token |
− | #define SUBSCRIBTOPIC "ca/ | + | //下面三条不变 |
− | #define SUBSCRIBTOPICP "cp/ | + | #define SUBSCRIBTOPIC "ca/5bf768cd38dd330017b61492" //在斜杠后复制ID |
− | #define PUBLISHTOPIC "dp/ | + | #define SUBSCRIBTOPICP "cp/5bf768cd38dd330017b61492" //在斜杠后复制ID |
+ | #define PUBLISHTOPIC "dp/5bf768cd38dd330017b61492" //在斜杠后复制ID | ||
− | #define INTERVAL_sensor 2000 | + | #define INTERVAL_sensor 2000 // |
unsigned long sensorlastTime = millis(); | unsigned long sensorlastTime = millis(); | ||
− | MicroAi mAi(&Serial1); | + | MicroAi mAi(&Serial1); //使用串口1 |
− | String jsonData; | + | String jsonData; //用于存放上传的数据 |
char strBuf[64] = {0}; | char strBuf[64] = {0}; | ||
第54行: | 第55行: | ||
Serial.print("MicroAi set mode: MQTT."); | Serial.print("MicroAi set mode: MQTT."); | ||
Serial.println("start initiation. "); | Serial.println("start initiation. "); | ||
− | if (mAi.begin(MODE_MQTT)) { | + | if (mAi.begin(MODE_MQTT)) { //设置为MQTT工作模式 |
Serial.println("init OK!"); | Serial.println("init OK!"); | ||
} else { | } else { | ||
第61行: | 第62行: | ||
} | } | ||
− | if (mAi.setWifi(WIFI_SSID, WIFI_PWD)) { | + | if (mAi.setWifi(WIFI_SSID, WIFI_PWD)) { //连接路由器 |
Serial.println("set wifi ssid and password OK!"); | Serial.println("set wifi ssid and password OK!"); | ||
} else { | } else { | ||
第68行: | 第69行: | ||
} | } | ||
Serial.print("wifi connecting"); | Serial.print("wifi connecting"); | ||
− | while (mAi.getRssi() == 0) { | + | while (mAi.getRssi() == 0) { //获取信号强度,成功获得强度则连接成功 |
Serial.print("."); | Serial.print("."); | ||
delay(1000); | delay(1000); | ||
第74行: | 第75行: | ||
Serial.println("\r\nwifi connected."); | Serial.println("\r\nwifi connected."); | ||
− | if (mAi.mqttSetServer(MQTTSERVER)) { | + | if (mAi.mqttSetServer(MQTTSERVER)) { //MQTT服务器地址 |
Serial.println(F("set mqtt server OK!")); | Serial.println(F("set mqtt server OK!")); | ||
} else { | } else { | ||
第81行: | 第82行: | ||
} | } | ||
− | if (mAi.mqttConnect(ID, USER, PASS)) { | + | if (mAi.mqttConnect(ID, USER, PASS)) { //MQTT连接,使用设备ID与Secure token |
Serial.println(F("set mqtt connect params OK!")); | Serial.println(F("set mqtt connect params OK!")); | ||
} else { | } else { | ||
第89行: | 第90行: | ||
Serial.print("mqtt server connecting"); | Serial.print("mqtt server connecting"); | ||
− | while (mAi.mqttGetStatus() <= 0) { | + | while (mAi.mqttGetStatus() <= 0) { //获取MQTT状态 |
Serial.print("."); | Serial.print("."); | ||
delay(1000); | delay(1000); | ||
第95行: | 第96行: | ||
Serial.println("\r\nmqtt server connected."); | Serial.println("\r\nmqtt server connected."); | ||
− | if (mAi.mqttSetSubscrib(SUBSCRIBTOPIC)) { | + | if (mAi.mqttSetSubscrib(SUBSCRIBTOPIC)) { //设置订阅 |
Serial.println(F("set mqtt subscrib ca topic OK!")); | Serial.println(F("set mqtt subscrib ca topic OK!")); | ||
} else { | } else { | ||
第101行: | 第102行: | ||
} | } | ||
− | if (mAi.mqttSetSubscrib(SUBSCRIBTOPICP)) { | + | if (mAi.mqttSetSubscrib(SUBSCRIBTOPICP)) { //设置订阅 |
Serial.println(F("set mqtt subscrib cp topic OK!")); | Serial.println(F("set mqtt subscrib cp topic OK!")); | ||
} else { | } else { | ||
第107行: | 第108行: | ||
} | } | ||
− | char *str = "{\"Humidity\":66.66}"; | + | char *str = "{\"Humidity\":66.66}"; //初始化字符串 |
− | if (mAi.mqttPublish(PUBLISHTOPIC, str)) { | + | if (mAi.mqttPublish(PUBLISHTOPIC, str)) { //推送一次 |
Serial.println(F("mqtt publish OK!")); | Serial.println(F("mqtt publish OK!")); | ||
} else { | } else { | ||
第119行: | 第120行: | ||
void loop() { | void loop() { | ||
− | if (mAi.mqttQuery(strBuf) > 0) { | + | if (mAi.mqttQuery(strBuf) > 0) { //接收buffer |
Serial.print("recv: "); | Serial.print("recv: "); | ||
Serial.println(strBuf); | Serial.println(strBuf); | ||
} | } | ||
− | if (mAi.mqttGetStatus() > 0) { | + | if (mAi.mqttGetStatus() > 0) { //成功获取状态 |
− | if (sensorlastTime > millis()) | + | if (sensorlastTime > millis()) //同步时间,防止错位和millis超过范围时的重制 |
sensorlastTime = millis(); | sensorlastTime = millis(); | ||
− | if (millis() - sensorlastTime > INTERVAL_sensor) { | + | if (millis() - sensorlastTime > INTERVAL_sensor) { //每两秒推送一次数据 |
− | float lightness = map(analogRead(A0), 0, 1023, 0, 255); | + | float lightness = map(analogRead(A0), 0, 1023, 0, 255); //此处为示例,使用模拟传感器光敏 |
− | jsonData = "{\"Lightness\":"; | + | jsonData = "{\"Lightness\":"; //通信格式{"Lightness":xxxx} |
jsonData += String(lightness); | jsonData += String(lightness); | ||
jsonData += "}"; | jsonData += "}"; | ||
第136行: | 第137行: | ||
int str_len = jsonData.length() + 1; | int str_len = jsonData.length() + 1; | ||
− | jsonData.toCharArray(strBuf, str_len); | + | jsonData.toCharArray(strBuf, str_len); |
// Serial.println(strBuf); | // Serial.println(strBuf); | ||
− | if (mAi.mqttPublish(PUBLISHTOPIC, strBuf)) { | + | if (mAi.mqttPublish(PUBLISHTOPIC, strBuf)) { //数据推送 |
Serial.println(F("mqtt publish OK!")); | Serial.println(F("mqtt publish OK!")); | ||
} else { | } else { | ||
Serial.println(F("mqtt publish failed!")); | Serial.println(F("mqtt publish failed!")); | ||
} | } | ||
− | sensorlastTime = millis(); | + | sensorlastTime = millis(); //同步时间 |
− | } | + | } |
} | } | ||
delay(100); | delay(100); |
2018年12月12日 (三) 03:27的最新版本
Mqtt上传示例
所需硬件
电路搭建 将Battery、Core+、hub堆叠在一起,通过MicroUSB数据线接入电脑,使用传感器线连接MicroAI的UART(串口)并下载如下程序至Core+
代码 发射端程序: #include <Microduino_Ai.h>
#include <aJSON.h>
#define WIFI_SSID "ssid" //路由器名称
#define WIFI_PWD "password" //路由器密码
#define MQTTSERVER "mqtt://mCotton.microduino.cn"
#define ID "5bf768cd38dd330017b61492" //mCotton上的设备ID,建立设备后可以找到
#define USER "5bf768cd38dd330017b61492" //同上
#define PASS "tAP5CjYYKgX1" //此处为Secure token
//下面三条不变
#define SUBSCRIBTOPIC "ca/5bf768cd38dd330017b61492" //在斜杠后复制ID
#define SUBSCRIBTOPICP "cp/5bf768cd38dd330017b61492" //在斜杠后复制ID
#define PUBLISHTOPIC "dp/5bf768cd38dd330017b61492" //在斜杠后复制ID
#define INTERVAL_sensor 2000 //
unsigned long sensorlastTime = millis();
MicroAi mAi(&Serial1); //使用串口1
String jsonData; //用于存放上传的数据
char strBuf[64] = {0};
void setup() {
Serial.begin(115200);
Serial.print("MicroAi set mode: MQTT.");
Serial.println("start initiation. ");
if (mAi.begin(MODE_MQTT)) { //设置为MQTT工作模式
Serial.println("init OK!");
} else {
Serial.println("init failed!");
while (1);
}
if (mAi.setWifi(WIFI_SSID, WIFI_PWD)) { //连接路由器
Serial.println("set wifi ssid and password OK!");
} else {
Serial.println("set wifi ssid and password failed!");
while (1);
}
Serial.print("wifi connecting");
while (mAi.getRssi() == 0) { //获取信号强度,成功获得强度则连接成功
Serial.print(".");
delay(1000);
}
Serial.println("\r\nwifi connected.");
if (mAi.mqttSetServer(MQTTSERVER)) { //MQTT服务器地址
Serial.println(F("set mqtt server OK!"));
} else {
Serial.println(F("set mqtt server failed!"));
while (1);
}
if (mAi.mqttConnect(ID, USER, PASS)) { //MQTT连接,使用设备ID与Secure token
Serial.println(F("set mqtt connect params OK!"));
} else {
Serial.println(F("set mqtt connect params failed!"));
while (1);
}
Serial.print("mqtt server connecting");
while (mAi.mqttGetStatus() <= 0) { //获取MQTT状态
Serial.print(".");
delay(1000);
}
Serial.println("\r\nmqtt server connected.");
if (mAi.mqttSetSubscrib(SUBSCRIBTOPIC)) { //设置订阅
Serial.println(F("set mqtt subscrib ca topic OK!"));
} else {
Serial.println(F("set mqtt subscrib ca topic failed"));
}
if (mAi.mqttSetSubscrib(SUBSCRIBTOPICP)) { //设置订阅
Serial.println(F("set mqtt subscrib cp topic OK!"));
} else {
Serial.println(F("set mqtt subscrib cp topic failed"));
}
char *str = "{\"Humidity\":66.66}"; //初始化字符串
if (mAi.mqttPublish(PUBLISHTOPIC, str)) { //推送一次
Serial.println(F("mqtt publish OK!"));
} else {
Serial.println(F("mqtt publish failed!"));
}
delay(1000);
}
void loop() {
if (mAi.mqttQuery(strBuf) > 0) { //接收buffer
Serial.print("recv: ");
Serial.println(strBuf);
}
if (mAi.mqttGetStatus() > 0) { //成功获取状态
if (sensorlastTime > millis()) //同步时间,防止错位和millis超过范围时的重制
sensorlastTime = millis();
if (millis() - sensorlastTime > INTERVAL_sensor) { //每两秒推送一次数据
float lightness = map(analogRead(A0), 0, 1023, 0, 255); //此处为示例,使用模拟传感器光敏
jsonData = "{\"Lightness\":"; //通信格式{"Lightness":xxxx}
jsonData += String(lightness);
jsonData += "}";
//jsonData = "{\"Humidity\":\"mCookie\"}";
int str_len = jsonData.length() + 1;
jsonData.toCharArray(strBuf, str_len);
// Serial.println(strBuf);
if (mAi.mqttPublish(PUBLISHTOPIC, strBuf)) { //数据推送
Serial.println(F("mqtt publish OK!"));
} else {
Serial.println(F("mqtt publish failed!"));
}
sensorlastTime = millis(); //同步时间
}
}
delay(100);
}
运行结果 串口显示WIFI、MQTT分别连接成功并且publish OK,则意味着已经可以成功推送数据至云端了 此时点阵屏幕显示“M”图标 在云端上可以看到消息推送了. mCotton使用方法 |