sIoT及安卓app实现mqtt实验

一、实验目的及要求
【实验一】实现sIoT配置并实现sIoT上mqtt消息的通讯。
【实验二】通过AppInventor结合sIoT实现手机app控制掌控板的灯和显示内容。
二、实验原理与内容
sIoT可以在局域网上实现mqtt消息的通讯。借助sIoT可以通过手机app实现局域网内的设备操控。
三、实验软硬件环境
硬件:掌控板
软件:Mind+
实验一:
在这里插入图片描述

/*!
 * MindPlus
 * mpython
 *
 */
#include <MPython.h>
#include <DFRobot_Iot.h>
// 函数声明
void obloqMqttEventT0(String& message);
// 静态常量
const String topics[5] = {"DFRobot/Seifer","","","",""};
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("成功连接WI-FI");
	delay(1000);
	myIot.init("192.168.31.219","siot","","dfrobot", topics, 1883);
	myIot.connect();
	while (!myIot.connected()) {yield();}
	display.setCursorLine(2);
	display.printLine("MQTT连接成功");
}
void loop() {
	if ((buttonA.isPressed())) {
		myIot.publish(topic_0, "2018324108/陈镱");
		rgb.write(-1, 0x0000FF);
	}
}


// 事件回调函数
void obloqMqttEventT0(String& message) {
	rgb.write(-1, 0xFF0000);
	display.setCursorLine(3);
	display.printLine("成功接收");
}

实验二
在这里插入图片描述

/*!
 * 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,0x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1f,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] = {"2018324108/陈镱","","","",""};
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.219","siot","","dfrobot", topics, 1883);
	myIot.connect();
	while (!myIot.connected()) {yield();}
	display.fillScreen(0);
	display.setCursor(0, 0);
	display.print("连接成功");
}
void loop() {

}

// 事件回调函数
void obloqMqttEventT0(String& message) {
	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);
}

实验一实验结果:
在这里插入图片描述
实验二结果:
发送:
在这里插入图片描述
接收:
在这里插入图片描述
控制颜色蓝色:
发送:
在这里插入图片描述
结果:
在这里插入图片描述
控制绿色:
在这里插入图片描述
接收:
在这里插入图片描述
实验结论与体会:
1、本次实验我学会了实现sIoT配置并实现sIoT上mqtt消息的通讯和通过AppInventor结合sIoT实现手机app控制掌控板的灯和显示内容,发现自己的不足,对实验的设备和实验的操作原理的理解不足,实验完成速度慢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值