实现sIoT配置并实现sIoT上mqtt消息的通讯
一、实验目的和要求
实现sIoT配置并实现sIoT上mqtt消息的通讯
二、实验原理与内容
sIoT可以在局域网上实现mqtt消息的通讯。借助sIoT可以通过手机app实现局域网内的设备操控。
三、实验软硬件环境
硬件:掌控板
软件:Mind+
四、实验过程
siot服务器在网上下载,然后在浏览器上打开服务器
为完成实验,使两个掌控板都能访问这个服务器,得使它们位于同一个局域网中,这里把它们都连接到同一个热点上
在Mind+上写上如下的代码
效果图
代码如下
/*!
* MindPlus
* mpython
*
*/
#include <MPython.h>
#include <DFRobot_Iot.h>
// 函数声明
void onButtonAPressed();
void obloqMqttEventT0(String& message);
// 静态常量
const String topics[5] = {"8bDcSmKGg","RhtcSiFGR","","",""};
const MsgHandleCb msgHandles[5] = {obloqMqttEventT0,NULL,NULL,NULL,NULL};
// 创建对象
DFRobot_Iot myIot;
// 主程序开始
void setup() {
mPython.begin();
myIot.setMqttCallback(msgHandles);
buttonA.setPressedCallback(onButtonAPressed);
myIot.wifiConnect("Honor Play", "00000000");
display.setCursorLine(1);
display.printLine("正在连接");
while (!myIot.wifiStatus()) {yield();}
display.setCursorLine(1);
display.printLine("连接网络成功");
myIot.init("iot.dfrobot.com.cn","rGm5IiKGR","","rGmcSmFGRz",topics,1883);
myIot.connect();
while (!myIot.connected()) {yield();}
display.setCursorLine(1);
display.printLine("mqtt连接成功");
}
void loop() {
}
// 事件回调函数
void onButtonAPressed() {
while (!(!buttonA.isPressed())) {yield();}
myIot.publish(topic_1, "陈登祖");
}
void obloqMqttEventT0(String& message) {
rgb.write(-1, 0x0000FF);
display.setCursorLine(1);
display.printLine(message);
}