// *************************************************************/
#define BLYNK_TEMPLATE_ID "TMPL4vLs-XKTJ"
#define BLYNK_TEMPLATE_NAME "Temperatur"
#define BLYNK_AUTH_TOKEN "denu6kjNliVJyZ2-qGbKvRlhl5QR7yoe"
///////////
#include "DHT.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "time.h"
const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 3600;
// Global variables to store Blynk times
int startHour;
int startMinute;
int startSecond;
int stopHour;
int stopMinute;
int stopSecond;
bool selectedDays[7];
int temp;
int hum;
#define DHTPIN 4 // DHT22 sensor data pin
#define DHTTYPE DHT11 // DHT22 sensor model
DHT dht(DHTPIN, DHTTYPE);
int l1 = 18;//12; // Output connected to D12
int l2 = 22;//13; // Output connected to D13
int l3 = 23;//14; // Output connected to D14
int l4 = 12;
int l5 = 13;
int l6 = 14;
int l7 = 15;
int l8 = 18;
int setpointV1 = 20; // Adjust this setpoint value as needed
int setpointV2 = 2;
int mainSetpointV3 = 0;
int systemState = 0; // 0: Idle, 1: Demand
// Time variables (in milliseconds)
unsigned long timeV4 = 4000; // Time to wait in Idle state
unsigned long timeV6 = 6000; // Duration of Period A
unsigned long timeV7 = 7000; // Duration of Period B
unsigned long timeV8 = 8000; // Duration of Period C
unsigned long startTime; // Variable to store the start time
bool condition_for_timeV4 = true;
bool coolingSystem = false;
////////c2
bool cleaningSystem = false;
bool manualCleaning = false;
bool autoCleaning = true;
bool L5 = false;
bool L6 = false;
///////////C3
int a9 = 5;
int a11 = 2;
unsigned long a13 = 120;
unsigned long lastActivationTime = 0;
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Galaxy A24 C1E6";
char pass[] = "pizzafries";
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
// TempAndHumidity data = dhtSensor.getTempAndHumidity();
temp = dht.readTemperature();
hum = dht.readHumidity();
Blynk.virtualWrite(V0, temp);
Blynk.virtualWrite(V1, hum);
// Serial.println("Temp: " + String(data.temperature, 2) + "°C");
// Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
}
void startPeriodA() {
// Code for actions during Period A
digitalWrite(l1, HIGH);
digitalWrite(l2, HIGH);
digitalWrite(l3, LOW);
Serial.println("Period A");
}
void startPeriodB() {
// Code for actions during Period B
digitalWrite(l1, HIGH);
digitalWrite(l2, LOW);
digitalWrite(l3, HIGH);
Serial.println("Period B");
}
void startPeriodC() {
// Code for actions during Period C
digitalWrite(l1, LOW);
digitalWrite(l2, LOW);
digitalWrite(l3, LOW);
Serial.println("Period C");
}
void setup() {
Serial.begin(115200);
pinMode(l1, OUTPUT);
pinMode(l2, OUTPUT);
pinMode(l3, OUTPUT);
dht.begin();
pinMode(2, OUTPUT);
Serial.println("DHT22 test");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(1000L, myTimerEvent);
configTime(5 * 3600, 0, "pool.ntp.org");
// configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
delay(500);
}
BLYNK_WRITE(V3) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
setpointV1 = param.asInt();
}
BLYNK_WRITE(V2) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
setpointV2 = param.asInt();
}
BLYNK_WRITE(V4) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
timeV4 = param.asInt() * 1000;
}
BLYNK_WRITE(V5) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
//timeV5 = param.asInt();
}
BLYNK_WRITE(V6) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
//delay(2000);
timeV6 = param.asInt() * 1000;
}
BLYNK_WRITE(V7) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
timeV7 = param.asInt() * 1000;
}
BLYNK_WRITE(V8) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
timeV8 = param.asInt() * 1000;
}
BLYNK_WRITE(V9) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
if (param.asInt() == 1) {
coolingSystem = true;
}
else {
coolingSystem = false;
}
}
BLYNK_WRITE(V10) // Executes when the value of virtual pin 0 changes
{
TimeInputParam t(param);
// Process start time
if (t.hasStartTime())
{
Serial.println(String("Start: ") +
t.getStartHour() + ":" +
t.getStartMinute() + ":" +
t.getStartSecond());
startHour = t.getStartHour();
startMinute = t.getStartMinute();
startSecond = t.getStartSecond();
}
else if (t.isStartSunrise())
{
Serial.println("Start at sunrise");
}
else if (t.isStartSunset())
{
Serial.println("Start at sunset");
}
else
{
// Do nothing
}
// Process stop time
if (t.hasStopTime())
{
Serial.println(String("Stop: ") +
t.getStopHour() + ":" +
t.getStopMinute() + ":" +
t.getStopSecond());
stopHour = t.getStopHour();
stopMinute = t.getStopMinute();
stopSecond = t.getStopSecond();
}
else if (t.isStopSunrise())
{
Serial.println("Stop at sunrise");
}
else if (t.isStopSunset())
{
Serial.println("Stop at sunset");
}
else
{
// Do nothing: no stop time was set
}
// Process timezone
// Timezone is already added to start/stop time
Serial.println(String("Time zone: ") + t.getTZ());
// Get timezone offset (in seconds)
Serial.println(String("Time zone offset: ") + t.getTZ_Offset());
// Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
for (int i = 1; i <= 7; i++) {
if (t.isWeekdaySelected(i)) {
Serial.println(String("Day ") + i + " is selected");
}
}
for (int i = 1; i <= 7; i++) {
selectedDays[i - 1] = t.isWeekdaySelected(i);
Serial.print("day");
Serial.print(i);
Serial.print(" ");
Serial.println(selectedDays[i - 1]);
}
Serial.println();
delay(5000);
}
BLYNK_WRITE(V11) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
if (param.asInt() == 1) {
L5 = true;
}
else {
L5 = false;
}
}
BLYNK_WRITE(V12) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
if (param.asInt() == 1) {
L6 = true;
}
else {
L6 = false;
}
}
BLYNK_WRITE(V13) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
if (param.asInt() == 1) {
manualCleaning = true;
autoCleaning = false;
digitalWrite(l4, HIGH);
}
else {
manualCleaning = false;
autoCleaning = true;
digitalWrite(l4, LOW);
}
}
BLYNK_WRITE(V14) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
a9 = param.asInt() ;
}
BLYNK_WRITE(V15) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
a11 = param.asInt() ;
}
BLYNK_WRITE(V16) // Executes when the value of virtual pin 0 changes
{
Serial.println(param.asInt());
a13 = param.asInt() * 1000;
}
void loop() {
mainSetpointV3 = setpointV1 + setpointV2;
float temperatureS1 = dht.readTemperature();
float temperatureS2 = 25;
///C3 Calculation
float a10 = a9 + temperatureS1;
float a12 = a10 + a11;
float mainSetpointforC3 = a12;
Serial.print("mainSetpointV3: ");
Serial.println(mainSetpointV3);
Serial.print("Temperature S1: ");
Serial.println(temperatureS1);
Serial.print("timeV4: ");
Serial.println(timeV4);
// Serial.print("timeV5: ");
// Serial.println(timeV5);
Serial.print("timeV6: ");
Serial.println(timeV6);
Serial.print("timeV7: ");
Serial.println(timeV7);
Serial.print("timeV8: ");
Serial.println(timeV8);
// && coolingSystem == true
if (!isnan(temperatureS1) && temperatureS1 > mainSetpointV3 ) {
if (systemState == 0) {
// Transition to Demand state
systemState = 1;
startTime = millis(); // Record the start time
Serial.println("Demand State");
}
} else {
// Transition to Idle state
systemState = 0;
condition_for_timeV4 = true;
Serial.println("Idle State");
}
// Control outputs based on system state and elapsed time
if (systemState == 1) {
unsigned long elapsedTime = millis() - startTime;
if (elapsedTime < timeV4) {
// Wait for timeV4
if (condition_for_timeV4 == true)
{
digitalWrite(l1, LOW);
digitalWrite(l2, LOW);
digitalWrite(l3, LOW);
Serial.println("Waiting for timeV4");
}
if (condition_for_timeV4 == false) {
timeV4 = 0;
}
} else if (elapsedTime < (timeV4 + timeV6)) {
// Period A
startPeriodA();
} else if (elapsedTime < (timeV4 + timeV6 + timeV7)) {
// Period B
startPeriodB();
} else if (elapsedTime < (timeV4 + timeV6 + timeV7 + timeV8)) {
// Period C
startPeriodC();
} else {
// Reset to idle state after completing Period C
systemState = 0;
Serial.println("Resetting to New Time");
}
} else {
// Idle state
digitalWrite(l1, LOW);
digitalWrite(l2, LOW);
digitalWrite(l3, LOW);
}
// delay(1000); // Adjust the delay as needed
Blynk.run();
timer.run(); // Initiates BlynkTimer.
printLocalTime();
// Get and compare times
time_t now = time(nullptr);
struct tm timeinfo;
localtime_r(&now, &timeinfo);
// Compare with Blynk start time
// Compare with Blynk start time
if (selectedDays[(timeinfo.tm_wday - 1)]) {
if (timeinfo.tm_hour == startHour &&
timeinfo.tm_min == startMinute) {
// Perform action for Blynk start time
Serial.println("Blynk start time reached!");
digitalWrite(2, HIGH);
cleaningSystem = true;
}
// Compare with Blynk stop time
if (timeinfo.tm_hour == stopHour &&
timeinfo.tm_min == stopMinute) {
// Perform action for Blynk stop time
Serial.println("Blynk stop time reached!");
digitalWrite(2, LOW);
cleaningSystem = false;
}
}
// Serial.print("TEst ");
// Serial.println(timeinfo.tm_wday);
//digitalWrite(2, HIGH);
if (autoCleaning) {
cleaning();
}
if (manualCleaning) {
if (L5 == true) {
digitalWrite(l5, HIGH);
}
else {
digitalWrite(l5, LOW);
}
if (L6 == true) {
digitalWrite(l6, HIGH);
}
else {
digitalWrite(l6, LOW);
}
}
if (temperatureS2 > mainSetpointforC3) {
if (millis() - lastActivationTime >= a13 ) {
turnOffOutputs();
lastActivationTime = millis();
}
if (millis() - lastActivationTime >= a13 *2) {
// Start the activation process again
turnOnOutputs();
lastActivationTime = millis();
}
} else {
// If temperature is less than MAIN_SETPOINT_C3, turn off all outputs
turnOffOutputs();
// Reset lastActivationTime so that it can start again when the condition is met
lastActivationTime = millis();
}
}
void cleaning() {
if (cleaningSystem == true) {
digitalWrite(l4, HIGH);
}
if (cleaningSystem == false) {
digitalWrite(l4, LOW);
}
if (L5 == true && cleaningSystem == true) {
digitalWrite(l5, HIGH);
}
else {
digitalWrite(l5, LOW);
}
if (L6 == true && cleaningSystem == true) {
digitalWrite(l6, HIGH);
}
else {
digitalWrite(l6, LOW);
}
}
void turnOnOutputs() {
digitalWrite(l7, HIGH);
digitalWrite(l8, HIGH);
}
void turnOffOutputs() {
digitalWrite(l7, LOW);
digitalWrite(l8, LOW);
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
Serial.print("Day of week: ");
Serial.println(&timeinfo, "%A");
Serial.print("Month: ");
Serial.println(&timeinfo, "%B");
Serial.print("Day of Month: ");
Serial.println(&timeinfo, "%d");
Serial.print("Year: ");
Serial.println(&timeinfo, "%Y");
Serial.print("Hour: ");
Serial.println(&timeinfo, "%H");
Serial.print("Hour (12 hour format): ");
Serial.println(&timeinfo, "%I");
Serial.print("Minute: ");
Serial.println(&timeinfo, "%M");
Serial.print("Second: ");
Serial.println(&timeinfo, "%S");
Serial.println("Time variables");
char timeHour[3];
strftime(timeHour, 3, "%H", &timeinfo);
Serial.println(timeHour);
char timeWeekDay[10];
strftime(timeWeekDay, 10, "%A", &timeinfo);
Serial.println(timeWeekDay);
Serial.println();
}