Hi all! I have an idea to reduce traffic consumption of Blynk. In this case I decided to create a timer, that will disconnect blynk from blynk server for some time. After that, it has to connect Blynk again.
My hardware is: Arduino UNO with GSM shield SIM900 by keyestudio.
The issue is: This sleeping code works uncorrectly and I has no idea, why (Maybe, there is a reason to use setTimeout unstead of setInterval? But, as I understand it is not the “key” of the problem). And Second question: How is better to configure button V7 (switch or push).
Need your help.
#include "ACS712.h"
#include "MQ7.h"
#define BLYNK_PRINT Serial
#define TINY_GSM_MODEM_SIM900
#define pinBOOT 9 // нога BOOT или K на модеме M590, на SIM900 это 9
//8 нога НЕ РАБОТАЕТ, НЕ ВКЛЮЧАТЬ В ПРОГРАММУ
#include <TinyGsmClient.h>
#include <SoftwareSerial.h>
#include <BlynkSimpleSIM800.h>
#include <DHT.h>
BlynkTimer timer;
int timer_sensor = 0;
int timer_reconnect = 0;
int timer_CO = 0;
SoftwareSerial SerialAT(2, 3); // RX, TX
TinyGsm modem(SerialAT);
// Ваш токен
char auth[] = "31a0c84a8c23xxxxxxxxxxxxxxxxxxxxxx";
char apn[] = "internet";
char user[] = "";
char pass[] = "";
void reconnectBlynk() { //?WAKE UP METHOD!
if (!Blynk.connected()) {
Serial.println("Lost connection");
if(Blynk.connect()) {
Blynk.notify("Connected to internet!");
delay(1000);
timer.disable(timer_reconnect); //disable current timer
delay(100);
}
else {
Serial.println("Not reconnected");
}
}
}
BLYNK_WRITE(V9){ //THIS IS NEEDED FOR READING SLIDER, THAT SENDS TIME OF SLEEP (FROM 1 TO 300)
int sleep = param.asInt(); //SLEEP - VARIABLE FOR HOLDING THIS TIME OF SLEEP
delay(100);
Blynk.virtualWrite(V8, sleep);
//SET UP SLEEP TIMER (AFTER TIME, IT CALLS METHOD reconnectBlynk (SEE ABOVE))
timer_reconnect = timer.setInterval(sleep * 3600000L, reconnectBlynk); //3600000L-HOUR, 1000L - 1 SECOND
Serial.println(sleep);
delay(100);
}
BLYNK_WRITE(V7) /////////SLEEP WHEN V7 IS ON!
{
if (param.asInt())
{
delay(100);
Blynk.notify("Disconnected!");
delay(100);
Blynk.disconnect();
delay(100);
timer.disable(timer_sensor);
delay(100);
timer.enable(timer_reconnect); //ENABLE RECONNECT TIMER
delay(100);
}
}
void setup()
{
//GSM SHIELD ON
delay(1000);
pinMode(pinBOOT, OUTPUT);
digitalWrite(pinBOOT, HIGH);
delay(1000);
digitalWrite(pinBOOT, LOW);
// Debug console
Serial.begin(9600);
delay(10);
// Set GSM module baud rate (вставьте свою скорость)
SerialAT.begin(9600);
delay(3000);
Serial.println("Initializing modem...");
modem.restart();
Serial.begin(9600);
Blynk.begin(auth, modem, apn, user, pass);
if(Blynk.connected()){
Blynk.notify("Connected!");
}
else
{
digitalWrite(pinBOOT, HIGH);
delay(1000);
digitalWrite(pinBOOT, LOW);
delay(3000);
Blynk.notify("Connected!");
delay(1000);
}
delay(1000);
}
void loop()
{
Blynk.run();
timer.run();
}