#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// const char *ssid = "gta";
// const char *password = "gta123@";
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const char *serverUrl = "https://2b7c2c09-9609-409c-99e5-457a71ebfbd5-00-155xe4qkgewzo.sisko.replit.dev/";
// const char *userName = "atharva";
// const char *apiKey = "qx3w49r3-kifh-ix9z-04lr-mz1y4bpl8rer";
//Edge computing
#define MoisensorPin 33
int MoisensorValue = 0;
int limit = 55;
#define N_Pin 34
#define P_Pin 35
#define K_Pin 32
#define pump_pin 13
#define ph_pin 25
// http://127.0.0.1:5000/sensorvalue?
// a_id=acc1243&sm=81&ps=0&N=45&P=78&K=90&temp=28&hum=89&ph=5.5
// Variables for route
String a_id = "acc1234";
int pump = 0;
int N_val = 0;
int P_val = 0;
int K_val = 0;
float temp = 0.0;
float hum= 0.0;
float pH_val= 0.0;
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// pin declaration
dht.begin();
pinMode(pump_pin, OUTPUT); digitalWrite(pump_pin, LOW);
delay(2000);
// Make GET request
// makeGetRequest();
}
void makeGetRequest() {
HTTPClient http;
String url = String(serverUrl) +
"sensorvalue?a_id=" + a_id +
"&sm="+ String(MoisensorValue) +
"&ps=" + String(pump) +
"&N=" + String(N_val) +
"&P=" + String(P_val) +
"&K=" + String(K_val) +
"&temp=" + String(temp) +
"&hum=" + String(hum) +
"&ph=" + String(pH_val);
Serial.println("Sending GET request to: " + url);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("HTTP response code: %d\n", httpCode);
String payload = http.getString();
Serial.println("Server response: " + payload);
} else {
Serial.printf("HTTP request failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
void loop() {
//Read Soil moisture Sensor value
MoisensorValue = analogRead(MoisensorPin); //sprintf(buffer, "Soil Moisture Raw: %d", MoisensorValue);Serial.println(buffer);
MoisensorValue = map(MoisensorValue, 0, 4095, 0, 100); // 0 to 100%
MoisensorValue = 100 - MoisensorValue; // inverse the value
// Nitrogen (N): 20 to 200 parts per million (ppm)
// Phosphorus (P): 10 to 50 ppm
// Potassium (K): 100 to 400 ppm
//N Sensor value
N_val = analogRead(N_Pin); N_val = map(N_val, 0, 4095, 20, 200); // 0 to 100%
//P Sensor value
P_val = analogRead(P_Pin); P_val = map(P_val, 0, 4095, 10, 50); // 0 to 100%
//K Sensor value
K_val = analogRead(K_Pin); K_val = map(K_val, 0, 4095, 100, 400); // 0 to 100%
//pH Sensor value
pH_val = analogRead(ph_pin); pH_val = map(pH_val, 0, 4095, 0, 14); // 0 to 100%
//controll Pump
if (MoisensorValue<limit){digitalWrite(pump_pin, LOW); pump = 1; }
else { digitalWrite(pump_pin, HIGH); pump = 0; }
temp = dht.readTemperature();
hum = dht.readHumidity();
Serial.print("weather ");
if (isnan(temp) || isnan(hum)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
String message = "temp: " + String(temp) + " humidity: " + String(hum);
Serial.println(message);
makeGetRequest();
// delay(40000); //40sec
delay(10000); //60sec
}