Arduino Mega, ESP8266-01 and Firebase

Hello everyone!

I have a small project I really would appreciate some advice on.

Now I have an ESP8266-01 connected to a Mega 2560 on RX0 and TX0 (Serial0). What I'm trying to do is simply to get a FirebaseObject into the Mega for processing.

I have already uploaded a sketch to the ESP8266 where it connects to my Wifi and the Firebase Real-Time database. The problem is not to get the FirebaseObject into the ESP8266, which I have done using the Firebase.get() function from FirebaseArduino.h.
I would like to send the entire object to the Mega and handle the JSON/Firebase object there, instead of programming the ESP8266 very specific for this project, if you understand. It seems like the Firebase object is a json object?
What I already done is like this:


to send the state of relay 1 to the Mega. But then I must write this specifically on the ESP8266, instead I'm looking for something like this:


Any advice on how I can do this? Or can this be done with AT-commands using the default program on the ESP8266? Solution with godd efficiency and speed is prefered.

Thank you for your time!

Now I have an ESP8266-01 connected to a Mega 2560 on RX0 and TX0 (Serial0).

Why? That port is for debugging. Put the ESP on another serial port.

It seems like the Firebase object is a json object?

JSON is a text format. IF the "Firebase object" is a text string in JSON format, then getting it into the Mega's SRAM will be relatively easy.

Any advice on how I can do this?

Step 1: Post a link to the Firebase library.
Step 2: Post your code. Your snippets do not define what a firebaseObject is, or how you got it.

Thanks for taking your time!

Sure, I was uploading code to the ESP8266 through the Mega when I wrote this and I forgot to change the RX/TX connections. The ESP8266 (RX, TX) is connected to Mega (TX3, RX3) when running.

Firebase real time database:

Relay1: {Count: “0”, State:“OFF”}
Relay2: {Count: “0”, State:“OFF”}
Relay3: {Count: “0”, State:“OFF”}
Relay4: {Count: “0”, State:“OFF”}
Relay5: {Count: “0”, State:“OFF”}
Relay6: {Count: “0”, State:“OFF”}
Relay7: {Count: “0”, State:“OFF”}
Relay8: {Count: “0”, State:“OFF”}

ESP8266 code:

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

void setup() {

  pinMode(2, OUTPUT);
  Serial.println("<Connecting to SSID>");
  WiFi.begin(SSID, Password);

  while (WiFi.status() != WL_CONNECTED) { // Blink the led until connected to Wifi
    digitalWrite(2, LOW);
    digitalWrite(2, HIGH);

  // Set led ON when connected
  digitalWrite(2, HIGH);
  Serial.println("<WiFi connected>");
  Serial.println("<IP address:>");

  // Connect to Firebase (FIREBASE_HOST, FIREBASE_AUTH) - Database API url and database secret key
  if (Firebase.success()) {
      Serial.println("<Connected to Firebase>");
  } else {
      Serial.println("<Unable to connect to Firebase>");

void loop() {

void sendFirebaseData() {
  // Download the entire database and save as a FirebaseObject
  FirebaseObject firebaseObject = Firebase.get("/");
  if (Firebase.success()) {
    char relayStates[10];

    relayStates[0] = '<';

    if (firebaseObject.getString("Relay1/State") == "ON") relayStates[1] = '1';
    else relayStates[1] = '0';
    if (firebaseObject.getString("Relay2/State") == "ON") relayStates[2] = '1';
    else relayStates[2] = '0';
    if (firebaseObject.getString("Relay3/State") == "ON") relayStates[3] = '1';
    else relayStates[3] = '0';
    if (firebaseObject.getString("Relay4/State") == "ON") relayStates[4] = '1';
    else relayStates[4] = '0';
    if (firebaseObject.getString("Relay5/State") == "ON") relayStates[5] = '1';
    else relayStates[5] = '0';
    if (firebaseObject.getString("Relay6/State") == "ON") relayStates[6] = '1';
    else relayStates[6] = '0';
    if (firebaseObject.getString("Relay7/State") == "ON") relayStates[7] = '1';
    else relayStates[7] = '0';
    if (firebaseObject.getString("Relay8/State") == "ON") relayStates[8] = '1';
    else relayStates[8] = '0';

    relayStates[9] = '>';

  } else{
      Serial.print("<Reading data from Firebase failed:>\n");

Code from Mega that reads the message from ESP8266 and set relays:

const byte numRelays = 8;
const byte numChars = 100;
bool relayArray[numRelays];
char receivedChars[numChars];

void  readSerialMessage(){
  static bool recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while(Serial3.available() > 0 && newData == false){
    rc =;
    if (rc == startMarker) {
      recvInProgress = true;
    } else if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        if (ndx >= numChars) {
          ndx = numChars - 1;
      } else {
        receivedChars[ndx] = '\0';  // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;

void updateRelayArray() {
  for (int i=0; i<numRelays; i++) {
    if (receivedChars[i] == '1') relayArray[i] = RELAY_ON;
    else relayArray[i] = RELAY_OFF;

void setRelays() {
  for (int i=0; i<numRelays; i++) {
    digitalWrite(i+2, relayArray[i]);

void loop() {

This is working at the moment, where I ESP8266 sends a string to Mega including 8 digits 0 or 1 representing the relay off/on. But if I change the database (e.g. add a relay) I also have to edit the ESP8266 code in addition to the Mega code. Also, it would be nice to have the code on ESP8266 working with different databases as well, where I only edit the wifi, firebase host/auth in the Mega code and send this to ESP8266 through serial, and ESP8266 sends the whole database to Mega. Like this:

  1. Power the boards [OK]
  2. Serial communication between ESP8266 and Mega [OK]
  3. Mega sends wifi name/password and Firebase host/auth to ESP8266 [OK]
  4. ESP8266 receives the whole Firebase database [OK]
  5. ESP8266 sends the whole Firebase database to Mega, no matter how the database look like [how to do this?]
  6. I edit my database in Firebase, then edit the code in Arduino Mega, then use the same code unchanged in ESP8266 [How?]

I thought this was the main purpose of the ESP8266 together with arduino? But I can’t find any examples or solutions to this, only simple AT commands to ESP8266 or where ESP8266 handles everything without arduino.

I don't know if you made this,

but I was recently trying the same.

My solution was:

void loop() {

FirebaseObject object = Firebase.get("/");
JsonVariant variant = object.getJsonVariant();



serial printed result:

14:57:58.610 -> {
14:57:58.610 -> "Rele": 0,
14:57:58.610 -> "Rele1": 0,
14:57:58.657 -> "Rele2": 0,
14:57:58.657 -> "Rele3": 0
14:57:58.657 -> }

hope this help!