Advertisement
DuboisP

esp32_wifi_multibox_ntp_date_hour

Nov 27th, 2024 (edited)
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | Source Code | 0 0
  1. /*********
  2.  
  3.    target :    ESP32 Dev Module
  4.                ESP32 pinout
  5.                LCD1602 display on I2C
  6.                one button
  7.    
  8.    author :    Patrick Dubois
  9.    license:    public domain
  10.                  
  11. *********/
  12.  
  13. // Import required libraries
  14. // if the library is global   use <>
  15. // if the library is local    use ""
  16.  
  17. #include <LiquidCrystal_I2C.h>
  18. #include <time.h>
  19. #include <WiFi.h>
  20. #include <Wire.h>
  21.  
  22. #define button0Pin        0      // GPIO 0 or PRG button
  23. #define BuiltInLed        2      // GPIO led allumée par erreur dans la boucle Wifi not connected ?
  24. #define tempo          1000      // 1000
  25. #define baudrateSerial  115200   // vitesse communication Arduino - PC  
  26.  
  27. #define SCL_GPIO  32  
  28. #define SDA_GPIO  33
  29. #define numCols   16             // for lcd
  30. #define numRows    2
  31. LiquidCrystal_I2C lcd(0x27, numCols, numRows);    // use this for I2C LCD.
  32.  
  33. #define ntpServer "europe.pool.ntp.org"
  34. char dtDateTime[20];
  35.  
  36. #define HOSTNAME  "My_Clock"
  37.  
  38. String WifiID[4][2]= {{ "FreeBox",  "password1"},
  39.                     {  "OrangeBox", "password2"},
  40.                     {  "SFRBox",    "password3"},
  41.                     {  "BouyguesBox", "password4"}};
  42.  
  43. bool bLCDDisplay = false;
  44. bool bButton0Pressed = false;
  45. int nIndex = 0;
  46. uint32_t oldMillis, newMillis;
  47.  
  48. // function prototype declarations
  49. void Display_Init(void);
  50. void DisplayIMessage(String sMessage, bool bClear, bool bCRLN);
  51. String readLocalTime(void);
  52. int WiFi_Init(void);
  53. // end declarations
  54.  
  55. void IRAM_ATTR onButton0Event() {
  56.    bButton0Pressed = true;
  57. }
  58.  
  59. void setup() {
  60.    btStop();
  61.    Wire.begin(SDA_GPIO, SCL_GPIO);  
  62.    Serial.begin(baudrateSerial);    // Serial0
  63.    Display_Init();
  64.    WiFi_Init(nIndex);
  65.    configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", ntpServer);   // "Europe/Paris","CET-1CEST,M3.5.0,M10.5.0/3"  
  66.                                                             // https://raw.githubusercontent.com/nayarsystems/posix_tz_db/master/zones.csv
  67.    oldMillis = millis();
  68.    pinMode(BuiltInLed, OUTPUT);    
  69.    digitalWrite(BuiltInLed, LOW);                           // ON after Wifi connection so I set it OFF
  70.    pinMode(button0Pin, INPUT_PULLDOWN);                     // GPIO 0
  71.    attachInterrupt(digitalPinToInterrupt(button0Pin), onButton0Event, RISING);  
  72. }
  73.  
  74. void loop() {
  75.    String sString;
  76.    newMillis = millis();
  77.     if (bButton0Pressed) {
  78.         nIndex = nIndex < 3 ? ++nIndex : 0;
  79.         bButton0Pressed = false;
  80.         detachInterrupt(digitalPinToInterrupt(button0Pin));
  81.         WiFi_Init(nIndex);
  82.         attachInterrupt(digitalPinToInterrupt(button0Pin), onButton0Event, RISING);
  83.     }
  84.    if (newMillis > oldMillis + tempo) {
  85.       oldMillis = newMillis;
  86.       sString = readLocalTime();
  87.       DisplayIMessage("    " + sString.substring(0, 8), true, true);
  88.       DisplayIMessage("   " + sString.substring(9, 19), false, true);
  89.    }  
  90. }
  91.  
  92. String readLocalTime() {
  93.    struct tm timeinfo;
  94.    if (getLocalTime(&timeinfo)) {
  95.       strftime(dtDateTime, sizeof(dtDateTime), "%H:%M.%S %d/%m/%Y", &timeinfo);
  96.    }
  97.    return dtDateTime;
  98. }
  99.  
  100. int WiFi_Init(int nIndex) {
  101.    String SSID_NAME = WifiID[nIndex][0];
  102.    DisplayIMessage("Connecting to", true, true);
  103.    DisplayIMessage(SSID_NAME, false, true );
  104.    WiFi.setHostname(HOSTNAME);
  105.    WiFi.begin(SSID_NAME, WifiID[nIndex][1]);    // Password
  106.    while (WiFi.status() != WL_CONNECTED) {
  107.       delay(500);
  108.       DisplayIMessage(".", false, false);
  109.       if (millis() > newMillis + 5000) return -2;
  110.    }
  111.     return 0;
  112. }
  113.  
  114. void Display_Init() {
  115.    lcd.init();                      // I2C LCD init command
  116.    bLCDDisplay = true;
  117.    lcd.backlight();                 // I2C LCD turn backlight on
  118.    lcd.setCursor(4, 0); lcd.print(HOSTNAME);  
  119.    delay(2000);
  120.    lcd.clear();
  121. }
  122.  
  123. void DisplayIMessage(String sMessage, bool bClear, bool bCRLN) {
  124.    static int nRow = 0;
  125.  
  126.    if (bLCDDisplay) {
  127.       if (bClear) {
  128.          lcd.clear();
  129.          lcd.setCursor(0, nRow=0);
  130.       }
  131.       if (bCRLN) {
  132.          lcd.setCursor(0, nRow++);
  133.          lcd.print(sMessage);
  134.       }
  135.       else
  136.          lcd.print(sMessage);
  137.    }
  138.    else
  139.       Serial.println(sMessage);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement