Continuing the discussion from Modbus: NPK-Sensor does not respond at all:
I've already made my NPK work, however I am having a problem transferring it to my firebase code for the baud rate for my NPK is 9600 and the require baud rate for wifi setup is 115200 can anyone enlighten me or help me on what to do.
This is the code 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);
}
and this is a draft code for sending in firebase:
#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 "ssid"
#define WIFI_PASSWORD "pass"
// Insert Firebase project API Key
#define API_KEY "my api_key"
// Insert RTDB URLefine the RTDB URL
#define DATABASE_URL "my database"
//Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
bool signupOK = false;
float Fnitrogen = 0.0;
float Fphophorus = 0.0;
float Fpotassium = 0.0;
float FpH = 0.0;
void setup() {
// put your setup code here, to run once:
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);
}
void loop() {
// put your main code here, to run repeatedly:
if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0)){
sendDataPrevMillis = millis();
// Write an Int number on the database path test/int
if (Firebase.RTDB.setFloat(&fbdo, "Sensor/moisture", Fnitrogen)){
Serial.println(Fnitrogen);
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}
}
}