Mqtt mode

来自Microduino Wikipedia
跳转至: 导航搜索

Mqtt上传示例


此示例为MicroAI使用MQTT协议连接mCotton示例。

所需硬件



电路搭建

将Battery、Core+、hub堆叠在一起,通过MicroUSB数据线接入电脑,使用传感器线连接MicroAI的UART(串口)并下载如下程序至Core+

MicroAI连接.png



代码

发射端程序:

#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

在云端上可以看到消息推送了.

mCotton使用方法

进入页面

返回MicroAI Reference界面