Easy IoT实现mqtt消息通讯
一、实验目的及要求
【实验一】实现Easy IoT配置。
【实验二】实现Easy IoT上mqtt消息的通讯。
二、实验原理及内容
实现mind+下Easy IoT上mqtt消息的通讯。
三、实验软硬件环境
硬件:掌控板
软件:Mind+
物联网平台:Easy Iot
MQTT平台地址:http://iot.dfrobot.com/workshop.html
四、实验过程
1、进入EasyIoT平台注册账号,并添加两个设备
2、在Min+中编写代码
MQTT初始化参数
3、程序代码
#include <MPython.h>
#include <DFRobot_Iot.h>
// 函数声明
void obloqMqttEventT1(String& message);
// 静态常量
const String topics[5] = {"2U4FUldGg","VEggQ_OGg","","",""};
const MsgHandleCb msgHandles[5] = {NULL,obloqMqttEventT1,NULL,NULL,NULL};
// 创建对象
DFRobot_Iot myIot;
// 主程序开始
void setup() {
mPython.begin();
myIot.setMqttCallback(msgHandles);
myIot.wifiConnect("602iotmqtt", "18wulian");
while (!myIot.wifiStatus()) {yield();}
display.setCursorLine(1);
display.printLine(myIot.getWiFiLocalIP());
myIot.init("iot.dfrobot.com","YkcWs_dGg","","LkcZs_dGgz",topics,1883);
myIot.connect();
while (!myIot.connected()) {yield();}
display.setCursorLine(2);
display.printLine("\"mqtt is ok\"");
}
void loop() {
if ((buttonA.isPressed())) {
myIot.publish(topic_0, "你好!晓彤");
display.setCursorLine(3);
display.printLine("发送成功!");
}
}
// 事件回调函数
void obloqMqttEventT1(String& message) {
display.fillScreen(0);
display.setCursorLine(3);
display.printLine(message);
rgb.write(0, 0xCC33CC);
}
4、把代码上传到设备
5、收到小伙伴的消息
收到消息后小伙伴的掌控版
在Easy Iot的T_1设备中可以查看到所收到的信息
6、按下A键发送消息
发送成功后小伙伴的掌控板
在Easy Iot的T_0设备中可以查看所收到的信息