Last active
March 4, 2024 05:54
-
-
Save SimedruF/69b5cc2b9998b37b9497bb5faafe896e to your computer and use it in GitHub Desktop.
ESP32_LCD20x4_with_inductive_sensor.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Florin Simedru | |
Project details at https://blog.automatic-house.ro | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files. | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
*/ | |
/* | |
; PlatformIO Project Configuration File | |
; | |
; Build options: build flags, source filter | |
; Upload options: custom upload port, speed and extra flags | |
; Library options: dependencies, extra library storages | |
; Advanced options: extra scripting | |
; | |
; Please visit documentation for the other options and examples | |
; https://docs.platformio.org/page/projectconf.html | |
[env:esp32doit-devkit-v1] | |
platform = espressif32 | |
board = esp32doit-devkit-v1 | |
framework = arduino | |
upload_port = COM9 | |
monitor_port = COM9 | |
monitor_speed = 115200 | |
lib_deps = | |
iakop/LiquidCrystal_I2C_ESP32@^1.1.6 | |
fbiego/ESP32Time@^2.0.4 | |
*/ | |
#include <Arduino.h> | |
#include <LiquidCrystal_I2C.h> | |
#include <Time.h> | |
#include <ESP32Time.h> | |
#include <Wifi.h> | |
#include <HTTPClient.h> | |
#define INDUCTIV_SENSOR_PIN 13 | |
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20 chars and 4 line display | |
int inductive_activated; | |
// ESP32Time rtc; | |
ESP32Time rtc(3600); // offset in seconds GMT+1 | |
// Put your SSID & Password | |
const char *ssid = "WIFI SSID"; // Enter SSID here | |
const char *password = "WIFI PASSORD"; // Enter Password here | |
const char *ntpServer = "pool.ntp.org"; | |
const long gmtOffset_sec = 3600; | |
const int daylightOffset_sec = 7200; | |
String wifi_networks; | |
struct tm timeinfo; | |
wl_status_t wifi_connection; | |
wl_status_t wifi_connect() | |
{ | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
#ifdef SERIAL_DEBUG_ON | |
Serial.println("Connecting"); | |
#endif | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
#ifdef SERIAL_DEBUG_ON | |
Serial.print("."); | |
#endif | |
} | |
#ifdef SERIAL_DEBUG_ON | |
Serial.println(""); | |
Serial.print("Connected to WiFi network with IP Address: "); | |
Serial.println(WiFi.localIP()); | |
#endif | |
WiFi.setAutoReconnect(true); | |
WiFi.persistent(true); | |
return WiFi.status(); | |
} | |
// Function called when inductiv senzor have a rising signal | |
void handleInterrupt() | |
{ | |
//Inductive activated counter | |
inductive_activated++; | |
Serial.printf("Inductive sensor activated :%d\n", inductive_activated); | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
rtc.setTime(00, 18, 20, 3, 3, 2024); // 3th March 2024 20:18:30 | |
wifi_connection = wifi_connect(); | |
/*---------set with NTP---------------*/ | |
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); | |
struct tm timeinfo; | |
if (getLocalTime(&timeinfo)) | |
{ | |
rtc.setTimeStruct(timeinfo); | |
} | |
// PIN configuration as INPUT | |
pinMode(INDUCTIV_SENSOR_PIN, INPUT); | |
// Attach interrupt for signal change | |
attachInterrupt(digitalPinToInterrupt(INDUCTIV_SENSOR_PIN), handleInterrupt, RISING); | |
lcd.init(); | |
lcd.clear(); | |
lcd.backlight(); // Make sure backlight is on | |
// Print a message on both lines of the LCD. | |
lcd.setCursor(1, 0); // Set cursor to character 2 on line 0 | |
lcd.print("Hello world!"); | |
lcd.setCursor(1, 1); // Move cursor to character 2 on line 1 | |
lcd.print("Inductive test!"); | |
delay(1000); | |
Serial.println(); | |
} | |
void loop() | |
{ | |
// clears the display to print new message | |
lcd.clear(); | |
// set cursor to first column, second row | |
lcd.setCursor(0, 0); | |
// print message | |
lcd.printf("Inductive sensor %d ", inductive_activated); | |
lcd.setCursor(0, 1); | |
if (wifi_connection == WL_CONNECTED) | |
lcd.print(" Wifi Connected !"); | |
else | |
{ | |
lcd.print(" Wifi not connected !"); | |
} | |
lcd.setCursor(0, 2); | |
lcd.printf("%s", rtc.getTime("%H:%M:%S")); | |
lcd.setCursor(0, 3); | |
lcd.printf("%d-%d-%d", rtc.getDay(),rtc.getMonth()+1,rtc.getYear()); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment