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

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 @!###.

Did you do this? A screenshot of your datastreams screen would be a good starting point.

In the short term maybe, you’ll be back asking questions about why your device randomly disconnects from Blynk and won’t re-connect without a physical reset soon though, and the answer will be the delays you are using.

Strange how tens of thousands of other Blynk users disagree, and millions of SimpleTimer users also disagree :thinking:

You have some flaws in your code structure.
The sensible approach would be to use a timer to call a function which takes readings from your sensors, applies the logical tests to them and then pushes the results to Blynk. Instead, that is spread across two functions, called with timers which are set to coincide at precisely the same time - yet you’re doing this on a board which doesn’t support multi-threading.

When it comes to debugging programming issues, the serial print command is your most powerful tool, but I don’t see it being used to try to gather clues about where the problem may lie.

Anyway, I’ll mark this topic as solved but leave it open for further comments.
When you decide that you want to re-visit the issue please continue the conversation here, rather than creating a new topic.

Pete.

Only using virtual pins. Not using any digital pin.

Look at the code I sent before… Just uncomment the BlynkTimer timer declaration line, and it doesn’t connect to blynk. Leaving every other Timer line commented. Just uncommenting line 10. That for me is totally weird. Only declaring a Timer object causes Blynk not to connect…

I’ll try again to make Timer work. But simply making a code that connects to blynk and runs a timer doesn’t work for me. Googled every example and yet couldn’t make it work.

Thanks again
Manuel

Maybe you have a problem with your library installation.

Pete.

Got the latest version available on Arduino IDE.

But is every file correctly installed and un corrupted, and is the SimpleTimer installation okay?

Pete.

How can I check that?

A fresh IDE install and library installs is the most radical approach, but deleting and re-installing the various libraries usually does the trick.

Pete.