Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********
- target : ESP32 Dev Module
- ESP32 pinout
- LCD1602 display on I2C
- one button
- author : Patrick Dubois
- license: public domain
- *********/
- // Import required libraries
- // if the library is global use <>
- // if the library is local use ""
- #include <LiquidCrystal_I2C.h>
- #include <time.h>
- #include <WiFi.h>
- #include <Wire.h>
- #define button0Pin 0 // GPIO 0 or PRG button
- #define BuiltInLed 2 // GPIO led allumée par erreur dans la boucle Wifi not connected ?
- #define tempo 1000 // 1000
- #define baudrateSerial 115200 // vitesse communication Arduino - PC
- #define SCL_GPIO 32
- #define SDA_GPIO 33
- #define numCols 16 // for lcd
- #define numRows 2
- LiquidCrystal_I2C lcd(0x27, numCols, numRows); // use this for I2C LCD.
- #define ntpServer "europe.pool.ntp.org"
- char dtDateTime[20];
- #define HOSTNAME "My_Clock"
- String WifiID[4][2]= {{ "FreeBox", "password1"},
- { "OrangeBox", "password2"},
- { "SFRBox", "password3"},
- { "BouyguesBox", "password4"}};
- bool bLCDDisplay = false;
- bool bButton0Pressed = false;
- int nIndex = 0;
- uint32_t oldMillis, newMillis;
- // function prototype declarations
- void Display_Init(void);
- void DisplayIMessage(String sMessage, bool bClear, bool bCRLN);
- String readLocalTime(void);
- int WiFi_Init(void);
- // end declarations
- void IRAM_ATTR onButton0Event() {
- bButton0Pressed = true;
- }
- void setup() {
- btStop();
- Wire.begin(SDA_GPIO, SCL_GPIO);
- Serial.begin(baudrateSerial); // Serial0
- Display_Init();
- WiFi_Init(nIndex);
- configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", ntpServer); // "Europe/Paris","CET-1CEST,M3.5.0,M10.5.0/3"
- // https://raw.githubusercontent.com/nayarsystems/posix_tz_db/master/zones.csv
- oldMillis = millis();
- pinMode(BuiltInLed, OUTPUT);
- digitalWrite(BuiltInLed, LOW); // ON after Wifi connection so I set it OFF
- pinMode(button0Pin, INPUT_PULLDOWN); // GPIO 0
- attachInterrupt(digitalPinToInterrupt(button0Pin), onButton0Event, RISING);
- }
- void loop() {
- String sString;
- newMillis = millis();
- if (bButton0Pressed) {
- nIndex = nIndex < 3 ? ++nIndex : 0;
- bButton0Pressed = false;
- detachInterrupt(digitalPinToInterrupt(button0Pin));
- WiFi_Init(nIndex);
- attachInterrupt(digitalPinToInterrupt(button0Pin), onButton0Event, RISING);
- }
- if (newMillis > oldMillis + tempo) {
- oldMillis = newMillis;
- sString = readLocalTime();
- DisplayIMessage(" " + sString.substring(0, 8), true, true);
- DisplayIMessage(" " + sString.substring(9, 19), false, true);
- }
- }
- String readLocalTime() {
- struct tm timeinfo;
- if (getLocalTime(&timeinfo)) {
- strftime(dtDateTime, sizeof(dtDateTime), "%H:%M.%S %d/%m/%Y", &timeinfo);
- }
- return dtDateTime;
- }
- int WiFi_Init(int nIndex) {
- String SSID_NAME = WifiID[nIndex][0];
- DisplayIMessage("Connecting to", true, true);
- DisplayIMessage(SSID_NAME, false, true );
- WiFi.setHostname(HOSTNAME);
- WiFi.begin(SSID_NAME, WifiID[nIndex][1]); // Password
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- DisplayIMessage(".", false, false);
- if (millis() > newMillis + 5000) return -2;
- }
- return 0;
- }
- void Display_Init() {
- lcd.init(); // I2C LCD init command
- bLCDDisplay = true;
- lcd.backlight(); // I2C LCD turn backlight on
- lcd.setCursor(4, 0); lcd.print(HOSTNAME);
- delay(2000);
- lcd.clear();
- }
- void DisplayIMessage(String sMessage, bool bClear, bool bCRLN) {
- static int nRow = 0;
- if (bLCDDisplay) {
- if (bClear) {
- lcd.clear();
- lcd.setCursor(0, nRow=0);
- }
- if (bCRLN) {
- lcd.setCursor(0, nRow++);
- lcd.print(sMessage);
- }
- else
- lcd.print(sMessage);
- }
- else
- Serial.println(sMessage);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement