概述
一、实验目的及要求
【实验一】实现sIoT配置并实现sIoT上mqtt消息的通讯。
【实验二】通过AppInventor结合sIoT实现手机app控制掌控板的灯和显示内容。
二、实验原理与内容
sIoT可以在局域网上实现mqtt消息的通讯。借助sIoT可以通过手机app实现局域网内的设备操控。
三、实验软硬件环境
硬件:掌控板
软件:Mind+
四、实验过程
【实验一】
1、先下载SIoT1.2,打开运行,如下图所示。
2、打开命令行提示符,得知本电脑的ip地址,打开浏览器,输入(本机ip地址+:8888),输入用户名和密码,创建两个项目。
3、打开mind+与掌控版连接,在扩展板选择掌控板,在网络服务中选择WIFI和MQTT.
4、编写代码:
核心代码:
/*!
* MindPlus
* mpython
*
*/
#include <MPython.h>
#include <DFRobot_Iot.h>
// 函数声明
void obloqMqttEventT0(String& message);
// 静态常量
const String topics[5] = {"lf/1号","lyz/1号","","",""};
const MsgHandleCb msgHandles[5] = {obloqMqttEventT0,NULL,NULL,NULL,NULL};
// 创建对象
DFRobot_Iot myIot;
// 主程序开始
void setup() {
mPython.begin();
myIot.setMqttCallback(msgHandles);
display.setCursor(42, 22);
display.print("Mind+");
myIot.wifiConnect("abc", "123456..");
while (!myIot.wifiStatus()) {yield();}
display.fillScreen(0);
display.setCursor(42, 22);
display.print("连接成功");
myIot.init("192.168.137.1","602","","iot", topics, 1883);
myIot.connect();
while (!myIot.connected()) {yield();}
display.setCursor(42, 22);
display.print("MQTT连接成功");
delay(1000);
}
void loop() {
if ((buttonA.isPressed())) {
myIot.publish(topic_0, "hello");
display.setCursor(42, 22);
display.print("发送成功");
}
}
// 事件回调函数
void obloqMqttEventT0(String& message) {
display.setCursor(42, 22);
display.print(message);
}
将代码上传到设备,连接WIFI 成功后,按下A键发送信息并接受伙伴的信息。
5、实验效果:
双方都发送和接收信息成功。
【实验二】
1、打开浏览器,搜索App inventor,登录进去,点击导入项目,把老师发的LightColorCtr.aia导进去。
点击逻辑设计,修改相关变量:SIOT的登录名、登录密码,个人IP地址。
2、点击打包apk,选择打包apk到电脑,并发送到手机上,在手机上安装好app后,打开mind+,导入老师发的AppCtrLightColor.sb3.连接好掌控板,点击上传设备。用手机上的MQTT IOT进行测试。
代码截图:
核心代码:
/*!
* 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] = {"App/lightCtr","","","",""};
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("abc", "123456..");
while (!myIot.wifiStatus()) {yield();}
display.fillScreen(0);
display.setCursor(0, 0);
display.print("WIFI已连接");
myIot.init("178.16.206.80","SyW1ZPJi57","","dfrobot", 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((dfstring.substring(message,0,0,0,mind_n_DouHao1)));
mind_n_R = (String("123").toInt());
display.setCursor(40, 44);
display.print((dfstring.substring(message,0,(mind_n_DouHao1 + 1),0,mind_n_DouHao2)));
mind_n_G = (String("123").toInt());
display.setCursor(80, 44);
display.print((dfstring.substring(message,0,(mind_n_DouHao2 + 1),0,(String(message).length()))));
mind_n_B = (String("123").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);
}
3、实验效果:
五、实验结果分析与结论
在实验一中,合作双方的个人电脑需要连接在同一个WiFi热点中。MQTT初始化参数中的服务器地址是个人电脑的局域网IP地址,双方需要填写一致。Topic_0和Topic_1中的信息要与在Web服务器中创建的设备信息一致。
实验二过程不难,但是要认真细致对待,此次实验让我对“万物互联”这个概念有了更深刻的了解,多动手练习才能体会到物联网实验的乐趣,总之让我受益匪浅。
最后
以上就是时尚荷花为你收集整理的实现sIoT上mqtt消息的通讯以及通过AppInventor结合sIoT实现手机app控制掌控板的灯和显示内容。的全部内容,希望文章能够帮你解决实现sIoT上mqtt消息的通讯以及通过AppInventor结合sIoT实现手机app控制掌控板的灯和显示内容。所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复