#include <ESP8266WiFi.h>
float analog_voltage;
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxxxx";
const char *ssid = "xxxx";
const char *password = "xxxx";
BlynkTimer timer;
long last_sensor_reading = 0;
long bojlerpower = 0;
const int numReadings = 30;
float readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
float total = 0; // the running total
float average = 0; // the average
unsigned long lastExecutedMillis = 0;
void setup() {
Serial.begin(115200);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
pinMode(13,OUTPUT); //13 D7 (distribution line contactor)
pinMode(14,OUTPUT); //14 je D5 (PV contactor)
pinMode(12,OUTPUT); //12 je D6 (inverter)
pinMode(16,OUTPUT); //16 je D0 (water heater)
Blynk.begin(auth, ssid, password);
}
void batteryvoltagerele(){
total = total - readings[readIndex];
readings[readIndex] = analogRead(A0)* 3.3 / 1023*18.86;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
average = total / numReadings; // battery voltage measurement with sampling of 30
if (average>52.6) {
digitalWrite(12,LOW); // inverter turns on if battery voltage reaches certain point
unsigned long currentMillis = millis();
if (currentMillis-lastExecutedMillis>=8000){ //8s delay and then switch two contactors
lastExecutedMillis = currentMillis;
digitalWrite(13,LOW); // disconnect distribution line
delay(40);
digitalWrite(14,LOW); //connect PV system
}}
else if (average<51.5){ // if voltage does not need meet requirements then contactors switch from PV to Distribution line
digitalWrite(14,HIGH);
delay(40);
digitalWrite(13 ,HIGH);
digitalWrite(12 ,HIGH);
}
Blynk.virtualWrite(V5, average);
Blynk.run();
}
void bojlerssr(){ //if battery is full and house does not need much power then electic water heater is turned on.
if (average>52.8) {
digitalWrite(16,HIGH);}
//if(millis() - bojlerpower >= 120000) {
else if (analog_voltage<52.5) {
digitalWrite(16,LOW);
//bojlerpower = millis();
//digitalWrite(15,LOW);
//if(millis() - bojlerpower >= 120000) {
//digitalWrite(15,HIGH);
//bojlerpower = millis(); }
}}
void loop() {
batteryvoltagerele();
bojlerssr();
}
It cannot be used to add delay to your code. For adding delay, you have to use delay() function.