#include "ThingSpeak.h"
#include <HTTPClient.h>
#include <WiFi.h>
#include <Wire.h>
#include <DHT.h>
#include <RTClib.h>
#include <Adafruit_SSD1306.h>
#define DHTPIN 12 // The pin to which DHT22 is connected
#define DHTTYPE DHT22 // DHT sensor type
#define LED_PIN 33 // The pin to which the red LED is connected
#define LED_PINBLUE 25 // The pin to which the blue LED is connected
#define SCREEN_WIDTH 128 // OLED display width in pixels
#define SCREEN_HEIGHT 64 // OLED display height in pixels
#define OLED_RESET -1 // No OLED reset
int engine_status;
int engine_status_pump;
int relayPinWarm = 13;
int relayPinPump = 14;
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2551090; // Your Channel Number
const char* myApiKey = "IVACARA64FXQG7LH"; // Replace with your actual API key
const char* myApiKeyR = "Z158LAQ7HUZ0QFMP"; // Replace with your actual API key
const char* server = "api.thingspeak.com";
const unsigned long myTalkBackID = 52328; // Replace with your TalkBack ID
const char* myTalkBackKey = "R68JQR99Y9VL1JVR"; // Replace with your TalkBack API Key
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // Initializing an object for an OLED display
DHT dht(DHTPIN, DHTTYPE); // Initializing an object to work with DHT22
RTC_DS1307 rtc; // Initializing an object to work with RTC DS1307
WiFiClient client;
String getTalkBackCommand() {
HTTPClient http;
String serverPath = "https://api.thingspeak.com/talkbacks/" + String(myTalkBackID) +
"/commands/execute?api_key=" + String(myTalkBackKey);
http.begin(serverPath.c_str());
int httpResponseCode = http.GET();
String payload = "";
if (httpResponseCode > 0) {
payload = http.getString(); // Get the response payload
Serial.println("Connect ir correct");
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end(); // Free resources
return payload;
}
void setup() {
Serial.begin(115200);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
Serial.println("Hello, ESP32!");
pinMode(LED_PIN, OUTPUT); // Assign pin to LED as output
digitalWrite(LED_PIN, LOW); // Make sure the LED is off at startup
pinMode(LED_PINBLUE, OUTPUT); // Assign pin to LED as output
digitalWrite(LED_PINBLUE, LOW); // Make sure the LED is off at startup
pinMode(relayPinWarm, OUTPUT);
pinMode(relayPinPump, OUTPUT);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is not running");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Initializing an OLED display with I2C address 0x3C
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.display(); // Display a welcome message on the OLED display
delay(2000);
display.clearDisplay();
}
void loop() {
// Reading data from temperature and humidity sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
String command = getTalkBackCommand();
// Get current time from RTC
DateTime now = rtc.now();
// Checking data for turning LEDs on/off
if (temperature < 15){
engine_status = 0;
}
if (temperature > 25){
engine_status = 1;
}
if (humidity < 40){
engine_status_pump = 0;
}
if (humidity > 65){
engine_status_pump = 1;
}
// Turn on the red LED if the temperature is below 15
if (temperature < 15 & engine_status == 0 or command == "TURN_ON_WARM") {
digitalWrite(LED_PIN, HIGH);
digitalWrite(relayPinWarm, HIGH);
delay(500);
engine_status = 1;
}
// Turn off the red LED when the temperature gets above 25
if (temperature > 25 & engine_status == 1 ) {
digitalWrite(LED_PIN, LOW);
digitalWrite(relayPinWarm, LOW);
delay(500);
engine_status = 0;
}
// Turn on the blue LED if the humidity is below 40
if (humidity < 40 & engine_status_pump == 0 or command == "TURN_ON_PUMP") {
digitalWrite(LED_PINBLUE, HIGH);
digitalWrite(relayPinPump, HIGH);
delay(500);
engine_status_pump = 1;
}
// Turn off the blue LED when the humidity rises above 65
if (humidity > 65 & engine_status_pump == 1 ) {
digitalWrite(LED_PINBLUE, LOW);
digitalWrite(relayPinPump, LOW);
delay(500);
engine_status_pump = 0;
}
// Output of received data to an OLED display
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Temperature: ");
display.print(temperature);
display.println(" *C");
display.print("Humidity: ");
display.print(humidity);
display.println(" %");
display.print("Time: ");
display.print(now.hour());
display.print(':');
display.print(now.minute());
display.print(':');
display.println(now.second());
display.display();
// Sending data via serial port for monitoring
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Time: ");
Serial.print(now.hour());
Serial.print(':');
Serial.print(now.minute());
Serial.print(':');
Serial.println(now.second());
ThingSpeak.setField(1,temperature);
ThingSpeak.setField(2,humidity);
ThingSpeak.writeFields(myChannelNumber, myApiKey);
delay(10000); // Delay before next iteration (10 seconds)
}