diff --git a/PlatformIO/load_settings.py b/PlatformIO/load_settings.py index 8a555e9..f906e98 100644 --- a/PlatformIO/load_settings.py +++ b/PlatformIO/load_settings.py @@ -9,6 +9,7 @@ sectionWifi = "Wifi" sectionOta = "OTA" sectionMqtt = "MQTT" +staticIp = False if os.path.isfile(settings): config = configparser.RawConfigParser() @@ -16,7 +17,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 +30,20 @@ ("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 "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_DNS1", "\\\"" + config[sectionWifi]["dns1"] + "\\\"")) + + 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") : env.Replace( @@ -45,7 +59,7 @@ if (config[sectionOta]["method"] == "ota") : env.Replace( UPLOAD_PROTOCOL="espota", - UPLOAD_PORT=config[sectionGeneral]["hostname"], + UPLOAD_PORT=config[sectionWifi]["ip"] if staticIp else config[sectionGeneral]["hostname"], UPLOAD_FLAGS=[ "--port=" + config[sectionOta]["port"], "--auth=" + config[sectionOta]["password"], diff --git a/PlatformIO/settings.ini.example b/PlatformIO/settings.ini.example index 7f9a6bf..04a5ba6 100644 --- a/PlatformIO/settings.ini.example +++ b/PlatformIO/settings.ini.example @@ -9,6 +9,14 @@ device_type=0 ssid=SSID password=password +;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 +;dns1=192.168.xxx.xxx +;optional: second dns server +;dns2=192.168.xxx.xxx + [OTA] ;supported: upload, ota, matrix ;-upload: device should be attached to computer via usb 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..2fc4093 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,32 @@ 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_DNS1) + IPAddress ip; + IPAddress gateway; + IPAddress subnet; + IPAddress dns1; + IPAddress dns2; + + ip.fromString(HOST_IP); + gateway.fromString(HOST_GATEWAY); + subnet.fromString(HOST_SUBNET); + dns1.fromString(HOST_DNS1); + + #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); WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASS); @@ -441,7 +463,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 +525,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); } } }