-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebasecode1
93 lines (90 loc) · 2.84 KB
/
firebasecode1
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
#define DEBUG_SW 1
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
//Provide the token generation process info.
#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"
// Insert your network credentials
#define WIFI_SSID "PONRAJ"
#define WIFI_PASSWORD "9842466751"
// Insert Firebase project API Key
#define FIREBASE_HOST "https://esp32test-16f40-default-rtdb.asia-southeast1.firebasedatabase.app/"
#define API_KEY "AIzaSyCI0g26vBrL4-BNMOBJzNwDkXc0I5Gu8kk"
// Insert RTDB URLefine the RTDB URL */
#define DATABASE_URL "https://esp32test-16f40-default-rtdb.asia-southeast1.firebasedatabase.app/"
#define LED1_PIN 12
#define LDR_PIN 34
FirebaseData fbdo;
FirebaseAuth auth;
int i=1;
int j=300;
int ledStatus = false;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
bool signupOK = false;
int ldrData = 0;
float voltage = 0.0;
void setup()
{
pinMode(LED1_PIN,OUTPUT);
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED){
Serial.print(".");delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
config.api_key = API_KEY;
config.database_url = DATABASE_URL;
/* Sign up */
if (Firebase.signUp(&config, &auth, "", "")) {
Serial.println("signup ok");
signupOK = true;
}
else {
Serial.print("%s\n" );
}
/* Assign the callback fun ction for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
}
void loop()
{
if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 5000 || sendDataPrevMillis == 0)) {
sendDataPrevMillis = millis();
int ldrData=analogRead(LDR_PIN);
float voltage =analogRead(LDR_PIN)/1000;
if (Firebase.RTDB.setInt(&fbdo,"Sensor/ldr_data",ldrData) ){
Serial.println();
Serial.println(ldrData);
Serial.println("- successfully saved to:"+ fbdo.dataPath());
Serial.println("("+ fbdo.dataType() +")");
}
else{
Serial.println("FAILED: " + fbdo.errorReason() );
}
if (Firebase.RTDB.setFloat(&fbdo,"Sensor/voltage",voltage) ){
Serial.println();Serial.println(voltage);
Serial.println("- successfully saved to:"+ fbdo.dataPath());
Serial.println("("+ fbdo.dataType() +")");
}
else{
Serial.println("FAILED: " + fbdo.errorReason() );
}
if(Firebase.RTDB.getBool(&fbdo, "/LED/digital")){
if(fbdo.dataType()="boolean"){
ledStatus=fbdo.boolData();
Serial.println("READ from" + fbdo.dataPath() + " : " + ledStatus);
digitalWrite(LED1_PIN,ledStatus);
}
else{
Serial.println("FAILED: " + fbdo.errorReason() );
}
}
}
}