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!
Pulasthi Narada
Published © LGPL

Send multiple sensors data to Firebase using ESP8266

Send multiple sensors data to Firebase realtime database using ESP8266 and Arduino.

IntermediateFull instructions provided32,849
Send multiple sensors data to Firebase using ESP8266

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Proximity Sensor
Proximity Sensor
×2
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
USB-A to B Cable
USB-A to B Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE
Firebase
Google Firebase

Story

Read more

Schematics

Circuit diagram for to upload code to ESP8266 using Arduino.

Circuit diagram for 2 IR sensors, one ultrasonic sensor and ESP8266.

Code

Code for ESP8266

Arduino
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

#define FIREBASE_AUTH "Your secret" 
#define FIREBASE_HOST "your FIREBASE HOST"
#define WIFI_SSID " your WIFI SSID"
#define WIFI_PASSWORD " your WIFI PASSWORD"

String values,sensor_data;

void setup() {
  //Initializes the serial connection at 9600 get sensor data from arduino.
   Serial.begin(9600);
   
  delay(1000);
  
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    
  }
  
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); 
  
}
void loop() {

 bool Sr =false;
 
  while(Serial.available()){
    
        //get sensor data from serial put in sensor_data
        sensor_data=Serial.readString(); 
        Sr=true;    
        
    }
  
delay(1000);

  if(Sr==true){  
    
  values=sensor_data;
  
  //get comma indexes from values variable
  int fristCommaIndex = values.indexOf(',');
  int secondCommaIndex = values.indexOf(',', fristCommaIndex+1);
  int thirdCommaIndex = values.indexOf(',', secondCommaIndex + 1);
  
  //get sensors data from values variable by  spliting by commas and put in to variables  
  String ultrasonic_value = values.substring(0, fristCommaIndex);
  String IR_sensor1_value = values.substring(fristCommaIndex+1, secondCommaIndex);
  String IR_sensor2_value = values.substring(secondCommaIndex+1);

  //store ultrasonic sensor data as string in firebase 
  Firebase.setString("ultrasonic_value",ultrasonic_value);
   delay(10);
  //store IR sensor 1 data as string in firebase 
  Firebase.setString("IR_sensor1_value",IR_sensor1_value);
   delay(10);
  //store IR sensor 2 data as string in firebase 
  Firebase.setString("IR_sensor2_value",IR_sensor2_value);

  delay(10);
  
  //store previous sensors data as string in firebase
  
  Firebase.pushString("previous_ultrasonic_value",ultrasonic_value);
   delay(10);
  Firebase.pushString("previous_IR_sensor1_value",IR_sensor1_value);
   delay(10);
  Firebase.pushString("previous_IR_sensor2_value",IR_sensor2_value);
  
  
  delay(1000);
  
  if (Firebase.failed()) {  
      return;
  }
  
}   
}

Arduino Uno Code

Arduino
#define IR_sensor1Pin A1 
#define IR_sensor2Pin A2 

int trigger_pin = 2;
int echo_pin = 3;
 
int IR_sensor1_Value;
int IR_sensor2_Value;
int time;
int distance; 
String values;

 void setup() { 
  
 pinMode (trigger_pin, OUTPUT); 
 
 pinMode (echo_pin, INPUT);
 
 //Initializes the serial connection at 9600 to sent sensor data to ESP8266.
 Serial.begin(9600); 
 
delay(2000);  
  
 }

 
void loop() {

  // get sensors data and put in to values variables as a string.
   values= (get_distance_Value()+','+get_IR_sensor1_Value()+','+get_IR_sensor2_Value());
       delay(1000);
       // removed any buffered previous serial data.
       Serial.flush();
       delay(1000);
       // sent sensors data to serial (sent sensors data to ESP8266)
       Serial.print(values);
       delay(2000);
 

}

// get Ultrasonic sensor data
String get_distance_Value(){  
 
 
    digitalWrite (trigger_pin, HIGH);

    delayMicroseconds (10);

    digitalWrite (trigger_pin, LOW);

    time = pulseIn (echo_pin, HIGH);

    distance = (time * 0.034) / 2;
      return String(distance);  
}

// get IR sensor 1 data
String get_IR_sensor1_Value(){ 
   
IR_sensor1_Value =analogRead(IR_sensor1Pin);
    delay(1000);
    return String(IR_sensor1_Value);  
    
}

// get IR sensor 2 data
String get_IR_sensor2_Value(){ 
   
IR_sensor2_Value =analogRead(IR_sensor2Pin);
delay(1000);
    return String(IR_sensor2_Value);  
}
 
  

Credits

Pulasthi Narada

Pulasthi Narada

0 projects • 8 followers

Comments