掌控板:实现mind+下Easy IoT上mqtt消息的通讯

简介

利用掌控板,实现Mind+下Easy IoT上mqtt消息的通讯。甲按A键(或触摸P)发送消息至topic1,同时监听topic0。乙监听topic1,接收到甲的消息后按A(或触摸P)回复消息给topic0甲显示。

这个实验要用到的硬件和软件有:

  1. 掌控板 ×2
  2. typeC数据线 ×2
  3. Mind+电脑端 1.6.5

图形化编程

在这里插入图片描述
在这里插入图片描述

C语言源码

/*!
 * MindPlus
 * mpython
 *
 */
#include <MPython.h>
#include <DFRobot_Iot.h>
// 函数声明
void onButtonAPressed();
void obloqMqttEventT0(String& message);
// 静态常量
const String topics[5] = {"-yNQbCdMR","WvTwxCdGR","","",""};
const MsgHandleCb msgHandles[5] = {obloqMqttEventT0,NULL,NULL,NULL,NULL};
// 创建对象
DFRobot_Iot myIot;


// 主程序开始
void setup() {
	mPython.begin();
	myIot.setMqttCallback(msgHandles);
	buttonA.setPressedCallback(onButtonAPressed);
	myIot.wifiConnect("Zhao", "12345678");
	display.setCursorLine(1);
	display.printLine("WIFI正在连接");
	while (!myIot.wifiStatus()) {yield();}
	display.setCursorLine(1);
	display.printLine("WIFI连接成功");
	myIot.init("iot.dfrobot.com.cn","CG9UxjdGg","","jGr8xjdGRz",topics,1883);
	myIot.connect();
	while (!myIot.connected()) {yield();}
	display.setCursorLine(2);
	display.printLine("MQTT连接成功");
}
void loop() {

}

// 事件回调函数
void onButtonAPressed() {
	myIot.publish(topic_1, "赵芳俪,你好");
	display.setCursorLine(3);
	display.printLine("钟苑发送成功");
}
void obloqMqttEventT0(String& message) {
	display.setCursorLine(4);
	display.printLine(message);
}

/*!
 * MindPlus
 * mpython
 *
 */
#include <MPython.h>
#include <DFRobot_Iot.h>
// 函数声明
void obloqMqttEventT1(String& message);
void onButtonAPressed();
// 静态常量
const String topics[5] = {"Yhpn0aCZg","85OdBrOGg","","",""};
const MsgHandleCb msgHandles[5] = {NULL,obloqMqttEventT1,NULL,NULL,NULL};
// 创建对象
DFRobot_Iot myIot;


// 主程序开始
void setup() {
	mPython.begin();
	myIot.setMqttCallback(msgHandles);
	buttonA.setPressedCallback(onButtonAPressed);
	myIot.wifiConnect("Zhao", "12345678");
	while (!myIot.wifiStatus()) {yield();}
	display.setCursorLine(1);
	display.printLine("WIFI连接成功");
	myIot.init("iot.dfrobot.com.cn","CLqmAajZR","","jLqiAaCZRz",topics,1883);
	myIot.connect();
	while (!myIot.connected()) {yield();}
	display.setCursorLine(2);
	display.printLine("MQTT连接成功");
}
void loop() {

}

// 事件回调函数
void obloqMqttEventT1(String& message) {
	display.setCursorLine(3);
	display.printLine(message);
}
void onButtonAPressed() {
	myIot.publish(topic_0, "赵芳俪收到");
	display.setCursorLine(4);
	display.printLine("赵芳俪发送成功");
}

实验效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值