BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Timer interrupts on the Particle photon and Blynk Communication via Bluetooth problem

android

#1

Good day Blynk Community

I am having troubling figuring out why my wind sensor measurements become erratic every time i interface the particle photon microcontroller with the Blynk application (on the android os) via a JDY08 BLE module. The program runs perfectly when Blynk is not connected. Here is the code below:


#include <BlynkSimpleSerialBLE.h>
#include <CE_BME280.h>
#define Addr 0x76                                           //BME280 address 118

char auth[] = "AUTH TOKKEN HIDDEN";
void BlynkComms(void);
void DisplayOnSerialMonitor();

void GetBME280SensorData(void);                             //Get BME280 Sensor data
float Temperature;
float Pressure;
float pressure1 = 991;                                      
float pressure2 = 991;
float Humidity;
float Altitude;
float Dewpoint;
byte BME280SensorConnected=0;
String BME280SensorState("");                               //To store the connection status of the sensor

CE_BME280 bme;                                              // I2C


void WindsensorSetup(void);                                 //Function to initialse the windsensor
void Revolution(void);                                      //Interrupt function ISR
void WindDirection(void);                                   //To get wind direction data
void Windsensor(void);                                      //Function to get windsensor data
int WindspeedPin=D3;                                         //Digital Read pin to detect revolutions of wind speed sensor 
float SampleTime=5000;                                      //Sample time in milliseconds
float Speed=0;                                              //Wind speed in in Km/hr 
volatile int RevCount=0;                                    //Since the main program and ISR are sharing this variable
int AnalogPin1=A1;                                          //Analog read pin for Wind Direction
int AnalogPin6=A6;                                          //Analog pin used as an output to the wind sensor detection circuit
int AnalogPin4=A4;                                          //Analog pin used as an input to the microcontroller from the wind sensor detection circuit
float AnalogValue=0;                                        //Voltage of the potentiometer from the wind direction sensor
String Direction;
String North("North");
String NorthEast("North East");
String East("East"); 
String SouthEast("South East");
String South("South");
String SouthWest("South West");
String West("West");
String NorthWest("North West");
String WindsensorState("");                                 //Store the connection status of the windsensor
byte WindsensorConnected=0;                                 //Variable used to control the execution of the WindsensorSetup function
Timer timer(SampleTime, WindSpeed);                         //Use of timers to calculate Windspeed, every 5 seconds. It has more precise timing than milli 


void setup() {
    
    Serial.begin(9600);
    Serial1.begin(9600);
    Blynk.begin(Serial1, auth);
    pinMode(WindspeedPin,INPUT_PULLDOWN);
    pinMode(AnalogPin6, OUTPUT);                            //Set analog pin A6 as Output
    pinMode(AnalogPin4, INPUT_PULLDOWN);                    //Set analog pin A4 as Input with a weak internal pull down resistor
    
    Serial.println("BME280 and Windsensor Blynk detection test");
    delay(3000);
   

}

void loop() {
    
    GetBME280SensorData();
    Windsensor();
    BlynkComms();
    DisplayOnSerialMonitor();
    

}


void WindsensorSetup(){
        
    timer.start();                                           //Initialize Timer interrupt
    attachInterrupt(WindspeedPin, Revolution, FALLING);      //When the reed switch closes, ISR triggered on the FALLING edge only 
    WindsensorConnected=1;
    
}


void Revolution(){

    //Hardware solves debounce issue
    RevCount=RevCount+1;    
   
}


void WindSpeed(){
     
    Speed=(3.621015*RevCount)/(SampleTime/1000);            //Speed in km/hr. Formula adpated from technical manual check Bookmarks Davis says 1600 rev/hr ---> 1 mile/hour
    RevCount=0;
    timer.reset();                                          //RESET THE TIMER TO RESTART THE COUNT FROM ZERO 
    
}
 
 
void WindDirection(){
    
    int RawValue=0;

   
    RawValue=analogRead(AnalogPin1);
    AnalogValue=(3.3*RawValue)/4096;
    
    if(AnalogValue>= 2.8875){
       
        Direction=NorthWest; 
    
    }
    
    else if(AnalogValue>= 2.475){
        
        Direction=West; 
      
    }
    
    else if(AnalogValue>= 2.0625){
        
        Direction=SouthWest; 
       
    }
    
    else if(AnalogValue>= 1.65){
        
        Direction=South; 
 
    }
    
    else if(AnalogValue>= 1.2375){
        
        Direction=SouthEast; 
   
        
    }
    else if(AnalogValue>= 0.825){
        
        Direction=East; 
        
    }
    
    else if(AnalogValue>=0.4125 ){
        
        Direction=NorthEast; 
     
    }
    
    else if(AnalogValue<0.4125 ){
       
        Direction=North; 
        
    }
    
}


