Hi!
I have an iot project where I am sending data from a board to both the iot cloud and my own webserver. I want to send this data mainly through a local wifi, however if the power cuts out, I want my data transfer to happen through a 4G cellular connection. I should mention that the cellular connection is also established with wifi, through a portable router with a sim card inserted.
I have my arduino iot 33 board configured to detect the loss of an internet connection, and when this happens, it can connect without a problem to the cellular network. The preferred connection for the iot cloud is the wifi and so it keeps on trying to reconnect, therefore disconnecting my board from the cellular connection.
Is there a way to stop the cloud from trying to reconnect every 500 ms?
Alternatively, the best option would be if I could add the cellular connection's SSID and PASS as a second connection option for the cloud, however if I could just stop the cloud from attempting to reconnect, that would save my issue too.
My code:
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/cd1e9668-0bdf-4950-af6a-6a6c2c1575cb
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
String conn_type;
float bat_voltage;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include <SPI.h>
#include <WiFiNINA.h>
#include "thingProperties.h"
#define bat_pin A6
int status = WL_IDLE_STATUS;
bool setup_connect=false;
unsigned long time_elapsed;
unsigned long time_setup_wifi=0;
float bat_volt;
float R1 = 7500.0;
float R2 = 750.0;
float ref_volt = 2.048;
float adc_value;
float adc_volt;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(3000);
analogReference(AR_EXTERNAL);
analogReadResolution(12);
pinMode(LED_BUILTIN, OUTPUT);
conn_type="nc";
status= WL_IDLE_STATUS;
int wifisetup_counter = 0;
while(status!=WL_CONNECTED)
{
if(wifisetup_counter>3) break;
status = WiFi.begin(SSID1,PASS1);
if(status==WL_CONNECTED){
Serial.println("Connected with wifi!");
setup_connect=true;
conn_type="W";
}
else delay(10000);
wifisetup_counter++;
}
int cellularsetup_counter = 0;
while(status!=WL_CONNECTED)
{
if(cellularsetup_counter>3) break;
status = WiFi.begin(SSID2,PASS2);
if(status==WL_CONNECTED){
Serial.println("Connected with cellular!");
conn_type="C";
setup_connect=true;
}
else delay(10000);
cellularsetup_counter++;
}
setup_connect=false;
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection, false);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
Serial.println(conn_type);
Serial.println(WiFi.status());
TestWiFiConnection();
ArduinoCloud.update();
// Your code here
delay(1000);
adc_value=analogRead(bat_pin);
adc_volt=(analogRead(bat_pin)*ref_volt)/4096.0;
bat_volt = ((analogRead(bat_pin)*ref_volt)/4096.0)/(R2/(R1+R2));
Serial.println(adc_value);
Serial.println(adc_volt);
Serial.println(bat_volt);
bat_voltage = bat_volt;
Serial.println(bat_voltage);
}
void TestWiFiConnection()
//test if always connected
{
Serial.println("Testing conn...");
Serial.println(WiFi.SSID());
Serial.println("Wifi status:");
Serial.print(WiFi.status());
int StatusWiFi=WiFi.status();
if(strcmp(WiFi.SSID(),SSID2)==0 &&(StatusWiFi!=WL_CONNECTION_LOST && StatusWiFi!=WL_DISCONNECTED)){
if(ScanSSIDs()==1) {
Serial.println("Switching to wifi...");
WiFiConnect();
}
StatusWiFi=WL_CONNECTED;
}
if(StatusWiFi==WL_CONNECTION_LOST || StatusWiFi==WL_DISCONNECTED ||
setup_connect == false) //if no connection
{
Serial.println("Connection lost...");
conn_type="nc";
//ArduinoCloud.end();
if(ScanSSIDs()==1) WiFiConnect();
if(ScanSSIDs()==2) CellularConnect();
}
}
void WiFiConnect()
//connect to my SSID
{
Serial.println("Connectin to wifi");
status= WL_IDLE_STATUS;
int wifi_timeout=0;
while(status!=WL_CONNECTED)
{
if(wifi_timeout>6) break;
status = WiFi.begin(SSID1,PASS1);
if(status==WL_CONNECTED){
Serial.println("Connected with wifi!");
conn_type="W";
/*
if (ArduinoCloud.connected()==false) {
ArduinoCloud.begin(ArduinoIoTPreferredConnection, false); <---The cloud cannot handle 2 connection attempts at the same time
}
*/
}
else {
delay(10000);
wifi_timeout++;
}
}
}
void CellularConnect()
//connect to my SSID
{
Serial.println("Connecting to cellular");
status= WL_IDLE_STATUS;
int cellular_timeout=0;
while(status!=WL_CONNECTED)
{
if(cellular_timeout>6) break;
status = WiFi.begin(SSID2,PASS2);
if(status==WL_CONNECTED){
Serial.println("Connected with cellular!");
conn_type="C";
/*
if (ArduinoCloud.connected()==false) {
ArduinoCloud.begin(ArduinoIoTSecondConnection, false); <---The cloud cannot handle 2 connection attempts at the same time
}
*/
}
else {
delay(10000);
cellular_timeout++;
}
}
}
int ScanSSIDs()
//scan SSIDs, and if my SSID is present return 1
{
Serial.println("Scanning...");
int score=0;
int numSsid = WiFi.scanNetworks();
if(numSsid==-1) return(0); //error
for(int thisNet=0;thisNet<numSsid;thisNet++){
Serial.println(WiFi.SSID(thisNet));
if(strcmp(WiFi.SSID(thisNet),SSID1)==0) score=1;
else if(strcmp(WiFi.SSID(thisNet),SSID2)==0){
score=2;
}
}
Serial.println("Score:");
Serial.println(score);
return(score);
}
Code in thingProperties.h:
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
const char SSID1[] = SECRET_SSID1; // Network SSID (name)
const char PASS1[] = SECRET_OPTIONAL_PASS1; // Network password (use for WPA, or use as key for WEP)
const char SSID2[] = SECRET_SSID2; // Network SSID (name)
const char PASS2[] = SECRET_OPTIONAL_PASS2; // Network password (use for WPA, or use as key for WEP)
String conn_type;
float bat_voltage;
void initProperties(){
ArduinoCloud.addProperty(conn_type, READ, ON_CHANGE, NULL);
ArduinoCloud.addProperty(bat_voltage, READ, ON_CHANGE, NULL);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID1, PASS1);
WiFiConnectionHandler ArduinoIoTSecondConnection(SSID2, PASS2);
Thank you very much for everybody's response in advance and have nice day!