[Solved] Loosing connection to blynk after 2-5 minutes

hi

now I finally have my Heatipump monitor ready and running.
Its an arduino uno with a w5100 shield and a protoshield and a attiny85.

Its primary job:

  1. send temperature logs for 4 ds18b20 sensors every 3 seconds.

  2. Detect when the heating is using power and report it to blynk (sends 3-4 times a minute)
    Using attiny85 to monitor a blinking led with a LDR sensor.

Problem

But now its has begun to loose connection to blynk server.
The connection last about 2-5 minutes, then I can see in the android app that its offline.
It still has lan network connection, I can ping it.

Anyone have a idea why this is happening or how to debug to narrow down the problem?

here’s my code:

#include <SimpleTimer.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define DEBUG



// #define ONE_WIRE_BUS 2        // This is the ESP8266 pin
// OneWire oneWire(ONE_WIRE_BUS);
// DallasTemperature sensors(&oneWire);

// DeviceAddress tempSensor1 = { 0x28, 0xFF, 0x35, 0x11, 0x01, 0x16, 0x04, 0x25 }; // Temperature probe #1
// DeviceAddress tempSensor2 = { 0x28, 0xC6, 0x89, 0x1E, 0x00, 0x00, 0x80, 0xAA }; // Temperature probe #2


// Digital pin the DS18B20 is connected to. Do not use digital pins 0 or 1 since those conflict with the use of Serial.
const int tmpPin1 = 7;
const int tmpPin2 = 6;
const int tmpPin3 = 5;
const int tmpPin4 = 3;


OneWire oneWire1(tmpPin1);
OneWire oneWire2(tmpPin2);
OneWire oneWire3(tmpPin3);
OneWire oneWire4(tmpPin4);


DallasTemperature sensors1(&oneWire1);
DallasTemperature sensors2(&oneWire2);
DallasTemperature sensors3(&oneWire3);
DallasTemperature sensors4(&oneWire4);





SimpleTimer timer;
char auth[] = "";


// powermeter

long pulseCount = 0;   
//Used to measure power.
unsigned long pulseTime, lastTime, elapsedTime;

//power and energy
double power, elapsedkWh;

//Number of pulses per wh - found or set on the meter.
int ppwh = 1; //1000 pulses/kwh = 1 pulse per wh






void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  
  // KWH interrupt attached to gpio2
  attachInterrupt(digitalPinToInterrupt(2), onPulse, FALLING);

 while (Blynk.connect() == false) {
    // Wait until connected
 }

  
  // These timers are used so that data does not flood Blynk
  timer.setInterval(3003L, sendSensor1);
  timer.setInterval(3006L, sendSensor2);
  timer.setInterval(3009L, sendSensor3);
  timer.setInterval(3012L, sendSensor4);
 

  
  
  
}

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





void sendSensor1() {
  sensors1.requestTemperatures();
  Blynk.virtualWrite(1, sensors1.getTempCByIndex(0) + 6.5);     
}

void sendSensor2() {
  sensors2.requestTemperatures();
  Blynk.virtualWrite(2, sensors2.getTempCByIndex(0) + 6.5); 
}

void sendSensor3() {
  sensors3.requestTemperatures();
  Blynk.virtualWrite(3, sensors3.getTempCByIndex(0) + 6.5); 
}

void sendSensor4() {
  sensors4.requestTemperatures();
  Blynk.virtualWrite(10, sensors4.getTempCByIndex(0)); 
}



// The interrupt routine
void onPulse()
{

//used to measure time between pulses.
  lastTime = pulseTime;
  pulseTime = millis();

//pulseCounter
  pulseCount++;

//Calculate power
  power = (36000000 / (pulseTime - lastTime))*(ppwh*0.01);

  elapsedTime = (pulseTime - lastTime);
  
//Find kwh elapsed
  elapsedkWh = (1.0*pulseCount/(ppwh*100)); //multiply by 1000 to pulses per wh to kwh convert wh to kwh

//Print the values.

  Serial.print("Average Power pr kwh: ");
  Serial.print(power);
  //Serial.print("\npulseTime: ");
  //Serial.print(pulseTime);
  //Serial.print("\nlastTime: ");
  //Serial.print(lastTime);
  //Serial.print("\nelapsedTime: ");
  //Serial.print(elapsedTime);
  Serial.print("\nelapsedkwh: ");
  Serial.print(elapsedkWh);
  Serial.print("\nsending power result\n\n");
  Blynk.virtualWrite(4, power); // pin virtual 
  delay(1000);
  Blynk.virtualWrite(5, elapsedkWh); // pin virtual 
  delay(1000);
}

And the attiny85 code

  pinMode(0, OUTPUT); //pin 0 output 
}
 
void loop()
{
  int FotoR = analogRead(3); //input pin 3 to LDR sensor
  {
    
    if (FotoR >= 100) // LDR sensibility 
    {    
      digitalWrite(0, HIGH); // 
     delay (100);    
    } 
    else
    digitalWrite(0, LOW); //
  }
  
}

Added a fitzing drawing

Check if it’s one of the many faulty W5100’s at https://forum.arduino.cc/index.php?topic=372779.0

Not sure if its the W5100 shield, because It has run for quite a time.

Its after I have added the attiny85 and the power/LDR section.

If I comment out

attachInterrupt(digitalPinToInterrupt(2), onPulse, FALLING);

so its not running the void onpulse function, it gets stable again.

When the interrupt is triggered disable the trigger, do your processing and then re-enable the interrupt.

You should really be using SimpleTimer not delay().

Think I got it solved.

Looked up Attachinterrupts and something about not using the direct pin but instead declare it.

like:

const byte interruptPin = 2;

attachInterrupt(digitalPinToInterrupt(interruptPin), onPulse, FALLING);

But that didnt solve it.

Then I looked back at the orriginal source

and remembered I removed one section:

void sendUptime, containing the blynk virtualwrite
because I couldnt see the point when I could send the blynk virtualwrite in the void onpulse.

But now I changed it

added

timer.setInterval(3016L, sendUptime);

void sendUptime()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
 
  Blynk.virtualWrite(4, power); // pin virtual 
  Blynk.virtualWrite(5, elapsedkWh); // pin virtual 
}

and in void onPulse
remove the delays
and the blynkvirtualwrites thats now moved to the senduptime.

And it works…

The conclusion and lesson for me, dont have the Blynk.virtualWrite in a interrupt routine.

thanks for helping.

Will post my project later in a complete form.

1 Like