void GetBME280SensorData(){
    
    if(!bme.begin(Addr)){                                   
    
        Serial.println("\nBME280 sensor not connected");
        BME280SensorState="Disconnected";
        Serial.println(BME280SensorState);
        BME280SensorConnected=0;  
        Temperature = 0;
        Humidity = 0;
        Pressure = 0;       
        Dewpoint = 0;
     

    }
    else if(bme.begin(Addr)){
       
       if(BME280SensorConnected==0){                                        
        
        Serial.println("BME280 sensor is connected and setup is complete"); //Remove
        BME280SensorState="Connected";
        BME280SensorConnected=1;                                            
        
       }
       
        
        Temperature = bme.readTemperature();
        Humidity = bme.readHumidity();
        Pressure = (bme.readPressure()/100);                                //To convert to hPa
        Dewpoint = Temperature - ((100 - Humidity)/5);                      
         
        
        
    }
     
     
}     

void Windsensor(){
    
    digitalWrite(AnalogPin6, HIGH);
    
    if(digitalRead(AnalogPin4) == HIGH){                                    //To detect if the windsensor is connected to the weather station
        
        if (WindsensorConnected == 0){                                      //Setup windsensor only the first time after it is detected
        
            WindsensorSetup();
            WindsensorState="Connected";                                  
            Serial.println("Windsensor is connected and setup is complete");      //REMOVE AFTER TESTING
            //delay(1000);                                                        //REMOVE AFTER TESTING
       
        }
        
        if(Speed==0){                                                       //if there is a wind speed measurement than look for the wind direction
        
            Direction="None";
        
        }
        else
        WindDirection();                                                    //Call wind direction function
        
        
    }
    else if(digitalRead(AnalogPin4) == LOW){                                //If the sensor is not connected disable the timer and interrupt once 
        
        timer.stop();
        timer.reset();
        detachInterrupt(WindspeedPin);
        WindsensorConnected=0;
        WindsensorState="Disconnected";
        Serial.println("Windsensor is not connected");
        RevCount=0;
        Speed=0;
        Direction="None";
        
    }
    
    digitalWrite(AnalogPin6, LOW);                             
    
}


void BlynkComms(){
    
    Blynk.run();
    Blynk.virtualWrite(V0, Temperature);
    Blynk.virtualWrite(V1, Humidity);
    Blynk.virtualWrite(V2, Pressure);
    Blynk.virtualWrite(V4, Dewpoint);
    Blynk.virtualWrite(V5, Direction); 
    Blynk.virtualWrite(V6, Speed);
    Blynk.virtualWrite(V8, BME280SensorState); 
    Blynk.virtualWrite(V9, BME280SensorState);
    Blynk.virtualWrite(V10, BME280SensorState);
    Blynk.virtualWrite(V11, BME280SensorState);
    Blynk.virtualWrite(V12, WindsensorState);
    Blynk.virtualWrite(V13, WindsensorState);
    
}

void DisplayOnSerialMonitor(){
    
    Serial.print("Temperature ");                          
    Serial.print(Temperature);
    Serial.println(" C");
    Serial.print("Humidity ");
    Serial.print(Humidity);
    Serial.println(" %");
    Serial.print("Pressure ");
    Serial.print(Pressure);
    Serial.println(" hPa");
    Serial.print("Dewpoint ");
    Serial.print(Dewpoint);
    Serial.println(" C");
    Serial.println("");
    Serial.printlnf("\nRev Count: %d", RevCount);
    Serial.print("Wind direction: ");
    Serial.print(Direction);
    Serial.printlnf("\nAnalog voltage: %fV", AnalogValue);
    Serial.printlnf("Windspeed %f km/h\n", Speed);
    
    
}

Strangely the code i wrote here, an earlier version which includes most of the same functions as the above but not all, runs perfectly fine. Here is it below:

#include <CE_BME280.h>
#include <BlynkSimpleSerialBLE.h>
#include<Wire.h>                                            //Is preinstalled on the particle IDE    https://community.particle.io/t/porting-from-arduino-to-particle-io-simplified-hopefully/20072
//#include<Particle.h>                                      //Does not seem to be required. Only required if arduino program requires arduino.h or application.h    https://community.particle.io/t/porting-from-arduino-to-particle-io-simplified-hopefully/20072
#define SEALEVELPRESSURE_HPA (1013.25)                     
#define Addr 0x76                                           //BME280 address 118
#define Addr1 0x39                                          //TSL2561 address  57
#define Addr2 0x4A                                          //Max4409 addres 74


//char auth[] = "AUTH TOKKEN 1";         //For Bluetooth Classic
char auth[] = "AUTH TOKKEN 2";           //Bluetooth BLE

void WindsensorSetup(void);                                 //Function to initialse the windsensor
void TSL2561LuxSensorSetup(void);                           //TSL2561 Sensor Setup
void Max44009LuxSensorSetup(void);                          //Max44009 Sensor data
void GetBME280SensorData(void);                             //Get BME280 Sensor data
void GetTSL2561SensorData(void);                            //Get TSL2561 Sensor data
void GetMax44009SensorData(void);                           //Get Max44009 Sensor data
void Revolution(void);                                      //Interrupt function ISR
void WindDirection(void);                                   //To get wind direction data
void BlynkComms(void);                                      //To send weather data to the Blynk application
void DisplayOnSerialMonitor(void);                         

float Temperature;
float Pressure;
float pressure1 = 991;
float pressure2 = 991;
float Humidity;
float Altitude;
float Dewpoint;
byte BME280SensorConnected=0;                               
String BME280SensorState;                                   //To store the connection status of the sensor

int WindspeedPin=D3;                                         //Digital Read pin to detect revolutions of wind speed sensor 
int AnalogPin1=A1;                                          //Analog read pin for Wind Direction
float AnalogValue=0;                                        //Voltage of the potentiometer from the wind direction sensor
float SampleTime=5000;                                      //Sample time in milliseconds
float Speed=0;                                              //Wind speed in in Km/hr 
volatile int RevCount=0;                                    //Since the main program and ISR are sharing this variable
byte WindsensorConnected=0;                                 //Variable used to control the execution of the WindsensorSetup function
String WindsensorState;                                     //Store the connection status of the windsensor
String Direction;                                          
String North("North");
String NorthEast("North East");
String East("East"); 
String SouthEast("South East");
String South("South");
String SouthWest("South West");
String West("West");
String NorthWest("North West");

byte LuxSensorResponse;                                     //Stores the response from the sensor to test connection of sensor to the weather station 
double ch0;                                                 //IR + visible light
double ch1;                                                 //IR light 
double LightIntensity;                                  //Visible light


CE_BME280 bme;                                              // I2C

Timer timer(SampleTime, WindSpeed);                       


void setup() {

    Serial.begin(9600);
    //Serial1.begin(9600);                                  //Baudrate compatible with Bluetooth Classic
    Serial1.begin(9600);                                    //Baudrate 115200 is the default but can be changed using AT+BOUD4
    
    Blynk.begin(Serial1, auth);                             //Initilaise communication with Blynk application via bluetooth 
    
    timer.start();                                          //Initialize Timer interrupt
    pinMode(WindspeedPin,INPUT_PULLDOWN);
    attachInterrupt(WindspeedPin, Revolution, FALLING);      //When the reed switch closes, ISR triggered on the FALLING edge only
                                                            //A hardware debounce circuit is applied to prevent reed switch chatter 
    
   
    //BME280 DETECTION
    if(!bme.begin(Addr)){                                   
    
    Serial.println("BME280 Error");
    
    }
    else
    Serial.println("BME280 interfaced with BLYNK test");
    delay(1000);
    
    
    //TSL2561 DETECTION
    TSL2561LuxSensorSetup();                                //Set up of the Lux Sensor
    if(LuxSensorResponse == 0){                             //Detect lux sensor is connected to the weather station
        
    Serial.println("Lux sensor Interfaced with BLYNK test");
   
    }
    else
    Serial.println("TSL2561 Lux sensor Error");
    delay(1000);
    
    
    //WIND SENSOR INITIALIZATION WILL GO HERE
    Serial.println("Windspeed and Wind direction Interfaced with BLYNK test\n");
    delay(1000);
    
}


