#define WATER 34
#define RED 2
#define YELLOW 19
#define GREEN 21
#define BUZZER 4
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const char* serverName = "https://kofdpsa4ivfnfvw6s5b2v4yrk40boyof.lambda-url.ap-south-1.on.aws/";
void setup() {
// put your setup code here, to run once:
Wire.begin(5, 18);
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(RED, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(WATER, INPUT);
lcd.init();
lcd.backlight();
setupWifi();
}
void loop() {
int16_t j = analogRead(WATER) * 100 / 4095;
Serial.println(j);
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
lcd.setCursor(0, 2);
lcd.print("Water level:");
lcd.setCursor(0, 3);
lcd.print(j);
lcd.print("% ");
if (j >= 0)
digitalWrite(GREEN, HIGH);
if (j > 50)
digitalWrite(YELLOW, HIGH);
if (j > 80) {
digitalWrite(RED, HIGH);
sendData(j);
tone(BUZZER, 262, 250); // Plays 262Hz tone for 0.250 seconds
}
// put your main code here, to run repeatedly:
delay(1000); // this speeds up the simulation
}
void setupWifi() {
lcd.setCursor(0, 0); // move cursor the first row
lcd.print("Connecting to ");
lcd.setCursor(0, 1);
lcd.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
lcd.clear();
lcd.setCursor(0, 0); // move cursor the first row
lcd.print("Wifi connected!");
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
}
void sendData(int water_level) {
Serial.println("Sending data...");
WiFiClient client;
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "text/plain");
String data = "{";
data.concat("\"water\":");
data.concat(water_level );
data.concat(",\"app\":\"water_level\"");
data.concat("}");
int httpResponseCode = http.POST(data);
if (httpResponseCode > 0) {
String response = http.getString(); //Get the response to the request
Serial.println(httpResponseCode); //Print return code
Serial.println(response); //Print request answer
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
lcd.setCursor(15, 1);
lcd.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}