sIoT及安卓app实现mqtt实验
一、实验目的及要求
【实验一】实现sIoT配置并实现sIoT上mqtt消息的通讯。
【实验二】通过AppInventor结合sIoT实现手机app控制掌控板的灯和显示内容。
二、实验原理与内容
sIoT可以在局域网上实现mqtt消息的通讯。借助sIoT可以通过手机app实现局域网内的设备操控。
三、实验软硬件环境
硬件:掌控版
软件:Mind+,AppInventor
四、实验过程(实验步骤、记录、数据、分析)
实验一:实现sIoT配置并实现sIoT上mqtt消息的通讯。
核心代码:
/*!
* MindPlus
* mpython
*
*/
#include <MPython.h>
#include <DFRobot_Iot.h>
// 函数声明
void obloqMqttEventT0(String& message);
// 静态常量
const String topics[5] = {
"2018764243","2018764217","","",""};
const MsgHandleCb msgHandles[5] = {
obloqMqttEventT0,NULL,NULL,NULL,NULL};
// 创建对象
DFRobot_Iot myIot;
// 主程序开始
void setup() {
mPython.begin();
myIot.setMqttCallback(msgHandles);
myIot.wifiConnect("DADY", "88888888");
myIot.init("192.168.43.156","602","","iot", topics, 1883);
display.setCursor(42, 22);
display.print("正在连接");
while (!myIot.wifiStatus()) {
yield();}
myIot.connect();
display.setCursor(42, 22);
display.print("wifi 连接成功");
while (!myIot.connected()) {
yield();}
delay(1000);
display.fillScreen(0);
display.setCursor(42, 22);
display.print("MQTT 连接成功");
delay(1000);
display.fillScreen(0);
}
void loop() {
if ((buttonA.isPressed())) {
myIot.publish(topic_1, "2018764243 吕宣杰");
}
if ((buttonB.isPressed())) {
myIot.publish(topic_0, "hello");
}
}
// 事件回调函数
void obloqMqttEventT0(String& message) {
display.fillScreen(0);
display.setCursorLine(1);
display.printLine(message);
}
实验结果: