sIoT及安卓app实现mqtt实验
实验目的
- 实现sIoT配置并实现sIoT上mqtt消息的通讯。
- 通过AppInventor结合sIoT实现手机app控制掌控板的灯和显示内容。
实验原理与内容
sIoT可以在局域网上实现mqtt消息的通讯。借助sIoT可以通过手机app实现局域网内的设备操控。
实验软硬件环境
- 硬件:掌控板
- 软件:Mind+ SIoT1.3
实验过程
实验一
实现sIoT配置并实现sIoT上mqtt消息的通讯。
- 首先将电脑和掌控板同时连接到实验室的wifi,在同一wifi环境下才能进行实验。
- 在Windows用管理员身份打开cmd并输入命令
netstat -abon
查看本机端口使用情况。 - 打开IoT1.3目录下的json文件其中端口不能与本机正在使用的端口重复,否则会因为端口占用导致出错。这里我把端口改成空闲端口8089:
- 打开运行SIoT:
如图表示成功运行。 - 在浏览器输入本地地址
http://127.0.0.1:8089/
进入SIoT网站
账号密码为json文件中的值,输入可以成功登录。
在发送消息出输入用户名点击发送可以添加设备,此处我添加了学号和姓名为新设备. - 打开Mind+软件,切换到上传模式,并在扩展中选择ESP32主控板,MQTT,WIFI网络服务。将电脑与ESP32掌控板连接后,进行程序的编辑。
/*! * MindPlus * mpython * */
#include <MPython.h>
#include <DFRobot_Iot.h>
// 函数声明
void obloqMqttEventT1(String& message);
// 静态常量
const String topics[5] = {
"2018324126/潘金华","DFRobot/Seifer","","",""};
const MsgHandleCb msgHandles[5] = {
NULL,obloqMqttEventT1,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 ok");
myIot.init("192.168.31.162","siot","","dfrobot", topics, 1883);
myIot.connect();
while (!myIot.connected()) {
yield();}
display.setCursorLine(2);
display.printLine("mqtt ok");
}
void loop() {
if ((buttonA.isPressed())) {
myIot.publish(topic_1, "2018324126潘金华");
display.setCursorLine(3);
display.printLine("发送成功");
}
}
// 事件回调函数
void obloqMqttEventT1(String& message) {
display.setCursorLine(4);
display.printLine(message);
rgb.write(-1, 0x0000FF);
}
- 在MTQQ初始化参数中添加SIoT中自己的账号密码以及设备
对掌控板进行测试:等待掌控板WIFI连接、和MQTT连接
连接成功后按下A按钮发送“2018324126潘金华”到SIoT平台。
接收到mqtt消息后掌控板灯光全亮为蓝***r> 已实现sIoT配置并实现sIoT上mqtt消息的通讯。
实验二
通过AppInventor结合sIoT实现手机app控制掌控板的灯和显示内容。
- 打开 AppInventor平台,导入项目并打包apk,在安卓手机上下载并安装该apk。
安装后(此处信息为后来填写):
- 打开Mind+,导入该项目,更改MQTT初始化参数
/*! * 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,0x0,0x0,0x0,0x60,0x60,0x0,0x0,0x0,0x0,0x0,0x40,0x20,0x0,0x0,0x0,0x0,0x0,0x40,0x20,0x0,0x0,0x0,0x0,0x0,0x40,0x20,0x0,0x0,0x0,0x0,0x0,0x60,0x60,0x0,0x0,0x0,0x0,0x0,0x30,0xc0,0x0,0x0,0x0,0x0,0x0,0x33,0x80,0x0,0x0,0x0,0x1e,0x0,0x22,0x0,0x0,0x0,0x0,0x33,0x80,0x62,0x0,0x3c,0x0,0x0,0x60,0xc0,0x46,0x0,0xee,0x0,0x0,0x40,0xc0,0xc6,0x1,0x83,0x0,0x0,0xc0,0x40,0x86,0x1,0x1,0x80,0x0,0x40,0xc1,0x8c,0x1,0x1,0x80,0x0,0x7c,0x9f,0xff,0x1,0x1,0x80,0x0,0x37,0x98,0x3,0x81,0xa3,0x0,0x0,0x24,0x30,0x0,0x83,0x26,0x0,0x0,0x64,0x30,0x0,0xc6,0x7c,0x0,0x0,0xc7,0xff,0xff,0xf8,0x40,0x0,0x7f,0xff,0xff,0xff,0xff,0xfc,0x0,0xfe,0x0,0x0,0x0,0x1,0xff,0xc0,0xc0,0x0,0x0,0x0,0x0,0x1,0xc0,0xc0,0x0,0x0,0x0,0x0,0x0,0xc0,0xc0,0x0,0x0,0x0,0x0,0x0,0xc0,0xc0,0x0,0x0,0x0,0x0,0x0,0xc0,0xc0,0x0,0x0,0x0,0x0,0x0,0xc0,0xc0,0x3f,0xff,0xff,0xe0,0x1,0x80,0xdc,0x0,0x0,0x0,0x3,0xf9,0x80,0xcf,0xff,0xff,0xff,0xfc,0x5,0x80,0xcb,0xff,0xff,0xff,0xff,0xf5,0x80,0xcb,0xf8,0xff,0xff,0x9f,0xf9,0x80,0xcb,0xf8,0x7f,0xff,0xf,0xf9,0x80,0xcb,0xf8,0x7f,0xff,0xf,0xe9,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);
}
- 对掌控板进行测试,等待WIFI和MQTT连接成功后,在app上点击发送
- 本程序可以发送rgb值改变灯光。发送在app发送消息"0,0,255",为蓝***r>
5.发送在app发送消息"255,165,79",为浅黄***r>
测试/调试及实验结果分析
siot平台消息列表
实验一:
实验二:
实验结论与体会
通过这次实验让我学习到了掌控板可以借助sIoT可以在局域网上实现mqtt消息的通讯,也可以通过app控制发送消息。