From 45749ad279f211fb495112b4115aa90fb283236b Mon Sep 17 00:00:00 2001 From: H3adcra5h Date: Wed, 3 Feb 2021 16:08:18 +0100 Subject: [PATCH 1/5] add static ip confuration --- PlatformIO/load_settings.py | 14 +++++++++++--- PlatformIO/src/General.hpp | 3 ++- PlatformIO/src/StateMachine.hpp | 27 ++++++++++++++++++++++----- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/PlatformIO/load_settings.py b/PlatformIO/load_settings.py index 8a555e9..e97220d 100644 --- a/PlatformIO/load_settings.py +++ b/PlatformIO/load_settings.py @@ -16,7 +16,7 @@ otaPasswordHash = hashlib.md5(config[sectionOta]['password'].encode()).hexdigest() - env.Append(CPPDEFINES=[ + cpp_defines=[ ("WIFI_SSID", "\\\"" + config[sectionWifi]["ssid"] + "\\\""), ("WIFI_PASS", "\\\"" + config[sectionWifi]["password"] + "\\\""), ("OTA_PASS_HASH", "\\\"" + otaPasswordHash + "\\\""), @@ -29,7 +29,15 @@ ("MQTT_MAX_PACKET_SIZE", 2000), ("CONFIG_ASYNC_TCP_RUNNING_CORE", 1), ("DEVICE_TYPE", config[sectionGeneral]["device_type"]) - ]) + ] + + if ("ip" in config[sectionWifi] and "gateway" in config[sectionWifi] and "subnet" in config[sectionWifi] and "dns" in config[sectionWifi]) : + cpp_defines.append(("HOST_IP", "\\\"" + config[sectionWifi]["ip"] + "\\\"")) + cpp_defines.append(("HOST_GATEWAY", "\\\"" + config[sectionWifi]["gateway"] + "\\\"")) + cpp_defines.append(("HOST_SUBNET", "\\\"" + config[sectionWifi]["subnet"] + "\\\"")) + cpp_defines.append(("HOST_DNS", "\\\"" + config[sectionWifi]["dns"] + "\\\"")) + + env.Append(CPPDEFINES=cpp_defines) if (config[sectionOta]["method"] == "upload") : env.Replace( @@ -45,7 +53,7 @@ if (config[sectionOta]["method"] == "ota") : env.Replace( UPLOAD_PROTOCOL="espota", - UPLOAD_PORT=config[sectionGeneral]["hostname"], + UPLOAD_PORT=config[sectionWifi]["ip"] if "ip" in config[sectionWifi] else config[sectionGeneral]["hostname"], UPLOAD_FLAGS=[ "--port=" + config[sectionOta]["port"], "--auth=" + config[sectionOta]["password"], diff --git a/PlatformIO/src/General.hpp b/PlatformIO/src/General.hpp index ce38866..d6a42eb 100644 --- a/PlatformIO/src/General.hpp +++ b/PlatformIO/src/General.hpp @@ -342,7 +342,8 @@ void loadConfiguration(const char *filename, Config &config) { Serial.println(F("Failed to read file, using default configuration")); config.mqtt_valid = config.mqtt_host.fromString(MQTT_IP); } else { - serializeJsonPretty(doc, Serial); + serializeJsonPretty(doc, Serial); + Serial.println(); config.mqtt_valid = config.mqtt_host.fromString(doc.getMember("mqtt_host").as()); config.mqtt_port = doc.getMember("mqtt_port").as(); config.mqtt_user = doc.getMember("mqtt_user").as(); diff --git a/PlatformIO/src/StateMachine.hpp b/PlatformIO/src/StateMachine.hpp index e63eeea..e5504cc 100644 --- a/PlatformIO/src/StateMachine.hpp +++ b/PlatformIO/src/StateMachine.hpp @@ -113,7 +113,7 @@ class Idle : public StateMachine class MQTTConnected : public StateMachine { void entry(void) override { Serial.println("Enter MQTTConnected"); - Serial.printf("Connected as %s\n\r",SITEID); + Serial.printf("Connected as %s\r\n",SITEID); publishDebug("Connected to asynch MQTT!"); asyncClient.subscribe(playBytesTopic.c_str(), 0); asyncClient.subscribe(hotwordTopic.c_str(), 0); @@ -214,10 +214,27 @@ class WifiDisconnected : public StateMachine xEventGroupClearBits(audioGroup, PLAY); xTaskCreatePinnedToCore(I2Stask, "I2Stask", 30000, NULL, 3, NULL, 0); Serial.println("Enter WifiDisconnected"); - Serial.printf("Total heap: %d\n\r", ESP.getHeapSize()); - Serial.printf("Free heap: %d\n\r", ESP.getFreeHeap()); + Serial.printf("Total heap: %d\r\n", ESP.getHeapSize()); + Serial.printf("Free heap: %d\r\n", ESP.getFreeHeap()); device->updateBrightness(config.brightness); device->updateColors(COLORS_WIFI_DISCONNECTED); + + // Set static ip address + #if defined(HOST_IP) && defined(HOST_GATEWAY) && defined(HOST_SUBNET) && defined(HOST_DNS) + Serial.printf("Set static ip: %s, gateway: %s, subnet: %s, dns: %s\r\n", HOST_IP, HOST_GATEWAY, HOST_SUBNET, HOST_DNS); + IPAddress ip; + IPAddress gateway; + IPAddress subnet; + IPAddress dns; + + ip.fromString(HOST_IP); + gateway.fromString(HOST_GATEWAY); + subnet.fromString(HOST_SUBNET); + dns.fromString(HOST_DNS); + + WiFi.config(ip, gateway, subnet, dns); + #endif + WiFi.onEvent(WiFiEvent); WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASS); @@ -441,7 +458,7 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties audioData.pop(WaveData[k]); } XT_Wav_Class Message((const uint8_t *)WaveData); - Serial.printf("Samplerate: %d, Channels: %d, Format: %d, Bits per Sample: %d\n\r", (int)Message.SampleRate, (int)Message.NumChannels, (int)Message.Format, (int)Message.BitsPerSample); + Serial.printf("Samplerate: %d, Channels: %d, Format: %d, Bits per Sample: %d\r\n", (int)Message.SampleRate, (int)Message.NumChannels, (int)Message.Format, (int)Message.BitsPerSample); sampleRate = (int)Message.SampleRate; numChannels = (int)Message.NumChannels; bitDepth = (int)Message.BitsPerSample; @@ -503,7 +520,7 @@ void I2Stask(void *p) { bytes_written = bytes_to_write; } if (bytes_written != bytes_to_write) { - Serial.printf("Bytes to write %d, but bytes written %d\n\r",bytes_to_write,bytes_written); + Serial.printf("Bytes to write %d, but bytes written %d\r\n",bytes_to_write,bytes_written); } } } From c592505e3ebdc1c509ea3a669fc6bf10f0c6d66d Mon Sep 17 00:00:00 2001 From: H3adcra5h Date: Wed, 3 Feb 2021 16:20:00 +0100 Subject: [PATCH 2/5] add sample for static ip address --- PlatformIO/settings.ini.example | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/PlatformIO/settings.ini.example b/PlatformIO/settings.ini.example index 7f9a6bf..48bfc1b 100644 --- a/PlatformIO/settings.ini.example +++ b/PlatformIO/settings.ini.example @@ -9,6 +9,12 @@ device_type=0 ssid=SSID password=password +;uncomment all 4 lines and fill with your own values if you want to use a static ip address +;ip=192.168.xxx.xxx +;gateway=192.168.xxx.xxx +;subnet=255.255.255.0 +;dns=192.168.xxx.xxx + [OTA] ;supported: upload, ota, matrix ;-upload: device should be attached to computer via usb From 404f610e8331d63e4a0f6fb2aa8005dbebb8eb60 Mon Sep 17 00:00:00 2001 From: H3adcra5h Date: Fri, 5 Feb 2021 10:39:17 +0100 Subject: [PATCH 3/5] add secondary dns server --- PlatformIO/load_settings.py | 7 +++++-- PlatformIO/settings.ini.example | 6 ++++-- PlatformIO/src/StateMachine.hpp | 15 ++++++++++----- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/PlatformIO/load_settings.py b/PlatformIO/load_settings.py index e97220d..c983249 100644 --- a/PlatformIO/load_settings.py +++ b/PlatformIO/load_settings.py @@ -31,11 +31,14 @@ ("DEVICE_TYPE", config[sectionGeneral]["device_type"]) ] - if ("ip" in config[sectionWifi] and "gateway" in config[sectionWifi] and "subnet" in config[sectionWifi] and "dns" in config[sectionWifi]) : + if ("ip" in config[sectionWifi] and "gateway" in config[sectionWifi] and "subnet" in config[sectionWifi] and "dns1" in config[sectionWifi]) : cpp_defines.append(("HOST_IP", "\\\"" + config[sectionWifi]["ip"] + "\\\"")) cpp_defines.append(("HOST_GATEWAY", "\\\"" + config[sectionWifi]["gateway"] + "\\\"")) cpp_defines.append(("HOST_SUBNET", "\\\"" + config[sectionWifi]["subnet"] + "\\\"")) - cpp_defines.append(("HOST_DNS", "\\\"" + config[sectionWifi]["dns"] + "\\\"")) + cpp_defines.append(("HOST_DNS1", "\\\"" + config[sectionWifi]["dns1"] + "\\\"")) + + if ("dns2" in config[sectionWifi]) : + cpp_defines.append(("HOST_DNS2", "\\\"" + config[sectionWifi]["dns2"] + "\\\"")) env.Append(CPPDEFINES=cpp_defines) diff --git a/PlatformIO/settings.ini.example b/PlatformIO/settings.ini.example index 48bfc1b..04a5ba6 100644 --- a/PlatformIO/settings.ini.example +++ b/PlatformIO/settings.ini.example @@ -9,11 +9,13 @@ device_type=0 ssid=SSID password=password -;uncomment all 4 lines and fill with your own values if you want to use a static ip address +;uncomment next 4 lines and fill with your own values if you want to use a static ip address ;ip=192.168.xxx.xxx ;gateway=192.168.xxx.xxx ;subnet=255.255.255.0 -;dns=192.168.xxx.xxx +;dns1=192.168.xxx.xxx +;optional: second dns server +;dns2=192.168.xxx.xxx [OTA] ;supported: upload, ota, matrix diff --git a/PlatformIO/src/StateMachine.hpp b/PlatformIO/src/StateMachine.hpp index e5504cc..2fc4093 100644 --- a/PlatformIO/src/StateMachine.hpp +++ b/PlatformIO/src/StateMachine.hpp @@ -220,19 +220,24 @@ class WifiDisconnected : public StateMachine device->updateColors(COLORS_WIFI_DISCONNECTED); // Set static ip address - #if defined(HOST_IP) && defined(HOST_GATEWAY) && defined(HOST_SUBNET) && defined(HOST_DNS) - Serial.printf("Set static ip: %s, gateway: %s, subnet: %s, dns: %s\r\n", HOST_IP, HOST_GATEWAY, HOST_SUBNET, HOST_DNS); + #if defined(HOST_IP) && defined(HOST_GATEWAY) && defined(HOST_SUBNET) && defined(HOST_DNS1) IPAddress ip; IPAddress gateway; IPAddress subnet; - IPAddress dns; + IPAddress dns1; + IPAddress dns2; ip.fromString(HOST_IP); gateway.fromString(HOST_GATEWAY); subnet.fromString(HOST_SUBNET); - dns.fromString(HOST_DNS); + dns1.fromString(HOST_DNS1); - WiFi.config(ip, gateway, subnet, dns); + #ifdef HOST_DNS2 + dns2.fromString(HOST_DNS2); + #endif + + Serial.printf("Set static ip: %s, gateway: %s, subnet: %s, dns1: %s, dns2: %s\r\n", ip.toString().c_str(), gateway.toString().c_str(), subnet.toString().c_str(), dns1.toString().c_str(), dns2.toString().c_str()); + WiFi.config(ip, gateway, subnet, dns1, dns2); #endif WiFi.onEvent(WiFiEvent); From 95aa9373021cf1c46570fad13a442e014192ba7d Mon Sep 17 00:00:00 2001 From: H3adcra5h Date: Fri, 5 Feb 2021 12:26:15 +0100 Subject: [PATCH 4/5] only use config[sectionWifi]["ip"] for OTA if all 4 necessary parts enabled --- PlatformIO/load_settings.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/PlatformIO/load_settings.py b/PlatformIO/load_settings.py index c983249..3971d6f 100644 --- a/PlatformIO/load_settings.py +++ b/PlatformIO/load_settings.py @@ -9,6 +9,7 @@ sectionWifi = "Wifi" sectionOta = "OTA" sectionMqtt = "MQTT" +bool staticIp = False if os.path.isfile(settings): config = configparser.RawConfigParser() @@ -40,6 +41,8 @@ if ("dns2" in config[sectionWifi]) : cpp_defines.append(("HOST_DNS2", "\\\"" + config[sectionWifi]["dns2"] + "\\\"")) + staticIp = True + env.Append(CPPDEFINES=cpp_defines) if (config[sectionOta]["method"] == "upload") : @@ -56,7 +59,7 @@ if (config[sectionOta]["method"] == "ota") : env.Replace( UPLOAD_PROTOCOL="espota", - UPLOAD_PORT=config[sectionWifi]["ip"] if "ip" in config[sectionWifi] else config[sectionGeneral]["hostname"], + UPLOAD_PORT=config[sectionWifi]["ip"] if staticIp else config[sectionGeneral]["hostname"], UPLOAD_FLAGS=[ "--port=" + config[sectionOta]["port"], "--auth=" + config[sectionOta]["password"], From 6133373c5638f7c00cc1b5982d207ea20aee2aee Mon Sep 17 00:00:00 2001 From: H3adcra5h Date: Fri, 5 Feb 2021 12:37:25 +0100 Subject: [PATCH 5/5] mistake: too much programming languages at the same time --- PlatformIO/load_settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PlatformIO/load_settings.py b/PlatformIO/load_settings.py index 3971d6f..f906e98 100644 --- a/PlatformIO/load_settings.py +++ b/PlatformIO/load_settings.py @@ -9,7 +9,7 @@ sectionWifi = "Wifi" sectionOta = "OTA" sectionMqtt = "MQTT" -bool staticIp = False +staticIp = False if os.path.isfile(settings): config = configparser.RawConfigParser()