Recieve a retrieved firebase data from esp8266-01 to Arduino mega 2560

First of all i have to say that i am a noob to these arduino works. . :-[ I am tring to cut of a current using a relay according to the signal given by the esp-01. For that i have already got a signal(data) from firebase. it means i have programmed the esp8266. But now i want to get that data to the arduino mega and according to that signal, i need to control the relay. But i couldn’t found a clear resource to do this. So can you please help me to solve that problem? Following image will show you the sketch of getting data from firebase using esp-01.

for the esp wiring:

esp - arduino mega2560
tx - tx
rx - rx
VCC - 3.3v
CHPD - 3.3v
Reset- GND
GND -GND
GPIO0- GND

#include <ESP8266WiFi.h>

#include <FirebaseArduino.h>

#define WIFI_SSID "****"
#define WIFI_PASSWORD "*****"
#define FIREBASE_HOST "****.firebaseio.com"
#define FIREBASE_AUTH "******"

void setup()
{
 pinMode(13,OUTPUT);
 digitalWrite(13,HIGH);
  Serial.begin(115200);

  wifiConnect();

  Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
   if(Firebase.failed())
   {
     Serial.println("error in firebase connection");
   }
   else{
     Serial.println("firebase connection");
     }
}

void loop()
{  
 String a= Firebase.getString("Charging/e84448a75646abc0/status");
  if(Firebase.failed())
   {
     Serial.println("error in firebase connection");
     Serial.println(Firebase.error()); 
   }
  
 Serial.println(a);
 if(a=="true"){
   digitalWrite(13,LOW);
   Serial.println("Relay ON");
   }
  else if(a=="false"){
   digitalWrite(13,HIGH);
   Serial.println("Relay OFF");
   }
 
 
  
 delay(3000);

if(WiFi.status() != WL_CONNECTED)
 {
   wifiConnect();
 }
delay(10);
}

void wifiConnect()
{
 WiFi.begin(WIFI_SSID,WIFI_PASSWORD);             // Connect to the network
 Serial.print("Connecting to ");
 Serial.print(WIFI_SSID); Serial.println(" ...");

 int teller = 0;
 while (WiFi.status() != WL_CONNECTED)
 {                                       // Wait for the Wi-Fi to connect
   delay(1000);
   Serial.print(++teller+ "\n"); Serial.print(' ');
 }

 Serial.println('\n');
 Serial.println("Connection established!");  
 Serial.print("IP address:\t");
 Serial.println(WiFi.localIP());         // Send the IP address of the ESP8266 to the computer
}

esp - arduino mega2560
tx - tx
rx - rx

Is this actually working? Typically, the transmitter on one side is connected to the receiver on the other.

Since the ESP knows the data you want to use, why doesn't it manage the relay? Why do you need two microcontrollers for such a simple task?

Yes, this is working properly, i got a data in firebase realtime databse usin this. I uploaded this sketch to the esp82266 generic board. But the probelm is i couldn't control the pin 13 using digital.Write(13,LOW). If i can get that that data to the arduino board then i can control the relay.. so can you tell me what should i do to get that 1data to arduino board or are there any other way ?? :-[

To echo PaulS, I don't see any need for the Arduino at all. The esp8266 has a few digital i/o pins available on its header. Why can't you use one of those to turn the relay on and off?

What code do you have on the Mega?

wildbill:
To echo PaulS, I don’t see any need for the Arduino at all. The esp8266 has a few digital i/o pins available on its header. Why can’t you use one of those to turn the relay on and off?

You mean GPIO0 and GPIO2 pins?? can you please explain more.

I don't have the same hardware to test it, but I suspect that you can replace your use of pin 13 with D0 and use that to drive a relay. You should probably check what voltage your relay board needs, the 3V3 you have may not be sufficient if you're unlucky.

wildbill:
I don't have the same hardware to test it, but I suspect that you can replace your use of pin 13 with D0 and use that to drive a relay. You should probably check what voltage your relay board needs, the 3V3 you have may not be sufficient if you're unlucky.

Yes, you are correct. relay board needs 5v to power up. Are there any way to get the data in the serial moniter of esp8266 generic to the arduino board?? if yes, can you give me a sample code? However i need to control the relay according to the signal recieved by the esp8266-01 :slight_smile:

However i need to control the relay according to the signal recieved by the esp8266-01

You've said that. You've also failed to answer all the questions that have been asked.

PaulS:
You've said that. You've also failed to answer all the questions that have been asked.

i am a noob to all these stuff. So i am really sorry and thank you so much for helping me.

PaulS:
What code do you have on the Mega?

I didn't program on Arduino mega yet

PaulS:
What code do you have on the Mega?

Today i have tried this code to the arduino mega 2560. bt it shows the followin error.

char data;

void setup() {

// put your setup code here, to run once:

Serial.begin(115200);

pinMode(13,OUTPUT);

}

void loop()

{

// put your main code here, to run repeatedly:

while(Serial.available()) {

data=Serial.read();

Serial.println(data);

if(data=="false"){
digitalWrite(13,LOW);
}
else if(data=="true")
{

digitalWrite(13,HIGH);

}
}
}

error:

Sketch uses 2408 bytes (0%) of program storage space. Maximum is 253952 bytes.
Global variables use 199 bytes (2%) of dynamic memory, leaving 7993 bytes for local variables. Maximum is 8192 bytes.
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
An error occurred while uploading the sketch

can you help me to solve this please?

char data;

if(data=="false"){

One character is NEVER going to equal the string "false" (or "true").

People put a LOT of effort into writing tutorials. Find Robin2's "Serial Input Basics - Updated" post and learn how to do serial input properly.

avrdude: stk500v2_ReceiveMessage(): timeout

The COM port that you are trying to upload to is not the one that the Mega is connected to (or you haven't installed the necessary drivers for the Mega).

Can you post a link to the relay board you have?

PaulS:

char data;

if(data==“false”){



One character is NEVER going to equal the string "false" (or "true").

People put a LOT of effort into writing tutorials. Find Robin2's "Serial Input Basics - Updated" post and learn how to do serial input properly.
The COM port that you are trying to upload to is not the one that the Mega is connected to (or you haven't installed the necessary drivers for the Mega).

Thank you PaulS , after seeing this post i change the variable type and i could upload the sketch to the board. But still i coulden’t control the relay.

#include <SoftwareSerial.h>

char data;

void setup() {

// put your setup code here, to run once:

Serial.begin(115200);

pinMode(13,OUTPUT);

}

void loop()

{

// put your main code here, to run repeatedly:

while(Serial.available()) {

data=Serial.read();

Serial.println(data);

if(data==false){
digitalWrite(13,LOW);
}
else if(data==true)
{

digitalWrite(13,HIGH);

}
}
}

As an extra thing; I have set the reset pin of arduino mega to the GND itself. when i remove the reset pin after uploading the sketch to the esp-01, sereal monitor stops the recieving data flow. However i need to control the relay model. what should i do next?

wildbill:
Can you post a link to the relay board you have?

I am using this 5v relay module (black one)

I am trying to cut off the current once the phone is fully charged. For that i have already set up the mobile app with firebase and i recieved a boolean value as a signal through the esp-01 module. I mean i could recieve the data to the serial monitor. And also i hope to use a 5v relay module to cut the current from between the charging wire according to the recieved signal through the arduino mega 2560 board…

I uploaded the code for esp-01 successfully and i got the required data from firebase by connecting esp-01 to the arduino mega board. And also i connect the relay module to the arduino board. Even i got the required value i can’t control the relay module as i want. there was no error.

for the esp wiring:

esp - arduino mega2560
tx - tx
rx - rx
VCC - 3.3v
CHPD - 3.3v
Reset- GND
GND -GND
GPIO0- GND

PS: i connected the reset pin in arduino mega to it’s GND. I couldn’t upload the esp code without connecting this.

#include <ESP8266WiFi.h>

#include <FirebaseArduino.h>


#define WIFI_SSID "***"
#define WIFI_PASSWORD "*****"
#define FIREBASE_HOST "***.firebaseio.com"
#define FIREBASE_AUTH "****"

void setup()
{
  
  Serial.begin(115200);
   pinMode(13, OUTPUT);
   wifiConnect();

   Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
    if(Firebase.failed())
    {
      Serial.println("error in firebase connection");
    }
    else{
      Serial.println("firebase connection");
      }
}

void loop()
{   
   boolean a= Firebase.getBool("Charging/e84448a75646abc0/status");
   if(Firebase.failed())
    {
      Serial.println("error in firebase connection");
      Serial.println(Firebase.error()); 
    }
   
 Serial.println(a);


  if(a==false){
  
digitalWrite(13,LOW);
Serial.print("relay ON");
}
else if(a==true)
{

digitalWrite(13,HIGH);
Serial.print("relay OFF");
}
  delay(3000);


 
if(WiFi.status() != WL_CONNECTED)
  {
    wifiConnect();
  }
delay(10);
  
}

void wifiConnect()
{
  WiFi.begin(WIFI_SSID,WIFI_PASSWORD);             // Connect to the network
  Serial.print("Connecting to ");
  Serial.print(WIFI_SSID); Serial.println(" ...");

  int teller = 0;
  while (WiFi.status() != WL_CONNECTED)
  {                                       // Wait for the Wi-Fi to connect
    delay(1000);
    Serial.print(++teller+ "\n"); Serial.print(' ');
  }

  Serial.println('\n');
  Serial.println("Connection established!");  
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());         // Send the IP address of the ESP8266 to the computer
}

i have uploaded this code to the esp8266 genaric board. It works. But digital.write(13,HIGH) and LOW are not worked.

(i connected the data pin of relay module to the pin 13).

Questions???

  1. Why the relay is not working???
  2. Are there any other way to achive this task and if yes explain me please?

Welcome to the Forum. You have posted code without the necessary inline code tags. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question ...

Have you tried loading a simple sketch that only operates the relay, nothing else?

aarg:
Welcome to the Forum. You have posted code without the necessary inline code tags. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question ...

Have you tried loading a simple sketch that only operates the relay, nothing else?

thank you aarg i ll refer these posts.

yes i tried. i uploaded a simple sketch to control the relay and it works. even now, when i remove the reset pin from arduino, relay works according to that simple sketch. But here i need to work it with the signal i got from firebase.. it is not woking.

And also i hope to use a 5v relay module to cut the current from between the charging wire according to the recieved signal through the arduino mega 2560 board..

Is there a reason not to just let the ESP-01 do it directly, most of the 5vRelay boards can be made to function on 3.3v (actually they usually already do)
Also i hope this is not an attempt to save power !
Ok, now i see you want to control it directly well the ESP-01 only has 4 exposed data pins, GPIO 0-3
GPIO1 is the UART TX line which is in service, many times this is also the BUILTin LED. GPIO3 is the UART RX line, not really in use but connected to something else, Sometimes the LED BUILTIN is on GPIO2, but if not either GPIO2 & GPIO0 can be used as output pins, but only as active LOW, they need to be pulled HIGH at startup.
My Suggestion. connect a LED with a 1K(or 470R or even 270R is still OK) resistor in line, with the Kathode to GPIO2 and the Anode to VCC (3.3v)
and change the pin number to 2 in these lines

pinMode(2, OUTPUT);
digitalWrite(2,LOW);
digitalWrite(2,HIGH);

And swap HIGH and LOW so the LED lights up when the relay should be on.