“Mqtt mode”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
第153行: 第153行:
 
<br>
 
<br>
 
<p style="color: #E87E05;font-size:135%">运行结果</p>
 
<p style="color: #E87E05;font-size:135%">运行结果</p>
{|style="width: 90%; text-align: center;"
+
 
|-
+
[[File:MicroAIMQTTSerial.png|300px|center|thumb|串口打印]]
|style="width: 25%;"|[[File:MicroAIMQTTSerial.png|300px|center|thumb|串口打印]]
+
串口显示WIFI、MQTT分别连接成功并且publish OK,则意味着已经可以成功推送数据至云端了
|style="width: 25%;"|[[File:MicroAIMQTTshow.png|300px|center|thumb|点阵屏幕]]
+
[[File:MicroAIMQTTshow.png|300px|center|thumb|点阵屏幕]]
|}
+
此时点阵屏幕显示“M”图标
 +
[[File:MicroAIMcotton.png|500px|center|thumb|mCotton]]
 +
在云端上可以看到消息推送了.
 +
<p style="color: #E87E05;font-size:135%">mCotton使用方法</p>
 +
<big>[[Mqtt连接mCotton | 进入页面]]</big>
 +
 
 
|}
 
|}
 
<p style="font-size:115%">[[MicroAI Reference|返回MicroAI Reference界面]]</p>
 
<p style="font-size:115%">[[MicroAI Reference|返回MicroAI Reference界面]]</p>

2018年12月11日 (二) 09:23的版本

Mqtt上传示例


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

所需硬件



电路搭建

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

MicroAI连接.png



代码

发射端程序:

#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);
}


运行结果

串口打印

串口显示WIFI、MQTT分别连接成功并且publish OK,则意味着已经可以成功推送数据至云端了

点阵屏幕

此时点阵屏幕显示“M”图标

mCotton

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

mCotton使用方法

进入页面

返回MicroAI Reference界面