void loop() {                                               
    
    
    GetBME280SensorData();
   
   //_____________________________________________________________________________________________________________________________+
    if(Speed==0){                                           //if there is a wind speed measurement than look for the wind direction
        
        Direction="None";
        
    }
    else
    WindDirection();                                        //Call wind direction function
  //______________________________________________________________________________________________________________________________+
    
     
  //______________________________________________________________________________________________________________________________-
    GetTSL2561SensorData();                                 //Call light sensor function
  //______________________________________________________________________________________________________________________________-
    BlynkComms();
    DisplayOnSerialMonitor();                               //For testing purposes only
    
}


void TSL2561LuxSensorSetup(){
    
     
// Initialise I2C communication as MASTER 
  Wire.begin();                                             
  
  Wire.beginTransmission(Addr1);          
  LuxSensorResponse = Wire.endTransmission();              //Detect if Lux sensor is connected to weather Station
  
   // Starts I2C communication
  Wire.beginTransmission(Addr1);
  // Select control register
  Wire.write(0x00 | 0x80);
  // Power ON mode
  Wire.write(0x03);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Starts I2C communication
  Wire.beginTransmission(Addr1);
  // Select timing register
  Wire.write(0x01 | 0x80);
  // Nominal integration time = 402ms
  Wire.write(0x02);
  // Stop I2C Transmission and the response from the sensor at the end of this transmission is stored in TSL2561SensorResponse
  Wire.endTransmission();
  
     
       
}


void GetBME280SensorData(){
     
    Temperature = bme.readTemperature();
    Humidity = bme.readHumidity();
    Pressure = (bme.readPressure()/100);                    //To convert to hPa
    Dewpoint = Temperature - ((100 - Humidity)/5);          
     
}
 
   
void Revolution(){

    //Hardware solves debounce issue
    RevCount=RevCount+1;    
   
}


void WindSpeed(){
     
    Speed=(3.621015*RevCount)/(SampleTime/1000);            //Speed in km/hr. Formula adapted from technical manual check Bookmarks Davis says 1600 rev/hr ---> 1 mile/hour
    RevCount=0;
    timer.reset();                                          //RESET THE TIMER TO RESTART THE COUNT FROM ZERO 
    
}
 
 
void WindDirection(){
    
    int RawValue=0;

   
    RawValue=analogRead(AnalogPin1);
    AnalogValue=(3.3*RawValue)/4096;
    
    if(AnalogValue>= 2.8875){
       
        Direction=NorthWest; 
    
    }
    
    else if(AnalogValue>= 2.475){
        
        Direction=West; 
      
    }
    
    else if(AnalogValue>= 2.0625){
        
        Direction=SouthWest; 
       
    }
    
    else if(AnalogValue>= 1.65){
        
        Direction=South; 
 
    }
    
    else if(AnalogValue>= 1.2375){
        
        Direction=SouthEast; 
   
        
    }
    else if(AnalogValue>= 0.825){
        
        Direction=East; 
        
    }
    
    else if(AnalogValue>=0.4125 ){
        
        Direction=NorthEast; 
     
    }
    
    else if(AnalogValue<0.4125 ){
       
        Direction=North; 
        
    }
    
}


