Interference with PubSubClient
Hello. I am using SimpleButton liblary in my project and it is working verry well. I use ESP8266 and buttons connected to GPIO expander - MCP23017.
The issue i have is when i try to use PubSubClient and SimpleButton liblary together.
My code:
`#include <Arduino.h> #include <ESP8266WiFi.h> //WIFI liblary
#include <PubSubClient.h> //MQTT liblary WiFiClient espClient; PubSubClient client(espClient); void callback(char* topic, byte* payload, unsigned int length);
const char* ssid = "xxxx"; // Enter your WiFi name const char* password = "xxxxx."; // Enter WiFi password const char* mqttServer = "xxxxx"; const int mqttPort = 1883; const char* mqttUser = "xxxxx"; const char* mqttPassword = "xxxxx"; const char* clientID = "ESP8266";
#include <SimpleButton.h> //button listener liblary using namespace simplebutton; //Initialize buttons: Button* Button1 = NULL;
void setup() { // put your setup code here, to run once: // put your setup code here, to run once: Serial.begin(115200); Serial.println();
pinMode(D3, OUTPUT); digitalWrite(D3, LOW);
//Connect to WIFI network: WiFi.begin(ssid, password);
Serial.print("Connecting"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println();
Serial.print("Connected, IP address: "); Serial.println(WiFi.localIP());
//MQTT broker: client.setServer(mqttServer, mqttPort); // set server settings for connection client.setCallback(callback); // for incoming messages
//Connect to MQTT broker: while (!client.connected()) { Serial.println("Connecting to MQTT...");
if (client.connect(clientID, mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.subscribe("Subscribe1");
Wire.begin(D2, D1); // begin i2c bus on SDA=D2 and SCL=D1
//Configure the input MCP23017 modules: GPIOExpander* mcpDI = new MCP23017(0x20);
//Make sure cnnection is sucessfull:
if(mcpDI->connected()){Serial.println("mcpDO connected"); } //Define connection between output module I/Os and buttons A0=0, A1=1, ......, B0=8, ..., B8=15 Button1 = new ButtonPullupGPIOExpander(mcpDI, 0);
}
void OutputAction(int o){
switch (o) {
case 0:
digitalWrite(D3, LOW);
client.publish("Publish1", "0");
break;
case 1:
digitalWrite(D3, HIGH);
client.publish("Publish1", "1");
break;
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: "); Serial.println(topic);
Serial.print("Message:"); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); }
String thisTopic = topic; byte thisPayload = *payload; int ukaz = ((int)thisPayload-48);
if(thisTopic.equals("Subscribe1")){ switch(ukaz){ case 0: OutputAction(0); break; case 1: OutputAction(1); break; } } }
void loop() { // put your main code here, to run repeatedly: client.loop();
if (!client.connected()) { Serial.println("MQTT not connected"); client.connect(clientID, mqttUser, mqttPassword ); client.subscribe("Subscribe1"); }
Button1->update();
if(Button1->clicked()){ OutputAction(1); } if(Button1->holding()){ OutputAction(0); }
}`
The issue i am having is that MQTT gets disconnected when i use Button1->update(); and it never connects again. If i comment out the Button1->update(); it works well. But of course the button does not respond.
I was hoping someone can share some light on this issue. I do not have enough experience to find a solution.
I have both: SimpleButton and PubSubClient and I don't observe such issue