SIoT及安卓App实现MQTT实验
实验目的及要求
【实验一】实现sIoT配置并实现sIoT上mqtt消息的通讯。
【实验二】通过App Inventor结合sIoT实现手机app控制掌控板的灯和显示内容。
实验原理与内容
sIoT可以在局域网上实现mqtt消息的通讯。借助sIoT可以通过手机app实现局域网内的设备操控。
实验软硬件环境
硬件:掌控板
软件:Mind+
实验过程
-
源码仓库地址:https://gitee.com/kwong-chi-ho/internet-of-things-course.git
-
核心代码截图:
【实验一】
详细代码:
/*!
* 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);
// 静态常量
const uint8_t imageMatrix[][350] = {
{0x0,0x0,0x0,0x1f,0x80,0x0,0x0,0x0,0x0,0x0,0x30,0xc0,0x0,0x0,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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向掌控板发送消息,掌控板消息成功接收消息并亮灯。