void GetTSL2561SensorData(){
    
     unsigned int data[4];
  for(int i = 0; i < 4; i++) 
  {
    // Starts I2C communication
    Wire.beginTransmission(Addr1);
    // Select data register
    Wire.write((140 + i));   
    // Stop I2C Transmission
    Wire.endTransmission();
    
    // Request 1 byte of data
    Wire.requestFrom(Addr1, 1);
    
    // Read 1 bytes of data
    if(Wire.available() == 1)
    {
      data[i] = Wire.read();
     }
     delay(200);
  }
  
  // Convert the data
  ch0 = ((data[1] & 0xFF) * 256) + (data[0] & 0xFF);        //Full spectrum light (Visible + IR))
  ch1 = ((data[3] & 0xFF) * 256) + (data[2] & 0xFF);        //IR spectrum light 
  LightIntensity=ch0-ch1;                                   //Visible light
    
}

 
void BlynkComms(){
     
    Blynk.run();
    Blynk.virtualWrite(V0, Temperature);
    Blynk.virtualWrite(V1, Humidity);
    Blynk.virtualWrite(V2, Pressure);
    Blynk.virtualWrite(V4, Dewpoint);
    Blynk.virtualWrite(V5, Direction); 
    Blynk.virtualWrite(V6, Speed);
    Blynk.virtualWrite(V7, LightIntensity);
    
}  
 
 
 
//DISPLAYS ALL MEASUREMENTS ONTO THE SERIAL MONITOR. COMMENT OUT/REMOVE AFTER TESTING PHASE COMPLETE
void DisplayOnSerialMonitor(){
     
    Serial.print("Temperature ");                           
    Serial.print(Temperature);
    Serial.println(" C");
    Serial.print("Humidity ");
    Serial.print(Humidity);
    Serial.println(" %");
    Serial.print("Pressure ");
    Serial.print(Pressure);
    Serial.println(" hPa");
    Serial.print("Dewpoint ");
    Serial.print(Dewpoint);
    Serial.println(" C");
    Serial.printlnf("RevCount= %d", RevCount);              //Count changed to RevCount
    Serial.printlnf("Voltage8 %fV",AnalogValue);
    Serial.printlnf("Wind speed %f km/hr", Speed);
    Serial.print("Wind direction: ");
    Serial.println(Direction);
    Serial.printlnf("Full Spectrum(IR + Visible) : %f Lux", ch0 );
    Serial.printlnf("Infrared Value : %f Lux", ch1);
    Serial.printlnf("Visible Value : %f Lux", LightIntensity);
    Serial.println("");
    
}

I cannot identify what is disrupting my timer interrupt for the wind speed measurement every time i establish communication with the Blynk application. I am hoping someone might be able to shed some light on the matter.


#2

You are trying to run all these functions potentially thousands of times a second…

And that is NOT the correct way to run the Blynk.run() command and most definitely NOT the correct way to run all those Blynk.virtualWrite() commands :scream:

Please read this (and the Docs, etc)… Try out some basic Blynk examples until you really understand how Blynk works and don’t try to reinvent the wheel :stuck_out_tongue:


#3

Good day Gunner

Thank you for your speedy reply. I actually have completed a program in the manner described in your post. However it does not help. Here is that version of my code:


#include <BlynkSimpleSerialBLE.h>
#include <CE_BME280.h>
#define Addr 0x76                                           //BME280 address 118

char auth[] = "c39c9a12d53241fca5eb0975bc7c1b41";
void BlynkComms(void);
void DisplayOnSerialMonitor();

void GetBME280SensorData(void);                             //Get BME280 Sensor data
float Temperature;
float Pressure;
float pressure1 = 991;
float pressure2 = 991; 
float Humidity;
float Altitude;
float Dewpoint;
byte BME280SensorConnected=0;
String BME280SensorState("");                               //To store the connection status of the sensor

CE_BME280 bme;                                              // I2C


