Battery operated blynk project. - hints and questions

Hi there, since some time, i try to build battery testing unit using blynk for connectivity.

Until now I have some hints for other people:

  1. You want your device to wait for a legit connection with blynk at the begining of each “wake up” cycle. To perform this but also secure yourself from draining the battery if there is no possibility to connect to any Wifi I use a timer. Use this to wait for your connection in setup:

    while (Blynk.connect() == false) {
    // Wait until connected
    if(millis()>=10000){
    System.sleep(SLEEP_MODE_DEEP, 60); // if the connection is not established for 10 seconds - go to sleep
    }
    }

2.If you want to detect any values that are being input in blynk app than you need to use syncAll() method, but this needs some more checking to be sure, that in every “wake up” cycle the thing gets the right data and than does something (this is due to asynchronous nature of blynk - I’ve read) and so I use this small method to check was the value in question realy “synced”

BLYNK_CONNECTED() {
    Blynk.syncAll();
}

BLYNK_WRITE(V5)                                     //firmware
{   
  firmware = param.asInt();
  syncTest = 1;
}

void loop() {
    Blynk.run();
    if (syncTest == 1 && firmware==0){
      System.sleep(SLEEP_MODE_DEEP, 60);
      }
}

I’ve run into this problem during this project (there you have a little more info):

Now my battery longtime tester works:) but with a small problem. The values are being send to the Blynk app since I can see those on the History Graph, but for unknown reason, the “Value Display” widgets keep the old data, from the last time the app was on screen. I would prefer to see the most updated data on those displays not the one from say, 2 days ago. To get the new updated values I have to either wait for a very long time (my device sends those every one minute, but I have to wait much longer), and values get updated randomly (not all in one time). The only method to get all the “value display’s” updated is to push the “firmware” button which makes the Redbear Duo not go into sleep for some time, and than all the data gets updated in the app. Should I figure some “boolean checking” procedure for this like I did with syncAll() or this is just the feature of the “Value Display” widget, that it only updates when the app is running, and doesn’t refresh when the app is being opened?

What I want is to have the most recent data on all my Value Display’s the moment i open the app.

My code below, and my app on screenshot.

// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_DHT/Adafruit_DHT.h"

// This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"

// This #include statement was automatically added by the Particle IDE.
#include "BH1750Lib/BH1750Lib.h"

char auth[] = "MY_AUTH";

#define DHTPIN 5     // what pin we're connected to
#define DHTTYPE DHT22		// DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);

BH1750Lib bh1750; //luminosity sensor

boolean syncTest = 0;
int firmware = 0;
int refVcc = 500;

void setup() {

    Blynk.begin(auth);
   // delay(50);
	pinMode(4, OUTPUT);
	bh1750.begin(BH1750LIB_MODE_CONTINUOUSHIGHRES); //luminosity sensor
	dht.begin(); //temp/humidity sensor
	
	while (Blynk.connect() == false) {
    // Wait until connected
        if(millis()>=10000){
            System.sleep(SLEEP_MODE_DEEP, 60);
        }
    }
    //here I read the DHT sensor and write it to Blynk
	digitalWrite(4, HIGH);  //DHT is powered via D4 pin not to consume power during sleep time
    delay(550); //needed for the DHT sensor to get in working condition
	float h = dht.getHumidity();
	float t = dht.getTempCelcius();
	digitalWrite(4, LOW);
	Blynk.virtualWrite(V0, h);                                  //humidity
	Blynk.virtualWrite(V1, t);                                  //temperature
	
	//here I measure the battery voltage and write it to Blynk
	int sensorValue = analogRead(A0);
	Blynk.virtualWrite(V4, sensorValue*refVcc/4096);            //battery voltage
	
    //here I check for luminosity and write it to Blynk
    uint16_t luxvalue = bh1750.lightLevel();
    luxvalue = map(luxvalue, 0, 13107, 0, 500);
    Blynk.virtualWrite(V2, luxvalue);                           //brightness
    
    //here I count uptime and write it to Blynk
    unsigned long startTime = 1460197721;   
    unsigned long nowTime = (Time.now() - startTime)/3600;
    Blynk.virtualWrite(V3, nowTime);    //for how many hours does the device work until now
}

BLYNK_CONNECTED() {
    Blynk.syncAll();
}

BLYNK_WRITE(V5)                                     //firmware button
{   
  firmware = param.asInt();
  syncTest = 1;
}

void loop() {
    
    Blynk.run();
    
    if (syncTest == 1 && firmware==0){      
    //only if the syncAll() happened and the firmware button is Low will the RedBear Duo sleep
      System.sleep(SLEEP_MODE_DEEP, 60);
      }
  
}

2 Likes

Is your value display widget set to push? or a time?

Tried both, not to get any spectacular difference. Push set right now. Why?

Added, a LED widget to show me the status for when the device goes into Firmware mode. This gave me a strange outcome. When I switch the led off, in the beginning of the code, this works every time, but when I want it to switch off after the button was released (no firmware was flashed) but prior to going to sleep - it doesn’t switch off, and the led stay on, until the next “wake up” cycle occurs and the first led1.off() happens…

This would mean that the sequence in which I do things may change the behavior of Blynk. Which when spoken out loud seams trivial, but isn’t. I expect that everytime I do Blynk.virtualWrite(pin, value); it makes a change to a widget that is connected to it, and set to push. And from what I experience - it doesn’t.

// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_DHT/Adafruit_DHT.h"

// This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"

// This #include statement was automatically added by the Particle IDE.
#include "BH1750Lib/BH1750Lib.h"

char auth[] = "f049398b5782406f8e08ac7c638be954";

#define DHTPIN 5     // what pin we're connected to
#define DHTTYPE DHT22		// DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);

BH1750Lib bh1750; //luminosity sensor

boolean syncTest = 0;
int firmware = 0;
int refVcc = 500;

int sleepTime = 60;

WidgetLED led1(V6);

void setup() {

    Blynk.begin(auth);
	pinMode(4, OUTPUT);
	bh1750.begin(BH1750LIB_MODE_CONTINUOUSHIGHRES); //luminosity sensor
	dht.begin(); //temp/humidity sensor
	
	while (Blynk.connect() == false) {
    // Wait until connected
        if(millis()>=10000){
            System.sleep(SLEEP_MODE_DEEP, sleepTime);
        }
    }
    led1.off(); // here it does work
    
    //here I read the DHT sensor and write it to Blynk
	digitalWrite(4, HIGH);              //DHT sensor is powered via D4 pin not to consume power during sleep time
    delay(550);                         // this delay time is needed for the DHT senor to get in working condition (any smaller delay will make the sensor not respond with values)
	float h = dht.getHumidity();
	float t = dht.getTempCelcius();
	digitalWrite(4, LOW);
	Blynk.virtualWrite(V0, h);                                  //humidity
	Blynk.virtualWrite(V1, t);                                  //temperature
	
	//here I measure the battery voltage and write it to Blynk
	int sensorValue = analogRead(A0);
	Blynk.virtualWrite(V4, sensorValue*refVcc/4096);            //battery voltage
	
    //here I check for luminosity and write it to Blynk
    uint16_t luxvalue = bh1750.lightLevel();
    luxvalue = map(luxvalue, 0, 65535, 0, 500);
    Blynk.virtualWrite(V2, luxvalue);                           //brightness
    
    //here I count uptime and write it to Blynk
    unsigned long startTime = 1460312327;   
    unsigned long nowTime = (Time.now() - startTime)/3600;
    Blynk.virtualWrite(V3, nowTime);                  //for how many hours does the device work until now
    //boolean charge = digitalRead(3);
    //if (charge == LOW) {Blynk.virtualWrite(V7, 200);}
    //else if (charge == HIGH) {Blynk.virtualWrite(V7, 0);}
    	int charge = analogRead(A1);
    	Blynk.virtualWrite(V7, charge*refVcc/4096);         //charge
    	
}

BLYNK_CONNECTED() {
    Blynk.syncAll();
}

BLYNK_WRITE(V5)                                     //firmware button
{   
  firmware = param.asInt();
  syncTest = 1;
  led1.on();
}

void loop() {
    
    Blynk.run();
    
    if (syncTest == 1 && firmware==0){      //only if the syncAll() happened and the firmware button is Low will the RedBear Duo go to sleep
      led1.off(); // here it does not work
      System.sleep(SLEEP_MODE_DEEP, sleepTime);
      }
}

Hi i try your code and in my Arduino IDE it was write this mistake:


bh1750_test.ino: In function 'void setup()':
bh1750_test:34: error: 'System' was not declared in this scope
bh1750_test:34: error: 'SLEEP_MODE_DEEP' was not declared in this scope
bh1750_test:42: error: 'class DHT' has no member named 'getHumidity'
bh1750_test:43: error: 'class DHT' has no member named 'getTempCelcius'
bh1750_test:59: error: 'Time' was not declared in this scope
bh1750_test:64: error: 'A1' was not declared in this scope
bh1750_test.ino: In function 'void loop()':
bh1750_test:86: error: 'System' was not declared in this scope
bh1750_test:86: error: 'SLEEP_MODE_DEEP' was not declared in this scope
'System' was not declared in this scope


but why? thanks

Like the error messages say: you have not declared variables properly.

If you don’t know what “declared” or “variables” mean, you might need to do some learning about code before copy/paste other people’s code…

https://www.arduino.cc/en/Reference/HomePage

Hi there @Jiri_Bam

  1. @Dave1829 is rather true. You should dig up a bit before you try someones code - everybody has got different way of arranging things.
  2. This code was not written in Arduino IDE but in Particle IDE. This means that for example the declaration of libraries in this sketch is different from what you would do normally and for that matter the libraries are different.
  3. This code was written for Particle Photon - don’t know what you are trying it on, but for example the second error about system comes from this https://docs.particle.io/reference/firmware/electron/#sleep-sleep-

If you need more info, try writing here, what are you trying to achieve and how, so that we can try to guide you in right direction.