Hi everyone, good day. I am currently developing an NPK sensor that can be send through firebase using ESP32 via WiFi but I am having trouble integrating the two--connection to wireless and npk reading code, can anyone help i've been struggling for almost a month. Hoping you could help me guys I will post my code here:
this is the sample code I have made but not working:
#include <SoftwareSerial.h>
#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"
// Insert your network credentials
#define WIFI_SSID "OFFICEONLY"
#define WIFI_PASSWORD "OFFICE_FACULTY_ONLY123!@#"
// Insert Firebase project API Key
#define API_KEY "AIzaSyAUqztGpm0W4rRwsvr6o8cXF2C7JI-oX1g"
// Insert RTDB URL
#define DATABASE_URL "https://papaya-soil-sensor-default-rtdb.asia-southeast1.firebasedatabase.app/"
// Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
bool signupOK = false;
float Fmoist = 65.0;
float Fnitrogen = 0.0;
float Fphosphorus = 0.0;
float Fpotassium = 0.0;
float FpH = 0.0;
SoftwareSerial mod(26, 27);
// Modbus RTU requests for reading NPK values
const byte nitro[] = {0x01, 0x03, 0x00, 0x1E, 0x00, 0x01, 0xE4, 0x0C};
const byte phos[] = {0x01, 0x03, 0x00, 0x1F, 0x00, 0x01, 0xB5, 0xCC};
const byte pota[] = {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xC0};
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
// 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("signUp 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);
// Set up SoftwareSerial for Modbus
mod.begin(9600);
delay(500);
}
void loop() {
if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0)) {
sendDataPrevMillis = millis();
// Read NPK values
Fnitrogen = readNPKValue(nitro);
Fphosphorus = readNPKValue(phos);
Fpotassium = readNPKValue(pota);
// Write NPK values to Firebase
writeToFirebase();
// Print NPK values
Serial.print("Nitrogen: ");
Serial.print(Fnitrogen);
Serial.println(" mg/kg");
Serial.print("Phosphorus: ");
Serial.print(Fphosphorus);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(Fpotassium);
Serial.println(" mg/kg");
Serial.println();
}
}
float readNPKValue(const byte* request) {
mod.write(request, 8);
delay(1000);
byte values[8];
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
}
return values[4];
}
void writeToFirebase() {
// Write NPK values to the database path
if (Firebase.RTDB.setFloat(&fbdo, "Sensor/nitrogen", Fnitrogen) &&
Firebase.RTDB.setFloat(&fbdo, "Sensor/phosphorus", Fphosphorus) &&
Firebase.RTDB.setFloat(&fbdo, "Sensor/potassium", Fpotassium)) {
Serial.println("Firebase write successful");
} else {
Serial.println("Firebase write failed");
Serial.println("Reason: " + fbdo.errorReason());
}
delay(1000);
}
this is for my NPK:
#include <SoftwareSerial.h>
// Modbus RTU requests for reading NPK values
const byte nitro[] = {0x01,0x03,0x00,0x1E,0x00,0x01,0xE4,0x0C};
const byte phos[] = {0x01,0x03, 0x00, 0x1F, 0x00, 0x01, 0xB5, 0xCC};
const byte pota[] = {0x01,0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xC0};
// Array variable used to store NPK values
byte values[8];
SoftwareSerial mod(26, 27);
void setup() {
// Set the baud rate for the Serial port & Soft serial
Serial.begin(9600);
mod.begin(9600);
delay(500) ;
}
void loop() {
// Read values
byte val1,val2,val3;
val1 = nitrogen();
delay(500);
val2 = phosphorous();
delay(500);
val3 = potassium();
delay(500);
// Print values to the serial monitor
Serial.print("Nitrogen: ");
Serial.print(val1);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(val2);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(val3);
Serial.println(" mg/kg");
delay(2000);
}
byte nitrogen(){
mod.write(nitro,8);
delay(1000);
for(byte i=0;i<7;i++){
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
return values[4];
delay(1000);
}
byte phosphorous(){
mod.write(phos,8);
delay(1000);
for(byte i=0;i<7;i++){
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
return values[4];
delay(1000);
}
byte potassium(){
mod.write(pota,8);
delay(1000);
for(byte i=0;i<7;i++){
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
return values[4];
delay(1000);
}