Hi. I’m using the ESP8266Wi-Fi module. In my project, the ESP will read data through UART and send corresponding data to Firebase. The data receive through UART will be in the form of SWx/y where x will be 0/1/2 and y will be 0/1. The code will check for character x and y in the data and send the corresponding data to Firebase. After being powered on, the ESP was able to send data to Firebase correctly when it receives data through UART, but data is not sent after first time. It seems like if(UARTDone) always return false.
Here is my code:
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#define FIREBASE_HOST "xxx"
#define FIREBASE_AUTH "xxx"
#define WIFI_SSID "xxx"
#define WIFI_PASSWORD "xxx"
int dataCnt = 0;
int num;
char UARTData[10];
char UARTTemp;
bool UARTValid;
bool UARTDone = false;
void setup() {
Serial.begin(9600);
delay(1000);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
delay(1000);
}
void loop() {
if(Serial.available()>0) //Checks is there any data in buffer
{
dataCnt++;
UARTTemp = Serial.read();
if (dataCnt == 1 && UARTTemp == 'S'){ // If first character received is S
UARTValid = true;
}
else if (dataCnt == 1 && UARTTemp != 'S'){
UARTValid == false;
dataCnt = 0;
}
if (UARTValid){
UARTData[num] = UARTTemp;
num++;
if (num == 5){
num = 0;
dataCnt = 0;
UARTDone = true;
}
}
if (UARTDone){
UARTDone = false;
switch(UARTData[2]){
case '0':
if (UARTData[4] == '0'){
Firebase.setString("SW0/status", "0");
Serial.print("SW0/0");
}
else if (UARTData[4] == '1'){
Firebase.setString("SW0/status", "1");
Serial.print("SW0/1");
}
break;
case '1':
if (UARTData[4] == '0'){
Firebase.setString("SW1/status", "0");
Serial.print("SW1/0");
}
else if (UARTData[4] == '1'){
Firebase.setString("SW1/status", "1");
Serial.print("SW1/1");
}
break;
case '2':
if (UARTData[4] == '0'){
Firebase.setString("SW2/status", "0");
Serial.print("SW2/0");
}
else if (UARTData[4] == '1'){
Firebase.setString("SW2/status", "1");
Serial.print("SW2/1");
}
break;
}
}
}
delay(1000);
}