Skip to content

Commit

Permalink
Added OTA update
Browse files Browse the repository at this point in the history
  • Loading branch information
rjdekker committed Jun 20, 2018
1 parent 68818f3 commit 90aeb8c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ In the Arduino IDE, select <i>Generic ESP8266 Module</i> under <i>Tools</i> > <i
I have used the following settings (running at 160 MHz is probably not necessary):<br>

![Arduino IDE settings](https://rjdekker.github.io/MHI2MQTT/docs/images/Arduino-IDE_ESP-01-settings.jpg)
<br>
After the sketch is flashed for the first time, future updates to the ESP-01 can also be uploaded OTA. Name and password are equal to those set for the configuration access point (Default: <i>MHI Roomname</i> with password <i>mitsubishi</i>). The Arduino Mini Pro cannot be updated OTA.

### Connecting and configuring the system
* Disconnect mains
Expand Down
25 changes: 23 additions & 2 deletions src/MHI-ESP2MQTT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ R.J. Dekker, June 2018
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include <PubSubClient.h> //https://github.com/knolleary/pubsubclient
#include <EasyTransfer.h> //https://github.com/madsci1016/Arduino-EasyTransfer

//Access point that WiFiManager starts for configuration. Name and password should be set below before flashing. This is hardcoded and cannot be changed later.
#define configSSID "MHI Roomname" //AP name (give every unit a different before flashing)
#define configSSID "MHI Roomname" //AP name (give every unit a unique name before flashing)
#define configPW "mitsubishi" //Password to connect to the AP

//Variables below are initial values that can be changed at any time from the WiFiManager configuration portal and will be stored in flash memory. If there are different values in config.json, they are overwritten.
char mqtt_server[16] = "192.168.2.50";
char mqtt_server[16] = "0.0.0.0";
char mqtt_port[9] = "1883";
char mqtt_user[20] = "";
char mqtt_pass[20] = "";
Expand Down Expand Up @@ -339,8 +341,25 @@ void setup()
//Serial.print("Local IP adres: ");
//Serial.println(WiFi.localIP());

//Connect to MQTT broker and set callback
client.setServer(mqtt_server, atoi(mqtt_port));
client.setCallback(callback);

//Configure Arduino OTA updater
ArduinoOTA.setHostname(configSSID); //Set OTA hostname and password (same as local access point for WiFiManager)
ArduinoOTA.setPassword((const char *)configPW);
//ArduinoOTA.setPort(8266); //Port defaults to 8266

ArduinoOTA.onError([](ota_error_t error) //Send OTA error messages to MQTT debug topic
{
if (error == OTA_AUTH_ERROR) client.publish(debug, "<OTA> ERROR -> Auth failed", true);
else if (error == OTA_BEGIN_ERROR) client.publish(debug, "<OTA> ERROR -> Begin failed", true);
else if (error == OTA_CONNECT_ERROR) client.publish(debug, "<OTA> ERROR -> Connect failed", true);
else if (error == OTA_RECEIVE_ERROR) client.publish(debug, "<OTA> ERROR -> Receive failed", true);
else if (error == OTA_END_ERROR) client.publish(debug, "<OTA> ERROR -> End failed", true);
});

ArduinoOTA.begin();
}

void connect()
Expand Down Expand Up @@ -578,6 +597,8 @@ void callback(char* topic, byte* payload, unsigned int length)

void loop()
{
ArduinoOTA.handle(); //Handle Arduino OTA updates

if (!client.connected()) //Check MQTT connection
{
connect(); //Connect first time. Reconnect when connection lost.
Expand Down

0 comments on commit 90aeb8c

Please sign in to comment.