SOLVED: First script works, second times out on login - help, newbie!

Yesterday I got my first Blynk script running. I was very pleased at how easy it was.
Today I wrote my second script, and I can’t get it to log in to the server.

Here’s the background:
I am using ESP12’s as the hardware, with the Arduino IDE.
I have enabled DEBUG and here’s the log that Blynk generates:

[34240] Connecting to house-net 
[39741] Connected to WiFi 
[39741] IP: 192.168.1.54 
[39741] Blynk v0.3.7 on ESP8266 
[39741] Connecting to blynk-cloud.com:8442 
[39831] <[02|00|01|00] edda09ed65974cdaa8b4c4a20b74082a 
Light: 911 
[41907] Login timeout 
 PIR high 
2.6 PIR low 
[44907] Connecting to blynk-cloud.com:8442 
[44988] <[02|00|01|00] edda09ed65974cdaa8b4c4a20b74082a 
[47069] Login timeout 
Light: 985 
[50069] Connecting to blynk-cloud.com:8442 
[50899] <[02|00|01|00] edda09ed65974cdaa8b4c4a20b74082a 
6.4 PIR high 
[52977] Login timeout 
3.1 PIR low 
[55977] Connecting to blynk-cloud.com:8442 
[56053] <[02|00|01|00] edda09ed65974cdaa8b4c4a20b74082a 
[58123] Login timeout 
5.1 PIR high 
Light: 1024 
[61123] Connecting to blynk-cloud.com:8442 
[61204] <[02|00|01|00] edda09ed65974cdaa8b4c4a20b74082a 

Here’s the code. The main difference as far as I can see is that in this example
I’m using virtual pins for all the communications with the app on my phone.
The project is a clone of the first one, so all the configuration data is identical.

What I have tried so far:

  • Waiting a few minutes and retrying

  • Checking that the original app still works

  • Getting a new AUTH code

  • Beer

  • More beer

  • Reading the documentation

// Button controlled relays
// Using ESP-12



#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#define BLYNK_DEBUG        // Optional, this enables more detailed prints

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define RLA1 16  // opto-isolated relays
#define RLA2 14  //
#define PIR 13   // PIR detector High -- hit
#define LDR 0    // Light Dependent Resistor
#define SW1 2    // switch for RLA1
#define SW2 12   // switch for RLA2
#define LED1 4   // LED state of RLA1
#define LED2 5   // LED state of RLA2


// char auth[] = "2f75960dca504b28a5e5abaebae278b3";
char auth[] = "edda09ed65974cdaa8b4c4a20b74082a";

