Created
April 12, 2020 12:38
-
-
Save adiparamartha/894a1d256f785b201fe77b9b1bf3aec1 to your computer and use it in GitHub Desktop.
Example of uploading data into Firebase Database using Arduino and FirebaseArduino library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <FirebaseArduino.h> | |
// Set these to run example. | |
#define FIREBASE_HOST ".." | |
#define FIREBASE_AUTH "..." | |
#define WIFI_SSID ".." | |
#define WIFI_PASSWORD "." | |
void setup() { | |
Serial.begin(9600); | |
// connect to wifi. | |
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); | |
} | |
int n = 0; | |
void loop() { | |
// set value | |
Firebase.setFloat("number", 42.0); | |
// handle error | |
if (Firebase.failed()) { | |
Serial.print("setting /number failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
delay(1000); | |
// update value | |
Firebase.setFloat("number", 43.0); | |
// handle error | |
if (Firebase.failed()) { | |
Serial.print("setting /number failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
delay(1000); | |
// get value | |
Serial.print("number: "); | |
Serial.println(Firebase.getFloat("number")); | |
delay(1000); | |
// remove value | |
Firebase.remove("number"); | |
delay(1000); | |
// set string value | |
Firebase.setString("message", "hello world"); | |
// handle error | |
if (Firebase.failed()) { | |
Serial.print("setting /message failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
delay(1000); | |
// set bool value | |
Firebase.setBool("truth", false); | |
// handle error | |
if (Firebase.failed()) { | |
Serial.print("setting /truth failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
delay(1000); | |
// append a new value to /logs | |
String name = Firebase.pushInt("logs", n++); | |
// handle error | |
if (Firebase.failed()) { | |
Serial.print("pushing /logs failed:"); | |
Serial.println(Firebase.error()); | |
return; | |
} | |
Serial.print("pushed: /logs/"); | |
Serial.println(name); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment