/*********
Rui Santos // thanks to Rui for downloading my code via WIFI
Arduino IDE example: Examples > Arduino OTA > BasicOTA.ino
*********/
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include <string.h>
#define MSG_BUFFER_SIZE (128)
//#define MQTT_KEEPALIVE 1000
WiFiClient espWIFIClient;
PubSubClient Client(espWIFIClient);
byte mac [] = { xxxxxxxx };
IPAddress ip (10,0,3,65);
//constants
const char* ssid = "xxxxxxx";
const char* password = "xxxxxxxxxx";
const int ESP_BUILTIN_LED = 2;
const char* mqttServer = "10.0.3.28";
const int mqttPort = 1883;
const char* mqttUser = "";
const char* mqttPassword = "";
unsigned long lastMsg = 0;
char msg[MSG_BUFFER_SIZE];
int value = 0;
// setup of parameters as minimum to also download code via WIFI
void setup() {
//serial port setting for this code for debugging, when using serial port for serial communication between ESP8266 and Arduino remove serial print !
Serial.begin(9600);
while (!Serial) continue;
//WIFI start connection and authentication
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
// this part is important to download code over WIFI
ArduinoOTA.setHostname("SENSOR2");
// No authentication by default
// ArduinoOTA.setPassword((const char *)"123");
ArduinoOTA.onStart( {
Serial.println("Start");
});
ArduinoOTA.onEnd( {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (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("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// setting the loging credentials for the MQTT server connection
Client.setServer(mqttServer, mqttPort);
Client.setCallback(callback);
//
while (!Client.connected()) {
Serial.println("Connecting to MQTT...");
if (Client.connect("SENSOR3", mqttUser, mqttPassword )) {
Serial.println("initial connect");
Client.subscribe("domoticz/out/497/"); // when I use domoticz/out/350 I get a response I have checked with other MQTT tools if domoticz/out/497 responds and this is the case. also on mosquitto level this topic I can see. Don't understand this strange behaviour.
// Client.setKeepAlive( MQTT_KEEPALIVE );
} else {
Serial.print("failed with state ");
Serial.print(Client.state());
delay(2000);
}
//
}
}
//END of SETUP PART
//void callback(char* topic, byte* payload, unsigned int length) {
// StaticJsonDocument<256> doc;
// deserializeJson(doc, payload, length);
//
//
//}
//void callback(char* topic, byte* payload, unsigned int length) {
// Serial.print("Message arrived [");
// Serial.print(topic);
// Serial.print("] ");
// for (int i=0;i<length;i++) {
// Serial.print((char)payload[i]);
// }
// Serial.println();
//}
void callback(char* topic, byte* payload, unsigned int length) { //callback includes topic and payload ( from which (topic) the payload is coming)
char str[length+1];
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
int i=0;
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
str[i]=(char)payload[i];
}
str[i] = 0; // Null termination
Serial.println();
}
//END VOID callback
void reconnect() {
// Loop until we're reconnected
while (!Client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (Client.connect("SENSOR2", mqttUser, mqttPassword)) {
Serial.println("re-connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics (to control more outputs)
Client.subscribe("domoticz/out/497"); // in my log is see that the code is not reconnecting at the end of this code a concatenate the print log.
Serial.print("Subscribe");
// Client.setKeepAlive( MQTT_KEEPALIVE );
} else {
Serial.print("failed, rc=");
Serial.print(Client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
ArduinoOTA.handle();{
Client.loop();
}
}
// if (!Client.connected())
//
// {
// reconnect();
//
// }
// Client.loop();
//
//
//
// }
//}