void WindsensorSetup(void);                                 //Function to initialse the windsensor
void Revolution(void);                                      //Interrupt function ISR
void WindDirection(void);                                   //To get wind direction data
void Windsensor(void);                                      //Function to get windsensor data
int WindspeedPin=D3;                                        //Digital Read pin to detect revolutions of wind speed sensor 
float SampleTime=5000;                                      //Sample time in milliseconds
float Speed=0;                                              //Wind speed in in Km/hr 
volatile int RevCount=0;                                    //Since the main program and ISR are sharing this variable
int AnalogPin1=A1;                                          //Analog read pin for Wind Direction
int AnalogPin6=A6;                                          //Analog pin used as an output to the wind sensor detection circuit
int AnalogPin4=A4;                                          //Analog pin used as an input to the microcontroller from the wind sensor detection circuit
float AnalogValue=0;                                        //Voltage of the potentiometer from the wind direction sensor
String Direction;
String North("North");
String NorthEast("North East");
String East("East"); 
String SouthEast("South East");
String South("South");
String SouthWest("South West");
String West("West");
String NorthWest("North West");
String WindsensorState("");                                 //Store the connection status of the windsensor
byte WindsensorConnected=0;                                 //Variable used to control the execution of the WindsensorSetup function
Timer timer(SampleTime, WindSpeed);                         //Use of timers to calculate Windspeed, every 5 seconds. It has more precise timing than milli 
BlynkTimer timer1;                                          //Create timer called timer1 

void setup() {
    
    Serial.begin(9600);
    Serial1.begin(9600);
    Blynk.begin(Serial1, auth);
    pinMode(WindspeedPin,INPUT);                    //Specifies the Digital pin as an input with a weak internal pull down resistor ??????????***IS THE PULL UP REQUIRED???***
    pinMode(AnalogPin6, OUTPUT);                            //Set analog pin A6 as Output
    pinMode(AnalogPin4, INPUT_PULLDOWN);                    //Set analog pin A4 as Input with a weak internal pull down resistor
    timer1.setInterval(1000L, BlynkComms);                  //Set timer1 to periodically send data to Blynk every second
    Serial.println("BME280 and Windsensor Blynk detection test");
    delay(3000);
   

}

void loop() {
    
    GetBME280SensorData();
    Windsensor();
    timer1.run();
    Blynk.run(); 
    DisplayOnSerialMonitor();
    delay(500);

}


void WindsensorSetup(){
     
    Speed=0;
    RevCount=0;    
    timer.start();                                           //Initialize Timer interrupt
    attachInterrupt(WindspeedPin, Revolution, FALLING);      //When the reed switch closes, ISR triggered on the falling edge only 
    WindsensorConnected=1;
    
}


void Revolution(){

    //Hardware solves debounce issue
    RevCount=RevCount+1;    
   
}


void WindSpeed(){
     
    Speed=(3.621015*RevCount)/(SampleTime/1000);            //Speed in km/hr. Formula adpated from technical manual check Bookmarks Davis says 1600 rev/hr ---> 1 mile/hour
    RevCount=0;
    timer.reset();                                          //RESET THE TIMER TO RESTART THE COUNT FROM ZERO 
    
}
 
 
void WindDirection(){
    
    int RawValue=0;

   
    RawValue=analogRead(AnalogPin1);
    AnalogValue=(3.3*RawValue)/4096;
    
    if(AnalogValue>= 2.8875){
       
        Direction=NorthWest; 
    
    }
    
    else if(AnalogValue>= 2.475){
        
        Direction=West; 
      
    }
    
    else if(AnalogValue>= 2.0625){
        
        Direction=SouthWest; 
       
    }
    
    else if(AnalogValue>= 1.65){
        
        Direction=South; 
 
    }
    
    else if(AnalogValue>= 1.2375){
        
        Direction=SouthEast; 
   
        
    }
    else if(AnalogValue>= 0.825){
        
        Direction=East; 
        
    }
    
    else if(AnalogValue>=0.4125 ){
        
        Direction=NorthEast; 
     
    }
    
    else if(AnalogValue<0.4125 ){
       
        Direction=North; 
        
    }
    
}


void GetBME280SensorData(){
    
    if(!bme.begin(Addr)){                                  
    
        Serial.println("\nBME280 sensor not connected");    
        BME280SensorState="Disconnected";
        Serial.println(BME280SensorState);
        BME280SensorConnected=0;  
        Temperature = 0;
        Humidity = 0;
        Pressure = 0;       
        Dewpoint = 0;
     

    }
    else if(bme.begin(Addr)){
       
       if(BME280SensorConnected==0){                                        
        
        BME280SensorState="Connected";
        BME280SensorConnected=1;                                            
        Serial.println("BME280 sensor is connected and setup is complete"); //Remove
        delay(300);                                                         //To allow BME280 to stablise before measurements are taken
       
       }
       
        
        Temperature = bme.readTemperature();
        Humidity = bme.readHumidity();
        Pressure = (bme.readPressure()/100);                                //To convert to hPa
        Dewpoint = Temperature - ((100 - Humidity)/5);                      //!!!USE BETTER FORMULA if result not accurate!!!!
         
        
        
    }
     
     
}     

