/*********
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();
//
//
//
// }
//}
20:52:07.020 -> SDK:2.2.2-dev(38a443e)/Core:3.0.2=30002000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-48-g7421258/BearSSL:6105635
20:52:07.129 -> Booting
20:52:07.129 -> fpm close 1
20:52:07.163 -> mode : sta(a0:20:a6:23:35:af)
20:52:07.196 -> add if0
20:52:07.196 -> wifi evt: 8
20:52:07.653 -> wifi evt: 2
20:52:09.881 -> scandone
20:52:10.808 -> state: 0 -> 2 (b0)
20:52:10.808 -> state: 2 -> 3 (0)
20:52:10.845 -> state: 3 -> 5 (10)
20:52:10.845 -> add 0
20:52:10.845 -> aid 2
20:52:10.881 -> cnt
20:52:10.881 ->
20:52:10.881 -> connected with isegimplein, channel 5
20:52:10.881 -> dhcp client start...
20:52:10.917 -> wifi evt: 0
20:52:10.917 -> ip:10.0.3.65,mask:255.255.255.0,gw:10.0.3.139
20:52:10.953 -> wifi evt: 3
20:52:10.986 -> Ready
20:52:10.986 -> IP address: 10.0.3.65
20:52:11.019 -> Connecting to MQTT...
20:52:11.053 -> [hostByName] Host: 10.0.3.28 is a IP!
20:52:11.155 -> initial connect
20:52:20.809 -> pm open,type:2 0