查看“Mqtt mode”的源代码
←
Mqtt mode
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
{| style="width: 800px;" |- | <p style="color: #4F4E4E;font-size:220%">'''Mqtt上传示例'''</p> <br> 此示例为MicroAI使用MQTT协议连接mCotton示例。 <br> <br> <p style="color: #E87E05;font-size:135%">所需硬件</p> *[[mCookie-Core+]] *[[MBattery+]] *[[MicroAI]] *[[MCookie-Hub/zh]] *1.27mm 4P传感器线 *Micro USB线 <br> <br> <p style="color: #E87E05;font-size:135%">电路搭建</p> 将Battery、Core+、hub堆叠在一起,通过MicroUSB数据线接入电脑,使用传感器线连接MicroAI的UART(串口)并下载如下程序至Core+ <br> [[File:MicroAI连接.png|400px|center]] <br> <br> <p style="color: #E87E05;font-size:135%">代码</p> 发射端程序: <source lang="cpp"> #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); } </source> <br> <p style="color: #E87E05;font-size:135%">运行结果</p> [[File:MicroAIMQTTSerial.png|300px|center|thumb|串口打印]] 串口显示WIFI、MQTT分别连接成功并且publish OK,则意味着已经可以成功推送数据至云端了 [[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>
返回至
Mqtt mode
。
导航菜单
个人工具
创建账户
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
Welcome
首页
创客大赛
大赛详情
3D打印
安装月球车
图形化编程
操控月球车
升级月球车
编程工具下载
软件下载
Arduino
Processing
Mixly
Scratch
模块套件
Microduino 102
mCookie 102
mCookie 202
mCookie 302
IBC
其他
应用套件
四轴飞行器
平衡车
小车CUBE
音乐播放器
刷卡音乐播放器
wifi气象站
彩虹音乐触摸灯
分贝检测仪
迎门汇报
LED点阵时钟
LED点阵屏幕
硬件
mCookie
Sensor
Microduino
MicroWrt
MicroNux
MicroRobot-Core
MicroRobot-CoreESP
ideaBoard
ideaBox
MicroMV
MicroAI
帮助
常见问题
帮助
工具
链入页面
相关更改
特殊页面
页面信息