Skip to content

Instantly share code, notes, and snippets.

@vivekanandRdhakane
Created May 4, 2020 16:27
Show Gist options
  • Save vivekanandRdhakane/f691ac7e4a9ff33c3fb31437e07cc417 to your computer and use it in GitHub Desktop.
Save vivekanandRdhakane/f691ac7e4a9ff33c3fb31437e07cc417 to your computer and use it in GitHub Desktop.
//Author: Vivekanand Dhakane
//Updated on: 28/4/2020
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define highest_tank 91 // distance sensor reading in cm when tank is at lowest level
#define tank_offset 19 // distance sensor reading in cm when tank is Full
#define Total_tank_capacity 930 // Total Tank capacity in litres
int tank_fill_length = highest_tank - tank_offset;
boolean first_time = true;
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
WiFiServer server(80); // 80 for HTTP server
IPAddress IP(192, 168, 4, 15); // setting IP address
IPAddress mask = (255, 255, 255, 0); // setting Subnet Mask
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_AP); // starting ESP in Access Point Mode
WiFi.softAP("ShriRamNiwas", "password"); // Setting Hotspot name and password
WiFi.softAPConfig(IP, IP, mask); // Configuring AP
server.begin(); // Starting server
Serial.println();
Serial.println("Server started.");
Serial.print("IP: ");
Serial.println(WiFi.softAPIP());
Serial.print("MAC:");
Serial.println(WiFi.softAPmacAddress());
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.print(" WELCOME ");
lcd.setCursor(0, 1);
lcd.print("Starting Server.");
delay(2000);
}
void loop() {
WiFiClient client = server.available(); //checking for new message
if (!client) {
return; //if no new message then return
}
String request = client.readStringUntil('\r'); // reading incoming message till '\r'
Serial.println("********************************");
Serial.println("From the station: " + request);
client.flush();
Serial.print("Byte sent to the station: ");
Serial.println(client.println(request + "ca" + "\r"));
//seperating distance reading from incoming string
String Tank_reading = request.substring(2, request.length());
float tank_level = Tank_reading.toFloat();
Serial.println("tank_level: " + (String)tank_level);
float filled = tank_fill_length - (tank_level - tank_offset);
float Tank_percentage = 100 * filled / tank_fill_length;
Serial.println("Tank_percentage: " + (String)Tank_percentage);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Tank Remaining");
if (tank_level < (highest_tank + 5) && tank_level > (tank_offset - 2) && Tank_percentage >= 0)
{
//Printing Tank remaining in litres
lcd.setCursor(0, 1);
//lcd.print((String)((int)(Tank_percentage * 9.31)) + " Ltr"); // 9.31 = Total_tank_capacity/100
lcd.print((String)((int)(Tank_percentage * Total_tank_capacity/100)) + " Ltr"); // Display in Litres
//Printing Tank remaining in Percentage
lcd.setCursor(12, 1);
lcd.print((String)(int)Tank_percentage + "%");
first_time = false;
}
else
{
if (first_time)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting For");
lcd.setCursor(0, 1);
lcd.print("Client.......");
}
else {
lcd.setCursor(0, 1);
lcd.print(" Waiting... ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment