From 681f99d2f4f913a144929cb3b5a23ef8575b0a81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Vigan=C3=B2?= Date: Wed, 8 Dec 2021 22:27:42 +0100 Subject: [PATCH] Allow to use a customer NTP server IP (#27) * Add support for custom IP/Port for NTP on ESP32 * Backport changes from esp32 * NTP port is not currently used * Make pytz dep explicit and update alpine --- arduino/eeprom_prog/eeprom_prog.ino | 6 +++-- arduino/eeprom_prog/settings.h.tmpl | 6 +++-- arduino/tempLightSensor/tempLightSensor.ino | 28 +++++++++++---------- docker/ntpd/Dockerfile | 2 +- esp32/nvs_prog/nvs_prog.ino | 6 +++-- esp32/nvs_prog/settings.h.tmpl | 6 +++-- esp32/rssiHall/rssiHall.ino | 22 +++++++++------- requirements.txt | 1 + 8 files changed, 46 insertions(+), 31 deletions(-) diff --git a/arduino/eeprom_prog/eeprom_prog.ino b/arduino/eeprom_prog/eeprom_prog.ino index 7010281..4bb263d 100644 --- a/arduino/eeprom_prog/eeprom_prog.ino +++ b/arduino/eeprom_prog/eeprom_prog.ino @@ -30,8 +30,10 @@ const byte mac[] = { const char serial[] = SERIAL; struct netConfig { - IPAddress address = REMOTE_IP; - unsigned int port = REMOTE_PORT; + IPAddress iot_address = IOT_IP; + unsigned int iot_port = IOT_PORT; + IPAddress ntp_address = NTP_IP; + unsigned int ntp_port = NTP_PORT; } config; void setup() { diff --git a/arduino/eeprom_prog/settings.h.tmpl b/arduino/eeprom_prog/settings.h.tmpl index 830e0d6..4bd3505 100644 --- a/arduino/eeprom_prog/settings.h.tmpl +++ b/arduino/eeprom_prog/settings.h.tmpl @@ -19,5 +19,7 @@ */ #define SERIAL "uno_1" -#define REMOTE_IP {192, 168, 0, 1} -#define REMOTE_PORT 80 +#define IOT_IP {192, 168, 10, 1} +#define IOT_PORT 80 +#define NTP_IP {192, 168, 10, 1} +#define NTP_PORT 123 diff --git a/arduino/tempLightSensor/tempLightSensor.ino b/arduino/tempLightSensor/tempLightSensor.ino index 9f72695..5891ca1 100644 --- a/arduino/tempLightSensor/tempLightSensor.ino +++ b/arduino/tempLightSensor/tempLightSensor.ino @@ -31,8 +31,6 @@ #define TELEMETRY_DELAY 10 // second between telemetry samples #define AREF_VOLTAGE 3.3 // set aref voltage to 3.3v instead of default 5v -char serial[9]; - // const String serverName = "sensor.server.domain"; const size_t capacity = 2 * JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(2) + 20; @@ -50,11 +48,13 @@ EthernetClient ethClient; PubSubClient clientMQTT(ethClient); struct netConfig { - IPAddress address; - unsigned int port; -}; -netConfig config; + IPAddress iot_address; + unsigned int iot_port; + IPAddress ntp_address; + unsigned int ntp_port; // not used +} config; +char serial[9]; const String apiURL = "/api/device/subscribe/"; const String telemetryURL = "/telemetry/"; @@ -89,15 +89,17 @@ void setup(void) { Serial.println(Ethernet.localIP()); Serial.println(); Serial.print("Connecting to: "); - Serial.print(config.address); + Serial.print(config.iot_address); Serial.print(":"); - Serial.print(config.port); + Serial.print(config.iot_port); Serial.print(" every "); Serial.print(TELEMETRY_DELAY); Serial.println("s"); #if USE_INTERNAL_NTP - timeClient.setPoolServerIP(config.address); + Serial.print("Using NTP: "); + Serial.println(config.ntp_address); + timeClient.setPoolServerIP(config.ntp_address); #endif timeClient.begin(); if (timeClient.update()) { @@ -115,7 +117,7 @@ void setup(void) { // payload["id"] = serverName; #if USE_MQTT - clientMQTT.setServer(config.address, 1883); + clientMQTT.setServer(config.iot_address, 1883); #endif } @@ -175,14 +177,14 @@ void publishData(const netConfig &mqtt, const DynamicJsonDocument &json) { #endif void postData(const netConfig &postAPI, const String &URL, const DynamicJsonDocument &json) { - if (ethClient.connect(postAPI.address, postAPI.port)) { + if (ethClient.connect(postAPI.iot_address, postAPI.iot_port)) { ethClient.print("POST "); ethClient.print(URL); ethClient.println(" HTTP/1.1"); ethClient.print("Host: "); - ethClient.print(postAPI.address); + ethClient.print(postAPI.iot_address); ethClient.print(":"); - ethClient.println(postAPI.port); + ethClient.println(postAPI.iot_port); ethClient.println("Content-Type: application/json"); ethClient.print("Content-Length: "); ethClient.println(measureJson(json)); diff --git a/docker/ntpd/Dockerfile b/docker/ntpd/Dockerfile index b79c42d..6b50ff6 100644 --- a/docker/ntpd/Dockerfile +++ b/docker/ntpd/Dockerfile @@ -17,7 +17,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -FROM alpine:3.12 +FROM alpine:3.15 RUN apk update && apk add chrony && \ chown -R chrony:chrony /var/lib/chrony diff --git a/esp32/nvs_prog/nvs_prog.ino b/esp32/nvs_prog/nvs_prog.ino index cb05d1f..7e20b68 100644 --- a/esp32/nvs_prog/nvs_prog.ino +++ b/esp32/nvs_prog/nvs_prog.ino @@ -31,8 +31,10 @@ const char* ssid = SECRET_SSID; const char* password = SECRET_PASSWORD; struct netConfig { - IPAddress address = REMOTE_IP; - unsigned int port = REMOTE_PORT; + IPAddress iot_address = IOT_IP; + unsigned int iot_port = IOT_PORT; + IPAddress ntp_address = NTP_IP; + unsigned int ntp_port = NTP_PORT; } config; void setup() { diff --git a/esp32/nvs_prog/settings.h.tmpl b/esp32/nvs_prog/settings.h.tmpl index b260549..6f45458 100644 --- a/esp32/nvs_prog/settings.h.tmpl +++ b/esp32/nvs_prog/settings.h.tmpl @@ -19,7 +19,9 @@ */ #define SERIAL "" -#define REMOTE_IP {192, 168, 0, 1} -#define REMOTE_PORT 80 +#define IOT_IP {192, 168, 10, 1} +#define IOT_PORT 80 +#define NTP_IP {192, 168, 10, 1} +#define NTP_PORT 123 #define SECRET_SSID "" #define SECRET_PASSWORD "" diff --git a/esp32/rssiHall/rssiHall.ino b/esp32/rssiHall/rssiHall.ino index e18413f..8059ddb 100644 --- a/esp32/rssiHall/rssiHall.ino +++ b/esp32/rssiHall/rssiHall.ino @@ -48,8 +48,10 @@ WiFiClient ethClient; PubSubClient clientMQTT(ethClient); struct netConfig { - IPAddress address; - unsigned int port; + IPAddress iot_address; + unsigned int iot_port; + IPAddress ntp_address; + unsigned int ntp_port; // not used } config; char* serial; @@ -94,15 +96,17 @@ void setup(void) { Serial.println(WiFi.localIP()); Serial.println(); Serial.print("Connecting to: "); - Serial.print(config.address.toString()); + Serial.print(config.iot_address.toString()); Serial.print(":"); - Serial.print(config.port); + Serial.print(config.iot_port); Serial.print(" every "); Serial.print(TELEMETRY_DELAY); Serial.println("s"); #if USE_INTERNAL_NTP - timeClient.setPoolServerIP(config.address); + Serial.print("Using NTP: "); + Serial.println(config.ntp_address.toString()); + timeClient.setPoolServerIP(config.ntp_address); #endif timeClient.begin(); if (timeClient.update()) { @@ -120,7 +124,7 @@ void setup(void) { // payload["id"] = serverName; #if USE_MQTT - clientMQTT.setServer(config.address, 1883); + clientMQTT.setServer(config.iot_address, 1883); #endif } @@ -172,14 +176,14 @@ void publishData(const netConfig &mqtt, const DynamicJsonDocument &json) { #endif void postData(const netConfig &postAPI, const String &URL, const DynamicJsonDocument &json) { - if (ethClient.connect(postAPI.address, postAPI.port)) { + if (ethClient.connect(postAPI.iot_address, postAPI.iot_port)) { ethClient.print("POST "); ethClient.print(URL); ethClient.println(" HTTP/1.1"); ethClient.print("Host: "); - ethClient.print(postAPI.address.toString()); + ethClient.print(postAPI.iot_address.toString()); ethClient.print(":"); - ethClient.println(postAPI.port); + ethClient.println(postAPI.iot_port); ethClient.println("Content-Type: application/json"); ethClient.print("Content-Length: "); ethClient.println(measureJson(json)); diff --git a/requirements.txt b/requirements.txt index 0de5e34..27023fb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +pytz Django djangorestframework django-health-check