Skip to content

Commit

Permalink
Merge pull request Romkabouter#37 from H3adcra5h/add_static_ip_settings
Browse files Browse the repository at this point in the history
Add static ip settings
  • Loading branch information
Romkabouter authored Feb 5, 2021
2 parents 362b795 + 6133373 commit 3995440
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
20 changes: 17 additions & 3 deletions PlatformIO/load_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
sectionWifi = "Wifi"
sectionOta = "OTA"
sectionMqtt = "MQTT"
staticIp = False

if os.path.isfile(settings):
config = configparser.RawConfigParser()
config.read(settings)

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 + "\\\""),
Expand All @@ -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(
Expand All @@ -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"],
Expand Down
8 changes: 8 additions & 0 deletions PlatformIO/settings.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion PlatformIO/src/General.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*>());
config.mqtt_port = doc.getMember("mqtt_port").as<int>();
config.mqtt_user = doc.getMember("mqtt_user").as<std::string>();
Expand Down
32 changes: 27 additions & 5 deletions PlatformIO/src/StateMachine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 3995440

Please sign in to comment.