Because we are on a metered Wifi connection, I would like to be able to schedule connection and disconnection to the Iot cloud to monitor data. Until recently I've been using Blynk to do that. The program connected to Blynk server, confirmed connection, downloaded data, then disconnected. This was set to happen once every half hour. This worked out well as we were only connected to the internet for a few minutes each time. The question is can I do something similar on the cloud. For reference here's the relevant void in the Blynk sketch.
void send_to_blynk()
{
if (WiFi.status() == WL_CONNECTED)
{
Serial.println ("Connecting to Blynk");
Blynk.connect();
if (Blynk.connected())
{
Serial.println ("Connected");
sync_completed = false;
Blynk.syncVirtual(V1); // force the server to send the latest value for the segmented switch
while (!sync_completed) // Wait until BLYNK_WRITE(V1) has completed
{
Blynk.run();
}
take_a_reading(); // take a reading from the tank indicated by the segmented switch
Blynk.virtualWrite(V0, adjusted_reading); // To Display Widget
Blynk.run();
Blynk.disconnect();
Serial.println ("Blynk Disconnected");
}
else
{
Serial.println (">>>>>>>>>>>> Unable to connect to Blynk <<<<<<<<<<<<");
}
}
else
{
// we get here if we there is no WiFi connection
Serial.println (">>>>>>>>>>>> No WiFi Connection, so Blynk connection not attempted <<<<<<<<<<<<");
}
}
BLYNK_WRITE(V1) // Segmented switch to select tank
{
Serial.println("BLYNK_WRITE(V1) Executed");
tank_to_read = param.asInt();
switch (tank_to_read)
{
case 1:
{
adjustment = 0;
sync_completed = true;
break;
}
case 2:
{
adjustment = 13;
sync_completed = true;
break;
}
case 3:
{
adjustment = 15;
sync_completed = true;
break;
}
case 4:
{
adjustment = 12;
sync_completed = true;
break;
}
}
}
void check_connections()
{
if (WiFi.status() != WL_CONNECTED)
{
WiFi_Connect();
}
else
{
Serial.println(F(">>>> WiFi checked - all good <<<<"));
}
}
void WiFi_Connect() // New functon to handle the connectuon to the WiFi network
{
wifi_connect_count=0; // reset the counter
Serial.println(F("Connecting to WiFi"));
if (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass); // connect to the network
}
while (WiFi.status() != WL_CONNECTED && wifi_connect_count < wifi_connect_max_retries) // Loop until we've connected, or reached the maximum number of attemps allowed
{
delay(500);
wifi_connect_count++;
Serial.print(F("WiFi connection - attempt number "));
Serial.println(wifi_connect_count);
}
if (WiFi.status() == WL_CONNECTED)
{
WiFi.mode(WIFI_STA);
Serial.println(F("Wi-Fi CONNECTED"));
Serial.println();
}
else
{
Serial.println(F(">>>>>>>>>>>> Unable to connect to WiFi <<<<<<<<<<<<"));
}
}