-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirebase-basic-iot.ino
185 lines (157 loc) · 3.86 KB
/
Firebase-basic-iot.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
177
178
179
180
181
182
183
184
185
/*=== Libraries ===*/
#include <WiFiManager.h>
#include <FirebaseESP8266.h>
#include "Interval.h"
/*
* config.h should contain all the required credentials used by firebase to connect to the database
* As it contains all the sensitive informations, therefore it is not uploaded to Github
* FIREBASE_HOST "PROJECT_ID.firebaseio.com"
* FIREBASE_AUTH "DATABASE_SECRET"
*/
#include "config.h"
/*=== All the GPIO Pins ===*/
#define LED 2
#define OutputPin 5
#define analogInput A0
#define digitalInput 4
/*Define the Firebase Data object*/
FirebaseData fbdo;
/*Define the Interval Objects*/
Interval showDebugDataSchedule;
Interval inputHandler;
Interval outputHandler;
/*=== prototypes ===*/
void WiFiSetup(void);
void configModeCallback(WiFiManager *wifi);
void showDebugData();
void handleInput();
void handleOutput();
void setup()
{
// For debugging
Serial.begin(115200);
Serial.setDebugOutput(true);
// Setting GPIO pins as Inputs/Outputs
pinMode(LED, OUTPUT);
pinMode(OutputPin, OUTPUT);
pinMode(analogInput, INPUT);
pinMode(digitalInput, INPUT);
// Setting and connecting to Wifi using WiFiManager
WiFiSetup();
// Firebase setup section
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
fbdo.setBSSLBufferSize(1024, 1024);
fbdo.setResponseSize(1024);
// Interval task setup
showDebugDataSchedule.setTicker(true);
showDebugDataSchedule.setDelay(10000); // every 10 seconds
inputHandler.setTicker(true);
inputHandler.setDelay(1300);
outputHandler.setTicker(true);
outputHandler.setDelay(1200);
}
void loop()
{
showDebugDataSchedule.tick(showDebugData);//every 10 seconds
inputHandler. tick( handleInput );// every 1.3 sec
outputHandler. tick(handleOutput );// every 1.2 sec
}
/*=== functions ===*/
void WiFiSetup()
{
WiFiManager wifi;
wifi.setDebugOutput(false);
wifi.setTimeout(300);
wifi.setAPCallback(configModeCallback);
if(!wifi.autoConnect("FirebaseIOT", "12345678"))
{
Serial.println("Couldn't Connect to remote AP");
ESP.restart();
digitalWrite(LED, LOW);
delay(2500);
digitalWrite(LED, HIGH);
delay(2500);
}
else
{
// blink thrice on connection
for(int i = 0; i < 3; i++)
{
digitalWrite(LED, LOW);
delay(200);
digitalWrite(LED, HIGH);
delay(200);
}
}
}
void configModeCallback (WiFiManager *wifi)
{
Serial.println("Entered config mode: ");
Serial.println(WiFi.softAPIP());
Serial.println(wifi->getConfigPortalSSID());
}
void showDebugData()
{
if(Firebase.get(fbdo, "/devices"))
{
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
Serial.print("VALUE: ");
Serial.println(fbdo.jsonString());
}
else
{
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
Serial.println("...");
Serial.println();
}
}
void handleInput()
{
Serial.println("handleInput");
int a_data = analogRead(analogInput);
bool d_data = digitalRead(digitalInput);
while(!Firebase.setInt(fbdo, "/devices/Inputs/A0", a_data)
|| !Firebase.setBool(fbdo, "/devices/Inputs/D0", d_data) )
{
Serial.println("Updating");
}
Serial.println(a_data);
Serial.println(d_data);
}
void handleOutput(){
Serial.println("handleOutput");
int level;
bool state;
bool _isUpdated = false;
if (Firebase.getInt(fbdo, "/devices/Keys/level"))
{
if (fbdo.dataType() == "int")
{
level = fbdo.intData();
Serial.println(level);
_isUpdated = true;
}
}
else
{
Serial.println(fbdo.errorReason());
}
if (Firebase.getBool(fbdo, "/devices/Keys/state"))
{
if (fbdo.dataType() == "boolean")
{
state = fbdo.boolData();
Serial.println(state);
_isUpdated = true;
}
}
else
{
Serial.println(fbdo.errorReason());
}
if(_isUpdated)
analogWrite(OutputPin, state?level:0);
}