Removing a delay and adding a timer on a voltmeter sketch?

So Im very new to all of this but i managed to get this sketch working from copying from different places, its a simple blynk voltage monitor with OTA and a server running so I dont have to manually reset each WEMOS to do the OTA updates…

All works great, besides the Delay at the very bottom in the void loop, which delays everything, i have 3 relays hooked up to this wemos also and would prefer for them not to have a delay

What Im trying for is to get a timer for the V1 VirtuaWrite so it only updates every 30 min. to an hour since its just monitoring a battery bank…
and if possible maybe every 30 seconds when im looking at it in the Blynk App, but thats not mandatory

Seems like it would be a simple fix if I knew what I was doing lol

Any advice on how to make this happen would be greatly appreciated

setup is a wemos D1 mini and a voltage divider

Dont know how to share the sketch on here so link below is to a .txt of the sketch.

Welcome to the community.

Please read this article
http://help.blynk.cc/en/articles/2091699-keep-your-void-loop-clean

Having task running inside loop and having delays will disconnect the device from the cloud. delay(1000); is a blocking function.

Other functions must be called by a BlynkTimer
Instead of delay you should use LAMBDA TIMER
You can go through this excellent topic written by @Gunner

You can go through this topic written by @Gunner

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WebServer.h>
#include <SoftwareSerial.h>

BlynkTimer timer;

char auth[] = "Auth CODE";
char ssid[] = "Scotts";     
char pass[] = "password"; 

int analogInput = A0;    
float correctionfactor = 0; 
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0; 
float R2 = 7900.0; 
int value = 0;

#ifndef STASSID
#define STASSID "Scotts"    
#define STAPSK  "password"        
#endif

const char* ssid2 = STASSID;
const char* password2 = STAPSK;
ESP8266WebServer server;

bool ota_flag = true;
uint16_t time_elapsed = 0;

void setup() {
  pinMode(analogInput, INPUT);
  Blynk.begin(auth, ssid, pass);
  Blynk.virtualWrite(V1, vin); 

  Serial.begin(115200);
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid2, password2);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }
  ArduinoOTA.setHostname("TrailerVoltageMonitorWEMOS");
  ArduinoOTA.setPassword("1421");
  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_FS
      type = "filesystem";
    }
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  server.on("/restart", []() { 
    server.send(200, "text/plain", "Restarting...Trailer Voltage Monitor WEMOS");  //<------------What web page says
    delay(1000);
    ESP.restart();
  });
  server.begin();

  timer.setInterval(3000L, voltageRead);
}

void voltageRead() {
  value = analogRead(analogInput);
  vout = (value * 3.3) / 1023.0;
  vin = vout / (R2 / (R1 + R2));
  vin = vin - correctionfactor;
  Serial.print("INPUT V= ");
  Serial.println(vin, 4);
  timer.setTimeout(30000L, []() {
    Blynk.virtualWrite(V1, vin);
  });
}

void loop() {
  Blynk.run();
  timer.run();
  if (ota_flag)
  {
    while (time_elapsed < 15000)       
    {
      ArduinoOTA.handle();
      time_elapsed = millis();
      delay(10);
    }
    ota_flag = false;
  }
  server.handleClient();
}

And go through the sketch, and see if it works. And also study the code and understand what are the modifications made and how it works, so that you can use the same technic in the future.

2 Likes

YOUR the man… THANK YOU SO MUCH
that is exactly what i was looking for…
Iam looking for a way to donate $$ to you for helping me…
new to this site…is there a way? or a way to like? or ?
VERY VERY much appreciate it… code worked perfect…thanks

whats the difference in the
timer.setInterval(3000L, voltageRead);
and the
timer.setTimeout(30000L, { ??

Thanks so much
Scott V.

1 Like

Thank you. But here we like to learn and also share what we know. We dont expect anything but good gratitude in the forum :slightly_smiling_face:.

Here the function void voltageRead() is called every 30sec by the BlynkTimer.
By following this we dont disturb the Blynk.run(); in the loop and cause any sought of disconnections.

This is just like delay(3000);. But delay will cause the whole program to halt till the delay time is over. By doing this the blynk server will get no response from the device and this results in disconnection from server. So its a good practice to always use Lambda timers.

1 Like

@Madhukesh so as not to confuse @scottjvincent I think you have the 3 seconds and 30 seconds switched around. 3000L is 3s and 30000L is 30s. Just divide by 1000.:+1:t3:

This was just an example i gave. But yes it may confuse. Thank you for the correction.