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:
#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:
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:
- Power the boards [OK]
- Serial communication between ESP8266 and Mega [OK]
- Mega sends wifi name/password and Firebase host/auth to ESP8266 [OK]
- ESP8266 receives the whole Firebase database [OK]
- ESP8266 sends the whole Firebase database to Mega, no matter how the database look like [how to do this?]
- 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.