#include <WiFi.h>
#include <HTTPClient.h>
// Replace with your network credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
// Replace with your IoT platform endpoint
const char* serverName = "http://example.com/api/data"; // Example: ThingSpeak URL
// GPIO pin for soil moisture sensor
const int soilMoisturePin = 34; // Adjust based on your wiring
// Dummy function to simulate nutrient level
int readNutrientLevel() {
// Replace this with actual sensor reading code
return random(0, 100); // Returns a random value between 0 and 100
}
void setup() {
Serial.begin(115200);
// Initialize WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
}
void loop() {
// Read soil moisture sensor
int soilMoistureValue = analogRead(soilMoisturePin);
// Read nutrient level (simulated here)
int nutrientLevel = readNutrientLevel();
// Prepare data for sending
String postData = "soilMoisture=" + String(soilMoistureValue) + "&nutrientLevel=" + String(nutrientLevel);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(postData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println("HTTP Response code: " + String(httpResponseCode));
Serial.println("Response: " + response);
} else {
Serial.println("Error code: " + String(httpResponseCode));
}
http.end();
} else {
Serial.println("Error in WiFi connection");
}
// Delay before the next reading
delay(60000); // Send data every 60 seconds
}
Loading
esp32-s3-box
esp32-s3-box