SIoT及安卓app实现mqtt实验

一、实验目的及要求

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

二、实验原理与内容

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

三、实验软硬件环境

硬件:掌控板
软件:Mind+

四、实验过程

点击下载实验要用的资源
提取码:q82m

【实验一】实现sIoT配置并实现sIoT上mqtt消息的通讯。

(一)电脑连接手机热点

(二)运行SIoT

1、下载SIoT1.2。
SIoT及安卓app实现mqtt实验_第1张图片
2、打开config.json,记录账号602,密码iot。
SIoT及安卓app实现mqtt实验_第2张图片
3、打开SIoT_windows_1_2.exe。
SIoT及安卓app实现mqtt实验_第3张图片
4、搜索cmd进入命令提示符窗口,输入ipconfig,记录无线局域网适配器WLAN的IPv4地址。
SIoT及安卓app实现mqtt实验_第4张图片
5、打开浏览器输入网址“IPv4地址:8888”,比如我的是192.168.43.100:8888。进入SIoT后输入账号602,密码iot登录。
SIoT及安卓app实现mqtt实验_第5张图片
6、发送消息创建两个主题(项目ID/设备名)。
SIoT及安卓app实现mqtt实验_第6张图片
SIoT及安卓app实现mqtt实验_第7张图片

(三)使用Mind+上传代码到掌控板

1、打开Mind+,连接掌控板,配置好图形模块代码,填写电脑连接的热点的名称以及密码。上传到设备,将代码上传到掌控板。另一块掌控板配置同理。

SIoT及安卓app实现mqtt实验_第8张图片

SIoT及安卓app实现mqtt实验_第9张图片
C代码:

#include 
#include 
// 函数声明
void obloqMqttEventT1(String& message);
// 静态常量
const String topics[5] = {
     "2018764207/LGT","2018764236/LYB","","",""};
const MsgHandleCb msgHandles[5] = {
     NULL,obloqMqttEventT1,NULL,NULL,NULL};
// 创建对象
DFRobot_Iot myIot;
// 主程序开始
void setup() {
     
	mPython.begin();
	myIot.setMqttCallback(msgHandles);
	myIot.wifiConnect("lyb", "lyb1234567890;");
	while (!myIot.wifiStatus()) {
     yield();}
	display.setCursorLine(1);
	display.printLine(myIot.getWiFiLocalIP());
	myIot.init("192.168.43.100","602","","iot", topics, 1883);
	myIot.connect();
	while (!myIot.connected()) {
     yield();}
	display.setCursorLine(2);
	display.printLine("Mqtt is OK!");
}
void loop() {
     
	if ((buttonA.isPressed())) {
     
		display.fillScreen(0);
		myIot.publish(topic_0, "你好!");
		display.setCursorLine(3);
		display.printLine("发送成功");
	}
}
// 事件回调函数
void obloqMqttEventT1(String& message) {
     
	display.setCursorLine(4);
	display.printLine(message);
	rgb.write(0, 0x0000FF);
}

(四)测试

显示出IP地址以及“Mqtt is OK”表示WiFi以及SIoT连接成功。
SIoT及安卓app实现mqtt实验_第10张图片
掌控板1发送消息测试:按下A键,掌控板1会清屏并显示“发送成功”,掌控板2会显示“你好”。
掌控板1接收消息测试:掌控板2发送消息过来时,掌控板1会亮蓝灯并在第四行显示“开灯”。
SIoT及安卓app实现mqtt实验_第11张图片

【实验二】通过AppInventor结合sIoT实现手机app控制掌控板的灯和显示内容。

(一)电脑连接手机热点

(二)运行SIoT

详细步骤见【实验一】

(三)Mind+

1、打开Mind+,连接掌控板,配置好图形模块代码,填写电脑连接的热点的名称以及密码。将代码上传到掌控板。
SIoT及安卓app实现mqtt实验_第12张图片
SIoT及安卓app实现mqtt实验_第13张图片
SIoT及安卓app实现mqtt实验_第14张图片
SIoT及安卓app实现mqtt实验_第15张图片

C代码:

#include 
#include 
#include 
// 动态变量
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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x83,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] = {
     "2018764236/LYB","","","",""};
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("lyb", "lyb1234567890;");
	while (!myIot.wifiStatus()) {
     yield();}
	display.fillScreen(0);
	display.setCursor(0, 0);
	display.print("WIFI已连接");
	myIot.init("192.168.43.100","602","","iot", topics, 1883);
	myIot.connect();
	while (!myIot.connected()) {
     yield();}
	display.fillScreen(0);
	display.setCursor(0, 0);
	display.print("连接成功");
}
void loop() {
     
}
// 事件回调函数
void obloqMqttEventT0(String& message) {
     
	mind_n_DouHao1 = (dfstring.indexOf(",",message));
	mind_n_DouHao2 = (dfstring.lastIndexOf(",",message));
	display.setCursor(0, 44);
	display.print("");
	mind_n_R = (String((dfstring.substring(message,0,1,0,mind_n_DouHao1))).toInt());
	display.setCursor(40, 44);
	display.print("");
	mind_n_G = (String((dfstring.substring(message,0,(mind_n_DouHao1 + 1),0,mind_n_DouHao2))).toInt());
	display.setCursor(80, 44);
	display.print("");
	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);
}

(四)MIT App Inventor

1、点击跳转到:MIT App Inventor2
2、使用QQ登录
SIoT及安卓app实现mqtt实验_第16张图片
3、导入lightColorCtr.aia

SIoT及安卓app实现mqtt实验_第17张图片
4、切换到逻辑设计模式
SIoT及安卓app实现mqtt实验_第18张图片
5、配置参数

6、打包APK安装到手机上。
SIoT及安卓app实现mqtt实验_第19张图片

(五)测试

打开安装软件,连接服务器,调节安装软件的灯光颜色,掌控板会显示连接成功,灯光颜色会随手机的调节变化。
SIoT及安卓app实现mqtt实验_第20张图片

SIoT及安卓app实现mqtt实验_第21张图片

SIoT及安卓app实现mqtt实验_第22张图片

SIoT及安卓app实现mqtt实验_第23张图片SIoT及安卓app实现mqtt实验_第24张图片

SIoT及安卓app实现mqtt实验_第25张图片

你可能感兴趣的:(物联网)