#include <HTTPClient.h>
// Your server URL (replace with your server's URL)
const char* serverUrl = "http://127.0.0.1:8000/api/soil-health/";
// Pin setup
const int moisturePin = 32;
const int phPin = 33;
const int nPin = 34;
const int pPin = 35;
const int kPin = 27;
const int tempPin = 14; // Analog Temperature Sensor connected to GPIO 14
const int ledPin = 26;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
}
void loop() {
// Simulate temperature readings with slight fluctuations
int rawValue = analogRead(tempPin);
float temperature = (rawValue / 4095.0) * 100 + random(-2, 2); // Add random fluctuation between -2°C and +2°C
// Simulate humidity and other sensor data with random values
float humidity = random(30, 70); // Random humidity between 30% and 70%
int soilMoisture = random(400, 700); // Random soil moisture value
int soilPH = random(0, 14); // Random soil pH value (assuming 0-14 range)
int soilN = random(0, 100); // Random nitrogen level
int soilP = random(0, 100); // Random phosphorus level
int soilK = random(0, 100); // Random potassium level
HTTPClient http;
// Prepare JSON data
String postData = "{";
postData += "\"date\":\"2024-08-15\","; // Enclose date value in quotes
postData += "\"soil_moisture\":" + String(soilMoisture) + ",";
postData += "\"temperature\":" + String(temperature) + ",";
postData += "\"nitrogen\":" + String(soilN) + ",";
postData += "\"phosphorus\":" + String(soilP) + ",";
postData += "\"potassium\":" + String(soilK) + ",";
postData += "\"soil_ph\":" + String(soilPH);
postData += "}";
// Send the request to the server
http.begin(serverUrl); // Your server URL
http.addHeader("Content-Type", "application/json"); // Set content type to JSON
// POST request with the sensor data
int httpResponseCode = http.POST(postData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode); // Print response code
Serial.println(response); // Print response payload
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end(); // Close connection
// Make LED blink based on soil moisture level
if (soilMoisture < 500) {
digitalWrite(ledPin, HIGH); // Turn on LED
delay(500); // Keep LED on for 500ms
digitalWrite(ledPin, LOW); // Turn off LED
delay(500); // Keep LED off for 500ms
} else {
digitalWrite(ledPin, LOW); // Turn off LED
}
delay(10000); // Wait for 2 seconds before next loop
}