Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Akshayan Sinha
Published © LGPL

Google Firebase | IoT Platform Series - 10 πŸš€

Serverless Backend for your IoT Device served by Google! No easier way to get started with IoT over Cloud Internet.

BeginnerProtip1 hour103
Google Firebase | IoT Platform Series - 10 πŸš€

Things used in this project

Hardware components

Espressif ESP32 Development Board - Developer Edition
Espressif ESP32 Development Board - Developer Edition
×1

Software apps and online services

Firebase
Google Firebase

Story

Read more

Code

ESP32 - View Data on Firebase (Without JSON Parse)

Arduino
#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = " ";
const char* password = " ";

String serverName = "https://hackster-iot-default-rtdb.asia-southeast1.firebasedatabase.app/Device1.json";
String auth = "?auth=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

unsigned long lastTime = 0;
unsigned long timerDelay = 10000;

void setup() {
  Serial.begin(115200); 
//-------------------- WIFI INIT ----------------------
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}


void loop() {
  
  if ((millis() - lastTime) > timerDelay) {
    
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;

      String serverPath = serverName + auth;
      http.begin(serverPath.c_str());
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

ESP32 - Update Data on Firebase

Arduino
#include "WiFi.h"
#include "HTTPClient.h"
#include <ArduinoJson.h>

const char* ssid = " ";
const char* password = " ";

String serverName = "https://hackster-iot-default-rtdb.asia-southeast1.firebasedatabase.app/";
String section = "Device1.json";
String auth = "?auth=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

char val[128];

void setup() {

  pinMode(2,OUTPUT);

  Serial.begin(115200);
 
  WiFi.begin(ssid, password); 
 
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(2, HIGH);
    delay(500);
    digitalWrite(2, LOW);
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.println("Connected to the WiFi network");
  digitalWrite(2, HIGH); 
}
 
void loop() {
 
 if(WiFi.status()== WL_CONNECTED){
 
    HTTPClient http;
    
    String serverPath = serverName + section + auth;
      
    // Your Domain name with URL path or IP address with path
    http.begin(serverPath.c_str());
    http.addHeader("Content-Type", "application/json");            
    
    JsonDocument out;
    out["Data"] = random(0,99);
    out["Status"] = "online";
    serializeJson(out, val);

   
//    Serial.println(out);
   
    int httpResponseCode = http.PUT(val); 
    
    if(httpResponseCode>0){
      String response = http.getString();   
      //Serial.println(httpResponseCode);
      Serial.println(response);          
    
    }
    else{
      Serial.print("Error on sending PUT Request: ");
      Serial.println(httpResponseCode);
    }
    
    http.end();
 
 }
 else{
    Serial.println("Error in WiFi connection");
 }
 
  delay(5000);
}  

Credits

Akshayan Sinha

Akshayan Sinha

28 projects β€’ 27 followers
Robotics Software Engineer with a makermind.

Comments