一、实验目的及要求
实现Easy IoT配置。
实现Easy IoT上mqtt消息的通讯。
二、实验原理与内容
实现mind+下Easy IoT上mqtt消息的通讯。
三、实验软硬件环境
硬件:掌控板
软件:Mind+
四、实验过程(实验步骤、记录、数据、分析)
在easyiot注册账号并添加新设备
在mind+拖拉代码块
/*!
* MindPlus
* mpython
*
*/
#include <MPython.h>
#include <DFRobot_Iot.h>
// 函数声明
void obloqMqttEventT0(String& message);
void obloqMqttEventT1(String& message);
// 静态常量
const String topics[5] = {"MFhNXylMR","e8wWqylMg","","",""};
const MsgHandleCb msgHandles[5] = {obloqMqttEventT0,obloqMqttEventT1,NULL,NULL,NULL};
// 创建对象
DFRobot_Iot myIot;
// 主程序开始
void setup() {
mPython.begin();
myIot.setMqttCallback(msgHandles);
myIot.wifiConnect("wefun", "12345678");
while (!myIot.wifiStatus()) {yield();}
display.setCursorLine(1);
display.printLine("wifi连接成功");
myIot.init("iot.dfrobot.com.cn","xUwdls_GR","","bUQOlslMgz",topics,1883);
myIot.connect();
while (!myIot.connected()) {yield();}
display.setCursorLine(2);
display.printLine("MQTT连接成功");
}
void loop() {
if ((buttonA.isPressed())) {
myIot.publish(topic_0, (light.read()));
}
if ((buttonB.isPressed())) {
myIot.publish(topic_1, (sound.read()));
}
}
// 事件回调函数
void obloqMqttEventT0(String& message) {
display.setCursorLine(3);
display.printLine((light.read()));
rgb.brightness(round(2));
rgb.write(-1, 0x00FF00);
}
void obloqMqttEventT1(String& message) {
display.setCursorLine(3);
display.printLine((sound.read()));
rgb.brightness(round(2));
rgb.write(-1, 0x0000FF);
}
五、测试/调试及实验结果分析
当按钮A按下显示绿灯并发送环境光强度,当接收到消息后在屏幕显示环境光强度
当按钮B按下显示红灯并发送麦克风声音强度,当接收到消息后在屏幕显示声音强度
六、实验结论与体会
通过实验认识了mqtt的功能和操作,并通过mind+对上传下载信息的运用,更加的熟悉了mind+的操作