#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define ONE_WIRE_BUS 4
// WiFi credentials
const char* ssid = "Wokwi-GUEST"; // WiFi SSID
const char* password = ""; // WiFi password
// ThingSpeak API
const char* apiWriteKey = "SE4LM687SXE0PVFC"; // ThingSpeak API write key
const char* server = "http://api.thingspeak.com/update"; // ThingSpeak server URL for updating data
OneWire oneWire(ONE_WIRE_BUS); // Initialize OneWire bus
DallasTemperature sensors(&oneWire); // Initialize DallasTemperature library
// Pins for relays and LEDs
const int waterRelayPin = 13;
const int lightRelayPin = 27;
const int waterLedPin = 14;
const int lightLedPin = 12;
const int potPin = 34; // Potentiometer pin used for simulating soil moisture
float simulatedTemperature = 20.0; // Good amount temperature
int simulatedSoilMoisture = 300; // Good amount soil moisture level
void setup() {
Serial.begin(115200);
// Set pin modes for relays and LEDs
pinMode(waterRelayPin, OUTPUT);
pinMode(lightRelayPin, OUTPUT);
pinMode(waterLedPin, OUTPUT);
pinMode(lightLedPin, OUTPUT);
sensors.begin();
// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi..");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to the WiFi network");
}
void loop() {
// Simulate temperature and soil moisture changes based on relay status
if (digitalRead(lightRelayPin) == HIGH) {
simulatedTemperature += 4.0; // Increase temperature if light is on
} else {
simulatedTemperature -= 2.0; // Decrease temperature if light is off
}
if (digitalRead(waterRelayPin) == HIGH) {
simulatedSoilMoisture += 50; // Increase soil moisture if water pump is on
} else {
simulatedSoilMoisture -= 20; // Decrease soil moisture if water pump is off
}
// Constrain simulated temperature and soil moisture within valid ranges
simulatedTemperature = constrain(simulatedTemperature, 10, 40);
simulatedSoilMoisture = constrain(simulatedSoilMoisture, 0, 1023);
// Send data to ThingSpeak if WiFi is connected
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(server); //Begin HTTP connnection
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "api_key=" + String(apiWriteKey) + "&field1=" + String(simulatedTemperature) + "&field2=" + String(simulatedSoilMoisture);
int httpResponseCode = http.POST(httpRequestData); // Send POST request
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(response); // Print response from ThingSpeak
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode); // Print HTTP error code
}
http.end();
}
// Control the water pump and moisture LED based on soil moisture
if (simulatedSoilMoisture < 300) {
digitalWrite(waterRelayPin, HIGH);
digitalWrite(waterLedPin, HIGH);
Serial.println("Water Pump: ON");
} else {
digitalWrite(waterRelayPin, LOW);
digitalWrite(waterLedPin, LOW);
Serial.println("Water Pump: OFF");
}
// Control the light system and sunlight LED based on temperature
if (simulatedTemperature < 20) { // Activate light if temperature is below 20°C
digitalWrite(lightRelayPin, HIGH);
digitalWrite(lightLedPin, HIGH);
Serial.println("Light System: ON");
} else {
digitalWrite(lightRelayPin, LOW);
digitalWrite(lightLedPin, LOW);
Serial.println("Light System: OFF");
}
delay(15000); // Wait 15 seconds between updates to avoid ThingSpeak rate limit
}