void setup() {
  Serial.begin(9600);
  Serial.println("Hello");
  
  pinMode(RLA1, OUTPUT);
  pinMode(RLA2, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  
  pinMode(PIR, INPUT_PULLUP);
  pinMode(LDR, INPUT);

  pinMode(SW1, INPUT);
  pinMode(SW2, INPUT);
  
  digitalWrite(RLA1, 0);
  digitalWrite(RLA2, 0);

  digitalWrite(LED1, 1);
  digitalWrite(LED2, 1);
  
//  Blynk.connectWiFi("house-net", "yeah, this really is my passcode");
//  Blynk.config(auth);
  Blynk.begin(auth, "house-net", "no, really. It is");

  
  digitalWrite(LED1, 0);
  digitalWrite(LED2, 0);

}

bool pir_state = 0;
unsigned int change_time = 0;
unsigned int last = 0;
unsigned int sw_poll_time = 0;

int light;
bool rla1_state = 0;
bool rla1_cmd = 0;
bool rla2_state = 0;
bool rla2_cmd = 0;

void loop() {
  Blynk.run();

  light = analogRead(LDR);

  if(rla1_cmd != rla1_state) {
    pr_time(millis());

    digitalWrite(RLA1, rla1_cmd);
    rla1_state = rla1_cmd;
    digitalWrite(LED1, rla1_state);
    if(rla1_state == 1) {
      Serial.println(" RLA1 closed");
    } else {
      Serial.println(" RLA1 opened");
    }
  }
  if(rla2_cmd != rla2_state) {
    pr_time(millis());

    digitalWrite(RLA2, rla2_cmd);
    rla2_state = rla2_cmd;
    digitalWrite(LED2, rla2_state);
    if(rla2_state == 1) {
      Serial.println(" RLA2 closed");
    } else {
      Serial.println(" RLA2 opened");
    }
  }
  
  bool new_state = digitalRead(PIR);
  if(new_state) {
    if(new_state != pir_state) {
      pir_state = new_state;
      if(change_time > 0) {
        pr_time(millis()-change_time);
      }
      change_time = millis();
      Serial.println(" PIR high");
    }
  } else {
    if(new_state != pir_state) {
      pir_state = new_state;
      if(change_time > 0) {
        pr_time(millis() - change_time);
      }
      change_time = millis();
      Serial.println(" PIR low");
    }
  }

// poll the switches every second, toggle state when pressed
  if((millis() - sw_poll_time) > 1000) {
    if(digitalRead(SW1) == 0) {
      if(rla1_state == rla1_cmd) {
        rla1_cmd = ! rla1_cmd;
      }
    }
    if(digitalRead(SW2) == 0) {
      if(rla2_state == rla2_cmd) {
        rla2_cmd = ! rla2_cmd;
      }
    }
    sw_poll_time = millis();
  }
  
  if((millis() - last) > 10000) {
    last = millis();
    Serial.print("Light: ");
    Serial.println(light);
  }
}

void pr_time(unsigned int s) {
  bool leading = 0;
  int secs = s / 1000;
  if(secs > 3600) {
    int hours = secs / 3600;
    Serial.print(hours);
    Serial.print(":");
    secs = secs - (hours * 3600);
    leading = 1;
  }
  if(secs > 60) {
    int mins = (secs / 60);
    if((mins < 10) && (leading == 1)) {
      Serial.print("0");
    }
    Serial.print(mins);
    Serial.print(":");
    secs = secs - (mins * 60);
    leading = 1;
  }
  if((secs < 10) && (leading == 1)) {
    Serial.print("0");
  }
  Serial.print(secs);
  Serial.print(".");
  Serial.print((int)(s % 1000) / 100);
}

// Update Relay1 LED
BLYNK_READ(V20)
{
  Blynk.virtualWrite(V20, rla1_state);
}

// Update Relay2 LED
BLYNK_READ(V21)
{
  Blynk.virtualWrite(V21, rla1_state);
}

// Value from Relay1 button
BLYNK_WRITE(V22)
{   
  int val = param.asInt(); // Get value as integer
  if(val) { rla1_cmd = 1; }
  else    { rla1_cmd = 0; }
}

// Value from Relay2 button
BLYNK_WRITE(V23)
{   
  int val = param.asInt(); // Get value as integer
  if(val) { rla2_cmd = 1; }
  else    { rla2_cmd = 0; }
}


// value from Timer
BLYNK_WRITE(V24)
{   
  int val = param.asInt(); // Get value as integer
  if(val) { rla1_cmd = 1; }
  else    { rla1_cmd = 0; }
}

Update. Another thing tried.

Changed the LED handlers in the ESP12 code to:

WidgetLED led1(V20); //register to virtual pin 1
WidgetLED led2(V21); //register to virtual pin 1

and commented out the BLYNK_READ stuff for pins V20 and V21

  • it didn’t make any difference.

A new day. More stuff tried:

  • Commented out everything in main() loop apart from Blynk.run() - WORKS!

  • Reinstated main() code, added another Blynk.run() at end - login timeout again

  • Removed all Serial.print() calls - login timeout as usual

A discovery!

  • Commented out the analogRead() line - CODE WORKS, logs in

Further investigation shows that, although the analogRead() line executes in < 1mS, running it every time main() executes kills Blynk.
Protecting the code by only running analogRead() after a timer expires seems to work. Even limiting the rate of analogRead() executions to one every 5 mSec seems to allow Blynk,run() to execute and log in successfully.
I reinstated all the Serial.print() code too. That also has no effect.

So, it would seem that you have to limit the rate of analog.Read() calls . Iuse a construct like this:

Declare a global variable (outside of init() or loop()
called analog_time, thus:

unsigned int analog_read = 0;

then wrap your analogRead calls in a test, like this:

  if((millis() - analog_time) >= 5) {
    analog_time = millis();
    light = analogRead(LDR);
  }

It’s vitally important to remember to update the value of analog_time inside the block (blush!)

Duuuuude.

Use simpletimer

Your void loop should be :
Blynk.Run
Timer.run

How does that allow me to use on-board inputs to control the hardware’s function?
Or to read and action the hardware’s sensors?
There are actual, hardware, push-button switches on the board.

Set timers to run functions…

By using push/pull data. There are a couple examples in the library. I highly recommend checking them out. I wonder why the beer didn’t help though, it usually works for me :wink:

I have to agree with @Dave1829 here. Keep the loop clean. Split everything up in functions and run them either timed or use pull data from the app in a set frequency (for example, to read data every 10s). Push buttons are al ready push of course with the BLYNK_WRITE() function.

You definitely have a timing issue here :slight_smile: