MQTT Epic Fail



  • I have several sensors and alarms running very similar code, I'm using a wemos d1 mini for this one and Arduino ide for the code. Problem is mqtt client disconnects repeatedly throwing a socket error in the server side logs. I've changed the variables and the void reconnect code and the delay times around a bunch as well as uploading parts of the code separately to try and debug, so far nothing has helped the issue.
    Here's the code I'm currently trying to run...
    #include <PubSubClient.h>
    #include <ESP8266WiFi.h>

    #define MQTT_SERVER "192.168.0.123"

    const char* ssid = "censored";
    const char* password = "censored";
    const int indpin = 15;
    const int indpin2 = 12;
    const int indpin3 = 16;
    const int alrmpin1 = 2;
    const int alrmpin2 = 0;
    const int button1 = 14;
    const int button2 = 13;
    int button1Stat = 0;
    int button1Count = 0;
    int button1last = 0;
    int button2Stat = 0;
    int button2Count = 0;
    int button2last = 0;

    void callback(char* topic, byte* payload, unsigned int length);

    WiFiClient wifiClient;
    PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);

    void setup() {
    pinMode(indpin2, OUTPUT);
    pinMode(indpin, OUTPUT);
    pinMode(indpin3, OUTPUT);
    pinMode(alrmpin1, OUTPUT);
    pinMode(alrmpin2, OUTPUT);
    pinMode(button1, INPUT);
    pinMode(button2, INPUT);

    digitalWrite(indpin, LOW);
    digitalWrite(indpin2, LOW);
    digitalWrite(indpin3, LOW);
    digitalWrite(alrmpin1, LOW);
    digitalWrite(alrmpin2, LOW);

    Serial.begin(115200);

    delay(100);

    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);

    reconnect();

    delay(2000);

    }

    void loop(){

    if (!client.connected() && WiFi.status() == 3) {reconnect();}
    client.loop();
    delay(1000);
    button1Stat = digitalRead(button1);

    if (button1Stat != button1last) {

    if (button1Stat == HIGH) {
      
      button1Count++;
     
    }
    
    delay(50);
    

    }

    button1last = button1Stat;

    if (button1Count % 2 == 0) {
    client.publish("indoor", "0");
    } else {
    client.publish("indoor", "1");
    }
    button2Stat = digitalRead(button2);

    if (button2Stat != button2last) {

    if (button2Stat == HIGH) {
      
      button2Count++;
     
    }
    
    delay(50);
    

    }

    button2last = button2Stat;

    if (button2Count % 2 == 0) {
    client.publish("Smotion", "0");
    } else {
    client.publish("Smotion", "1");
    }
    }
    void callback(char* topic, byte* payload, unsigned int length) {

    String topicStr = topic;

    Serial.println("Callback update.");

    Serial.print("Topic: ");

    Serial.println(topicStr);

    if(payload[0] == '1' && topic[0] == 'i'){

    digitalWrite(indpin, HIGH);
    

    }
    if (payload[0] == '0' && topic[0] == 'i'){

    digitalWrite(indpin, LOW);
    

    }

    if(payload[0] == '1' && topic[0] == 'S'){
    digitalWrite(indpin2, HIGH);
    }
    if(payload[0] == '0' && topic[0] == 'S'){
    digitalWrite(indpin2, LOW);
    }

    if (payload[0] == '1' && topic[0] == 's'){
    digitalWrite(indpin3, HIGH);
    }
    if (payload[0] == '0' && topic[0] == 's'){
    digitalWrite(indpin3, LOW);
    }
    if (payload[0] == '1' && topic[0] == 'o'){
    digitalWrite(alrmpin1, HIGH);
    }
    if (payload[0] == '0' && topic[0] == 'o'){
    digitalWrite(alrmpin1, LOW);
    }
    if (payload[0] == 'o' && topic[0] == 'o'){
    digitalWrite(alrmpin1, HIGH);
    }
    if (payload[0] == 'f' && topic[0] == 'o'){
    digitalWrite(alrmpin1, LOW);
    }
    if (payload[0] == '2' && topic[0] == 'm'){
    digitalWrite(alrmpin2, HIGH);
    delay(500);
    digitalWrite(alrmpin2, LOW);
    delay(500);
    digitalWrite(alrmpin2, HIGH);
    delay(500);
    digitalWrite(alrmpin2, LOW);
    delay(500);
    digitalWrite(alrmpin2, HIGH);
    delay(500);
    digitalWrite(alrmpin2, LOW);
    delay(500);
    digitalWrite(alrmpin2, HIGH);
    delay(500);
    digitalWrite(alrmpin2, LOW);
    delay(500);
    digitalWrite(alrmpin2, HIGH);
    delay(500);
    digitalWrite(alrmpin2, LOW);
    delay(500);
    digitalWrite(alrmpin2, HIGH);
    delay(500);
    digitalWrite(alrmpin2, LOW);
    delay(500);
    digitalWrite(alrmpin2, HIGH);
    delay(500);
    digitalWrite(alrmpin2, LOW);
    delay(500);
    digitalWrite(alrmpin2, HIGH);
    delay(500);
    digitalWrite(alrmpin2, LOW);
    }
    }

    void reconnect() {

    if(WiFi.status() != WL_CONNECTED){

    Serial.print("Connecting to ");
    
    Serial.println(ssid);
    
    while (WiFi.status() != WL_CONNECTED) {
    
      delay(500);
    
      Serial.print(".");
    
    }
    
    Serial.println("");
    
    Serial.println("WiFi connected");  
    
    Serial.println("IP address: ");
    
    Serial.println(WiFi.localIP());
    

    }

    if(WiFi.status() == WL_CONNECTED){
    while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    String clientName;
    clientName += "Master-";
    uint8_t mac[6];
    WiFi.macAddress(mac);
    clientName += macToStr(mac);
    if (client.connect((char*) clientName.c_str())) {
    Serial.print("\tMTQQ Connected");

        client.subscribe("indoor");
        client.subscribe("Smotion");
        client.subscribe("sec2");
        client.subscribe("motion");
        client.subscribe("outdoor");
        client.subscribe("outdoor2");
      }
    
      else{Serial.println("\tFailed."); abort();}
    
    }
    

    }

    }

    String macToStr(const uint8_t* mac){
    String result;
    for (int i = 0; i < 6; ++i) {
    result += String(mac[i], 16);
    if (i < 5){
    result += ':';
    }
    }
    return result;
    }


  • Mod

    Wellcome to the forum, but how is this related to mysensors?



  • @gohan didn't realize this was a forum for a specific development environment came here following a trail of other esp 8266 related issues and threw down a post. I see now I'm in the wrong place.


  • Mod

    well, if you are willing to make your alarm system to use Mysensors we would be very happy to help and learn from other people experience :)


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.