I have read data from npk sensors to aurduino. But it is not send to the esp8266. i have made both code in same serial serial also but not working.
mod bus we use:
Specification & Features :
• Compatibility: converts rs485 to ttl and vice- versa. (Transceiver Facility )
• Interface RS485 side: 2-pin (A+,B-) terminal connector.
• Interface TTL side:4-pin burg stick (5v,GND,tx,rx) .
• Transmission rate: 300 - 115200bps
• Transmission distance: RS485 max. 1200m,
• Working mode: asynchronous, half-duplex, differential transmission.
• Operating Voltage - 5volts DC
Code for arduino Uno to read data from 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(2, 3); // RX, TX
void setup() {
Serial.begin(115200); // Initialize serial communication
mod.begin(115200); // Initialize software serial
delay(500);
Serial.println("Setup complete.");
}
void loop() {
Serial.println("Starting loop.");
byte val1, val2, val3;
val1 = nitrogen();
delay(1500);
val2 = phosphorous();
delay(1500);
val3 = potassium();
delay(1500);
// Send values to ESP8266
Serial.print(val1);
Serial.print(",");
Serial.print(val2);
Serial.print(",");
Serial.println(val3);
Serial.println("Data sent to ESP8266");
delay(2000);
}
byte nitrogen() {
Serial.println("Reading nitrogen.");
mod.write(nitro, 8);
delay(100);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
}
return values[4];
}
byte phosphorous() {
Serial.println("Reading phosphorous.");
mod.write(phos, 8);
delay(100);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
}
return values[4];
}
byte potassium() {
Serial.println("Reading potassium.");
mod.write(pota, 8);
delay(100);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
}
return values[4];
}
code for esp8266
#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
#include <Firebase.h>
#define FIREBASE_HOST "host"
#define FIREBASE_API_KEY "dsnaklfldksnhllsakdlckla"
#define FIREBASE_DATABASE_URL "https://<firebase-url>"
#define WIFI_SSID "ssid"
#define WIFI_PASSWORD "password"
FirebaseData firebaseData;
FirebaseConfig firebaseConfig;
FirebaseAuth firebaseAuth;
bool signedUp = false; // Flag to track if signed up
void tokenStatusCallback(token_info_t info)
{
Serial.printf("Token Status: %s\n", info.status ? "Valid" : "Invalid");
}
void setup()
{
Serial.begin(115200); // Initialize serial communication with ESP8266
Serial.println("Serial communication started\n");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); // Connect to Wi-Fi
Serial.print("Connecting to Wi-Fi");
Serial.println(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Check 1");
if (!signedUp) // Check if not signed up
{
// Firebase configuration
firebaseConfig.host = FIREBASE_HOST;
firebaseConfig.api_key = FIREBASE_API_KEY;
firebaseConfig.database_url = FIREBASE_DATABASE_URL;
firebaseConfig.token_status_callback = tokenStatusCallback;
// Sign up
if (Firebase.signUp(&firebaseConfig, &firebaseAuth, "", ""))
{
Serial.println("Sign up completed!..............");
signedUp = true; // Set signedUp flag to true
}
else
{
Serial.printf("Sign up failed: %s\n", firebaseConfig.signer.signupError.message.c_str());
// Do something if sign up fails
}
}
// Initialize Firebase
Firebase.begin(&firebaseConfig, &firebaseAuth);
Firebase.reconnectWiFi(true);
Serial.println("Check 2");
}
void loop()
{
Serial.println("In loop");
if (Serial.available() > 0)
{
String data = Serial.readStringUntil('\n');
data.trim();
Serial.println("Received data: " + data);
int comma1 = data.indexOf(',');
int comma2 = data.indexOf(',', comma1 + 1);
if (comma1 != -1 && comma2 != -1)
{
int val1 = data.substring(0, comma1).toInt();
int val2 = data.substring(comma1 + 1, comma2).toInt();
int val3 = data.substring(comma2 + 1).toInt();
Serial.print("Nitrogen: ");
Serial.println(val1);
Serial.print("Phosphorous: ");
Serial.println(val2);
Serial.print("Potassium: ");
Serial.println(val3);
if (Firebase.setInt(firebaseData, "/sensor_data/nitrogen", val1))
{
Serial.println("Nitrogen data sent to Firebase");
}
else
{
Serial.println("Failed to send nitrogen data to Firebase");
Serial.println(firebaseData.errorReason());
}
if (Firebase.setInt(firebaseData, "/sensor_data/phosphorous", val2))
{
Serial.println("Phosphorous data sent to Firebase");
}
else
{
Serial.println("Failed to send phosphorous data to Firebase");
Serial.println(firebaseData.errorReason());
}
if (Firebase.setInt(firebaseData, "/sensor_data/potassium", val3))
{
Serial.println("Potassium data sent to Firebase");
}
else
{
Serial.println("Failed to send potassium data to Firebase");
Serial.println(firebaseData.errorReason());
}
}
else
{
Serial.println("Data format error");
}
}
else
{
Serial.println("No data available");
}
Serial.println("Check 3");
delay(5000);
}