-
Notifications
You must be signed in to change notification settings - Fork 1
/
ardyhome.ino.ino
176 lines (126 loc) · 6.06 KB
/
ardyhome.ino.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
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
#define WIFI_SSID "XXXXXXXXXX"
#define WIFI_PASSWORD "XXXXXXXXXXXXXX"
#define FIREBASE_HOST "XXXXXXX"
#define FIREBASE_AUTH "XXXXXX"
#include <ESP8266WiFi.h> // esp8266 library
#include <FirebaseArduino.h> // firebase library
#include <Servo.h> //motor library
#include<stdio.h>
//motor
Servo servorad1;
//lights
int light_one;
int light_two;
int light_three;
int light_four;
int luxOne;
int luxTwo;
int luxThree;
int luxFour;
//motor
int rad;
//fan
int ACsetting;
void setup() {
//set range and frequency
analogWriteRange(100); //instead of 0 - 1024
analogWriteFreq(10000);
//LED pins
pinMode(D0, OUTPUT); //YELLOW LED
pinMode(D1, OUTPUT); //BLUE LED
pinMode(D2, OUTPUT); //GREEN LED
pinMode(D3, OUTPUT); //WHITE LED
Serial.begin(9600);
//connect to wifi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
//sucess
Serial.println();
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
Serial.print("IP Address is : ");
Serial.println(WiFi.localIP()); //print local IP address
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); // connect to firebase
Serial.print("Connected to ");
Serial.println(FIREBASE_HOST);
//Set inital state (0 ON 1 OFF)
Serial.println("Setting Initial states");
Serial.println("...");
Firebase.setInt("LIGHT_ONE", 1);
Firebase.setInt("LIGHT_TWO", 1);
Firebase.setInt("LIGHT_THREE", 1);
Firebase.setInt("LIGHT_FOUR", 1);
Firebase.setInt("lux1", 100);
}
void loop() {
//Loop get info from db and assign it locally
rad = Firebase.getInt("motor1");
ACsetting = Firebase.getInt("AC");
light_one = Firebase.getInt("LIGHT_ONE");
luxOne = Firebase.getInt("lux1");
light_two = Firebase.getInt("LIGHT_TWO");
luxTwo = Firebase.getInt("lux2");
light_three = Firebase.getInt("LIGHT_THREE");
luxThree = Firebase.getInt("lux3");
light_four = Firebase.getInt("LIGHT_FOUR");
luxFour = Firebase.getInt("lux4");
//FAN
analogWrite(D4, ACsetting);
Serial.print("AC SPEED: ");
Serial.print(ACsetting);
Serial.println("%");
//MOTOR
servorad1.attach(14);
servorad1.write(rad);
Serial.print("Motor at angle: ");
Serial.println(rad);
//LIGHTS
if (light_one == 0) { // compare the input of led status received from firebase
Serial.print("Light ONE turned ON Brightness: ");
Serial.print(luxOne);
Serial.println("%");
analogWrite(D0, luxOne);
}
else if (light_one == 1) { // compare the input of led status received from firebase
Serial.println("Light ONE turned OFF (D0)");
analogWrite(D0, LOW);
}
if (light_two == 0) { // compare the input of led status received from firebase
Serial.print("Light TWO turned ON Brightness: ");
Serial.print(luxTwo);
Serial.println("%");
analogWrite(D1, luxTwo);
}
else if (light_two == 1) { // compare the input of led status received from firebase
Serial.println("Light TWO turned OFF (D1)");
digitalWrite(D1, LOW); // make external led OFF
}
if (light_three == 0) { // compare the input of led status received from firebase
Serial.print("Light THREE turned ON Brightness: ");
Serial.print(luxThree);
Serial.println("%");
analogWrite(D2, luxThree); // make external led ON
}
else if (light_three == 1) { // compare the input of led status received from firebase
Serial.println("Light THREE turned OFF (D2)");
digitalWrite(D2, LOW); // make external led OFF
}
if (light_four == 0) { // compare the input of led status received from firebase
Serial.print("Light FOUR turned ON Brightness: ");
Serial.print(luxFour);
Serial.println("%");
analogWrite(D3, luxFour); // make external led ON
}
else if (light_four == 1) { // compare the input of led status received from firebase
Serial.println("Light FOUR turned OFF (D3)");
digitalWrite(D3, LOW); // make external led OFF
}
else {
Serial.println("Error");
}
}