Connects sporadically and is not stable at all. TIMER problem. If I remove timer stuff it works

#define BLYNK_TEMPLATE_ID "xxxxxxxxxxxxxxxxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define SENSORV1 A0
#define SENSORV2 A1

#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>

BlynkTimer timer;
char auth[] = BLYNK_AUTH_TOKEN;

double voltageA1, voltageA2;

void myTimer(){
   Blynk.virtualWrite(V0, voltageA1);
   Blynk.virtualWrite(V1, voltageA2);
}


void updateSensors(){
  voltageA1 = readSensor(1);
  voltageA2 = readSensor(2);
}

int voltage;
int offset = 20;
double returnV;

double readSensor(int sensor){
  switch(sensor){
  case 1:
      voltage = analogRead(A0);
      break;
  case 2:
      voltage = analogRead(A1);
      break;
  default:
      break;
  }
  
  returnV = map(voltage,0,1023, 0, 2500) + offset;
  returnV /= 100;
  return returnV;
} 

void setup(){
  Serial.begin(115200);
  Blynk.begin(auth);
  voltageA1 = 0, voltageA2 = 0;
  timer.setInterval(1000L, myTimer);
}

void loop(){
  updateSensors();
  Blynk.run();
  timer.run();
}```

When I use timer, the relays don’t work at all. When I get timer stuff out, it works perfectly.

please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly. Triple backticks look like this: ```

Hi,
I have lately run into similar problems, eventhough I have different setup. The connection to Blynk server does not seem to work and there is not much information to work with, as I understand it. I even get the ping time after the “Connecting to blynk.cloud…” message but there seems to be some disconnection.

I have been trying my device and Ethernet shield without Blynk and then it works, but when using Blynk server there is “always” some problem.

Br Björn

@ebjowei there are multiple problems with @manudithur’s code, and when he gets around to fixing the formatting we’ll give him some advice about how to fix them.

Your issues seem to be entirely unconnected.

Pete.

Just edited the post! @PeteKnight

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Isn’t it clean enough? Even though I take out the updateSensors(); from the loop it still doesn’t work…

Isn’t this code “funny”?
The readSensor does not return anything … as I can read your code.

Edit:
Why not in your myTimer read the sensors instead. And maybe not so often as 1 per second. Run your myTimer not more often than once per 10 secs to let your sensors stabilize and not flood the Blynk severs. (I normally use 1 min or 5 min …)

Hey @jonas , thanks for your reply. It’s a alarm system. When alarm goes on it sends 12v for 1 second to the voltage sensors. So I believe that updating every 500ms would be ideal. By the other hand I’m still having connection issues with blynk. When I use BlynkTimer stuff it doesn’t work. When I comment every timer line it works perfectly fine. Any ideas that might help with this?

Tried this code and still it doesn’t maintain connection with blynk…

#define BLYNK_PRINT Serial
#define BLYNK_DEBUG        

#define BLYNK_TEMPLATE_ID "xxxxxxxxxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxxxxxxxxxxxxxxxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>

char auth[] = BLYNK_AUTH_TOKEN;

BlynkTimer timer;
int voltage = 0;

void myTimerEvent()
{
  Blynk.virtualWrite(V0,voltage);
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth);
  timer.setInterval(1000L, myTimerEvent);
}


void loop()
{
  voltage = analogRead(A0);
  Blynk.run();
  timer.run();

}

First of all, you should have a timer which calls a function that takes a reading, and sends the results to Blynk. This means consolidating three functions in to one.

Blynk.run and timer.run should be the only things in your void loop.

I’d put all of your variable declarations together, near the top of your code.

But you’re calling your timer every 1000ms.

I’m very surprised that your Ethernet adapter doesn’t require you to specify the gateway, MAC and DNS server.

Pete.

Hey @PeteKnight thanks for your response. I’m driving crazy… I’m trying the following code. Just write in a simple virtual pin. It doesn’t mantain connection, and when it does, the digital pin switches I have on the dashboard don’t work… (used to control some relays)

