Hi everyone,
I'm working on a project where I have multiple sensors connected to an Arduino Uno. The sensors I'm using are MQ5, MQ7, MQ135 (for gas detection), DHT11 (for temperature and humidity), and GP2Y1010AU0F (for dust density). The Arduino reads the sensor data and then sends this data to an ESP32. The ESP32 is supposed to upload the data to Firebase using the Firebase_ESP_Client
library.
However, I'm encountering an issue where the ESP32 throws an error about the data format being incorrect when trying to upload to Firebase.
code for arduino uno
void setup() {
Serial.begin(9600); // Initialize Serial communication at 9600 baud rate
}
void loop() {
// Read sensor values
float mq5Value = analogRead(A0); // Read MQ5 sensor value from analog pin A0
float mq7Value = analogRead(A1); // Read MQ7 sensor value from analog pin A1
// Format data as a CSV string
String data = String(mq5Value) + "," + String(mq7Value);
// Send data to ESP32
Serial.println(data);
delay(2000); // Wait for 2 seconds before sending the next data
}
code for esp32
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
// Provide the token generation process info.
#include <addons/TokenHelper.h>
// Provide the RTDB payload printing info and other helper functions.
#include <addons/RTDBHelper.h>
// Replace these with your WiFi credentials
#define WIFI_SSID "your_wifi_ssid"
#define WIFI_PASSWORD "your_wifi_password"
// Replace these with your Firebase project credentials
#define DATABASE_URL "your_database_url"
#define API_KEY "your_api_key"
// Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
bool signupOK = false;
// Use HardwareSerial for communication with Arduino Uno
HardwareSerial mySerial(2); // Use UART2 (TX2: GPIO17, RX2: GPIO16)
void setup() {
Serial.begin(115200); // Initialize Serial Monitor for debugging
mySerial.begin(9600, SERIAL_8N1, 16, 17); // Initialize UART2 at 9600 baud rate, TX2: GPIO17, RX2: GPIO16
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
// Assign the API key (required)
config.api_key = API_KEY;
// Assign the RTDB URL (required)
config.database_url = DATABASE_URL;
// Sign up
if (Firebase.signUp(&config, &auth, "", "")) {
Serial.println("ok");
signupOK = true;
} else {
Serial.printf("%s\n", config.signer.signupError.message.c_str());
}
// Assign the callback function for the long running token generation task
config.token_status_callback = tokenStatusCallback; // See addons/TokenHelper.h
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
}
void loop() {
if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 5000 || sendDataPrevMillis == 0)) {
sendDataPrevMillis = millis();
if (mySerial.available()) {
String data = mySerial.readStringUntil('\n'); // Read data from Arduino Uno
Serial.print("Received data from Arduino Uno: ");
Serial.println(data);
// Parse the received data
float mq5Value, mq7Value;
if (sscanf(data.c_str(), "%f,%f", &mq5Value, &mq7Value) == 2) {
Serial.print("Parsed MQ5 Value: ");
Serial.println(mq5Value);
Serial.print("Parsed MQ7 Value: ");
Serial.println(mq7Value);
FirebaseJson json;
json.set("MQ5", mq5Value);
json.set("MQ7", mq7Value);
if (Firebase.RTDB.setJSON(&fbdo, "/sensorData", &json)) {
Serial.println("Data sent to Firebase successfully");
} else {
Serial.println("Failed to send data to Firebase");
Serial.println("REASON: " + fbdo.errorReason());
}
} else {
Serial.println("Error: Received string format is incorrect");
}
}
}
}
4 posts - 3 participants