#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <TridentTD_LineNotify.h>
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Line Notify token
#define LINE_TOKEN "ij3BTrbSEN804X54cvkb4h2FdneatPFXkENlWBrXlRp"
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Ultrasonic sensor pins
const int trigPin = 5;
const int echoPin = 18;
// Button pin
const int buttonPin = 4;
// Variables to store button state
bool buttonPressed = false;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
Serial.begin(115200);
Serial.println("123");
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Connecting to");
lcd.setCursor(0, 1);
lcd.print("WiFi...");
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
lcd.setCursor(0, 1);
lcd.print(".");
Serial.print(".");
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi Connected");
delay(1000);
lcd.clear();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
// Initialize Line Notify
LINE.setToken(LINE_TOKEN);
}
void loop() {
long duration;
int distance;
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin on HIGH state for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = duration * 0.034 / 2;
// Display the distance on LCD
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm ");
// Check button state
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
if (!buttonPressed) {
buttonPressed = true;
lastDebounceTime = millis();
} else if (millis() - lastDebounceTime > debounceDelay) {
sendLineNotification(distance);
buttonPressed = false; // Reset button state to prevent multiple notifications
}
} else {
buttonPressed = false;
}
delay(100);
}
void sendLineNotification(int distance) {
String message = "Distance: " + String(distance) + " cm!";
LINE.notify(message);
Serial.println("Notification sent: " + message);
}