IoT Based Portable Soil N-P-K Measuring Device

Hi! Good day! We are developing a portable soil N-P-K measuring device using the Internet of Things. Our objective is to measure the NPK levels in the soil and send the results via IoT on the ThingSpeak platform. Although the information recorded by our npk sensor is reflected or visible on our oled screen display, it was not communicated or received. Please assist us in determining what to do or problems we need to resolve. For your reference, I've included our code and schematic diagrams to this discussion. Thanks in advance.

SENSOR NODE:
#include <SPI.h>
#include <SoftwareSerial.h>
#include <nRF24L01.h>
#include <RF24.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

RF24 radio(9, 10); // CE, CSN on Blue Pill
const uint64_t address = 0xF0F0F0F0E1LL;

#define ONE_WIRE_BUS 5

#define RE 8
#define DE 7

const byte code[] = {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
byte values[11];
SoftwareSerial mod(2, 3);

const int AirValue = 645; //you need to replace this value with Value_1
const int WaterValue = 254; //you need to replace this value with Value_2
int soilMoistureValue = 0;
int soilmoisturepercent = 0;

float temperature;
int nitrogen;
int phosphorus;
int potassium;

struct MyVariable
{
byte soilmoisturepercent;
byte nitrogen;
byte phosphorus;
byte potassium;
byte temperature;
};
MyVariable variable;

void setup()
{
Serial.begin(9600);
mod.begin(9600);
radio.begin(); //Starting the Wireless communication
radio.setChannel(115);
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
radio.setDataRate(RF24_250KBPS);
radio.stopListening(); //This sets the module as transmitter
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
delay(500);
display.clearDisplay();
display.setCursor(25, 15);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(" NPK Sensor");
display.setCursor(25, 35);
display.setTextSize(1);
display.print("Initializing");
display.display();
delay(3000);
}

void loop()
{
byte val;
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
if (mod.write(code, sizeof(code)) == 8)
{
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 11; i++)
{
//Serial.print(mod.read(),HEX);
values[i] = mod.read();
Serial.print(values[i], HEX);
}
Serial.println();
}
nitrogen = values[4];
phosphorus = values[6];
potassium = values[8];
delay(1500);

variable.nitrogen = nitrogen;
variable.phosphorus = phosphorus;
variable.potassium = potassium;;
delay(1500);

Serial.print("Nitrogen: ");
Serial.print(variable.nitrogen);
Serial.println(" mg/kg");
Serial.print("phosphorus: ");
Serial.print(variable.phosphorus);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(variable.potassium);
Serial.println(" mg/kg");

display.clearDisplay();

display.setTextSize(2);
display.setCursor(0, 5);
display.print("N: ");
display.print(variable.nitrogen);
display.setTextSize(1);
display.print(" mg/kg");

display.setTextSize(2);
display.setCursor(0, 25);
display.print("P: ");
display.print(variable.phosphorus);
display.setTextSize(1);
display.print(" mg/kg");

display.setTextSize(2);
display.setCursor(0, 45);
display.print("K: ");
display.print(variable.potassium);
display.setTextSize(1);
display.print(" mg/kg");

display.display();

Serial.println();
radio.write(&variable, sizeof(MyVariable));

Serial.println("Data Packet Sent");
Serial.println("");
delay(10000);
}

GATEWAY CIRCUIT:
#include <WiFi.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

String apiKey = "ARRB2YDUOKPNREA7";

//const char* ssid = "virus4G";
//const char* password = "V@yRus@lerT14";

const char* ssid = "NetworkName";
const char* password = "31ay9t5jE_SME";

const char* server = "api.thingspeak.com";

RF24 radio(4, 5);
const uint64_t address = 0xF0F0F0F0E1LL;
bool newData = false;

struct MyVariable
{
byte nitrogen;
byte phosphorous;
byte potassium;
};
MyVariable variable;

WiFiClient client;

void setup()
{
Serial.begin(115200);
radio.begin(); //Starting the Wireless communication
radio.openReadingPipe(0, address); //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
radio.startListening(); //This sets the module as receiver
radio.setChannel(115);
radio.setDataRate(RF24_250KBPS);

Serial.println("Receiver Started....");
Serial.print("Connecting to ");
Serial.println(ssid);
Serial.println();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}

int recvData()
{
if ( radio.available() )
{
radio.read(&variable, sizeof(MyVariable));
newData = true;
}
}

void loop(){

if (newData == true )
{

Serial.println("Data Received:");

Serial.print("Nitrogen: ");
Serial.print(variable.nitrogen);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(variable.phosphorous);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(variable.potassium);
Serial.println(" mg/kg");

Serial.println();

if (client.connect(server, 80))
{
String postStr = apiKey;
postStr += "&field1=";
postStr += String(variable.nitrogen);
postStr += "&field2=";
postStr += String(variable.phosphorous);
postStr += "&field3=";
postStr += String(variable.potassium);
postStr += "\r\n\r\n\r\n\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
    delay(1000);
    Serial.println("Data Sent to Server");
  }
    client.stop();

}

}

SCHEMATIC DIAGRAMS:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.