Here you have, the code is quite simple, since I'm new programming and forgive the begginners errors.
The sensor is connected to pin2
Relay to pin3
The board used is WIFI UNO Rev2
At the moment it is connecting to a Firebase database and stores a bit based on changes only, that's why I declared 2 variables: LiquidNew and LiquidOld.
Happy to hear your feedback!
#include <Config.h>
#include <Firebase.h>
#include <Firebase_Arduino_WiFiNINA.h>
#include <Firebase_TCP_Client.h>
#include <WCS.h>
#include <LiquidCrystal.h>
#include <WiFiClient.h>
#include <WiFiNINA.h>
//LDC
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int analogPin = A0; //Define the A0 as analogPin as integer type.
//INPUT SENSOR
#define Liquid_Detection_Pin 2
// OUTPUT RELAY
int relay = 3; // relay
//Sensor status for firebase
int LiquidOld = 0;
int LiquidNew = 0;
//WIFI
char ssid[] = "MY_WIFI"; // your network SSID (name) between the " "
char pass[] = "MY_PASWORD"; // your network password between the " "
int status = WL_IDLE_STATUS; //connection status
/*WiFiClientSecure client;*/
// Insert Firebase project API Key
#define API_KEY "API_KEY"
// Insert RTDB URLefine the RTDB URL */
#define DATABASE_URL "URL.firebasedatabase.app"
#define FIREBASE_AUTH "SECRET_KEY"
#define WIFI_SSID "MY_WIFI"
#define WIFI_PASS "MY_PASSWORD"
//Define Firebase Data object
FirebaseData firebaseData;
void setup ()
{
lcd.begin(16, 2); // set the lcd type: 16-character by 2-lines
lcd.clear(); // LCD screen clear
lcd.print("Smart Fountain"); // Send the ASCII code to the LCD for
// displaying the message
pinMode(10, OUTPUT); // sets backlight pin-10 as PWM output
analogWrite(10, 125); // Set backlight to 50% brightness
//CONNECT WIFI
enable_WiFi();
connect_WiFi();
printWifiStatus();
//SENSOR
pinMode(Liquid_Detection_Pin, INPUT);
//RELAY - OFF
pinMode(relay, OUTPUT);
digitalWrite(relay,LOW);
//Connect Firebase
Firebase.begin(DATABASE_URL, FIREBASE_AUTH, WIFI_SSID, WIFI_PASS);
delay(5000);
}
void loop()
{
if (digitalRead(Liquid_Detection_Pin)) {
LiquidNew = 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Liquid Detected!");
digitalWrite(relay, HIGH);
if(LiquidOld != LiquidNew) {
Firebase.setFloat(firebaseData, "Arduino1" ,1);
}
LiquidOld = 1;
}
else {
LiquidNew = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No Liquid!");
digitalWrite(relay,LOW);
if(LiquidOld != LiquidNew) {
Firebase.setFloat(firebaseData, "Arduino1" ,0);
}
else{
}
LiquidOld = 0;
}
delay(1000);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SSID: ");
lcd.setCursor(1, 0);
lcd.print(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("IP Address: ");
lcd.setCursor(1, 0);
lcd.print(ip);
// print the received signal strength:
// long rssi = WiFi.RSSI();
// lcd.print("signal strength (RSSI):");
// lcd.print(rssi);
// lcd.print(" dBm");
}
void enable_WiFi() {
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < "1.0.0") {
lcd.print("Please upgrade the firmware");
}
}
void connect_WiFi() {
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Attempting to connect to SSID: ");
lcd.setCursor(1, 0);
lcd.print(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
}