Hi all,
I hooked up some sensors to an Arduino Wifi Rev2 to monitor my garden, but it keeps getting disconnected for some reason. I'd like to add some code to reconnect after it gets disconnected but I've been struggling to do so. Below is the code I have so far, any suggestions would be much appreciated!
/*
Project: Send Data to Firebase Using Arduino Uno WiFi Rev2
Board: Arduino Uno WiFi Rev2
External libraries:
- Arduino_LSM6DS3 by Arduino V1.0.0
- Firebase Arduino based on WiFiNINA by Mobizt V1.1.4
*/
#include "RTClib.h"
#include "DFRobot_SHT20.h"
RTC_PCF8523 rtc;
DFRobot_SHT20 sht20(&Wire, SHT20_I2C_ADDR);
#include <Arduino_LSM6DS3.h>
#include <Firebase_Arduino_WiFiNINA.h>
#define FIREBASE_HOST ""
#define FIREBASE_AUTH ""
#define WIFI_SSID ""
#define WIFI_PASSWORD ""
FirebaseData firebaseData;
String path = "/MoistureSensors_outdoor";
String jsonStr;
void setup()
{
Serial.begin(9600);
sht20.initSHT20();
delay(100);
Serial.println("Sensor init finish!");
sht20.checkSHT20();
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (! rtc.initialized() || rtc.lostPower()) {
Serial.println("RTC is NOT initialized, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
delay(1000);
Serial.println();
Serial.print("Connecting to WiFi...");
int status = WL_IDLE_STATUS;
while (status != WL_CONNECTED) {
status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print(".");
delay(300);
}
Serial.print(" IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH, WIFI_SSID, WIFI_PASSWORD);
Firebase.reconnectWiFi(true);
}
void loop()
{
float A0, A1, A2, mo, d, h, m, s, humd, temp;
DateTime now = rtc.now();
A0 = analogRead(0);
A1 = analogRead(1);
A2 = analogRead(2);
mo = now.month();
d = now.day();
h = now.hour();
m = now.minute();
s = now.second();
humd = sht20.readHumidity();
temp = sht20.readTemperature();
// Send data to Firebase with specific path
if (Firebase.setFloat(firebaseData, path + "/1-setFloat/A0", A0)) {
Serial.println(firebaseData.dataPath() + " = " + A0);
}
if (Firebase.setFloat(firebaseData, path + "/1-setFloat/A1", A1)) {
Serial.println(firebaseData.dataPath() + " = " + A1);
}
if (Firebase.setFloat(firebaseData, path + "/1-setFloat/A2", A2)) {
Serial.println(firebaseData.dataPath() + " = " + A3);
}
if (Firebase.setFloat(firebaseData, path + "/1-setFloat/month", mo)) {
Serial.println(firebaseData.dataPath() + " = " + mo);
}
if (Firebase.setFloat(firebaseData, path + "/1-setFloat/day", d)) {
Serial.println(firebaseData.dataPath() + " = " + d);
}
if (Firebase.setFloat(firebaseData, path + "/1-setFloat/hour", h)) {
Serial.println(firebaseData.dataPath() + " = " + h);
}
if (Firebase.setFloat(firebaseData, path + "/1-setFloat/minute", m)) {
Serial.println(firebaseData.dataPath() + " = " + m);
}
if (Firebase.setFloat(firebaseData, path + "/1-setFloat/second", s)) {
Serial.println(firebaseData.dataPath() + " = " + s);
}
if (Firebase.setFloat(firebaseData, path + "/1-setFloat/temp", temp)) {
Serial.println(firebaseData.dataPath() + " = " + temp);
}
if (Firebase.setFloat(firebaseData, path + "/1-setFloat/humd", humd)) {
Serial.println(firebaseData.dataPath() + " = " + humd);
}
// Push data using pushJSON
jsonStr = "{\"A0\":" + String(A0,6) + ",\"A1\":" + String(A1,6) + ",\"A2\":" + String(A2,6) + ",\"month\":" + String(mo,6) +",\"day\":" + String(d,6) +",\"hour\":" + String(h,6) +",\"minute\":" + String(m,6) +",\"second\":" + String(s,6) + ",\"temp\":" + String(temp,6) + ",\"humd\":" + String(humd,6)+ "}";
if (Firebase.pushJSON(firebaseData, path + "/2-pushJSON", jsonStr)) {
Serial.println(firebaseData.dataPath() + " = " + firebaseData.pushName());
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println(')');
}
else {
Serial.println("Error: " + firebaseData.errorReason());
}
Serial.println();
delay(100000);
}