UNO [main microcontroller] <== Serial communication ==> ESP-8266-01 [Gateway]

  1. What should i do to build the communication?
  • UNO want to read the data from ESP8266-01 which the ESP8266-01 get from firebase
  • UNO want send data to ESP8266-01 then write to database
  1. My thought (dunno work or not)
    (a) setup up code in UNO for reading data
    (b) setup firebase connection in ESPP-01 and send data to UNO through serial

  2. This is the draft code for UNO (as shown below)
    ==> based on data that get from ESP-01, light up LED

  3. This is draft codee for ESP-01
    ==> generate loop to sending"0" and "1" for testing

  4. What's wrong with my concept or code?

UNO CODE

#include <SoftwareSerial.h>
#define LED_PIN 4

SoftwareSerial esp8266(2, 3); //(RX of UNO, TX of UNO)
int LED_Status = HIGH;

void setup()
{
pinMode ( LED_PIN, OUTPUT);
Serial.begin(115200);
while (!Serial) {}
Serial.println("Started");
esp8266.begin(115200);
}

void loop()
{
Serial.println ( LED_Status );
if (esp8266.available() > 0)
{
LED_Status = esp8266.read();
delay(500);
Serial.println ( "read from ESP-01" );
}

if ( LED_Status ==HIGH)
digitalWrite(LED_PIN, HIGH);
else
digitalWrite(LED_PIN, LOW);

delay(100);
}

ESP01

void setup() {
Serial.begin(115200);
}

void loop() {
Serial.println ( "1" );
Serial.write("1");
delay (200);
Serial.println ( "0" );
Serial.write("0");
delay (200);
}

  1. What's wrong with my concept or code?

First: You use SoftwareSerial. If ever possible, don't use the catastrophic piece of Software.

Second:

 esp8266.begin(115200);

You set the baud rate of this SoftwareSerial to 115200 baud. It won't work at that speed. If you have an extremely timing tolerant counterpart device you may perhaps reach 38400 baud at most, usually 9600 is a more realistic upper limit.

Your code is far from doing anything you described first.
If you just want to light up the LED, you should not send the characters '0' or '1' but the values 0 or 1 (I hope you understand the difference, otherwise ask).

Remote the println() calls from the ESP code and use

Serial.write(0);

or

Serial.write(1);

THANKS =D [Halfly Resolved]

The final code for now

  • ESP9266 sending send data to turn on and off every 0.5 seconds ( 0 to turn off, 1 to turn on)
  • UNO keep reading from ESP8266 and store the value in LED_status once esp8266 write something

ESP8266

void setup()
{
Serial.begin(9600);
}

void loop()
{
Serial.write(1);
delay (500);
Serial.write(0);
delay (500);
}

UNO

#include <SoftwareSerial.h>
#define LED_PIN 4
SoftwareSerial esp8266(0, 1); //(RX of UNO, TX of UNO)
int LED_Status = HIGH;
void setup() {
pinMode ( LED_PIN, OUTPUT);
Serial.println ( LED_Status );
Serial.begin(9600);
while (!Serial) {}
Serial.println("Started");
esp8266.begin(9600);
}

void loop()
{
if (esp8266.available() > 0)
{
LED_Status = esp8266.read();
Serial.print ( "esp8266 sending value, LED Status: " );
Serial.println ( LED_Status );
}

if ( LED_Status ==HIGH)
digitalWrite(LED_PIN, HIGH);
else
digitalWrite(LED_PIN, LOW);
}

Anyway, Now im using ESP8266 TX and RX to trasmit data to UNO.

  1. Is that still possible for ESP8266 connect to WIFI?
  • cuz from what i know, it use TX RX to connect wifi, and cant function simultaneously?

2 If dont want to use software serial, so how we can declare the esp8266.read(), esp8266.read()?
How to replace the below code?

SoftwareSerial esp8266(0, 1);
esp8266.begin(9600);
esp8266.read();

  1. Is that still possible for ESP8266 connect to WIFI?

Yes, that's the intended way to use the ESP8266 as a WiFi device from an Arduino.

2 If dont want to use software serial, so how we can declare the esp8266.read(), esp8266.read()?
How to replace the below code?

You have several possibilities, which one you choose depends on the requirements you have on the project (you didn't tell us yet):

  1. Use the hardware serial interface (pin 0 and 1) instead (Serial object to access it) but than you cannot use that channel for debugging.

  2. Use AltSoftSerial or NeoSWSerial instead of SoftwareSerial. Both have some limitations but are by far more reliable (based on interrupts instead of deactivating all of them) and leave you more processor power for the rest of the sketch.

Thanks a lot for the information about the serial things =D

what is the requirement for my project

hmm, what is the scope u meant for the requirement?
Actually my project is smart home automation with security system. The main controller is actually Arduino Mega, and communicate with the ESP-01 and pass data to firebase. It also call homeuser through GSM when home is not secure. So i think will do something like interrupt so that the system is much more efficient? (for now still no idea how interrupt works in arduino).

And actually, face some problem when including FirebaseArduino.h and ESP8266Wifi.h in ESP-01.

Just for extra question, the firebase library and the code that deal with firebase should upload to ESP-01 instead of Arduino uno/mega right?

do not use any form of software serial if you have a Mega with 4 serial pin pairs

Thanks for your suggestion :wink:

Actually my project is smart home automation with security system. The main controller is actually Arduino Mega, and communicate with the ESP-01 and pass data to firebase.

A Mega? Why does the thread title the specify an UNO? And in the first post you only wrote about an UNO, no mention of a Mega. It's completely stupid to use a emulation in software that blocks the complete CPU if you have 4 of these interfaces in hardware.

Please define what a "firebase" is. A link may help.

So i think will do something like interrupt so that the system is much more efficient?

Using interrupts doesn't automatically make a system more efficient, it even may be contrary. Programming in interrupt context requires a deep knowledge of the underlying processor and it's architecture otherwise raise conditions are inevitable.

Start with the simple solutions.

To be able to help you we need to know what hardware you're using. Telling us it's an UNO but using a Mega doesn't help. If you want to use GSM you must have some additional hardware. Provide a link to it.

we will use Mega for the project. For now i just test the ESP-01 module as well as Firebase with UNO because i have one UNO with me. After that will get a Mega.
p.s. is my mistake for not putting MEGA in title.

Firebase


Firebase is basically a cloud database, which is a platform owned by google. It is a real time database.
Link:

GSM

For GSM, we bought a SIM900A.

What is meant by 'use a emulation in software that blocks the complete CPU if you have 4 of these interfaces in hardware '?

Thanks :slight_smile:

What is meant by 'use a emulation in software that blocks the complete CPU if you have 4 of these interfaces in hardware '?

In your code you use SoftwareSerial which is a crippled piece of software that tries to emulate the functionality of a UART (serial interface in hardware) but does that by turning off all interrupts during the sending or reception of a complete byte. So if you have a sketch that needs to react in timely manner to external events you won't be able to do that if you use SoftwareSerial.

The Mega has 4 UARTs built in, so there's no need to use any software emulation.

If you want to go with the UNO and need the hardware serial interface for debugging, try at least if the alternative software emulations (AltSoftSerial or NeoSWSerial) might work with your setup before using that SoftwareSerial.

how did you connect it to firebase? I don't know how to connect it
I have the same ESP-01 as you have

please need your help thank you :slight_smile: