From 59bc3e80ae9372638386054b5ec953b0476b8601 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Wed, 24 Jan 2018 15:54:48 +0100 Subject: [PATCH] finish startup code --- platformio.ini | 1 + src/main.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/platformio.ini b/platformio.ini index 7f78e32..66c171c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -21,4 +21,5 @@ lib_deps = ; Basecamp https://github.com/merlinschumacher/Basecamp.git u8g2 + NTPClient lib_ignore = ESPAsyncTCP diff --git a/src/main.cpp b/src/main.cpp index 5772f51..737b72a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ //Define DEBUG to get the Output from DEBUG_PRINTLN #define DEBUG 1 +#include + //Include Basecamp in this sketch #include #include @@ -13,6 +15,9 @@ #include #endif +#include +#include + #include "main.h" U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16); @@ -20,6 +25,10 @@ U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 15, /* data=*/ 4, //Create a new Basecamp instance called iot Basecamp iot; +WiFiUDP ntpUDP; +NTPClient timeClient(ntpUDP, "de.pool.ntp.org", 3600, 60000); +char timeStr[20]; + //Variables for the sensor and the battery static const int ResetPin = 35; static const int SensorPin = 32; @@ -29,7 +38,7 @@ int sensorValue = 0; int batteryLimit = 3300; //This is used to control if the ESP should enter sleep mode or not -bool delaySleep = false; +bool delaySleep = true; //Variables for the mqtt packages and topics uint16_t statusPacketIdSub = 0; @@ -63,13 +72,6 @@ void setup() { resetToFactoryDefaults(); } - u8g2.begin(); - delay(50); - u8g2.clearBuffer(); // clear the internal memory - u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font - u8g2.drawStr(0,10,"Hello World!"); // write something to the internal memory - u8g2.sendBuffer(); // transfer internal memory to the display - //Initialize Basecamp iot.begin(); @@ -77,6 +79,22 @@ void setup() { DEBUG_PRINTLN("**** CONFIG HAS BEEN MANUALLY RESET ****"); } + u8g2.begin(); + delay(50); + u8g2.clearBuffer(); // clear the internal memory + u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font + u8g2.drawStr(2,10,"esp32-node by hendrik"); // write something to the internal memory + if (iot.configuration.get("WifiConfigured") != "True") { + u8g2.drawStr(2,30,"NOT CONFIGURED!"); + u8g2.drawStr(12,40,"SSID: \"ESP32\""); + u8g2.drawStr(12,55,"http://192.168.4.1"); + } else { + u8g2.drawStr(2,30,"Welcome!"); + u8g2.drawStr(12,40,"connecting.."); + } + u8g2.drawFrame(0,0,u8g2.getDisplayWidth(),u8g2.getDisplayHeight() ); + u8g2.sendBuffer(); // transfer internal memory to the display + //Configure the MQTT topics delaySleepTopic = "esp32-node/cmd/" + iot.hostname + "/delaysleep"; statusTopic = "esp32-node/stat/" + iot.hostname + "/status"; @@ -88,6 +106,24 @@ void setup() { iot.mqtt.onConnect(onMqttConnect); iot.mqtt.onPublish(suspendESP); iot.mqtt.onMessage(onMqttMessage); + + while (iot.wifi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + IPAddress localIP = iot.wifi.getIP(); + char ipStr[16]; + localIP.toString().toCharArray(ipStr, 16); + + timeClient.begin(); + timeClient.update(); + timeClient.getFormattedTime().toCharArray(timeStr, 50); + u8g2.clearBuffer(); // clear the internal memory + u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font + u8g2.drawStr(30, 30, timeStr); + u8g2.drawStr(30, 50, ipStr); + u8g2.sendBuffer(); } @@ -172,5 +208,10 @@ void suspendESP(uint16_t packetId) { void loop() { + timeClient.update(); + timeClient.getFormattedTime().toCharArray(timeStr, 50); + u8g2.drawStr(30, 30, timeStr); + u8g2.sendBuffer(); + delay(1000); }