Sorry for the long post.
I have a small sketch that publishes to a MQTT broker. However when I add the code to enable OTA updates some of the MQTT publishes fail.
The following sketch publishes a topic containing a count every 2 seconds. With the ArduinoOTA.handle(); commented out the program works properly, displaying the counter on Serial and publishing to MQTT. After adding the ArduinoOTA.handle() the program displays the counter on Serial correctly, but it displays the "Attempting MQTT connection...connected" message about every 14 times through the loop. In addition, only a few of the topics are received by the MQTT broker. For example, currently only counters 1,2,3,15,27, 174,332,333,1005,1159 have been received by the broker.
Any help would be appreciated.
#include <ESP8266WiFi.h> //For ESP8266
#include <PubSubClient.h> //For MQTT
#include <ESP8266mDNS.h> //For OTA
#include <WiFiUdp.h> //For OTA
#include <ArduinoOTA.h> //For OTA
// WIFI configuration
#define wifi_ssid "JimsWirelessLan"
#define wifi_password "My#Password"
// MQTT configuration
#define mqtt_server "192.168.0.102"
#define mqtt_user "MqttUserid"
#define mqtt_password "MQTT#Password"
String mqtt_client_id="OtaClient";
#define mqtt_temp_topic "home/mqttota/count"
// Start MQTT client
WiFiClient espClient;
PubSubClient mqtt_client(espClient);
// Necesary to make Arduino Software autodetect OTA device
//WiFiServer TelnetServer(8266);
void setup_wifi() {
delay(10);
Serial.print("Connecting to ");
Serial.print(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("OK");
Serial.print(" IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
Serial.println("\r\nSetting up....");
setup_wifi();
Serial.print("Configuring OTA device...");
// TelnetServer.begin(); //Necesary to make Arduino Software autodetect OTA device
ArduinoOTA.onStart([]() {Serial.println("OTA starting...");});
ArduinoOTA.onEnd([]() {Serial.println("OTA update finished!");Serial.println("Rebooting...");});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {Serial.printf("OTA in progress: %u%%\r\n", (progress / (total / 100)));});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("OK");
Serial.println("Configuring MQTT server...");
mqtt_client.setServer(mqtt_server, 1883);
Serial.printf(" Server IP: %s\r\n",mqtt_server);
Serial.printf(" Username: %s\r\n",mqtt_user);
Serial.println(" Cliend Id: "+mqtt_client_id);
Serial.println(" MQTT configured!");
Serial.println("Setup complete. Running loop...");
}
void mqtt_reconnect() {
// Loop until we're reconnected
while (!mqtt_client.connected()) {
Serial.print("Attempting MQTT connection...");
if (mqtt_client.connect(mqtt_client_id.c_str(), mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(mqtt_client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
long next_msg = 0; //in ms
int wait_ms = 2000;
int count = 0;
void loop() {
ArduinoOTA.handle();
if (!mqtt_client.connected()) {
mqtt_reconnect();
}
mqtt_client.loop();
if (millis() > next_msg) {
count = count + 1;
Serial.printf("Sending count %i\n", count);
mqtt_client.publish(mqtt_temp_topic, String(count).c_str(), true);
next_msg = millis() + wait_ms;
}
}
Serial Output
Setting up....
Connecting to JimsWirelessLan...........OK
IP address: 192.168.0.108
Configuring OTA device...OK
Configuring MQTT server...
Server IP: 192.168.0.102
Username: MqttUserid
Cliend Id: OtaClient
MQTT configured!
Setup complete. Running loop...
Attempting MQTT connection...connected
Sending count 1
Sending count 2
Sending count 3
Sending count 4
Sending count 5
Sending count 6
Sending count 7
Sending count 8
Sending count 9
Sending count 10
Sending count 11
Sending count 12
Sending count 13
Sending count 14
Attempting MQTT connection...connected
Sending count 15
Sending count 16
Sending count 17
Sending count 18
Sending count 19
Sending count 20
Sending count 21
Sending count 22
Sending count 23
Sending count 24
Sending count 25
Sending count 26
Attempting MQTT connection...connected
Sending count 27
Sending count 28
Sending count 29
Sending count 30
Sending count 31
Sending count 32
Sending count 33
Sending count 34
Sending count 35
Sending count 36
Sending count 37
Sending count 38
Sending count 39
Sending count 40
Sending count 41
Attempting MQTT connection...connected
Sending count 42
Sending count 43
Sending count 44
Sending count 45
Sending count 46
Sending count 47
Sending count 48
Sending count 49
Sending count 50
Sending count 51
Sending count 52
Attempting MQTT connection...connected
Sending count 53
Sending count 54
Sending count 55
Sending count 56
Sending count 57
Sending count 58
Sending count 59
Sending count 60
Sending count 61
Sending count 62
Sending count 63
Attempting MQTT connection...connected
Sending count 64
Sending count 65
Sending count 66