智能开关
演示视频:https://www.bilibili.com/video/BV1qz4y1k7xS/
资料
完整教学视频:
结构
硬件部分 服务器 安卓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
|
#include<Servo.h> #include <SoftwareSerial.h>
#include <ESP8266WiFi.h> #include <PubSubClient.h>
#include "dht11.h" #include "ArduinoJson-v6.15.2.h" #define dht11Pin D2
dht11 dht; Servo left_servo, right_servo;
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.begin(wifiSSID, password); while (WiFi.status() != WL_CONNECTED) { delay(500); } Serial.println("Connected to the WiFi network"); 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); int temp = (float)dht.temperature; int humi = (float)dht.humidity; delay(10); StaticJsonDocument<200> temperature_data; temperature_data["sensor"] = "DHT11"; temperature_data["temp"] = temp; temperature_data["humi"] = humi; serializeJson(temperature_data, 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]) { 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); client.publish(topic, msg ); delay(100); } void loop() { if (!client.connected()) { reconnect_mqtt(); } if (WiFi.status() != WL_CONNECTED) { reconnect_wifi(); } client.loop(); }
|