SIoT及安卓App实现MQTT实验

SIoT及安卓App实现MQTT实验

实验目的及要求

【实验一】实现sIoT配置并实现sIoT上mqtt消息的通讯。
【实验二】通过App Inventor结合sIoT实现手机app控制掌控板的灯和显示内容。

实验原理与内容

sIoT可以在局域网上实现mqtt消息的通讯。借助sIoT可以通过手机app实现局域网内的设备操控。

实验软硬件环境

硬件:掌控板
软件:Mind+

实验过程

  1. 源码仓库地址:https://gitee.com/kwong-chi-ho/internet-of-things-course.git

  2. 核心代码截图:
    【实验一】
    在这里插入图片描述
    详细代码:

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

// 主程序开始
void setup() {
	mPython.begin();
	myIot.setMqttCallback(msgHandles);
	myIot.wifiConnect("602iot", "18wulian");
	while (!myIot.wifiStatus()) {yield();}
	display.setCursorLine(1);
	display.printLine("WiFi连接成功");
	myIot.init("192.168.31.21","siot","","dfrobot", topics, 1883);
	myIot.connect();
	while (!myIot.connected()) {yield();}
	if (myIot.connected()) {
		myIot.publish(topic_0, "2018324107");
	}
	else {
		while (1) {
			display.setCursorLine(1);
			display.printLine("Mind+");
			yield();
		}
	}
}
void loop() {

}

// 事件回调函数
void obloqMqttEventT0(String& message) {
	display.setCursorLine(2);
	display.printLine("2018324107/kuang");
	rgb.write(-1, 0xFF0000);
}

【实验二】
掌控板程序设计:
在这里插入图片描述
在这里插入图片描述
详细代码:

/*!
 * MindPlus
 * mpython
 *
 */
#include <MPython.h>
#include <DFString.h>
#include <DFRobot_Iot.h>

// 动态变量
volatile float mind_n_DouHao1, mind_n_DouHao2, mind_n_R, mind_n_G, mind_n_B;
// 函数声明
void     obloqMqttEventT0(String& message);
uint32_t rgbToColor(uint8_t r, uint8_t g, uint8_t b);
//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xe9,0x80,0xcb,0xf8,0x7f,0xff,0xf,0xe9,0x80,0x4b,0xf8,0x7f,0xff,0xf,0xe9,0x80,0x68,0x7c,0xff,0xff,0x1f,0xf1,0x80,0x63,0x87,0xff,0xff,0xff,0x93,0x0,0x60,0x38,0x7f,0xff,0xf0,0x33,0x0,0x60,0x3,0x8f,0xfe,0xf,0x83,0x0,0x60,0x0,0x38,0x3,0xe0,0x3,0x0,0x60,0x0,0x7,0xf8,0x0,0x3,0x0,0x60,0x0,0x0,0x0,0x0,0x3,0x0,0x60,0x0,0x0,0x0,0x0,0x3,0x0,0x60,0x0,0x0,0x0,0x0,0x3,0x0,0x60,0x0,0x0,0x0,0x0,0x6,0x0,0x60,0x0,0x0,0x0,0x0,0x6,0x0,0x60,0x0,0x0,0x0,0x0,0x6,0x0,0x7f,0xff,0xff,0xff,0xff,0xfe,0x0,0x7f,0xff,0xff,0xff,0xff,0xfe,0x0}
};
const String topics[5] = {"2018324126/潘金华","","","",""};
const MsgHandleCb msgHandles[5] = {obloqMqttEventT0,NULL,NULL,NULL,NULL};
// 创建对象
DFRobot_Iot myIot;

// 主程序开始
void setup() {
	mPython.begin();
	myIot.setMqttCallback(msgHandles);
	display.fillScreen(0);
	display.drawImage(39, 7, 50, 50, imageMatrix[0]);
	myIot.wifiConnect("602iot", "18wulian");
	while (!myIot.wifiStatus()) {yield();}
	display.fillScreen(0);
	display.setCursor(0, 0);
	display.print("WIFI已连接");
	myIot.init("192.168.31.161","siot","","dfrobot", topics, 1883);
	myIot.connect();
	while (!myIot.connected()) {yield();}
	display.fillScreen(0);
	display.setCursor(0, 0);
	display.print("MQTT连接成功");
}
void loop() {

}

// 事件回调函数
void obloqMqttEventT0(String& message) {
	display.fillScreen(0);
	display.setCursor(0, 22);
	display.print(message);
	mind_n_DouHao1 = (dfstring.indexOf(",",message));
	mind_n_DouHao2 = (dfstring.lastIndexOf(",",message));
	mind_n_R = (String((dfstring.substring(message,0,0,0,mind_n_DouHao1))).toInt());
	mind_n_G = (String((dfstring.substring(message,0,(mind_n_DouHao1 + 1),0,mind_n_DouHao2))).toInt());
	mind_n_B = (String((dfstring.substring(message,0,(mind_n_DouHao2 + 1),0,(String(message).length())))).toInt());
	rgb.write(-1, rgbToColor(round(mind_n_R), round(mind_n_G), round(mind_n_B)));
}

// 静态函数
uint32_t rgbToColor(uint8_t r, uint8_t g, uint8_t b)
{
  return (uint32_t)((((uint32_t)r<<16) | ((uint32_t)g<<8)) | (uint32_t)b);
}

App Inventor程序设计:
(1)界面组件设计
在这里插入图片描述
(2)逻辑设计
在这里插入图片描述

3.实验效果
【实验一】
在局域网环境下连接网络成功后,MQTT自动发送消息“2018324107”给设备Topic_0
掌控板接收到Topic_0的MQTT消息以后,显示“2018324107/kuang”,所有灯显示红色
在这里插入图片描述
【实验二】
安卓App发送消息,掌控板接收信息并亮灯(以逗号为分隔符赋值给R,G,B三个变量,三个灯分别显示相应颜色)
(1)接收消息“0,0,255”,亮蓝灯
在这里插入图片描述
(2)接收文本消息,亮白灯
在这里插入图片描述

测试结果及分析

【实验一】
本次是局域网环境下实验,网络连接成功后MQTT发送消息到SIoT云端,云端设备成功接收到MQTT消息,Topic_0(设备名称为2018324107/kuang)消息发送列表如下:
在这里插入图片描述
【实验二】
消息接收与颜色展示正常,设备Topic_0消息发送列表如下:

在这里插入图片描述

实验结论与体会

本次实验环境虽换成局域网,但与连接外网环境下实验原理相同。MQTT发送消息到SIoT云端,云端设备发送消息到掌控板。App Inventor设计简单功能的安卓App向掌控板发送消息,掌控板消息成功接收消息并亮灯。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值