-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patht1.ino
85 lines (63 loc) · 1.59 KB
/
t1.ino
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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ESP8266mDNS.h>
void nhandulieu (char * tp, byte * nd, unsigned int length) // tạo hàm nhận dữ liệu
{
String topic(tp);
String noidung= String((char *)nd);
noidung.remove(length);
Serial.println(topic);
Serial.println(noidung);
if(topic=="quạt")
{
if(noidung=="bật")
digitalWrite(5,LOW);
if(noidung=="tắt")
digitalWrite(5,HIGH);
}
if(topic=="đèn")
{
if(noidung=="bật")
digitalWrite(16,LOW);
if(noidung=="tắt")
digitalWrite(16,HIGH);
}
}
WiFiClient c;
PubSubClient MQTT("homeauto.local", 1883, nhandulieu, c);
void setup()
{
Serial.begin(115200);
WiFiManager wifiManager;
wifiManager.autoConnect("Home auto switch");
delay(500);
while (1)
{
delay(500);
if(MQTT.connect("nodemcu"))
break;
}
Serial.println("da ket noi MQTT");
MQTT.subscribe("đèn"); MQTT.subscribe("hello");
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
pinMode(16, OUTPUT);
digitalWrite(16, HIGH);
pinMode(0, INPUT);
}
void loop()
{
if (digitalRead(0) == 0)
{
WiFi.disconnect();
delay(3000);
setup();
}
if (!MQTT.connect("ESP"))
{MQTT.connect("ESP");}
MQTT.loop();
// put your setup code here, to run once:
}