Hello guys!
I’m kind of new to this Blynk type of usage and i am not really aware of the Blynk.timer and Blynk.begin functions.
So maybe someone can help me out. I’ve found these similar problems on other threads but mine is a little bit different and i can’t sort it out. Testing is also painfull (takes so much time on each run) and i’ll tell you why:
I have a pocket 4g hotspot that only functions with the battery on (huawei e5577c) and does not have a typical switch for powering on/off. It works with a softbutton that you have to press for a while.
My idea is to have it on my car permanently on, but with some security features. I have flame and temp sensors that will cutoff the power , sensors that i monitor and control on the blynk app.
So here’s the catch: the hotspot only works with the batt on it, so i have a usb charger on a relay for 12V that is cut or powered through a TTL current on the hotspot itself. If the hotspot is on, the charger will be on too. If the hotspot is off, the charger is off too. Now:
If the charger starts before pressing the power , the hotspot will boot in charging mode, so no wifi. you have to wait a while and the press power again for it to boot as hotspot. I have done this with a timed relay so that it does not happen.
But for some reasons related to voltage drop when i start the car, sometimes it messes up and i have the hotspot on charge mode and not wifi.
I already made my code so that i can control the on/off softswitch of the hotspot. And it works. I use it for safety cut-out and it is nicely working.
The thing is that i need the ESP to know that if he can’t connect to wifi or blynk 40/60 seconds after booting, he has to change the pin 16 to high for 5 seconds (button press time needed to get it out of charge mode) , then put it low and wait another 40/60 seconds or until it connects and then proceed.
It’s easier said than done, i never can get pass the blynk begin part, and i tried the boolean connect but i never sort the part that i need it to loop the connection trial and button press if not succeeded.
Here’s my working code. It’s kinda messy, i guess.
When Pin16 is HIGH the hotspot button is pressing.
/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
This sketch shows how to read values from Virtual Pins
App project setup:
Slider widget (0...100) on Virtual Pin V1
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <math.h>
// which analog pin to connect
#define THERMISTORPIN A0
// resistance at 25 degrees C
#define THERMISTORNOMINAL 54000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 9700
uint16_t samples[NUMSAMPLES];
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxx";
char pass[] = "xxxxxx";
BlynkTimer timer;
float FailsafeTemp;
float ActualTemp;
int PWSW;
int mailcontrol;
int flamecontrol;
int corvermelha;
int corazul;
// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V1
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V1, tempreading());
if (tempreading() >= 54) {
if (corvermelha == 2) {
Blynk.setProperty(V1, "color", "#D3435C");
Serial.println("COR VERMELHA");
corvermelha = 1;
corazul = 2;
}
}
if (tempreading() <= 55) {
if (corazul == 2) {
Blynk.setProperty(V1, "color", "#04C0F8");
Serial.println("COR azul");
corazul = 1;
corvermelha = 2;
}
}
if (flamecontrol != 0) {
Blynk.virtualWrite(V3, flamecontrol );
Serial.println("V2 Valor flamecontrol");
}
if (FailsafeTemp <= ActualTemp || flamecontrol == 1 || PWSW == 0)
{
Blynk.virtualWrite(V2, 1);
Serial.println("V2 1");
digitalWrite(16, HIGH);
Serial.println("pin 16 high dentro da rotina do cutoff");
//delay(10000);
//digitalWrite(16, LOW);
if (flamecontrol == 1 && mailcontrol == 0)
{
Blynk.email("x@me.com", "Smart WIFI Hotspot Notification: FIRE WARNING!", "FLAME DETECTION ACTIVATED");
mailcontrol = 1;
}
if (FailsafeTemp <= ActualTemp && mailcontrol == 0)
{
Blynk.email("x@me.com", "Smart WIFI Hotspot Notification: TEMPERATURE CUT-OFF!", "TEMPERATURE ABOVE CUT-OFF LIMIT");
mailcontrol = 1;
}
if (PWSW == 0 && mailcontrol == 0)
{
Blynk.email("x", "Smart WIFI Hotspot Notification: Manual APP Shutdown", "WIFI HOTSPOT Shutdown");
mailcontrol = 1;
//Blynk.virtualWrite(V5, 1);
}
}
/*else {
digitalWrite(16, LOW);
Serial.println("p16 low dentro do else da rotina de cutoff");
// Blynk.virtualWrite(V2, 0);
}
*/
}
BLYNK_WRITE(V2)
{
int pinValue = param.asInt();
if (pinValue == 0) {
digitalWrite(16, LOW);
Serial.println("leitura valor v2 LOW");
}
else {
digitalWrite(16, HIGH);
Serial.println("leitura valor v2 high");
}
}
BLYNK_WRITE(V0)
{
float pinValue = param.asInt();
Blynk.virtualWrite(V4, pinValue);
Serial.println("virtualWrite V4 pinvalue dento do blinkwite v0");
FailsafeTemp = pinValue;
}
BLYNK_WRITE(V5)
{
int pinValue = param.asInt();
if (pinValue == 1)
{
Blynk.virtualWrite(V2, 0);
Serial.println("virtualWrite V2 para 0 pinvalue dento do blinkwite v5");
digitalWrite(16, LOW);
PWSW = 1;
}
else {
digitalWrite(16, HIGH);
Blynk.virtualWrite(V2, 1);
Serial.println("virtualWrite V2 para 1 pinvalue dento do else do blinkwite v5");
PWSW = 0;
}
}
void setup()
{
// Debug console
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
timer.setInterval(5000L, myTimerEvent);
Serial.begin(9600);
pinMode(16, OUTPUT);
pinMode(4, INPUT);
digitalWrite(16, LOW);
PWSW = 1;
mailcontrol = 0;
flamecontrol = 0;
Blynk.begin(auth, ssid, pass);
FailsafeTemp = 65;
corazul = 2;
corvermelha = 2;
Blynk.virtualWrite(V4, FailsafeTemp);
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V5, 1);
Blynk.virtualWrite(V3, 0);
Blynk.syncAll();
}
void loop()
{
Blynk.run();
timer.run();
if (digitalRead(4) == 1) {
flamecontrol = 1;
digitalWrite(16, HIGH);
}
}
float tempreading() // run over and over again
{
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i = 0; i < NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i = 0; i < NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
// Serial.print("Average analog reading ");
// Serial.println(average);
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
// Serial.print("Thermistor resistance ");
// Serial.println(average);
float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
ActualTemp = steinhart;
return steinhart;
}
Thanks a lot for your help.