WiFi/MQTT in setup() breaks code in loop()

ananonalias
Posts: 1
Joined: Thu Jan 23, 2020 10:22 pm

WiFi/MQTT in setup() breaks code in loop()

Postby ananonalias » Thu Jan 23, 2020 10:38 pm

Dear all,

Would hugely appreciate your help - pulling my hair out over this. I'm a relative beginner so be kind please.

Problem: Running WiFi.begin() or client.setserver(mqtt_server, 1883); in void setup() breaks void loop().

Intended Behaviour: Boots with outPin LOW regardless of state of inPin. inPin is connected to an SPST switch (flicks between on or off). inPin changes the state of outPin. outPin is connected to a light. An MQTT message is sent whenever outPin changes state.

Actual Behaviour: Always boots with outPin HIGH. Spams MQTT topic with many on & offs.

Action Taken: Code was written iteratively and works on the ESP32 as intended when stripped to the code below. Starting WiFi / MQTT seems to move from intended to actual behaviour as above.

What the heck am I missing!? I'm on OSX Catalina running both current and beta Arduino IDE releases on an ESP32 DevKitV1 board.


Code: Select all

#include <WiFi.h>
#include <PubSubClient.h>
#define wifi_ssid "SSID"
#define wifi_password "Password"
#define mqtt_server "192.168.1.101"
#define mqtt_user ""
#define mqtt_password ""
#define topic "asd"
WiFiClient espClient;
PubSubClient client(espClient);

int inPin = 13;
int outPin = 2;
int MQTT = 14;
int state = LOW;
int reading;
int previous = LOW;
long lastDebounceTime = 0;
long debounce = 100;

void setup()
{
//  WiFi.begin();
//  client.setServer(mqtt_server, 1883);
  pinMode(inPin, INPUT_PULLUP);
  pinMode(outPin, OUTPUT);
  pinMode(MQTT, OUTPUT);
} 

//void setup_wifi()
//{
//  delay(10);
//  WiFi.begin(wifi_ssid, wifi_password);
//  while (WiFi.status() != WL_CONNECTED)
//    {
//    delay(500);
//    }
//}

//void reconnect()
//{
//  while (!client.connected())
//    {
//      if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
//    }
//   else
//    {
//      delay(5000);
//    }
//  }
//}

void loop()
{    
//  if (!client.connected())
//      {
//        reconnect();
//      }
//    client.loop();
  
  reading = digitalRead(inPin);
    if (reading != previous && millis() - lastDebounceTime > debounce) {
      if (state == HIGH)
        state = LOW;
      else
        state = HIGH;
    lastDebounceTime = millis();    
    }
    
  digitalWrite(outPin, state);

  if (state == HIGH)
    {
//      client.publish(topic, String("OFF").c_str(), true);
    }
  else
    {
//      client.publish(topic, String("ON").c_str(), true);
    }    
  previous = reading;
}

ESP_Sprite
Posts: 8926
Joined: Thu Nov 26, 2015 4:08 am

Re: WiFi/MQTT in setup() breaks code in loop()

Postby ESP_Sprite » Sat Jan 25, 2020 9:09 am

Can you see what the serial output says? Are you sure that your power supply and USB cable are up to spec? It may be that the you're running into a power supply issue.

boarchuz
Posts: 559
Joined: Tue Aug 21, 2018 5:28 am

Re: WiFi/MQTT in setup() breaks code in loop()

Postby boarchuz » Sat Jan 25, 2020 11:54 am

Always boots with outPin HIGH
Try initialising 'previous' with the current level, rather than defaulting to LOW. I don't expect that alone will fix it though. eg.

Code: Select all

void setup()
{
  // ...
  previous = digitalRead(inPin);   //   <----
}
Spams MQTT topic
It's hard to tell which commented snippets are junk and which comments are part of your full sketch, but it looks to me like publish() is being called every loop(). I assume you only want it to run when the switch changes state. You could basically nest all of your code in the pin change check.

Code: Select all

void loop() {
  reading = digitalRead(inPin);
  if (reading != previous && millis() - lastDebounceTime > debounce) {
    state = !state;
    digitalWrite(outPin, state);
    client.publish(topic, state ? "ON" : "OFF", true);
    lastDebounceTime = millis();
    previous = reading;
  }
}
lastDebounceTime should be 'unsigned long'.

Who is online

Users browsing this forum: No registered users and 45 guests