Ho chiesto aiuto sulla community di OpenHab con esito positivo.
Posto la soluzione cosi' che possa essere di aiuto a qualcuno.
Il mio errore era nel fare due chiamate contemporaneamente, (e non e' possibile) e quindi ne veniva eseguita sempre e soltanto una.
A quel punto il problema si risolve in un unica funzione che comprende i topic necessari eseguiti in base alla struttura if...else
ecco il codice:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Bounce2.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
IPAddress staticIP(xxxxx); //ESP static ip
IPAddress gateway(xxxxx);
IPAddress subnet(255, 255, 255, 0);//IP Address of your WiFi Router (Gateway)
const char* ssid = "xxxxx"; //SSID WIFI Access Point
const char* password = "xxxxx"; //Access Point Password
const char* mqtt_server = "xxxxx"; //IP address MQTT server
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
const char* inTopic_luci = "openhab/out/ufficio/luci/command"; // MQTT Topic this switch subscribes
const char* inTopic_foto = "openhab/out/ufficio/foto/command"; // MQTT Topic this switch subscribes
const char* switchTopic_luci = "openhab/in/ufficio/luci/state"; //MQTT Topic wall switch publish
const char* switchTopic_foto = "openhab/in/ufficio/foto/state"; //MQTT Topic wall switch publish
const int relay_luci = 0; //D3 Pin for Relay
const int button_luci = 12; //D6 Pin for button
const int relay_foto = 13; //D7 pin fotocopiatore
const int button_foto = 16; //D0 Pin for
bool relayState_luci = HIGH;
bool relayState_foto = HIGH;
Bounce debouncer = Bounce(button_luci, 50);
Bounce debouncer2 = Bounce(button_foto, 50);
unsigned long previousMillis = 0; // will store last time STATUS was send to MQTT
const long interval = 60000; //one minute for status refresh
String topicString = "";
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.config(staticIP, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
for (int i = 0; i < 500; i++) {
delay(1);
}
Serial.print(".");
}
digitalWrite(2, LOW);
delay(500);
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
delay(500);
digitalWrite(2, HIGH);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
topicString = topic;
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if (topicString == "openhab/out/ufficio/luci/command") {
if ((char)payload[1] == 'F') {
digitalWrite(relay_luci, LOW);
Serial.println(relay_luci);// Turn the RELAY OFF
relayState_luci = LOW;
} else if ((char)payload[1] == 'N') {
digitalWrite(relay_luci, HIGH);
Serial.println(relay_luci);// Turn the RELAY ON by making the voltage HIGH
relayState_luci = HIGH;
}
}
// foto
if (topicString == "openhab/out/ufficio/foto/command") {
if ((char)payload[1] == 'F') {
digitalWrite(relay_foto, LOW);
Serial.println(relay_foto);// Turn the RELAY OFF
relayState_luci = LOW;
} else if ((char)payload[1] == 'N') {
digitalWrite(relay_foto, HIGH);
Serial.println(relay_foto);// Turn the RELAY ON by making the voltage HIGH
relayState_foto = HIGH;
}
}
}
/*
void callback_luci(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();
if ((char)payload[1] == 'F') {
digitalWrite(relay_luci, LOW);
Serial.println(relay_luci);// Turn the RELAY OFF
relayState_luci = LOW;
} else if ((char)payload[1] == 'N') {
digitalWrite(relay_luci, HIGH);
Serial.println(relay_luci);// Turn the RELAY ON by making the voltage HIGH
relayState_luci = HIGH;
Serial.println();
}
}
void callback_foto(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();
if ((char)payload[1] == 'F') {
digitalWrite(relay_foto, LOW);
Serial.println(relay_foto);// Turn the RELAY OFF
relayState_foto = LOW;
} else if ((char)payload[1] == 'N') {
digitalWrite(relay_foto, HIGH);
Serial.println(relay_foto);// Turn the RELAY ON by making the voltage HIGH
relayState_foto = HIGH;
}
}
*/
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("pippo")) {
Serial.println("connected");
//Subscribe to incoming commands
client.subscribe(inTopic_luci);
client.subscribe(inTopic_foto);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
for (int i = 0; i < 5000; i++) {
// extButton_luci();
// extButton_foto();
delay(1);
}
}
}
}
void extButton_luci() {
debouncer.update();
if ( debouncer.fell() ) {
Serial.println("Debouncer luci");
relayState_luci = !relayState_luci;
digitalWrite(relay_luci, relayState_luci);
if (relayState_luci == 1) {
client.publish(switchTopic_luci, "ON", false);
}
else if (relayState_luci == 0) {
client.publish(switchTopic_luci, "OFF", false);
}
}
}
void extButton_foto() {
debouncer2.update();
if ( debouncer2.fell() ) {
Serial.println("Debouncer foto");
relayState_foto = !relayState_foto;
digitalWrite(relay_foto, relayState_foto);
if (relayState_foto == 1) {
client.publish(switchTopic_foto, "ON", false);
}
else if (relayState_foto == 0) {
client.publish(switchTopic_foto, "OFF", false);
}
}
}
//*************************************SETUP*****************************************//
//***********************************************************************************//
void setup() {
String line = "";
pinMode(relay_luci, OUTPUT); // Initialize the relay pin as an output
pinMode(button_luci, INPUT); // Initialize the relay pin as an input
pinMode(relay_foto, OUTPUT);
pinMode(button_foto, INPUT);
pinMode(2, OUTPUT); // Initialize the onboard LED as output
debouncer.attach(button_luci); // Use the bounce2 library to debounce the built in button
debouncer.interval(50); // Input must be low for 50 ms
debouncer2.attach(button_foto);
debouncer2.interval(50);
digitalWrite(2, LOW); // Blink to indicate setup
delay(50);
digitalWrite(2, HIGH);
delay(50);
Serial.begin(115200);
setup_wifi(); // Connect to wifi
client.setServer(mqtt_server, 1883); //connect to MQTT
//client.setCallback(callback_luci);
client.setCallback(callback);
// Port defaults to 8266: ArduinoOTA.setPort(8266);
ArduinoOTA.setHostname("Domotica ");
ArduinoOTA.setPassword((const char *)"xxxxx");
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());
}
void loop() {
ArduinoOTA.handle();
if (!client.connected()) {
reconnect();
}
client.loop();
extButton_luci();
extButton_foto();
}
grazie a tutti