/*Me:a haghighat panah test
----------
♦ME: symbol meaning:♦{
☻ : "No need this seems extra"
}
*/ //
#define AHP_BLYNK_OFF // trun off comand for code
/*************************************************************
This is a simple demo of sending and receiving some data.
Be sure to check out other examples!
*************************************************************/
//==================[HEADER FILES]==================V
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiClient.h>
#ifdef AHP_BLYNK_OFF //temp
// -------[blynk]---------V
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL4Zyq3HNsV"
#define BLYNK_TEMPLATE_NAME "AHP BLYNK TEMPLATE"
#define BLYNK_AUTH_TOKEN "cUiNrA0de1qBFz5nVOOtcPwyADfKyThh"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp32.h>
// #include <Servo.h>
#endif
//==================[PIN DEFINE]==================V
#define GREEN_LCD_PIN 23
#define BLUE_LCD_PIN 34
#define POTENTIOMETER_PIN 33
#define PUSH_BUTTON 32
#define TRIG_PIN 2
#define ECHO_PIN 4
//==================[Objects]==================V
LiquidCrystal_I2C Green_LCD = LiquidCrystal_I2C(0x26, 16, 2);
LiquidCrystal_I2C Blue_LCD = LiquidCrystal_I2C(0x27, 16, 2);
#ifdef AHP_BLYNK_OFF //temp
BlynkTimer timer;
#endif
//==================[Golbal variable]==================V
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
unsigned int button_count = 0;
// // int LCD_view = 0;
// /--------------------
float lcd_state = 0;
float temp = 0;
int Plant_mode = 0;
//--------------{Blynk data send)----V
float water_send_percent = 0;
float moisture_send_percent = 0;
//-------------------{tanker)-=- V
//source: https://tankerplastic.com/%D9%85%D8%AE%D8%B2%D9%86-%D8%A2%D8%A8/
#define PI_NUM 3.14
uint8_t tanker_index = 21;
//مخزن آب عمودی
// const int tanker_volumes [] = { 80, 100, 150, 220, 350, 500, 550, 900, 1000, 1000, 1500, 1500, 2000, 3000, 4000, 5000, 5000, 6000, 8000, 10000, 15000, 20000};
//------- tanker_index : ---{ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21}
const int tanker_Diameter[] = { 50, 52, 55, 60, 69, 85, 72, 133, 88, 109, 121, 108, 132, 153, 183, 188, 222, 222, 195, 255, 255, 290 , 50 };
const int tanker_height [] = { 58, 75, 85, 102, 132, 119, 170, 132, 212, 145, 172, 200, 188, 208, 180, 225, 160, 190, 295, 235, 325, 335, 60 };
//------{LCD)-----V
char line0[17];
char line1[17];
#ifdef AHP_BLYNK_OFF //temp
//===========================[BLYNK : READ | WRITE | Functions]===========================V
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0) // Me: az site (Blynk) dade dar (ESP32) neveshte (write) mish
{
int value = param.asInt(); // assigning incoming value from pin V1 to a variable
Blynk.virtualWrite(V1, value); // Me: meghdare [value] be vritual pin (V1) dade mishe
Serial.println("blynk swicth: " + String(value));
}
BLYNK_WRITE(V4) //Me: plant silder get data
{
Plant_mode = param.asInt();
// Serial.println("Plant: " + String(value));
}
BLYNK_WRITE(V5) //Me: plant swicth button get data
{
test2_Blynk_plant (param.asInt());
}
BLYNK_CONNECTED()
{
}
void myTimerEvent()
{
Blynk.virtualWrite(V6,water_send_percent);
Blynk.virtualWrite(V7,moisture_send_percent);
Blynk.virtualWrite(V2, millis() / 1000);
}
#endif
//===========================[BLYNK : READ | WRITE | Functions]===========================V
//V==============V[♦ MAIN PORGRAM ♦}V==============V
void setup()
{
Serial.begin(115200); // baud rate
//---------------[Pin mode config]---------------
pinMode(PUSH_BUTTON, INPUT_PULLUP);
// pinMode(POTENTIOMETER_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT); // Sets the trigPin as an Output
pinMode(ECHO_PIN, INPUT); // Sets the echoPin as an Input
//---------------[LCD config]---------------
Green_LCD.init();
Green_LCD.backlight();
Green_LCD.clear();
Green_LCD.setCursor(0, 0);
Green_LCD.print("IOT Group 17");
logoo();
Blue_LCD.init();
Blue_LCD.backlight();
Blue_LCD.clear();
Blue_LCD.setCursor(0, 0);
Blue_LCD.print("Code by:");
Blue_LCD.setCursor(0, 1);
Blue_LCD.print("Haghighat panah");
//---------------[wi if connect]---------------
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
#ifdef AHP_BLYNK_OFF //temp
//---------------[blynk]---------------
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); // turn on
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent); // turn on
#endif
}
void loop()
{
#ifdef AHP_BLYNK_OFF //temp
Blynk.run();
timer.run();
#endif
test1_pote();
int temp = ultraSonic();
Serial.print(String(temp) + "cm");
tanker(temp);
}
//===============[MY FUNCTIONS]===============v
void test1_pote () {
temp = analogRead(POTENTIOMETER_PIN);
//Me: replacment for LCD,clear function lag for not having delay-------------V
// Green_LCD.clear();
// Green_LCD.print("value: ");
// if (lcd_state != temp) Green_LCD.clear();
updateDisplay(2);
lcd_state = temp; // Me: {temp} va {lcd_state} = (Global vars) hastand
Green_LCD.setCursor(0, 0);
//Me: replacment for LCD.clear function lag for not having delay-------------^
Green_LCD.print("value:" + String(temp));
#ifdef AHP_BLYNK_OFF //temp
// Blynk.virtualWrite(V6, temp);
moisture_send_percent = temp;
#endif
// if (button ()) {
// Serial.println("sending : " + String(temp));
// }
}
void test2_Blynk_plant(int plant_mode_button) {
if (plant_mode_button == 1) Serial.println("sending : " + String(Plant_mode));
}
/*a
void blynk_button () {
int t_array[] = {0, 1, 2, 3};
if (digitalRead(PUSH_BUTTON) == LOW) {
Blynk = t_array[button_count];
button_count++;
// Serial.println(dcount);
// delay(100);
// delay(100);
if (button_count >= 4 ) button_count = 0;
}
*/
boolean button () {
if (digitalRead(PUSH_BUTTON) == LOW) return true;
else return false;
}
int ultraSonic() { // MeE: this function converts sound wave to distance (CM)
long duration = 0;;
float distance = 0;;
// Clears the trigPin
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(ECHO_PIN, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // MeE : (0.034) is the sped of sound
return Postive_round(distance);
}
void tanker (int Distanceee) { // MeE: this function works works with cylinder tanks volume
// Me: Tammi mohasebat ba estfade az vahad (Cm) hasteh
// MeE: Here we are going to get the distance of the empty part of Tenaker
int empty_height = ( tanker_height [tanker_index] - Distanceee );
if (empty_height < 0) empty_height = 0; // Me: if the empty space was negative number it means it the tanker already empty so we give thw value [0]
//-----------------[ V = πr2 h]-------------------------V
//MeE: the reference had D: diameter we have to convert to radius first
unsigned int tanker_radius = tanker_Diameter[tanker_index] / 2; // (r=d/2)
unsigned int tanker_full_volume =( PI_NUM * (tanker_radius * tanker_radius) * tanker_height [tanker_index])/1000; // (V1 = πr2 h1)
unsigned int water_level = (PI_NUM * (tanker_radius * tanker_radius) * empty_height)/1000 ; // ( V2 = πr2 h2)
float water_level_percent = ((float)water_level / (float)tanker_full_volume) * 100;
// --------------------[(LCD or Serial),print]--------------------V
delayMicroseconds(50000);
// Blue_LCD.clear();
updateDisplay(1);
Blue_LCD.setCursor(0, 0);
Blue_LCD.println("wLV:" + String(water_level) + "L");
Blue_LCD.setCursor(10, 0);
Blue_LCD.println(String(water_level_percent) + "%");
// Blue_LCD.print(" ");
// Blue_LCD.println("wLV:" + String(water_level) + "L " + String(water_level_percent) + "%");
Blue_LCD.setCursor(0, 1);
Blue_LCD.println("TANK_VOL:" + String(tanker_full_volume )+"L");
// delay(1000);
Serial.println("wLV:" + String(water_level) + "L|" + String(water_level_percent) + "%");
Serial.println("TANK_VOL:" + String(tanker_full_volume)+"L");
water_send_percent = water_level_percent;
}
int Postive_round(float value) { //MeE: this function rounds the number
int upperVal = value;
float decimalVal = value - upperVal;
if (decimalVal < 0.5) return upperVal;
else return upperVal + 1;
}
void updateDisplay(uint8_t address) {
// if(address == 1){
Blue_LCD.setCursor(0,0);
Blue_LCD.print(line0);
Blue_LCD.print(line1);
// } else if (address == 2){
Green_LCD.setCursor(0,0);
Green_LCD.print(line0);
Green_LCD.print(line1);
logoo();
// }
}
void logoo(){
byte AHP[8] = {
0b00100,
0b01010,
0b01110,
0b01010,
0b00100,
0b01010,
0b01110,
0b01010
};
Green_LCD.createChar(0, AHP);
Green_LCD.setCursor(16,1);
Green_LCD.write(byte(0));
}
//^===============^[MY FUNCTIONS]^===============^