Hello,
I am a beginner in IOT and I wrote a small program that measures the electricity
consumption of my house and sends it to the remote application every second.
For this I use an arduino UNO R3 card which accesses the internet via WIFI and my box.
Everything works fine but here is my problem:
Every night I turn off the WIFI to turn it back on in the morning.
Sometimes too, due to various problems, he cuts himself during the day and comes back.
So I would like my code to regularly test the connection to Blynk and reset it if necessary.
How to do ?
Thanks for any help.
My code :
#include <SPI.h>
#include <WiFi.h>
#include <BlynkSimpleWifi.h>
char ssid[] = "XXXXXXX"; // your network SSID (name)
char pass[] = "XXXXXX"; // your network password
volatile int periode=0;
volatile int PuissanceInstantanee=0;
volatile int PuissanceInstantaneeDelta=0;
volatile int MemPuissanceInstantanee=0;
volatile int tempo=300;
volatile int TdernierFront=0;
volatile float CoutKwh=0.2228/1000;
volatile float DeltaP=0;
volatile float DeltaT=0;
volatile long NbFlash=0;
volatile long MemNbFlashDelta=0;
volatile int EtatBoutonDelta=0;
BlynkTimer timer;
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
// Set incoming value from pin V0 to a variable
EtatBoutonDelta = param.asInt();
if (EtatBoutonDelta==1)
{
MemNbFlashDelta=NbFlash;
Blynk.virtualWrite(V6, 0);
MemPuissanceInstantanee=PuissanceInstantanee;
DeltaT=millis();
}
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
// Change Web Link Button message to "Congratulations!"
Blynk.setProperty(V3, "offImageUrl", "********");
Blynk.setProperty(V3, "onImageUrl", "********");
Blynk.setProperty(V3, "url", "********");
}
// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, PuissanceInstantanee);
Blynk.virtualWrite(V2, CoutKwh*PuissanceInstantanee);
if (EtatBoutonDelta==1)
{
if (PuissanceInstantaneeDelta>=0)
Blynk.virtualWrite(V7, PuissanceInstantaneeDelta);
else
Blynk.virtualWrite(V7, 0);
Blynk.virtualWrite(V6, (NbFlash-MemNbFlashDelta)*CoutKwh);
long TDeltaMilliseconde = (millis()-DeltaT);
int TDeltaseconde = TDeltaMilliseconde/1000;
int TDeltaMinute=TDeltaseconde/60;
int TDeltaSeconde=TDeltaseconde-60*TDeltaMinute;
Blynk.virtualWrite(V4,TDeltaMinute );
Blynk.virtualWrite(V9,TDeltaSeconde );
}
}
void setup()
{
pinMode(2,INPUT);
// Debug console
Serial.begin(9500);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// init affichage
Blynk.virtualWrite(V7, 0);
Blynk.virtualWrite(V6, 0);
Blynk.virtualWrite(V8,"---");
timer.setInterval(1000L, myTimerEvent);
attachInterrupt(digitalPinToInterrupt(2), rpm_fan2, FALLING);
}
void loop()
{
Blynk.run();
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
}
void rpm_fan2()
{
periode=millis()-TdernierFront;
if (periode>tempo)
{
PuissanceInstantanee=3600000/periode;
PuissanceInstantaneeDelta=(PuissanceInstantanee-MemPuissanceInstantanee);
TdernierFront=millis();
NbFlash++;
}
}