#include <SPI.h>
#include <Wire.h>
#include <WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define TRIGPIN 27 //D27
#define ECHOPIN 26 //D26
#define wifiLed 2 //D2
#define ButtonPin1 12 //D12
#define BuzzerPin 13 //D13
#define GreenLed 14 //D14
int full_water = 500;
int waterLevelPer = 10;
int waterLevelLit = 0;
bool toggleBuzzer = HIGH;
bool wifiStatus = true;
bool BuzzerStatus = true;
bool toggleRelay = true;
bool cloudStatus = true;
bool modeFlag = true;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.begin(ssid, password, 6);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
pinMode(wifiLed, OUTPUT);
pinMode(GreenLed, OUTPUT);
pinMode(BuzzerPin, OUTPUT);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.setTextSize(1);
display.setTextColor(WHITE);
delay(100);
display.setCursor(30, 10);
display.print("Welcome to");
display.setCursor(30, 25);
display.print("MS IOT HOME");
display.display();
display.clearDisplay();
display.setCursor(10, 0);
display.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
display.print(".");
}
display.display();
if(WiFi.status() == WL_CONNECTED){
display.clearDisplay();
display.print("\nOK! IP=");
display.println(WiFi.localIP());
display.display();
}
}
void loop() {
BuzzerStatus = false;
toggleRelay = false;
cloudStatus = false;
modeFlag = false;
digitalWrite(GreenLed, HIGH);
digitalWrite(wifiLed, LOW);
delay(800);
displayData();
BuzzerStatus = true;
toggleRelay = true;
cloudStatus = true;
modeFlag = true;
digitalWrite(GreenLed, LOW);
digitalWrite(wifiLed, HIGH);
delay(800);
displayData();
}
void WaterLitter(){
waterLevelLit = (full_water*waterLevelPer)/100;
}
// Wifi Connection Check
void wifiConnection() {
if (WiFi.status() == WL_CONNECTED)
{
wifiStatus = true; // Change Wifi Status Active
} else {
wifiStatus = false; // Change Wifi Status Inactive
}
}
// Wifi
const unsigned char wifi_on [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xf0, 0x3c, 0x3c, 0x60, 0x06, 0x07, 0xe0, 0x1c, 0x38,
0x10, 0x08, 0x01, 0x80, 0x07, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00
};
const unsigned char wifi_off [] PROGMEM = {
0x80, 0x00, 0xc0, 0x00, 0x60, 0x00, 0x39, 0xf0, 0x38, 0x3c, 0x6c, 0x06, 0x07, 0x00, 0x1f, 0x18,
0x11, 0xc8, 0x01, 0xe0, 0x07, 0xf0, 0x00, 0x38, 0x00, 0x0c, 0x01, 0x86, 0x00, 0x03, 0x00, 0x01
};
// Bell
const unsigned char bell_on [] PROGMEM = {
0xff, 0xff, 0xf8, 0x1f, 0xf3, 0xcf, 0xf7, 0xef, 0xe7, 0xe7, 0xe7, 0xe7, 0xe7, 0xe7, 0xe7, 0xe7,
0xef, 0xf7, 0xef, 0xf7, 0xcf, 0xf3, 0x80, 0x01, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0xff, 0xff
};
const unsigned char bell_off [] PROGMEM = {
0x7f, 0xff, 0xbc, 0x3f, 0xdf, 0x8f, 0xe7, 0xef, 0xe7, 0xef, 0xe3, 0xe7, 0xe5, 0xe7, 0xe6, 0xf7,
0xef, 0x77, 0xef, 0xbf, 0xcf, 0x8f, 0x80, 0x0f, 0xff, 0xf7, 0xff, 0xfb, 0xfe, 0x7d, 0xff, 0xfe
};
// Cloud
const unsigned char cloud_on [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xc7, 0x3f, 0x9f, 0x9f, 0xbf, 0xdf, 0x7f, 0xc3, 0x7f, 0xf9,
0x7f, 0xfc, 0x3f, 0xfe, 0xbf, 0xfe, 0x9f, 0xfc, 0xcf, 0xf9, 0xe0, 0x03, 0xff, 0xff, 0xff, 0xff
};
const unsigned char cloud_off [] PROGMEM = {
0x7f, 0xff, 0xbf, 0xff, 0xcc, 0xff, 0xcf, 0x3f, 0x87, 0x9f, 0x3b, 0xdf, 0x7d, 0xc3, 0x7e, 0xf9,
0x7f, 0x7c, 0x3f, 0xbe, 0xbf, 0xde, 0x9f, 0xee, 0xcf, 0xe3, 0xe0, 0x03, 0xff, 0xfd, 0xff, 0xfe
};
// Pump
const unsigned char pump_on [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xf6, 0x6f, 0xf2, 0x4f,
0xf8, 0x1f, 0xfc, 0x7f, 0x9e, 0x79, 0x9f, 0xf9, 0x9f, 0xf9, 0xc0, 0x03, 0xe0, 0x07, 0xff, 0xff
};
const unsigned char pump_off [] PROGMEM = {
0xff, 0xff, 0xfe, 0x7f, 0xfc, 0x7f, 0xf8, 0x1f, 0xf2, 0x4f, 0xf6, 0x6f, 0xfe, 0x7f, 0xfe, 0x7f,
0xfe, 0x7f, 0xfe, 0x7f, 0x9f, 0xf9, 0x9f, 0xf9, 0x9f, 0xf9, 0xc0, 0x03, 0xe0, 0x07, 0xff, 0xff
};
// Mode (Auto, Manual)
const unsigned char mode_on [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0x3e, 0x00, 0x7f, 0x3c, 0x7c, 0x1f, 0x31,
0xff, 0x8f, 0x23, 0xff, 0xcf, 0x07, 0xff, 0xe7, 0x0f, 0xff, 0xe7, 0x00, 0xff, 0xf7, 0x00, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xef, 0xff, 0x00, 0xe7, 0xff, 0xf0,
0xe7, 0xff, 0xe0, 0xf3, 0xff, 0xc4, 0xf1, 0xff, 0x8c, 0xf8, 0x7e, 0x3c, 0xfe, 0x00, 0x7c, 0xff,
0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
const unsigned char mode_off [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0xfc, 0x00, 0x3f, 0xf8, 0x7e, 0x1f, 0xf1, 0xff, 0x8f, 0xe1,
0xff, 0xc7, 0xc0, 0xff, 0xe3, 0xcc, 0x7f, 0xf3, 0xce, 0x3f, 0xf3, 0x9f, 0x1f, 0xf9, 0x9f, 0x8f,
0xf9, 0x9f, 0xc7, 0xf9, 0x9f, 0xe3, 0xf9, 0x9f, 0xf1, 0xf9, 0x9f, 0xf8, 0xf9, 0xcf, 0xfc, 0x73,
0xcf, 0xfe, 0x33, 0xc7, 0xff, 0x03, 0xe3, 0xff, 0x87, 0xf1, 0xff, 0x8f, 0xf8, 0x7e, 0x1f, 0xfc,
0x00, 0x3f, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff
};
void displayData(){
WaterLitter();
wifiConnection();
display.clearDisplay();
display.setTextSize(2);
display.setCursor(5, 10);
display.print(waterLevelPer);
display.print("% ");
display.print(waterLevelLit);
display.print("L");
display.drawBitmap(0, 45, wifiStatus ? wifi_on : wifi_off, 16, 16, 1);
display.drawBitmap(25, 45, BuzzerStatus ? bell_on : bell_off, 16, 16, 1);
display.drawBitmap(50, 45, toggleRelay ? pump_on : pump_off, 16, 16, 1);
display.drawBitmap(75, 45, cloudStatus ? cloud_on : cloud_off, 16, 16, 1);
display.drawBitmap(100, 45, modeFlag ? mode_on : mode_off, 16, 16, 1);
display.display();
}