“Mqtt mode”的版本间的差异
502748957@qq.com(讨论 | 贡献) (创建页面,内容为“{| style="width: 800px;" |- | <p style="color: #4F4E4E;font-size:220%">'''Mqtt上传示例'''</p> <br> 此示例为MicroAI使用MQTT协议连接mCotton示例。 <br>…”) |
502748957@qq.com(讨论 | 贡献) |
||
第158行: | 第158行: | ||
|style="width: 25%;"|[[File:MicroAIMQTTshow.png|300px|center|thumb|点阵屏幕]] | |style="width: 25%;"|[[File:MicroAIMQTTshow.png|300px|center|thumb|点阵屏幕]] | ||
|} | |} | ||
− | + | |} | |
− | |||
<p style="font-size:115%">[[MicroAI Reference|返回MicroAI Reference界面]]</p> | <p style="font-size:115%">[[MicroAI Reference|返回MicroAI Reference界面]]</p> |
2018年12月11日 (二) 08:08的版本
Mqtt上传示例
所需硬件
电路搭建 将Battery、Core+、hub堆叠在一起,通过MicroUSB数据线接入电脑,使用传感器线连接MicroAI的UART(串口)并下载如下程序至Core+
代码 发射端程序: #include <Microduino_Ai.h>
#include <aJSON.h>
#define WIFI_SSID "Microduino"
#define WIFI_PWD "MakerModule2016"
#define MQTTSERVER "mqtt://mCotton.microduino.cn"
#define ID "5bf768cd38dd330017b61492"
#define USER "5bf768cd38dd330017b61492"
#define PASS "tAP5CjYYKgX1"
#define SUBSCRIBTOPIC "ca/58f881ae42c66e00013cf445"
#define SUBSCRIBTOPICP "cp/58f881ae42c66e00013cf445"
#define PUBLISHTOPIC "dp/58f881ae42c66e00013cf445"
#define INTERVAL_sensor 2000
unsigned long sensorlastTime = millis();
MicroAi mAi(&Serial1);
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)) {
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)) {
Serial.println(F("set mqtt server OK!"));
} else {
Serial.println(F("set mqtt server failed!"));
while (1);
}
if (mAi.mqttConnect(ID, USER, PASS)) {
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) {
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) {
Serial.print("recv: ");
Serial.println(strBuf);
}
if (mAi.mqttGetStatus() > 0) {
if (sensorlastTime > millis())
sensorlastTime = millis();
if (millis() - sensorlastTime > INTERVAL_sensor) {
float lightness = map(analogRead(A0), 0, 1023, 0, 255);
jsonData = "{\"Lightness\":";
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);
}
运行结果 |