Easy IoT实现mqtt实验

一、实验目的及要求
实现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+的操作

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值