Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. MQTT Epic Fail

MQTT Epic Fail

Scheduled Pinned Locked Moved Troubleshooting
4 Posts 2 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    danE
    wrote on last edited by
    #1

    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;
    }

    1 Reply Last reply
    0
    • gohanG Offline
      gohanG Offline
      gohan
      Mod
      wrote on last edited by gohan
      #2

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

      D 1 Reply Last reply
      1
      • gohanG gohan

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

        D Offline
        D Offline
        danE
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • gohanG Offline
          gohanG Offline
          gohan
          Mod
          wrote on last edited by
          #4

          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 :)

          1 Reply Last reply
          0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          9

          Online

          12.1k

          Users

          11.2k

          Topics

          113.4k

          Posts


          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • MySensors
          • OpenHardware.io
          • Categories
          • Recent
          • Tags
          • Popular