The use of the ESP8266 in the world of IoT
User avatar
By danE
#69927 I have several sensors and alarm running variations of this same code, but this one will not stay connected to the mqtt server. I've tried changing all the variables and messing with the void reconnect and the delays, the activity on the mqtt side shows new connection then socket error and disconnects. I'm using a wemos d1 mini. Here is the Arduino code...

Code: Select all#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;
}
Last edited by danE on Wed Sep 13, 2017 5:13 am, edited 1 time in total.
User avatar
By rudy
#69929 Do you get the problem when you run that code on a different board?

Also, you should wrap the code with the code tags. Select all the code then select the </> in the toolbar.

Code: Select all[code]your code here
[/code]
User avatar
By rudy
#69931 I didn't look much at your code but from what is happening on the serial monitor I am getting repeated disconnects followed by connects. And there seems to be a repeatable period to it. But it is too nice a day here to stay inside and debug someone else's code. :)

Edit

After more watching maybe not a consistent period.
User avatar
By davydnorris
#69933
rudy wrote:... But it is too nice a day here to stay inside and debug someone else's code. :)

Edit

After more watching maybe not a consistent period.


You are so like me hahaha.

11:00AM "Too nice a day to be looking at some other person's code..... oh wait I might have seen something....."
03:00AM "Nope guess not.... oh hang on...."

:-)