#define BLYNK_PRINT Serial
#define BLYNK_DEBUG        

#define BLYNK_TEMPLATE_ID "xxxxxxxxxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxxxxxxxxxxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxxxxxxxxxxxxxxx"

#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>

char auth[] = BLYNK_AUTH_TOKEN;
BlynkTimer timer;


void myTimerEvent()
{
  Blynk.virtualWrite(V0, 5);
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth);
  timer.setInterval(1000L, myTimerEvent);
}


void loop(){
  Blynk.run();
  timer.run();
}

I’m using a ENC28J60 ethernet adapter. Not familiar with ethernet stuff… I took a template from blynk examples

I can control the relays if I don’t use timer stuff on the code. And it works perfectly fine. I don’t get why when I add the timer nothing works, it doesn’t connect to blynk, etc.

Don’t use digital pins, use virtual pins.

Start by deleting your digital datastreams and just having your V0 datastream set up.

Pete.

So how may I control a relay? I must use virtual pins to control relays?
Thanks in advance Pete, appreciate your help
Manuel

Read this…

Pete.

Ok, gotcha!!! I think I got this. Later, I’m going to sit down and code all over again with this concept. Appreciate your help, Pete. Really helpful.

1 Like

Hey @PeteKnight I’ve redone my code. Im having the same problem again. When I use the following code everything works perfect. But if I uncomment the timer lines, it never establishes connection with the server. Im wondering why… Thanks in advance
Manuel

#define BLYNK_DEBUG        // Optional, this enables more detailed prints

#define BLYNK_TEMPLATE_ID "TMPLFkcKfCUz"
#define BLYNK_DEVICE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "6gOUQWhldz_9JUnfASqF2idTtNAXheFA"
#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>

char auth[] = BLYNK_AUTH_TOKEN;
//BlynkTimer timer;

int ALARM1 = 0;
int ALARM2 = 0;
int offset = 20;

void updateStatus(){
  double voltage1 = readSensor(1);
  double voltage2 = readSensor(2);
  
  if(voltage1 > 6){
    ALARM1 = 1;
  } else{
    ALARM1 = 0;
  }
  
  if(voltage2 > 6){
    ALARM2 = 1;
  } else{
    ALARM2 = 0;
  }
  
}

double readSensor(int sensor){
  int value;
  switch(sensor){
    case 1:
        value = analogRead(A0);
    case 2:
        value = analogRead(A1);
    default:
      break;
  }
  double voltage = map(value, 0, 1023, 0 , 2500) + offset;
  return voltage;
}

void pushStatus(){
    Blynk.virtualWrite(V0, ALARM1);
    Blynk.virtualWrite(V1, ALARM2);
}

BLYNK_WRITE(V2){
  int value = param.asInt();
  if(value == 1){
     digitalWrite(4, HIGH);
  }else{
    digitalWrite(4, LOW);
  }
}

BLYNK_WRITE(V3){
  int value = param.asInt();
  if(value == 1){
     digitalWrite(5, HIGH);
  }else{
    digitalWrite(5, LOW);
  }
}

BLYNK_WRITE(V4){
  int value = param.asInt();
  if(value == 1){
     digitalWrite(6, HIGH);
  }else{
    digitalWrite(6, LOW);
  }
}

BLYNK_WRITE(V5){
  int value = param.asInt();
  if(value == 1){
     digitalWrite(7, HIGH);
  }else{
    digitalWrite(7, LOW);
  }
}

void setup(){
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  Serial.begin(115200);
  Blynk.begin(auth);
  //timer.setInterval(1000L, updateStatus);
  //timer.setInterval(1000L, pushStatus);
}

void loop(){
  Blynk.run();
  //timer.run();
}

Ended up using delays and worked perfectly fine. Timer doesn’t work at all. Nice concept, pure @!###.