-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lighting system 3
131 lines (123 loc) · 3.66 KB
/
Lighting system 3
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
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#define FIREBASE_HOST "**********.firebaseio.com" //the Firebase Project URL goes here without "http:" , "\" and "/"
#define FIREBASE_AUTH "**********" //Firebase Database Secret
#define WIFI_SSID "Roo"
#define WIFI_PASSWORD "qwertyui"
#define led D7 // LED on Pin D7/GPIO 13 of NodeMCU
#define switch1 D0 // switch connected to D1 on nodemcu
int pirop; // Place to store power-saver mode input
int x1;
int y;
int b;
int switchstat;
int analogin = A0;
float vout = 0.0;
float vin = 0.0;
float avgvin = 0.0;
float r1 = 30000.0;
float r2 = 7500.0;
int value = 0;
int i =0;
/****************************/
/****************************/
/****************************/
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(led, OUTPUT);
pinMode(switch1, INPUT);
Serial.println("setup");
switchstat = digitalRead(switch1);
if (switchstat){
Serial.println("switch ON");
digitalWrite(led, HIGH);}
WiFiManager wifiManager;
wifiManager.autoConnect("SSL");
IPAddress ip = WiFi.localIP();
Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH); //connect to firebase cloud
Firebase.setInt("S15",0); //firebase variable S9 to get LED status
Firebase.setInt("S10",0); //variable to get brightness level
Firebase.setInt("S11",0); //variable to get power saving status
Firebase.setInt("S12",0); //variable to push battery percentage to the cloud and to the apps
Firebase.setInt("S13",0); //variable to push the power consumed to the cloud and to the apps
Firebase.setInt("S14",0);
}
void voltageread(){
value = analogRead(analogin);
vout = (value * 3.3)/ 1024.0;
vin = vout / (r2/(r1+r2));
Serial.print("Input V=");
Serial.println(vin,2);
if (vin > 8.99 && vin <12.69){
Firebase.setInt("S14",vin);
vin = vin - 9.0;
vin = (vin/0.036);
vin = ((int)vin);
Serial.println("battery level: ");
Serial.print(vin);
Serial.print("%");
Firebase.setInt("S12",vin);}
else{
vin = 0;
Firebase.setInt("S12",vin);
}
delay(1000);
}
void firebasereconnect()
{
Serial.println("Trying to reconnect");
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}
void loop()
{
Serial.println("started");
switchstat = digitalRead(switch1);// when switch on board the system is ON
if (switchstat){
Serial.println("switch ON");
digitalWrite(led, HIGH);}
if (!switchstat) //when switch on board the system is OFF
{
if (Firebase.failed())
{
Serial.print("setting number failed:");
Serial.println(Firebase.error());
firebasereconnect(); //If connection to firebase fails reconnect
return;
}
else {
x1 = Firebase.getString("S15").toInt();//get firebase variable S9 value and store it in x1
b = Firebase.getString("S11").toInt();//get firebase variable S11 value and store it in b
pirop = Firebase.getString("S10").toInt();//get firebase variable S10 value and store it in pirop
if(x1==1 && pirop==0)//If power ON button pressed and
{
//digitalWrite(led, HIGH);
Serial.println("LED ON");
if (b)
{
analogWrite(led, b);
Serial.println("brightness set to");
Serial.print(b);
}
}
else if (x1==0)
{
digitalWrite(led, LOW);
Serial.println("LED OFF");
//ESP.deepSleep(10000, WAKE_RF_DEFAULT);
}
if (pirop == 1)
{
analogWrite(led, 500);
Serial.println("power saver mode");
}
else {
Serial.println("Power saver mode off");
}
}
}
}