Hello I thinking of doing some smart robot where can self navigate and mission Control system, but for early development, i just want to make it controllable by using my app. I code the app using Flutter but the sync between my app to firebase is no noticeable delay, but when i want to retrieve the data by using ESP32 it delay, when realtime database change value it take around 3 to 8 second for it to retrieve. I retrieving it using esp 32 as it has integrated Wifi Module while Arduino uno does'nt. Is there any problem with the code or the esp32 cannot retrieve data fast enough. This also happen when i want to send gps data from esp32 to realtime database. Sometimes it not even updated. Currently this is my code after some research and review of others tips.
Code For esp32
#include <ESP32Firebase.h>
char c;
String dataIn;
#define _SSID "mmmm" // Your WiFi SSID
#define _PASSWORD "12345678"
#define REFERENCE_URL "reff" // Your Firebase project reference url
Firebase firebase(REFERENCE_URL);
int forward = 0;
int right = 0;
int left = 0;
int backward = 0;
void setup() {
Serial.begin(19200);
Serial2.begin(9600,SERIAL_8N1, 16,17);
// pinMode(LED_BUILTIN, OUTPUT);
// digitalWrite(LED_BUILTIN, LOW);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(1000);
// Connect to WiFi
WiFi.begin(_SSID, _PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// Print the IP address
Serial.print("IP Address: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
// digitalWrite(LED_BUILTIN, HIGH);
//================================================================//
//================================================================//
// Examples of setting String, integer and float values.
//firebase.setString("Example/setString", "It's Working");
//firebase.setInt("Example/setInt", 123);
//firebase.setFloat("Example/setFloat", 45.32);
// Examples of pushing String, integer and float values.
//firebase.pushString("push", "Hello");
//firebase.pushInt("push", 789);
//firebase.pushFloat("push", 89.54);
// Example of getting a String.
}
//Serial Code
//forward->1
//backward->2
//right->3
//left->4
//turnleft->5
//turnright->6
//
void loop() {
forward = firebase.getInt("Movement/Forward");
if(forward==1){
Serial2.write(1);
}
backward = firebase.getInt("Movement/Backward");
if(backward==1){
Serial2.write(2);
}
left = firebase.getInt("Movement/Left");
if(left==1){
Serial2.write(4);
}
right = firebase.getInt("Movement/Right");
if(right==1){
Serial2.write(3);
}
if(right==1 && forward==1){
Serial2.write(5);
}
if(right==1 && forward==1){
Serial2.write(6);
}
}
This is code for Arduino Uno
#include <AFMotor.h>
#include <SoftwareSerial.h>
SoftwareSerial Uno_Serial (10,11);
char c;
String dataIn;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
void setup() {
Serial.begin(19200);
Uno_Serial.begin(9600);
motor1.setSpeed(0);
motor2.setSpeed(0);
motor3.setSpeed(0);
motor4.setSpeed(0);
}
//forward->1
//backward->2
//right->3
//left->4
//turnleft->5
//turnright->6
void loop() {
if (Uno_Serial.available()) {
int command = Uno_Serial.read();
if (command == 1) {
motor1.setSpeed(200);
motor2.setSpeed(200);
motor3.setSpeed(200);
motor4.setSpeed(200);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
Serial.println("Forward");
} else if (command == 2) {
motor1.setSpeed(200);
motor2.setSpeed(200);
motor3.setSpeed(200);
motor4.setSpeed(200);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
Serial.println("Backward");
} else if (command == 3) {
motor1.setSpeed(200);
motor2.setSpeed(200);
motor3.setSpeed(200);
motor4.setSpeed(200);
motor1.run(FORWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(BACKWARD);
Serial.println("Right");
} else if (command == 4) {
motor1.setSpeed(200);
motor2.setSpeed(200);
motor3.setSpeed(200);
motor4.setSpeed(200);
motor1.run(BACKWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(FORWARD);
Serial.println("Left");
}
else if (command == 5) {
motor1.setSpeed(155);
motor2.setSpeed(200);
motor3.setSpeed(155);
motor4.setSpeed(200);
motor1.run(BACKWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(FORWARD);
Serial.println("Turn Left");
}
else if (command == 6) {
motor1.setSpeed(200);
motor2.setSpeed(155);
motor3.setSpeed(200);
motor4.setSpeed(155);
motor1.run(FORWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(BACKWARD);
Serial.println("Turn Right");
}
}
}
Output Received on Arduino Uno
16:36:54.468 -> Forward
16:37:04.291 -> Left
16:37:12.518 -> Right
16:37:15.861 -> Backward
16:37:22.394 -> Backward
16:37:24.044 -> Left
16:37:27.325 -> Forward
16:38:36.313 -> Left
16:38:44.528 -> Right
16:38:59.282 -> Forward
16:39:14.071 -> Backward
16:39:19.001 -> Forward
16:39:23.963 -> Right
16:39:23.963 -> Turn Left
16:39:23.963 -> Turn Right
16:39:25.617 -> Forward
As you can see it delay on received. Noted: I only use Arduino Uno R3. Please assist me.