智能开关

演示视频:https://www.bilibili.com/video/BV1qz4y1k7xS/

图像

Screenshot_2020-12-02-12-39-30-190_com.example.do

资料

完整教学视频:

结构

硬件部分 服务器 安卓APP

通信协议:MQTT

清单

NodeMCU:ESP8266串口wifi模块 NodeMCU Lua V3物联网开发板 CH340 CP2102(淘宝搜索)
舵机:180度
电源模块:提供5v,3.3v输出
dht11温湿度传感器
杜邦线 :公对公 公对母 母对母

esp8266库:https://arduino.esp8266.com/stable/package_esp8266com_index.json

开发工具

  • Arduino IDE
  • EMQ X
  • AndroidStudio

Android APP代码

GitHub:https://github.com/Asimok/dorm_light

nodeMCU代码

GitHub:https://github.com/Asimok/unoproject_backup/tree/master/dorm_light/dorm_light_temp_8266

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
智能开关nodemcu代码
D0 舵机左
D1 舵机右
D2 dht11

*/
/*arduino安装自带*/
#include<Servo.h>
#include <SoftwareSerial.h>
/*自行安装*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
/*引入本地头文件*/
#include "dht11.h"
#include "ArduinoJson-v6.15.2.h"
#define dht11Pin D2 //定义温湿度针脚号为D2号引脚
/*实例化对象*/
dht11 dht;
Servo left_servo, right_servo;

/**MQTT服务器参数配置*/
const char* wifiSSID = "wifi名称";
const char* password = "密码";
const char* mqttServer = "IP";
const int mqttPort = 1883;
const char* clientId = "设备id";
const char* topic = "订阅主题";

WiFiClient espClient;
PubSubClient client(espClient);

/*开关状态*/
bool leftStatue = false;
bool rightStatue = false;

void setup() {
delay(1000);
switchlight("closeLeft");
delay(500);
switchlight("closeRight");
delay(1000);

Serial.begin(9600);
/*连接wifi*/
WiFi.begin(wifiSSID, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
// Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
/*连接MQTT服务器*/
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect(clientId)) {
Serial.println("MQTT connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}

pinMode(dht11Pin, OUTPUT);

char buff[50];
memset(buff, 0, sizeof(buff));
strcpy(buff, clientId);
const char *buff2 = " 上线";
strcat(buff, buff2);

//发送连接成功消息
client.publish(topic, buff );
//订阅主题
client.subscribe(topic);
delay(100);
// 初始化开关状态
switchlight("closeLeft");
delay(1000);
switchlight("closeRight");
}
void get_dht11()
{

char msg[500];
int tol = dht.read(dht11Pin); //将读取到的值赋给tol
int temp = (float)dht.temperature; //将温度值赋值给temp
int humi = (float)dht.humidity; //将湿度值赋给humi

delay(10); //延时1秒
StaticJsonDocument<200> temperature_data;
temperature_data["sensor"] = "DHT11";
temperature_data["temp"] = temp;
temperature_data["humi"] = humi;
serializeJson(temperature_data, msg);
//Serial.println(msg);
client.publish(topic, msg);
}
/**
断开重连
*/
void reconnect_mqtt() {
while (!client.connected()) {
Serial.println("reConnecting to MQTT...");
if (client.connect(clientId)) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
void reconnect_wifi()
{
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("reConnected to the WiFi network");
}

void callback(char* topic, byte* payload, unsigned int length) {
//收到消息
Serial.print("Message:");
char a[2000] = "";
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
a[i] = (char)payload[i];
}

docode(a);
}
void docode(char json[2000])
{
// 解析指令
// {"code":"openLeft"}
StaticJsonDocument<200> doc;
deserializeJson(doc, json);
const char* code = doc["code"];
Serial.println();
Serial.println(code);
String tempcode;
tempcode = String(code);
if (tempcode == "get_dht11")
get_dht11();
else if (tempcode == "get_light_status")
send_light_data();
else
switchlight(tempcode);
}

void switchlight(String tempcode)
{

delay(50);
if (tempcode == "openLeft")
{
left_servo.attach(D0);
left_servo.write(82);
delay(300);
left_servo.write(45);
delay(100);
left_servo.detach();
leftStatue = true;
send_light_data();
}
else if (tempcode == "openRight")
{
right_servo.attach(D1);
right_servo.write(8);
delay(300);
right_servo.write(65);
delay(100);
right_servo.detach();
rightStatue = true;
send_light_data();
}
else if (tempcode == "closeLeft")
{
left_servo.attach(D0);
left_servo.write(2);
delay(300);
left_servo.write(45);
delay(100);
left_servo.detach();
leftStatue = false;
send_light_data();

}
else if (tempcode == "closeRight")
{
right_servo.attach(D1);
right_servo.write(130);
delay(300);
right_servo.write(65);
delay(100);
right_servo.detach();
rightStatue = false;
send_light_data();
}
}

void send_light_data() {
delay(10);
char msg[500];
StaticJsonDocument<200> light_data;
light_data["sensor"] = "servo";
light_data["left"] = leftStatue;
light_data["right"] = rightStatue;
serializeJson(light_data, msg);
// Serial.println(msg);

client.publish(topic, msg );
delay(100);
}
void loop() {
//重连机制
if (!client.connected()) {
reconnect_mqtt();
}
if (WiFi.status() != WL_CONNECTED)
{
reconnect_wifi();
}
client.loop();
}