I have been working on a telemetry project the last few months on-and-off using the blynk technology. However I have run into a few snags and I am not sure why yet.
My application is very basic, I have 5 offsite ‘no-voltage’ contact closures that I can use to trigger inputs on my Arduino board. I am using attachinterupts to detect a trigger on any input and then I use the ‘Push’ function to notify me that an input has been triggered.
Now for the snag, despite the very basic logic of: “If trigger then push” my board seems to disconnect from the blynk server after the push has been sent. I tried to put a resistor in between the Ground pin and the trigger to prevent large ‘shorts’ type triggers but it did not help.
I also tried to download the local blynk server, to monitor what is happening, but All I get is a more responsive disconnect and no data that indicates why.
I ALSO (once more) tried to write a 'if disconnect detected, then connect" logic, but after the disconnect the serial monitor STOPS to indicate its connection and does nothing. [It still however, detects inputs to the pins on the board, so it stops indicating that its connected to blynk] I will post my code shortly, it will be lengthy as I wrote it for 5 inputs, and also was playing around with a timer inside the interrupt to prevent using delays.
#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SPI.h>
#include <Ethernet2.h>
#include <BlynkSimpleEthernet2.h>
#include <SimpleTimer.h>
char auth[] = "xxxxxxxxxxxxxxxxxxxxxx"; // Auth Blanked to prevent trollers using my device =P
volatile int Button1 = HIGH;
volatile int Button2 = HIGH;
volatile int Button3 = HIGH;
volatile int Button4 = HIGH;
volatile int Button5 = HIGH;
WidgetLED led0(V0);
WidgetLED led1(V1);
WidgetLED led2(V2);
WidgetLED led3(V3);
WidgetLED led4(V4);
SimpleTimer timer;
bool Connected2Blynk = false;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
// Blynk.connect(3333);
while (Blynk.connect() == false) {Serial.println ("CONNECTING....");} //WAIT FOR CONNECTION
/* terminal.flush(); //TERMINAL START//
terminal.println("Device started");
terminal.println("-------------"); //TERMINAL END/*/
//PULLUP all pins I will monitor since if the pin is grounded it will be the signal. >PULLUP prevents 'fluctuations'
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
//Instead of POLLING I used attachInterrupts to catch a signal coming in
attachInterrupt(digitalPinToInterrupt(0), Input1, FALLING);
attachInterrupt(digitalPinToInterrupt(1), Input2, FALLING);
attachInterrupt(digitalPinToInterrupt(2), Input3, FALLING);
attachInterrupt(digitalPinToInterrupt(3), Input4, FALLING);
attachInterrupt(digitalPinToInterrupt(7), Input5, FALLING);
Serial.println("Connected to Blynk server");
timer.setInterval(10000L, CheckConnection); // Timer to POLL connection to blynk - attempt to fix the disconnection broblem
}
//THis method acts to re-establish connection to blynk if lost... not fullproof.
void CheckConnection(){
Connected2Blynk = Blynk.connected();
if(!Connected2Blynk){
Serial.println("Not connected to Blynk server");
Blynk.connect(); // timeout set to 10 seconds and then continue without Blynk
}
else{
Serial.println("Connected to Blynk server");
}
}
//ALL methods that run when a input is recieved 1-5 (Arduino Leonardo Ethernet)
void Input1()
{
Button1 = digitalRead (0);
boolean isPressed = Button1;
if (Button1 == LOW){
led0.on();
Serial.println("BUTTON1 HAS BEEN PRESSED!");
Blynk.notify("Button 1 has been pressed!");
delayMicroseconds (16383);
delayMicroseconds (16383);
CheckConnection();}
else if (Button1 == HIGH){
led0.off();
Serial.println("Button 1 is not pressed");
delayMicroseconds (16383);
delayMicroseconds (16383);}
}
void Input2()
{
Button2 = digitalRead (1);
if (Button2 == LOW){
led1.on();
Serial.println("Button 2 has been pressed!");
Blynk.notify("Button 2 has been pressed!");
delayMicroseconds (16383);
delayMicroseconds (16383);
CheckConnection();}
else if (Button2 == HIGH){
led1.off();
Serial.println("Button 2 is not pressed");
delayMicroseconds (16383);
delayMicroseconds (16383);}
}
void Input3()
{
Button3 = digitalRead (2);
if (Button3 == LOW){
led2.on();
Serial.println("BUTTON3 HAS BEEN PRESSED!");
Blynk.notify("Button 3 has been pressed!");
delayMicroseconds (16383);
delayMicroseconds (16383);
CheckConnection();}
else if (Button3 == HIGH){
led2.off();
Serial.println("Button 3 is not pressed");
delayMicroseconds (16383);
delayMicroseconds (16383);}
}
void Input4()
{
Button4 = digitalRead (3);
if (Button4 == LOW){
led3.on();
Serial.println("BUTTON4 HAS BEEN PRESSED!");
Blynk.notify("Button 4 has been pressed!");
delayMicroseconds (16383);
delayMicroseconds (16383);
CheckConnection();}
else if (Button4 == HIGH){
led3.off();
Serial.println("Button 4 is not pressed");
delayMicroseconds (16383);
delayMicroseconds (16383);}
}
void Input5()
{
Button5 = digitalRead (7);
if (Button5 == LOW){
led4.on();
Serial.println("BUTTON5 HAS BEEN PRESSED!");
Blynk.notify("Button 5 has been pressed!");
delayMicroseconds (16383);
delayMicroseconds (16383);
CheckConnection();}
else if (Button5 == HIGH){
led4.off();
Serial.println("Button 5 is not pressed");
delayMicroseconds (16383);
delayMicroseconds (16383);}
}
void loop(){
if(Connected2Blynk){
Blynk.run();
}
timer.run();
}
Blynk.run() will heal itself when it’s disconnected. Have you tried without the “if not connected” if-statement? I like the setup to as basic as possible and have Blynk do it’s thing.
Yes I have tried without the ‘if not connected’ logic. I started out with it being basic and then made the script all the more complex. However, I thought the connected code fixed my problem until i realized it still does not work.
The Blynk server keeps disconnecting after the attacthInterupt method is started… so it will push the notification to my app, but then 20 seconds later it sends the ‘offline’ push notification as well.
According to the serial monitor, it just never connects to Blynk on its own again, but keeps monitoring the pins. The only way to re-establish connection is to Upload my code with the IDE again.
I even tried to move the methods called by the attachInterupts out of the its own method and into the LOOP function through Integer switching [SO the attachInterupt changes the value of some integer] and the code then ‘polls’ to see if THAT value has changed and then executes the code. HOWEVER, this route requires the use of delays or similar to prevent a massive flux of notification to be logged. (Hence ‘LOOP’) That in itself was a challenge, so i decided rather to see if I can fix the disconnect issue.
I have inserted the Blynk_Debug, but it does not show anything strange going on. It also, keeps running the ‘Debug’ Code despite the server Disconnection.
The closest I’ve gotten to a reason for disconnection was a ‘LOGIN TIMEOUT’ on my local server. But no more info above that… perhaps i send ‘to much data’ although the serial monitor does not indicate it?
Just for the record I am running an Arduino Leonardo Eth, and using a ethernet cable to connect (stable)
Tbh, I do not know much about interrupts, but recently I had a similar episode, except without Blynk. For the modelrailroad I made a hot air balloon to move from left to right via some curtain rails. I used interrupts for the two ending switches as to turn it around and move to the other side.
What is basically recommended using interrupts, do as little as possible in the interrupt routine. The only thing I do there is catch my rising signal (using debounced hardware buttons with a capacitor, very important!).
// ISR to detect Interrupt. Keep as short as possible!
void balloonDetect()
{
state2 = digitalRead(button2);
state3 = digitalRead(button3);
}
This is the only thing I do in my INT routine. All other stuff happens later on because you want to spent as little time there as possible. Further along the line, where I actually make stuff happen I added this:
So, that is another routine all together. This prevents accidental triggering, but of course that depends on what you want to achieve with your interrupts and if the buttons are pushed in a certain order or just have to respond the whole time.
INT handling in the loop I would avoid at all costs, because that seems to me that you are misusing the function of INT. Of course I always know which way my balloon is going due to EEPROM writing, which makes things easier.
And one more question, why the delays? I assume it has something to do with debouncing? I’d solve that in the hardware anyway, with a little capacitor, either filling up or running empty when the switch is pressed.