Chat freely about anything...

User avatar
By Unyhhox
#95368 Hello. I seek help here. I do have a problem with following code. This code is going to be uploaded to D1 mini to control the fotovoltaic system. D1 mini will continuously read analog pin to measure battery voltage and when the battery voltage reaches certain point then contactors will be triggered to disconnect distribution network and connect fotovoltaic system. There need to be a delay between switching these two contactors so they do not meet each other which would cause a burn of an inverter. Anyway i need first to turn on the inverter with pin 12 then 8s dealy beucase it takes some time to get 230V at output of inverter and then trigger the contactors. All contactors and inverter are triggered with 230V AC so i use SSR (those from aliexpress with 4 channel, low trigger). If i use this code then the D1 mini is connecting to wifi, when connects to WIFI the leds of SSRs blinks, delay few seconds and then all of them are lighting at the same time which means all of the ssr are triggered even though i have delay in code. Obviously delay with millis does not work and i dont know why and there is also a mystery for me why all SSRs leds blink one time while connected to wifi? I am a bit worried that they will trigger in the start even if i dont want to. Thank you for your time, responses and advices. Tom

Code: Select all#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();
   }
User avatar
By rooppoorali
#95438 millis() function returns the number of milliseconds passed since the Arduino board began running the current program.
It cannot be used to add delay to your code. For adding delay, you have to use delay() function.