void Windsensor(){
    
    digitalWrite(AnalogPin6, HIGH);
    
    if(digitalRead(AnalogPin4) == HIGH){                        //To detect if the windsensor is connected to the weather station
        
        if (WindsensorConnected == 0){                          //Setup windsensor only the first time after it is detected
        
            WindsensorSetup();
            WindsensorState="Connected";                                  
            Serial.println("Windsensor is connected and setup is complete");    //REMOVE AFTER TESTING
            
       
        }
        
        if(Speed==0){                                           //if there is a wind speed measurement than look for the wind direction
        
            Direction="None";
        
        }
        else
        WindDirection();                                        //Call wind direction function
        
        
    }
    else if(digitalRead(AnalogPin4) == LOW){                    //If the sensor is not connected disable the timer and interrupt once 
        
        timer.stop();
        timer.reset();
        detachInterrupt(WindspeedPin);
        WindsensorConnected=0;
        WindsensorState="Disconnected";
        Serial.println("Windsensor is not connected");
        RevCount=0;
        Speed=0;
        Direction="None";
        
    }
    
    digitalWrite(AnalogPin6, LOW);                             
    
}


void BlynkComms(){
    
    Blynk.virtualWrite(V0, Temperature);
    Blynk.virtualWrite(V8, BME280SensorState); 
    Blynk.virtualWrite(V1, Humidity);
    Blynk.virtualWrite(V9, BME280SensorState);
    Blynk.virtualWrite(V2, Pressure);
    Blynk.virtualWrite(V10, BME280SensorState);
    Blynk.virtualWrite(V4, Dewpoint);
    Blynk.virtualWrite(V11, BME280SensorState); 
    Blynk.virtualWrite(V5, Direction); 
    Blynk.virtualWrite(V12, WindsensorState);
    Blynk.virtualWrite(V6, Speed);
    Blynk.virtualWrite(V13, WindsensorState);
    
}

void DisplayOnSerialMonitor(){
    
    Serial.print("Temperature ");                           
    Serial.print(Temperature);
    Serial.println(" C");
    Serial.print("Humidity ");
    Serial.print(Humidity);
    Serial.println(" %");
    Serial.print("Pressure ");
    Serial.print(Pressure);
    Serial.println(" hPa");
    Serial.print("Dewpoint ");
    Serial.print(Dewpoint);
    Serial.println(" C");
    Serial.println("");
    Serial.printlnf("\nRev Count: %d", RevCount);
    Serial.print("Wind direction: ");
    Serial.print(Direction);
    Serial.printlnf("\nAnalog voltage: %fV", AnalogValue);
    Serial.printlnf("Windspeed %f km/h\n", Speed);
    
    
}

The interrupt “Revolution” seems to be occurring more than it should. Instead of reading a count of say 10 it will read 20 or jump to 34 instead of 21 etc. Could the timer interrupt be affected by the Blynk code in a some weird and observable manner?


#4

Either you’ve posted the wrong code, or you’ve not understood the “Keep your void loop() clean” document

Pete.


#5

@PeteKnight @Gunner
Good day Pete

I have posted the right code. This is the least amount of code that I can use in order to execute all the other code I need to. Do simply put the rest of my functions in the function i created for the Blynk communication and have only blynk.run and timer.run in the void loop?

I guess i did not understand :disappointed:. How do i keep void loop as empty as the example in the post whilst also allowing my actual program to run? Please, tell me how should I be doing it? Thanks in advance.


#6

With a timer routine. Call your functions at timed intervals, not several thousand times a second.
Your device has it’s own timer functions.

https://docs.particle.io/reference/device-os/firmware/photon/#software-timers


#7

#8

