实验项目:
##实验目的及要求:实现Easy IoT配置、实现Easy IoT上mqtt消息的通讯。
##实验原理:实现mind+下Easy IoT上mqtt消息的通讯。
c++代码
#include <MPython.h>
#include <DFRobot_Iot.h>
// 函数声明
void obloqMqttEventT1(String& message);
// 静态常量
const String topics[5] = {"nYPociKGR","LIsx5mKGR","","",""};
const MsgHandleCb msgHandles[5] = {NULL,obloqMqttEventT1,NULL,NULL,NULL};
// 创建对象
DFRobot_Iot myIot;
// 主程序开始
void setup() {
mPython.begin();
myIot.setMqttCallback(msgHandles);
display.setCursorLine(1);
display.printLine("连接网络中");
myIot.wifiConnect("Chuang2", "hc666666");
while (!myIot.wifiStatus()) {yield();}
display.setCursorLine(1);
display.printLine("连接成功");
myIot.init("iot.dfrobot.com.cn","yVgocmKGg","","sVRociKGgz",topics,1883);
myIot.connect();
while (!myIot.connected()) {yield();}
}
void loop() {
if ((buttonA.isPressed())) {
myIot.publish(topic_0, "hello");
display.setCursorLine(2);
display.printLine("发送成功");
}
}
// 事件回调函数
void obloqMqttEventT1(String& message) {
rgb.write(-1, 0x0000FF);
delay(2000);
rgb.write(-1, 0x000000);
display.setCursorLine(3);
display.printLine(message);
}
实验效果图: