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:
-
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);
}
}