Easy IoT实现mqtt实验

一、实验目的及要求:

【实验一】实现Easy IoT配置。
【实验二】实现Easy IoT上mqtt消息的通讯。


二、实验原理与内容:

实现mind+下Easy IoT上mqtt消息的通讯。


三、实验软硬件环境:

软件:Mind+ V1.6.5 RC1.0
硬件:掌控板
Easy IoT实现mqtt实验
软件模式:上传模式
Easy IoT实现mqtt实验
网络服务:添加MQTT和Wi-Fi
Easy IoT实现mqtt实验
注:mqtt实现easy lot通信需要先在esayiot(网址:http://iot.dfrobot.com.cn)注册账号
Easy IoT实现mqtt实验

下图的lot_id、lot_pwd、topic_0、topic_1与上图对应
Easy IoT实现mqtt实验


四、完整代码:

/*! * MindPlus * mpython * */ #include

// 主程序开始
void setup() {
mPython.begin();
myIot.setMqttCallback(msgHandles);
myIot.wifiConnect(“first”, “aa123456789”);
while (!myIot.wifiStatus()) {yield();}
display.setCursorLine(1);
display.printLine(“wifi连接成功”);
myIot.init(“iot.dfrobot.com.cn”,“qimmdgFMR”,"",“3mmiOgFMRz”,topics,1883);
myIot.connect();
while (!myIot.connected()) {yield();}
display.setCursorLine(2);
display.printLine(“mqtt连接成功”);
}
void loop() {
if ((buttonA.isPressed())) {
myIot.publish(topic_1, “张三”);
display.setCursorLine(3);
display.printLine(“发送成功”);
delay(3000);
display.fillInLine(3, 0);
rgb.write(0, 0x0000FF);
delay(3000);
rgb.write(0, 0x000000);
}
}

// 事件回调函数
void obloqMqttEventT0(String& message) {
buzz.play(DADADADUM, Once);
rgb.write(2, 0xFF0000);
delay(2000);
rgb.write(2, 0x000000);
display.setCursorLine(4);
display.printLine(message);
delay(3000);
display.fillInLine(4, 0);
}
Easy IoT实现mqtt实验

五、效果图:

开启手机热点并连接掌控版后
Easy IoT实现mqtt实验

左边掌控版接收到右边掌控版发送的消息(内容为:孩子回家吃饭吗)
Easy IoT实现mqtt实验

左边掌控版发送消息(提示发送成功)内容为张三,且右掌控版收到了信息
Easy IoT实现mqtt实验

六、实验结论与体会:

1、两块掌控版要登陆同一个mqtt账号,并且要明确好发送哪个topic,接收哪个topic。
2、在编程途中遇到过因为没有嵌套循环执行会直接跳过操作步骤无法执行到想要的结果,因为动作如(发送信息)的开始执行条件是按钮A被按下,如果没有在循环内,会导致该判断条件直接被跳过而无法实现。
2020年 9月 28号
学号:2018764319
姓名:张宗裕