-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode
More file actions
153 lines (128 loc) · 3.88 KB
/
Code
File metadata and controls
153 lines (128 loc) · 3.88 KB
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
//#include <SerialESP8266wifi.h>
#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
#define FIREBASE_HOST "***************" //Your Firebase Project URL goes here without "http:" , "\" and "/"
#define FIREBASE_AUTH "***************" //Your Firebase Database Secret goes here
#define WIFI_SSID "***************" //your WiFi SSID for which yout NodeMCU connects
#define WIFI_PASSWORD "***********" //Password of your wifi network
#define Relay1 12 //D6
int val1;
#define Relay2 14 //D5
int val2;
#define Relay3 4 //D2
int val3;
#define Relay4 5 //D1
int val4;
FirebaseData firebaseData;
void setup()
{
Serial.begin(115200); // Select the same baud rate if you want to see the datas on Serial Monitor
pinMode(Relay1,OUTPUT);
pinMode(Relay2,OUTPUT);
pinMode(Relay3,OUTPUT);
pinMode(Relay4,OUTPUT);
digitalWrite(Relay1,HIGH);
digitalWrite(Relay2,HIGH);
digitalWrite(Relay3,HIGH);
digitalWrite(Relay4,HIGH);
WiFi.begin(WIFI_SSID,WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status()!=WL_CONNECTED)
{
Serial.print(" . ");
delay(500);
}
Serial.println();
Serial.print("connected:");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
Firebase.setInt(firebaseData,"S1",0); //Here the varialbe"S1","S2","S3" and "S4" needs to be the one which is used in our Firebase and MIT App Inventor
Firebase.setInt(firebaseData,"S2",0);
Firebase.setInt(firebaseData,"S3",0);
Firebase.setInt(firebaseData,"S4",0);
}
void firebasereconnect()
{
Serial.println("Trying to reconnect");
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}
void loop()
{
// if (Firebase.failed())
// {
// Serial.print("setting number failed:");
// Serial.println(Firebase.error());
//firebasereconnect();
//return;
//}
val1= Firebase.getString(firebaseData,"/HOME_AUTOMATION/S1") ? 1 : 0; // You must change name of firebaseData suitable with your project in MIT App Inventor
if(val1==1) {
if(firebaseData.stringData() == "1")
{
digitalWrite(Relay1,LOW);
Serial.println("light 1 ON");
}
else
{
digitalWrite(Relay1,HIGH);
Serial.println("light 1 OFF");
}
}
else
{
Serial.println("error getting data from firebase");
}
val2= Firebase.getString(firebaseData,"/HOME_AUTOMATION/S2") ? 1 : 0; // You must change name of firebaseData suitable with your project in MIT App Inventor
if(val2==1)
{
if(firebaseData.stringData() == "1")
{
digitalWrite(Relay2,LOW);
Serial.println("light 2 ON");
} else
{
digitalWrite(Relay2,HIGH);
Serial.println("light 2 OFF");
}
}
else
{
Serial.println("error getting data from firebase");
}
val3= Firebase.getString(firebaseData,"/HOME_AUTOMATION/S3") ? 1 : 0; // You must change name of firebaseData suitable with your project in MIT App Inventor
if(val3==1)
{
if(firebaseData.stringData() == "1")
{
digitalWrite(Relay3,LOW);
Serial.println("light 3 ON");
}
else
{
digitalWrite(Relay3,HIGH);
Serial.println("light 3 OFF");
}
}
else
{
Serial.println("error getting data from firebase");
}
val4= Firebase.getString(firebaseData,"/HOME_AUTOMATION/S4") ? 1 : 0; // You must change name of firebaseData suitable with your project in MIT App Inventor
if(val4==1)
{
if(firebaseData.stringData() == "1")
{
digitalWrite(Relay4,LOW);
Serial.println("light 4 ON");
}
else
{
digitalWrite(Relay4,HIGH);
Serial.println("light 4 OFF");
}
}
else
{
Serial.println("error getting data from firebase");
}
}