Hello everyone,
I am currently developing a smart fertilizer and crop recommendation system using Arduino (NodeMCU) along with an NPK sensor to measure soil nutrients. However, I am encountering an issue with the data readings. When I receive data from the sensor to the Arduino, it consistently displays a value of 255 for each parameter (Nitrogen, Phosphorus, and Potassium) regardless of the actual soil condition. This same value is also displayed even when the sensor is not inserted in the soil.
Has anyone experienced a similar issue or can provide insights on why this might be happening? Any suggestions on troubleshooting steps or potential fixes would be greatly appreciated.
Thank you!
this is the code
#include <SoftwareSerial.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
// #include <Adafruit_GFX.h>
// #include <Adafruit_SSD1306.h>
#define RE 4
#define DE 5
#define DI 14
#define RO 2
// const byte phv[] = {0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b};
// 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};
//const byte temp[] = {0x01,0x03, 0x00, 0x13, 0x00, 0x01, 0x75, 0xcf};//
const byte temp[] = {0x01,0x03, 0x01, 0x13, 0x22, 0x01, 0x75, 0xcf};
const byte mois[] = {0x01,0x03,0x00,0x12,0x00,0x01,0x24,0x0F};
const byte econ[] = {0x01,0x03, 0x00, 0x15, 0x00, 0x01, 0x95, 0xce};
const byte phv[] = {0x01,0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b};//0x0B64
//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 };
// const byte nitro[) = (0x01,0x03, 0x00, Oxle, 0x00, 0x01, Oxed, OxOc):
// const byte phos(] = (0x01,0x03, 0x00, Oxif, 0x00, 0x01, ORbS, Oxce):
// const byte pota(] = (0x01,0x03, 0x00, 0x20, 0x00, 0x0l, ORES, Oxc0);
// const byte soil ph(] = (0x01, 0x03, 0x00, 0x06, 0x00, 0x01, Oxéd, Cxib):
// const byte soil moist(] = (0X01, 0x03, 0800, 0x12, 0X00, 0X01, 0x24, OXOf):
// const bye templ] = (0x01, O03, 0x00, 0x12, 0x00, 0x02, Oxéd, Uxde):
// const byte ec] = (0301, 0x03, 0x00, OK1S, 0x00, 0x01, 0x95, Oxce):
const char* ssid = "cont";
const char* password = "098765432";
const char* serverName = "http://192.168.137.217/Gc/gc_project/display.php";
String apiKeyValue = "Y5UPLPLDCO4154WW";
byte values[11];
SoftwareSerial mod(RO, DI);
void setup() {
Serial.begin(9600);
mod.begin(4800);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("i am in setup");
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
Serial.print("Nitrogen: ");
delay(3000);
}
void loop() {
byte n, p, k, h;
n = nitrogen();
delay(250);
p = phosphorous();
delay(250);
k = potassium();
delay(250);
h = ph();
delay(250);
Serial.print("Nitrogen: ");
Serial.print(n);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(p);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(k);
Serial.println(" mg/kg");
Serial.print("pH: ");
Serial.println(h);
if (WiFi.status() == WL_CONNECTED) {
WiFiClient client;
HTTPClient http;
String serverPath = String(serverName) + "?nitrogen=" + String(n) + "&phosphorus=" + String(p) + "&potassium=" + String(k) + "&ph=" + String(h);
http.begin(client, serverPath);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String payload = http.getString();
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.println("Response payload: " + payload);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
delay(2000);
}
byte nitrogen() {
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (mod.write(nitro, sizeof(nitro)) == 8) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
//values[i] = mod.read();
values[i] = mod.read();
}
}
return values[3];
}
byte phosphorous() {
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (mod.write(phos, sizeof(phos)) == 8) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
}
}
return values[4];
}
byte potassium() {
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (mod.write(pota, sizeof(pota)) == 8) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
}
}
return values[5];
}
byte ph() {
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (mod.write(phv, sizeof(phv)) == 8) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
}
}
return values[6];
}