WiFi/MQTT in setup() breaks code in loop()
Posted: 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.
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;
}