aliyun_mqtt.cpp
本次设计利用ESP8266作为系统主控,利用超声波检测门的状态,利用DHT11检测环境温湿度、烟雾传感器检测空气中的气体浓度,利用火焰报警器模块检测火焰状态,使用OLED进行可视化显示,系统显示传感器数据,以及们的状态等。如果环境中烟雾浓度过高,风扇会进行转动将烟雾进行驱散。
一、硬件设计
1、主控
本次设计利用ESP8266作为系统主控,ESP8266是一款 WIFI开发板,可以利用Arduino IDE进行开发,由于ESP8266开发板对应arduino ide的标号与开发板显示标号有差异,因此为了方便进行开发,我们可以在此前创建一些静态变量完成其映射,参考如下:
,
static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;
static const uint8_t D9 = 3;
static const uint8_t D10 = 1;
2、传感器
本次设计利用的传感器包括烟雾传感器、火焰传感器、DHT11、超声波传感器四个传感器,借助arduino IDE框架,我们可以很轻松地获得这些传感器的驱动文件。
烟雾传感器利用模数转换进行实现,由于ESP8266有且只有一个ADC口,借助该ADC口,我们可以实现烟雾传感器的值获取,值的范围大致在0-1024的区间。
烟雾传感器模块:
火焰传感器有两种输出方式:模拟输出、数字输出,分别叫做 AO(模拟输出)、DO(数字输出),由于上面的烟雾传感器已经使用了ADC口,且ESP8266只有一个ADC口,因此火焰传感器只能使用数字输出口获取数据,当检测到火焰的时候,火焰传感器输出0,当没有检测到火焰的时候,火焰传感器输出1.
火焰传感器模块:
DHT11是一种温湿度传感器,他只有一根数据传输线,使用单总线协议进行传输,借助Arduino IDE软件,我们可以方便地进行传感器数据的读取。
DHT11温湿度模块:
超声波传感器模块使用简单,我们只需要发送10s左右的高电平,然后关闭,统计超声波传感器的 ECHIO端(接收端)的高电平时间,此时我们再根据声音在空气中的传播速度,进行距离的测算,实现非接触式测量。
超声波传感器模块:
3、风扇
风扇一般使用直流电机进行驱动扇叶,直流电机是一种简单的点击器件,我们只要控制其导电方向,就可以实现风扇的正反转,如果直流电机两端电平一致,则电机停止。使用简单方便,如果需要实现不同挡位的电机速度,可以使用PWM进行驱动,改变其占空比,实现直流电机调速。
L298N 是一种双H桥电机驱动模块,其中每个H桥可以提供2A的电流,功率部分的供电电压范围是2.5-48v,逻辑部分5v供电,接受5vTTL电平。一般情况下,功率部分的电压应大于6V否则芯片可能不能正常工作。
二、软件设计
1、ESP8266 Arduino IDE 程序设计
ESP8266端作为核心部分,需要负责数据采集、可视化显示、控制器件、上传数据等,对于传感器部分可以借助Arduino IDE中的第三方库方便地完成硬件驱动,可视化器件使用OLED,为了显示中文,本次设计使用了U8G库,控制直流电机非常简单,只是单纯的高低电平,而上传数据到阿里云平台则需要借助 aliyun_mqtt 库这是一个第三方库且并没有上传到Arduino IDE官方,设计的时候我们还需要创建一个.cpp、一个.h文件。下面会附上代码:
#include <Arduino.h> //引入Arduino开发板的基础头文件
//引入连接传感器需要使用的DHT库函数头文件
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <U8g2lib.h> //引入连接OLED显示屏需要使用的U8g2库函数头文件
//引入Arduino底层SPI和I2C通信协议相关的库函数头文件
#include <SPI.h>
#include <Wire.h>
#include <ESP8266WiFi.h> //引入连接WiFi网络需要使用的ESP8266 Arduino开发环境
#include <PubSubClient.h> //引入连接MQTT服务器需要使用的PubSubClient库函数头文件
#include <ArduinoJson.h> //引入使用JSON数据交互需要使用的ArduinoJson库函数头文件
#include "aliyun_mqtt.h" //引入连接阿里云物联网平台所需的MQTT库函数头文件
#include <AliyunIoTSDK.h> //引入阿里云IoT SDK
//配置阿里云环境和WIFI
#define WIFI_SSID "" //自己的WIFI ID
#define WIFI_PASSWD "" //自己的WIFI密码
#define PRODUCT_KEY "" //自己的PRODUCT_KEY
#define DEVICE_NAME "" //自己的DEVICE_NAME
#define DEVICE_SECRET "" //自己的DEVICE_SECRET
#define REGION_ID "cn-shanghai" //自己的REGION_ID
WiFiClient espClient; //创建WIFI连接对象
PubSubClient mqttClient(espClient); //创建MQTT连接对象
//订阅消息的TOPIC
#define ALINK_TOPIC_PROP_SET ""
//发布数据的TOPIC
#define ALINK_TOPIC_PROP_POST ""
//使用U8G2库驱动OLED显示屏
#ifdef U8X8_HAVE_HW_SPI
#endif
#ifdef U8X8_HAVE_HW_I2C
#endif
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;
static const uint8_t D9 = 3;
static const uint8_t D10 = 1;
//定义DHT11空气温湿度传感器引脚
#define DHTPIN D4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#define Smoke_Pin A0
#define Flame_Pin D3
#define Trig D5
#define Eoho D6
#define Led D7
#define Buz D8
int HCSR04_GetDistance()
{
int Min=0, cm=0;
digitalWrite(Trig,LOW);
delayMicroseconds(2);
digitalWrite(Trig,HIGH);
delayMicroseconds(10);
digitalWrite(Trig,LOW);
Min = pulseIn(Eoho,HIGH);
cm = Min/58;
return cm;
}
uint8_t start_flag=1;
void setup() {
Serial.begin(9600); //初始化串口
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
dht.begin(); //开启DHT11
//配置超声波引脚
pinMode(Trig,OUTPUT);
pinMode(Eoho,INPUT);
//配置led和蜂鸣器引脚
pinMode(Led, OUTPUT);
pinMode(Buz, OUTPUT);
//关闭led和蜂鸣器
digitalWrite(Led, LOW);
digitalWrite(Buz, LOW);
u8g2.begin(); //初始化U8G2库
u8g2.enableUTF8Print(); //设置U8G2支持UTF-8编码的字符输出
u8g2.setFont(u8g2_font_wqy15_t_gb2312); //设置字体
u8g2.clearBuffer(); //清除内部缓冲区
u8g2.setCursor(0, 15);
u8g2.print("正在联网");
u8g2.sendBuffer(); //发送到显示屏
init_wifi(WIFI_SSID, WIFI_PASSWD); //初始化WIFI
AliyunIoTSDK::begin(espClient, PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET, REGION_ID);
mqttClient.setCallback(mqtt_callback); //MQTT回调函数,用来处理微信向ESP8266传的指令
}
uint8_t df=0;
uint8_t mode=0;
void loop() {
if(start_flag==1){
u8g2.clearBuffer(); //清除内部缓冲区
u8g2.setCursor(0, 15);
u8g2.print("正在连接MQTT");
u8g2.sendBuffer(); //发送到显示屏
start_flag=0;
}
//配置MQTT
mqtt_check_connect();//订阅
mqttClient.loop();
//DHT11空气温湿度传感器获取数据
int t = dht.readTemperature();
int h = dht.readHumidity();
int s = map(analogRead(Smoke_Pin),0,1024,0,100);
int f = digitalRead(Flame_Pin);
int dis = HCSR04_GetDistance();
//OLED显示屏显示
u8g2.clearBuffer(); //清除内部缓冲区
//OLED显示温度
u8g2.setCursor(0, 15);
u8g2.print("温度:");
u8g2.print(t);
u8g2.print("C");
//OLED显示湿度
u8g2.setCursor(0, 31);
u8g2.print("湿度:");
u8g2.print(h);
u8g2.print("%");
//OLED显示烟雾
u8g2.setCursor(0, 47);
u8g2.print("烟雾:");
u8g2.print(s);
u8g2.print("%");
//OLED显示火焰
u8g2.setCursor(0, 63);
u8g2.print("火焰:");
if(f==0){
u8g2.print("异常");
}
else{
u8g2.print("正常");
}
//OLED显示门状态
u8g2.setCursor(70, 63);
u8g2.print("门:");
if(dis<10){
u8g2.print("关");
df=0;
}
else{
u8g2.print("开");
df=1;
}
u8g2.setCursor(80, 15);
u8g2.print(dis);
u8g2.print("cm");
u8g2.sendBuffer(); //发送到显示屏
//自动模式下,通过温度调节风扇
if(mode==0){
if(s > 10){
digitalWrite(Buz, HIGH);
digitalWrite(Led, HIGH);
}
else{
digitalWrite(Buz, LOW);
digitalWrite(Led, LOW);
}
}
mqtt_interval_post(t,h,s,f,df);//发布
delay(500);
}
//回调函数-阿里云端数据发送过来之后,此函数进行解析
void mqtt_callback(char *topic, byte *payload, unsigned int length)
{
if (strstr(topic, ALINK_TOPIC_PROP_SET)) { //调用strstr函数判断topic字符串是否包含订阅消息的子串
DynamicJsonDocument doc(100); JSON对象用于保存从MQTT消息payload中解析出来的JSON数据
DeserializationError error = deserializeJson(doc, payload); //将payload所包含的JSON数据解析出来,并存储到doc对象中。
//转换为JsonObject类型的对象,并从中提取"data"键对应的值,即WeChat_data
JsonObject setAlinkMsgObj = doc.as<JsonObject>();
int modeData = setAlinkMsgObj["mode"];
int WeChat_data = setAlinkMsgObj["fan"];
//微信小程序发送消息自动浇水或远程浇水
Serial.print("WeChat_data:");
Serial.println(WeChat_data);
Serial.print("modeData:");
Serial.println(modeData);
if(modeData==1){
mode=1;
}
else if(modeData==2){
mode=0;
}
if(mode==1){
if(WeChat_data==1){
digitalWrite(Led, HIGH);
}
else if(WeChat_data==2){
digitalWrite(Led, LOW);
}
}
}
}
//订阅topic,ESP8266接收来自阿里云的消息(接收微信小程序发的消息)
void mqtt_check_connect() {
while (!mqttClient.connected()) {
while (connect_aliyun_mqtt(mqttClient, PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET)) {
mqttClient.subscribe(ALINK_TOPIC_PROP_SET); //引用订阅消息的topic,用来接收消息
}
}
}
//发布topic,ESP8266上传数据到阿里云(发送数据给微信小程序)
void mqtt_interval_post(int tem,int hum,int smoke, int flame, int door) {
char param[512];
char jsonBuf[1024];
sprintf(param, "{\"currentTemperature\":%d,\"read\":%d,\"smoke\":%d, \"flame\":%d, \"door\":%d}", tem, hum, smoke, flame, door);
Serial.println(param);
mqttClient.publish(ALINK_TOPIC_PROP_POST, param); //引用上传数据的topic,jsonBuf这个是上传的数据
}
//连接WiFi
void init_wifi(const char *ssid, const char *password) {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi does not connect, try again ...");
delay(500);
}
}
aliyun_mqtt.cpp
/*
Aliyun_mqtt.h - Library for connect to Aliyun MQTT server.
*/
#include "aliyun_mqtt.h"
#include <SHA256.h>
#define MQTT_PORT 1883
#define SHA256HMAC_SIZE 32
// Verify tool: http://tool.oschina.net/encrypt?type=2
static String hmac256(const String &signcontent, const String &ds)
{
byte hashCode[SHA256HMAC_SIZE];
SHA256 sha256;
const char *key = ds.c_str();
size_t keySize = ds.length();
sha256.resetHMAC(key, keySize);
sha256.update((const byte *)signcontent.c_str(), signcontent.length());
sha256.finalizeHMAC(key, keySize, hashCode, sizeof(hashCode));
String sign = "";
for (byte i = 0; i < SHA256HMAC_SIZE; ++i)
{
sign += "0123456789ABCDEF"[hashCode[i] >> 4];
sign += "0123456789ABCDEF"[hashCode[i] & 0xf];
}
return sign;
}
static String mqttBroker;
static String mqttClientID;
static String mqttUserName;
static String mqttPassword;
// call this function once
void mqtt_prepare(const char *timestamp,const char *productKey, const char *deviceName,const char *deviceSecret,const char *region)
{
mqttBroker = productKey;
mqttBroker += ".iot-as-mqtt.";
mqttBroker += String(region);
mqttBroker += ".aliyuncs.com";
// Serial.println(mqttBroker);
mqttUserName = deviceName;
mqttUserName += '&';
mqttUserName += productKey;
//Serial.println(mqttUserName);
mqttClientID = deviceName; // device name used as client ID
mqttClientID += "|securemode=3,signmethod=hmacsha256,timestamp=";
mqttClientID += timestamp;
mqttClientID += '|';
//Serial.println(mqttClientID);
}
bool connect_aliyun_mqtt_With_password(PubSubClient &mqttClient, const char *password)
{
mqttClient.setServer(mqttBroker.c_str(), MQTT_PORT);
byte mqttConnectTryCnt = 5;
while (!mqttClient.connected() && mqttConnectTryCnt > 0)
{
Serial.println("Connecting to MQTT Server ...");
if (mqttClient.connect(mqttClientID.c_str(), mqttUserName.c_str(), password))
{
// Serial.println("MQTT Connected!");
return true;
}
else
{
byte errCode = mqttClient.state();
Serial.print("MQTT connect failed, error code:");
Serial.println(errCode);
if (errCode == MQTT_CONNECT_BAD_PROTOCOL || errCode == MQTT_CONNECT_BAD_CLIENT_ID || errCode == MQTT_CONNECT_BAD_CREDENTIALS || errCode == MQTT_CONNECT_UNAUTHORIZED)
{
Serial.println("No need to try again.");
break; // No need to try again for these situation
}
delay(3000);
}
mqttConnectTryCnt -= 1;
}
return false;
}
bool connect_aliyun_mqtt(
PubSubClient &mqttClient,
const char *productKey,
const char *deviceName,
const char *deviceSecret,
const char *region)
{
String timestamp = String(millis());
mqtt_prepare(timestamp.c_str(), productKey, deviceName, deviceSecret, region);
// Generate MQTT Password, use deviceName as clientID
String signcontent = "clientId";
signcontent += deviceName;
signcontent += "deviceName";
signcontent += deviceName;
signcontent += "productKey";
signcontent += productKey;
signcontent += "timestamp";
signcontent += timestamp;
String mqttPassword = hmac256(signcontent, deviceSecret);
//Serial.print("HMAC256 data: ");
//Serial.println(signcontent);
//Serial.print("HMAC256 key: ");
// Serial.println(deviceSecret);
// Serial.println(mqttPassword);
return connect_aliyun_mqtt_With_password(mqttClient, mqttPassword.c_str());
}
aliyun_mqtt.h
/*
Aliyun_mqtt.h - Library for connect to Aliyun MQTT server with authentication by
product key, device name and device secret.
https://www.alibabacloud.com/help/product/30520.htm
*/
#ifndef _ALIYUN_MATT_H
#define _ALIYUN_MATT_H
#include <Arduino.h>
#include <PubSubClient.h>
/**
* Connect to Alibaba Cloud MQTT server. In connection process, it will try several times for
* possible network failure. For authentication issue, it will return false at once.
*
* @param mqttClient: Caller provide a valid PubSubClient object (initialized with network client).
* @param productKey: Product Key, get from Alibaba Cloud Link Platform.
* @param deviceName: Device Name, get from Alibaba Cloud Link Platform.
* @param deviceSecret: Device Secret, get from Alibaba Cloud Link Platform.
*
* @param region: Optional region, use "cn-shanghai" as default. It can be "us-west-1",
* "ap-southeast-1" etc. Refer to Alibaba Cloud Link Platform.
*
*
* @return true if connect succeed, otherwise false.
*/
extern "C" bool connect_aliyun_mqtt(
PubSubClient &mqttClient,
const char *productKey,
const char *deviceName,
const char *deviceSecret,
const char *region = "cn-shanghai");
/**
* Two new added APIs are designed for devices with limited resource like Arduino UNO.
* Since it is hard to calculate HMAC256 on such devices, the calculation can be done externally.
*
* These two APIs should be used together with external HMAC256 calculation tools, e.g.
* http://tool.oschina.net/encrypt?type=2
* They can be used together to replace connectAliyunMQTT on resource-limited devices.
*/
/**
* This API should be called in setup() phase to init all MQTT parameters. Since HMAC256
* calculation is executed extenrally, a fixed timestamp string should be provided, such
* as "23668" etc. The same timestamp string is also used to calculate HMAC256 result.
*
* Other params are similar to them in connectAliyunMQTT.
*/
extern "C" void mqtt_prepare(
const char *timestamp,
const char *productKey,
const char *deviceName,
const char *deviceSecret,
const char *region = "cn-shanghai");
/**
* Use tools here to calculate HMAC256: http://tool.oschina.net/encrypt?type=2
* The calculated result should be defined as constants and passed when call this function.
*/
extern "C" bool connect_aliyun_mqtt_With_password(PubSubClient &mqttClient, const char *password);
#endif
2、微信小程序设计
本人作为嵌入式软件工程师,对于微信小程序的编写并没有特别了解,本次设计借助他人已经的微信小程序源码并再次基础上修改,得到属于我的小程序,参考如下:
index.xml
<view class="button-sp-are">
<button class="weui-btn" style="background-color:{{buttonColor}}" type="primary" size="mini" bindtap="onClickOpen">{{Text1}}</button>
<button class="weui-btn" style="background-color:{{modeColor}}" type="primary" size="mini" bindtap="onClickMode">{{Text2}}</button>
<button class="TemDis" size="mini">温度:{{temperature}}℃</button>
<button class="HumDis" size="mini">湿度:{{humidity}}</button>
<button class="Smoke" size="mini" >烟雾:{{smoke_val}}</button>
<button class="Flame" size="mini" style="background-color:{{flameColor}}" >火焰:{{flame_val}}</button>
<button class="Door" size="mini" style="background-color:{{doorColor}}">门:{{door_val}}</button>
</view>
index.js
import mqtt from'../../utils/mqtt.js';
const aliyunOpt = require('../../utils/aliyun/aliyun_connect.js');
let that = null;
let btn = 0;
let modef = 0;
Page({
data:{
buttonColor: '#E64340',
modeColor: '#E64340',
flameColor:"#2ADB71",
doorColor:"#2ADB71",
Text1:"风扇关",
Text2:"自动模式",
//设置温度值和湿度值
temperature:"",
humidity:"",
smoke_val:"",
flame_val:"",
door_val:"",
client:null,//记录重连的次数
reconnectCounts:0,//MQTT连接的配置
options:{
protocolVersion: 4, //MQTT连接协议版本
clean: false,
reconnectPeriod: 1000, //1000毫秒,两次重新连接之间的间隔
connectTimeout: 30 * 1000, //1000毫秒,两次重新连接之间的间隔
resubscribe: true, //如果连接断开并重新连接,则会再次自动订阅已订阅的主题(默认true)
clientId: '',
password: '',
username: '',
},
aliyunInfo: {
productKey: '', //阿里云连接的三元组 ,请自己替代为自己的产品信息!!
deviceName: '', //阿里云连接的三元组 ,请自己替代为自己的产品信息!!
deviceSecret: '', //阿里云连接的三元组 ,请自己替代为自己的产品信息!!
regionId: 'cn-shanghai', //阿里云连接的三元组 ,请自己替代为自己的产品信息!!
pubTopic: '', //发布消息的主题
subTopic: '', //订阅消息的主题
},
},
onLoad:function(){
that = this;
let clientOpt = aliyunOpt.getAliyunIotMqttClient({
productKey: that.data.aliyunInfo.productKey,
deviceName: that.data.aliyunInfo.deviceName,
deviceSecret: that.data.aliyunInfo.deviceSecret,
regionId: that.data.aliyunInfo.regionId,
port: that.data.aliyunInfo.port,
});
console.log("get data:" + JSON.stringify(clientOpt));
let host = 'wxs://' + clientOpt.host;
this.setData({
'options.clientId': clientOpt.clientId,
'options.password': clientOpt.password,
'options.username': clientOpt.username,
})
console.log("this.data.options host:" + host);
console.log("this.data.options data:" + JSON.stringify(this.data.options));
//访问服务器
this.data.client = mqtt.connect(host, this.data.options);
this.data.client.on('connect', function (connack) {
wx.showToast({
title: '连接成功'
})
console.log("连接成功");
})
//接收消息监听
this.data.client.on("message", function (topic, payload) {
console.log(" 收到 topic:" + topic + " , payload :" + payload);
that.setData({
//转换成JSON格式的数据进行读取
temperature:JSON.parse(payload).currentTemperature,
humidity:JSON.parse(payload).read,
smoke_val:JSON.parse(payload).smoke,
flame_val:JSON.parse(payload).flame,
door_val:JSON.parse(payload).door,
})
if(that.data.flame_val == "1"){
that.setData({
flame_val:"正常",
flameColor:"#2ADB71",
})
console.log(that.data.flame_val)
}
if(that.data.flame_val == "0"){
that.setData({
flame_val:"异常",
flameColor:"#CB3C21",
})
console.log(that.data.flame_val)
}
if(that.data.door_val == "1"){
that.setData({
door_val:"打开",
doorColor:"#2ADB71",
})
console.log(that.data.flame_val)
}
if(that.data.door_val == "0"){
that.setData({
door_val:"关闭",
doorColor:"#CB3C21",
})
console.log(that.data.flame_val)
}
/* wx.showModal({
content: " 收到topic:[" + topic + "], payload :[" + payload + "]",
showCancel: false,
}); */
})
//服务器连接异常的回调
that.data.client.on("error", function (error) {
console.log(" 服务器 error 的回调" + error)
})
//服务器重连连接异常的回调
that.data.client.on("reconnect", function () {
console.log(" 服务器 reconnect的回调")
})
//服务器连接异常的回调
that.data.client.on("offline", function (errr) {
console.log(" 服务器offline的回调")
})
},
onClickMode(){
if(modef==0){
that.sendMode(1);
this.setData({
modeColor: '#2049EE',
Text2:'手动模式',
})
modef=1;
return;
}
else{
that.sendMode(2);
this.setData({
modeColor: '#E64340',
Text2:'自动模式',
})
modef=0;
return;
}
},
sendMode(cmd, data){
let sendData = {
mode: cmd
};
//发布消息
if (this.data.client && this.data.client.connected) {
this.data.client.publish(this.data.aliyunInfo.pubTopic, JSON.stringify(sendData));
console.log("************************")
console.log(this.data.aliyunInfo.pubTopic)
console.log(JSON.stringify(sendData))
} else {
wx.showToast({
title: '请先连接服务器',
icon: 'none',
duration: 2000
})
}
},
onClickOpen() {
if(btn==0){
that.sendCommond(1);
this.setData({
buttonColor: '#2049EE',
Text1:'风扇开',
})
btn=1;
return;
}
else{
that.sendCommond(2);
this.setData({
buttonColor: '#E64340',
Text1:'风扇关',
})
btn=0;
return;
}
},
sendCommond(cmd, data) {
let sendData = {
fan: cmd
};
//此函数是订阅的函数,因为放在访问服务器的函数后面没法成功订阅topic,因此把他放在这个确保订阅topic的时候已成功连接服务器
//订阅消息函数,订阅一次即可 如果云端没有订阅的话,需要取消注释,等待成功连接服务器之后,在随便点击(开灯)或(关灯)就可以订阅函数
/*
this.data.client.subscribe(this.data.aliyunInfo.subTopic,function(err){
if(!err){
console.log("订阅成功");
};
wx.showModal({
content: "订阅成功",
showCancel: false,
})
}) */
//发布消息
if (this.data.client && this.data.client.connected) {
this.data.client.publish(this.data.aliyunInfo.pubTopic, JSON.stringify(sendData));
console.log("************************")
console.log(this.data.aliyunInfo.pubTopic)
console.log(JSON.stringify(sendData))
} else {
wx.showToast({
title: '请先连接服务器',
icon: 'none',
duration: 2000
})
}
}
})
3、阿里云产品流转
三、项目演示
1、程序运行OLED显示数据
由于是很久之前做的,现在东西已经拆除,所以只能在本人以前的视频中截屏,比较模糊。OLED显示信息,同时小程序也显示数据信息,分别是门的状态、风扇状态、温度、湿度、烟雾、火焰等信息,微信小程序数据和设备端同步变化。
2、检测门状态
超声波测算距离,可以判断门是否已经关闭,如果超声波测算的距离比较小,说明门靠近检测装置,门应当是关闭状态,反之门则是开启的状态,同时门如果关闭,显示屏显示关,小程序端门的框也会显示,同时会变红色,反之则是绿色。如下图:
3、检测火焰
检测火焰类似,当打火机碰到火焰传感器,此时检测到火焰,OLED会显示,同时小程序端火焰的位置会变成红色,表示检测到火焰。
4、检测烟雾并自动打开散热
检测到烟雾的时候会自动打开风扇,进行驱散,保持室内烟雾浓度适合。
5、远程控制功能
微信小程序提供了两个按键,分别是模式切换(自动/手动模式),风扇开关,在手动模式下,模式按键是蓝色的,这时我们可以使用按键操作风扇,按钮为蓝色,风扇开,按键为红色,风扇关。
四、项目总结
本次设计使用ESP8266作为主控制器,传感器有温湿度传感器、烟雾传感器、火焰传感器、超声波传感器,利用阿里云进行数据流转,微信小程序获取数据并执行操作,实现物联网。
详情可以参考本人的bilibili地址如下: