Go Down

Topic: Arduino Mega, ESP8266-01 and Firebase (Read 368 times) previous topic - next topic

JonasFN

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:

Serial.print(firebaseObject.getString("Relay1/State"));

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:

Serial.print(firebaseObject_as_string);

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!

PaulS

Quote
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.

Quote
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.

Quote
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.
The art of getting good answers lies in asking good questions.

JonasFN

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:

myDatabase{
   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:
Code: [Select]

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

 
void setup() {

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

  while (WiFi.status() != WL_CONNECTED) { // Blink the led until connected to Wifi
    delay(200);
    digitalWrite(2, LOW);
    delay(200);
    digitalWrite(2, HIGH);
    Serial.print("<.>");
  }

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

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

void loop() {
  sendFirebaseData();
  delay(100);
}

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] = '>';
    Serial.println(relayStates);
},"
                                "{\"Relay8\":{\"Count\":"+firebaseObject.getString("Relay8/Count")+",\"State\":"+firebaseObject.getString("Relay8/State")+"}}";

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



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

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 = Serial3.read();
    if (rc == startMarker) {
      recvInProgress = true;
    } else if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        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() {
  readSerialMessage();
  updateRelayArray();
  setRelays();
}



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.

Go Up