Skip to content

Commit

Permalink
Cleanup lint, add tests, resolve 8266 builds
Browse files Browse the repository at this point in the history
  • Loading branch information
h2zero committed May 23, 2021
1 parent 5e08a6c commit d7426c1
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 334 deletions.
13 changes: 13 additions & 0 deletions docs/use/gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ If you want the settings to be kept upon gateway restart, you can publish the co
Auto discovery is enable by default on release binaries, on platformio (except for UNO). With Arduino IDE please read the [advanced configuration section](../upload/advanced-configuration#auto-discovery) of the documentation.
:::

# Firmware update from MQTT (ESP only)

The gateway can be updated through an MQTT message by providing a JSON formatted message with a version number, OTA password (optional, see below), and URL to fetch the update from.

To enable this functionality, `MQTT_HTTPS_FW_UPDATE` will need to be defined or the line that defines in in user_config.h will need to be uncommented.

::: tip
If using an unsecure MQTT broker it is **highly recommended** to disable the password checking by setting the macro `MQTT_HTTPS_FW_UPDATE_USE_PASSWORD` to 0 (default is 1 (enabled)), otherwise a clear text password may be sent over the network.
:::

### Example firmware update message:
`mosquitto_pub -t firmware_update -m "{\"version\":\"version_tag\",\"password\":\"OTAPASSWORD\",\"url\":\"https://github.com/1technophile/OpenMQTTGateway/releases/download/v0.9.6/esp32-m5stack-ble-firmware.bin\"}"`

# State LED usage

The gateway can support up to 3 LED to display its operating state:
Expand Down
21 changes: 16 additions & 5 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,8 @@ void receivingMQTT(char* topicOri, char* datacallback) {
# endif
void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) {
if (strstr(topicOri, subjectFWUpdate) != NULL) {
if (HttpsFwUpdateData["version"] && strcmp(HttpsFwUpdateData["version"], OMG_VERSION) != 0) {
const char* version = HttpsFwUpdateData["version"];
if (version && ((strlen(version) != strlen(OMG_VERSION)) || strcmp(version, OMG_VERSION) != 0)) {
const char* url = HttpsFwUpdateData["url"];
if (url) {
if (!strstr((url + (strlen(url) - 5)), ".bin")) {
Expand Down Expand Up @@ -1664,16 +1665,22 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) {
# endif

Log.warning(F("Starting firmware update" CR));

# if defined(ZgatewayBT) && defined(ESP32)
stopProcessing();
# endif

t_httpUpdate_return result = HTTP_UPDATE_FAILED;
if (strstr(url, "http:")) {
WiFiClient update_client;
# ifdef ESP32
httpUpdate.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
result = httpUpdate.update(update_client, url);
# elif ESP8266
ESPhttpUpdate.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
result = ESPhttpUpdate.update(update_client, url);
# endif

} else {
WiFiClientSecure update_client;
# ifdef SECURE_CONNECTION
Expand All @@ -1698,7 +1705,7 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) {
}
# endif
# ifdef ESP32
if (strstr(url, "github") !=0) {
if (strstr(url, "github") != 0) {
update_client.setCACert(_github_cert);
} else {
update_client.setCACert(https_fw_server_cert);
Expand All @@ -1707,7 +1714,7 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) {
httpUpdate.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
result = httpUpdate.update(update_client, url);
# elif ESP8266
update_client.setInsecure(); // TODO: remove, temporary
update_client.setInsecure(); // TODO: replace with cert checking
update_client.setTimeout(12000);
ESPhttpUpdate.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
result = ESPhttpUpdate.update(update_client, url);
Expand All @@ -1716,7 +1723,11 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) {

switch (result) {
case HTTP_UPDATE_FAILED:
# ifdef ESP32
Log.error(F("HTTP_UPDATE_FAILED Error (%d): %s\n" CR), httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
# elif ESP8266
Log.error(F("HTTP_UPDATE_FAILED Error (%d): %s\n" CR), ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
# endif
break;

case HTTP_UPDATE_NO_UPDATES:
Expand Down
Loading

0 comments on commit d7426c1

Please sign in to comment.