Everything you need to do is outlined in the document that was linked earlier. If you don’t understand some of it then I’d suggest re-reading it a few times. If you still don’t understand some of it then quite the parts of the document that you’re having difficulty with. This will allow the document to be clarified and improved in future to make it more understandable…

Pete.


#9

Thank you very much guys. I had not thought about it that way. I will try that out.


#10

@PeteKnight @Costas

I have changed my code as you and the article has advised and everything is running smoothly. Thanks again.
Now I’m investigating the possibility of implementing some energy saving techniques associated with putting my controller in its sleep mode for any duration between 100ms to 500ms, I am still deciding. However, my concern is that whilst in sleep mode, the execution of the code in my void loop will be halted. I think running a timer interrupt to run the Blynk.run and timer.run code should solve this problem. Is there a minimum frequency that these two commands need to be executed per second to avoid any problems ? Thanks in advance.


#11

I have no idea about sleep modes on your board, especially using Bluetooth. If the sleep mode turns Bluetooth off then sleeping for a short period probably won’t help you, as the re-connection process will take a while.
If you aren’t using Wi-Fi and can turn this off permanently then that will save you power.

You almost certainly won’t be able to use timers successfully, or keep a Blynk connection going, in sleep mode.

Pete.


#12

That’s hardly going to stop global warming :slight_smile:
What are you really wanting to do?
Like @PeteKnight I don’t have any practical experience with your board type.


#13

@Costas @PeteKnight

Thank you guys for replying so quickly. I understand that you may not be familiar with the board i am using. I would like to know what is the minimum frequency, required by Blynk, that the Blynk.run and timer.run commands need to be executed so that i do not have Blynk timer issues nor Blynk connection issues. If i know what that minimum frequency is then i can try to put my board in sleep mode long enough to conserve energy but wake it up soon enough to allow the two commands to run.

I am not attempting to stop global warming :joy: But i am trying to maximize the number of hours i get from the battery powering my project :slightly_smiling_face:


#14

@Alli check the docs for heartbeat. You can change the default 10 or 20 seconds to your own preferences.


#15

@Costas

Thank you Costas again for your response. Reading through the doc regarding “heartbeat” i cannot help but wonder if i require blynk.run if i periodically call on heartbeat? If i am still required to use blynk.run in conjunction with heartbeat than the question still remains what is the minimum number of times that blynk.run needs to be executed per second? Also what is the minimum number of times that timer.run need to be called (For BlynkTimer)? I can’t seem to be find this information in the docs.


#16

Both Blynk.run() and Timer.run() should be called as frequently as is possible, hence their recommendation as some of the ONLY code in the void loop(), whenever possible, so as to eliminate any delays or blocking of them being frequently called… think of it like your own breathing, yes you could hold your breath occasionally, but not recommended long term for overall functionality :stuck_out_tongue_winking_eye:

The heartbeat is just Blynk’s way of keeping tabs between the Hardware and the Server link… Since if the Server looses the heartbeat, then it starts displaying such in the App and eventually shutting down the supposedly non-existent connection.


#17

@Gunner

So if i were to call these Blynk.run and Timer.run every 2 seconds this is acceptable?
And using this frequency means i will no longer need to use heartbeat since Blynk.run will take care of all blynk’s communication needs?


#18

Well, you are messing with essential timing in both libraries that will have foreseen and unforeseen consequences.

Adding Blynk to an existing Arduino process can be akin to adding a trailer to a vehicle… same old driving experience… until you try to accelerate, break, turn sharply, park and so on… Lots of extra things to consider now, and the solution would NOT be to snooze and only think about it every two seconds :wink:

Learn how Blynk works with the basics first, then look around at others examples to see how structured timing of functions and other tricks allow both Blynk and other timing sensitive code to co-exist.


#19

i am trying to do exactly that. I have looked at the information on timers on the blynk docs and as well heartbeat and Blynk but it does not have the answers i am looking for. Can you please give me some direction by posting a few links that i could follow?


#20

I am really not sure of the exact questions you need answered… You seem to be asking if you can disregard basic necessities in Blynk and timer operations… And the answers are essentially NO, as doing so is not recommended until you really know what you are doing (and even then, not likely)… And honestly, that comes with experience.