#include <WiFi.h>
#include <ThingSpeak.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// WiFi Credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak Settings
const char* apiKey = "CJUECCU568UNR58Y";
const long channelID = 3026181;
WiFiClient client;
// Pin Definitions
const int soilPin = 34;
const int oneWireBus = 4;
const int redLED = 27; // Changed from 19 (MISO conflict)
const int greenLED = 5;
const int yellowLED = 26; // Changed from 18 (SCK conflict)
// ILI9341 Pin Definitions
#define TFT_CS 15
#define TFT_RST 13
#define TFT_DC 2
// MOSI, MISO, SCK use default VSPI pins (23, 19, 18)
// Sensor Setup
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
// ILI9341 Setup
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Thresholds
const int soilThreshold = 1000;
const float tempThreshold = 35.0; // Changed from 25.0 to 35.0
// LED Blink States
bool blinkState = false;
unsigned long lastBlinkTime = 0;
const long blinkInterval = 500; // Changed from 250ms to 500ms (slower blink)
// Timing
unsigned long lastThingSpeakUpdate = 0;
const long thingSpeakInterval = 15000;
unsigned long lastSensorCheck = 0;
const long sensorCheckInterval = 1000;
unsigned long lastDisplayUpdate = 0;
const long displayUpdateInterval = 1000; // Update display every 1s
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
// Initialize pins
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
// Initialize sensors
sensors.begin();
ThingSpeak.begin(client);
// Initialize ILI9341
tft.begin();
tft.setRotation(3); // Adjust for landscape (0-3 based on orientation)
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(0, 0);
tft.println("Initializing...");
// Connect to WiFi
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(300);
Serial.print(".");
}
Serial.println("\n✅ Connected to WiFi!");
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.println("WiFi Connected!");
delay(1000);
}
void loop() {
unsigned long currentTime = millis();
// Check sensor values every second
if (currentTime - lastSensorCheck >= sensorCheckInterval) {
lastSensorCheck = currentTime;
int soilValue = analogRead(soilPin);
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
if (temperature == DEVICE_DISCONNECTED_C || temperature < -55 || temperature > 125) {
Serial.println("⚠️ Invalid temperature. Using 25.0°C default");
temperature = 25.0;
}
Serial.print("Soil: ");
Serial.print(soilValue);
Serial.print(" | Temp: ");
Serial.println(temperature);
// LED Logic
bool bothHigh = (soilValue > soilThreshold && temperature > tempThreshold);
bool eitherHigh = (temperature > tempThreshold || soilValue > soilThreshold) && !bothHigh;
bool bothLow = (soilValue <= soilThreshold && temperature <= tempThreshold);
// Blinking for Red LED
if (currentTime - lastBlinkTime >= blinkInterval) {
lastBlinkTime = currentTime;
blinkState = !blinkState;
// Red LED: blink when both high
digitalWrite(redLED, bothHigh ? blinkState : LOW);
}
// Yellow LED: on (steady) when either high but not both
digitalWrite(yellowLED, eitherHigh ? HIGH : LOW);
// Green LED: on (steady) when both low
digitalWrite(greenLED, bothLow ? HIGH : LOW);
// Update ILI9341 display
if (currentTime - lastDisplayUpdate >= displayUpdateInterval) {
lastDisplayUpdate = currentTime;
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.setTextSize(3);
tft.print("Soil: ");
tft.print(soilValue);
tft.setCursor(10, 50);
tft.print("Temp: ");
tft.print(temperature, 1);
tft.print(" C");
// Display LED status
tft.setCursor(10, 90);
tft.setTextSize(2);
tft.print("Red: ");
tft.print(bothHigh ? "Blink" : "Off");
tft.setCursor(10, 110);
tft.print("Yellow: ");
tft.print(eitherHigh ? "On" : "Off");
tft.setCursor(10, 130);
tft.print("Green: ");
tft.print(bothLow ? "On" : "Off");
}
// Send data to ThingSpeak every 15 seconds
if (WiFi.status() == WL_CONNECTED && (currentTime - lastThingSpeakUpdate >= thingSpeakInterval)) {
ThingSpeak.setField(1, soilValue);
ThingSpeak.setField(2, temperature);
int httpCode = ThingSpeak.writeFields(channelID, apiKey);
if (httpCode == 200) {
Serial.println("✅ Data sent to ThingSpeak!");
tft.setCursor(10, 170);
tft.setTextSize(2);
tft.print("ThingSpeak OK");
} else {
Serial.print("❌ Failed to send data. HTTP code: ");
Serial.println(httpCode);
tft.setCursor(10, 170);
tft.setTextSize(2);
tft.print("ThingSpeak Error");
}
lastThingSpeakUpdate = currentTime;
}
}
}Loading
ds18b20
ds18b20
Loading
grove-oled-sh1107
grove-oled-sh1107
Loading
grove-oled-sh1107
grove-oled-sh1107
Loading
ssd1306
ssd1306