1
0
mirror of https://github.com/daniviga/bite.git synced 2025-04-20 14:31:20 +02:00

Add support for custom IP/Port for NTP on ESP32

This commit is contained in:
Daniele Viganò 2021-12-08 19:02:26 +01:00
parent 37c7c49614
commit 3c27dcc8ec
Signed by: dani
GPG Key ID: DB49AFC03C40EE02
3 changed files with 21 additions and 13 deletions

View File

@ -31,8 +31,10 @@ const char* ssid = SECRET_SSID;
const char* password = SECRET_PASSWORD; const char* password = SECRET_PASSWORD;
struct netConfig { struct netConfig {
IPAddress address = REMOTE_IP; IPAddress iot_address = IOT_IP;
unsigned int port = REMOTE_PORT; unsigned int iot_port = IOT_PORT;
IPAddress ntp_address = NTP_IP;
unsigned int ntp_port = NTP_PORT;
} config; } config;
void setup() { void setup() {

View File

@ -19,7 +19,9 @@
*/ */
#define SERIAL "<fillme>" #define SERIAL "<fillme>"
#define REMOTE_IP {192, 168, 0, 1} #define IOT_IP {192, 168, 10, 1}
#define REMOTE_PORT 80 #define IOT_PORT 80
#define NTP_IP {192, 168, 10, 1}
#define NTP_PORT 123
#define SECRET_SSID "<fillme>" #define SECRET_SSID "<fillme>"
#define SECRET_PASSWORD "<fillme>" #define SECRET_PASSWORD "<fillme>"

View File

@ -48,8 +48,10 @@ WiFiClient ethClient;
PubSubClient clientMQTT(ethClient); PubSubClient clientMQTT(ethClient);
struct netConfig { struct netConfig {
IPAddress address; IPAddress iot_address;
unsigned int port; unsigned int iot_port;
IPAddress ntp_address;
unsigned int ntp_port;
} config; } config;
char* serial; char* serial;
@ -94,15 +96,17 @@ void setup(void) {
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
Serial.println(); Serial.println();
Serial.print("Connecting to: "); Serial.print("Connecting to: ");
Serial.print(config.address.toString()); Serial.print(config.iot_address.toString());
Serial.print(":"); Serial.print(":");
Serial.print(config.port); Serial.print(config.iot_port);
Serial.print(" every "); Serial.print(" every ");
Serial.print(TELEMETRY_DELAY); Serial.print(TELEMETRY_DELAY);
Serial.println("s"); Serial.println("s");
#if USE_INTERNAL_NTP #if USE_INTERNAL_NTP
timeClient.setPoolServerIP(config.address); Serial.print("Using NTP: ");
Serial.println(config.ntp_address.toString());
timeClient.setPoolServerIP(config.ntp_address);
#endif #endif
timeClient.begin(); timeClient.begin();
if (timeClient.update()) { if (timeClient.update()) {
@ -120,7 +124,7 @@ void setup(void) {
// payload["id"] = serverName; // payload["id"] = serverName;
#if USE_MQTT #if USE_MQTT
clientMQTT.setServer(config.address, 1883); clientMQTT.setServer(config.iot_address, 1883);
#endif #endif
} }
@ -172,14 +176,14 @@ void publishData(const netConfig &mqtt, const DynamicJsonDocument &json) {
#endif #endif
void postData(const netConfig &postAPI, const String &URL, const DynamicJsonDocument &json) { 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("POST ");
ethClient.print(URL); ethClient.print(URL);
ethClient.println(" HTTP/1.1"); ethClient.println(" HTTP/1.1");
ethClient.print("Host: "); ethClient.print("Host: ");
ethClient.print(postAPI.address.toString()); ethClient.print(postAPI.iot_address.toString());
ethClient.print(":"); ethClient.print(":");
ethClient.println(postAPI.port); ethClient.println(postAPI.iot_port);
ethClient.println("Content-Type: application/json"); ethClient.println("Content-Type: application/json");
ethClient.print("Content-Length: "); ethClient.print("Content-Length: ");
ethClient.println(measureJson(json)); ethClient.println(measureJson(json));