// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
//------------------------------------------------------------------------------
char auth[] = "sYZr0FpOuAE5kuDiRHA64_CD5hir2mjC";
char ssid[] = "HONOR 20 Lite";
char pass[] = "1234567890";
char server[] = "128.199.159.242"; // IP for your Local Server
int port = 8080;
//------------------------------------------------------------------------------
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
//------------------------------------------------------------------------------
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Stepper.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <SPI.h>
#include <Ethernet.h>
WidgetLED led1 (V5);
WidgetLED led2 (V4);
//------------------------------------------------------------------------------
BlynkTimer myTIMER;
BlynkTimer timer;
const int stepsPerRevolution = 2038; // change this to fit the number of steps per revolution
#define IN1 27
#define IN2 26
#define IN3 33
#define IN4 32
// IR SENSOR
const int IR_PIN = 15; // Pin connected to the IR sensor
int IR_STATE = HIGH;
//ULTRSONICSENSOR
#define trigPin 14
#define echoPin 12
//LED
#define greenLed 19
#define redLed 18
//declare variable
unsigned long travelTime;
unsigned long duration;
float level;
int distance;
// initialize the stepper library
Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);
#include <SoftwareSerial.h>
#define BLYNK_PRINT Serial
//Install the following Libraries
#include <TinyGPS++.h>
//GPS RX to D1 & GPS TX to D2 and Serial Connection
const int RXPin = 17, TXPin = 16;
const uint32_t GPSBaud = 9600;
SoftwareSerial gps_module(RXPin, TXPin);
TinyGPSPlus gps;
WidgetMap myMap(V0); //V0 - virtual pin for Map
//Variable to store the speed, no. of satellites, direction
float gps_speed;
float no_of_satellites;
String satellite_orientation;
//unsigned int move_index;
unsigned int move_index = 1;
#define NOTIFICATION_DELAY 10000 // 10 seconds in milliseconds
unsigned long lastNotificationTime = 0; // Track the time of the last notification
unsigned long peopleCount = 0;
bool personDetected = false;
void setup()
{
Serial.begin(115200);
Serial.println();
gps_module.begin(GPSBaud);
Blynk.connectWiFi(ssid, pass);
Blynk.config(auth, server, port);
Blynk.connect();
timer.setInterval(5000L, checkGPS);
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
lcd.init();
lcd.backlight();
timer.setInterval(1000L, sendSensor);
pinMode(IR_PIN, INPUT);
// set the speed at 5 rpm
myStepper.setSpeed(14);
}
void checkGPS(){
if (gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
Blynk.virtualWrite(V0, "GPS ERROR");
}
}
void loop()
{
while (gps_module.available() > 0)
{
//displays information every time a new sentence is correctly encoded.
if (gps.encode(gps_module.read()))
displayInfo();
}
Blynk.run();
timer.run();
}
void displayInfo()
{
if (gps.location.isValid())
{
//Storing the Latitude. and Longitude
float latitude = (gps.location.lat());
float longitude = (gps.location.lng());
//Send to Serial Monitor for Debugging
Serial.print("LAT: ");
Serial.println(latitude, 6); // float to x decimal places
Serial.print("LONG: ");
Serial.println(longitude, 6);
myMap.location(move_index, latitude, longitude, "GPS_Location");
// Add a 5-second delay
delay(5000); // 5000 milliseconds = 5 seconds
}
Serial.println();
}
void sendSensor() {
IR_STATE = digitalRead(IR_PIN); // Read the state of the IR sensor
if (IR_STATE == LOW && !personDetected) {
// Rotate the motor two full revolutions counterclockwise
myStepper.step(-1 * stepsPerRevolution);
// Mark that a person has been detected and increment the people count
personDetected = true;
peopleCount++;
}
// If the IR sensor returns to the HIGH state, mark that no person is detected
else if (IR_STATE == HIGH && personDetected) {
personDetected = false;
// Send the number of people detected to the Value Display widget in Blynk
Blynk.virtualWrite(V1, peopleCount);
// Send distance data to Blynk Super Chart widget
Blynk.virtualWrite(V7, peopleCount);
}
// Remove conflicting declaration of 'duration' from here
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
delay(1000); // Delay to control the frequency of data updates
Blynk.virtualWrite(V3, distance);
if (distance > 4) {
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
led2.on(); // Turn on LED in the app
digitalWrite(redLed, HIGH);
led1.off(); // Turn on LED in the app
digitalWrite(greenLed, LOW);
lcd.clear();
lcd.setCursor(5, 0); // Set cursor to first column, first row
lcd.print("TISSUE ");
lcd.setCursor(2, 1); // Set cursor to first column, first row
lcd.print("NOT AVAILABLE");
Blynk.notify ("TISSUE NOT AVAILABLE");
Blynk.email("[email protected]", "NOTI EMAIL", "TISSUE NOT AVAILABLE");
} else {
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
led2.off(); // Turn off LED in the app
digitalWrite(redLed, LOW);
led1.on(); // Turn on LED in the app
digitalWrite(greenLed, HIGH);
lcd.clear();
lcd.setCursor(0, 0); // Set cursor to first column, first row
lcd.print("TISSUE AVAILABLE");
lcd.setCursor(3, 1);
lcd.print("PULL TISSUE");
}
}