Hi Guys,
I have a project i'm working on with 2 WeMos D1 Mini Boards one is a light switch and the other one is a smart plug. They both work fine when only one of them is powered on and connected to MQTT.
But when I have both of them powered on they go into a loop off reconnecting to the MQTT Broker. They both use different topics so I'm not sure what's wrong?
This is my code for the plug
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiManager.h>
#include <DNSServer.h>
uint8_t relay_pin = D1;
String deviceID = "plug0000";
const char* mqtt_server = "192.168.1.9"; /// example 192.168.0.19
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(relay_pin, OUTPUT);
digitalWrite(relay_pin, LOW);
}
void setup_wifi() {
// WiFiManager
// Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
// Uncomment and run it once, if you want to erase all the stored information
//wifiManager.resetSettings();
wifiManager.autoConnect("AutoConnectAP");
// if you get here you have connected to the WiFi
Serial.println("Connected.");
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String recv_payload;
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
recv_payload = recv_payload + (char)payload[i];
}
Serial.println();
Serial.println(recv_payload );
if(recv_payload.indexOf("plug0000") >=0)
{
Serial.println("In");
//Switch on the relay if a 1 was received as first character
if ((char)payload[9] == '0')
{
Serial.println("LOW");
digitalWrite(relay_pin, LOW); // Turn the Relay Off
}
if ((char)payload[9] == '1')
{
Serial.println("HIGH");
digitalWrite(relay_pin, HIGH); // Turn the Relay on by making the voltage HIGH
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
client.publish("connect", "plug0000 connected");
client.subscribe("plug");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
//getACS712();
client.loop();
}
This is my code for the Switch
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiManager.h>
#include <DNSServer.h>
uint8_t relay_pin = D1;
uint8_t btn_pin = D5;
String deviceID = "switch0000";
int deviceStatus = 0; //Off by default
const char* mqtt_server = "192.168.1.9"; /// example 192.168.0.19
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(relay_pin, OUTPUT);
digitalWrite(relay_pin, LOW);
pinMode(btn_pin, INPUT_PULLUP);
}
void setup_wifi() {
// WiFiManager
// Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
// Uncomment and run it once, if you want to erase all the stored information
//wifiManager.resetSettings();
wifiManager.autoConnect("AutoConnectAP");
// if you get here you have connected to the WiFi
Serial.println("Connected.");
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String recv_payload;
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
recv_payload = recv_payload + (char)payload[i];
}
Serial.println();
Serial.println(recv_payload );
if(recv_payload.indexOf("switch0000") >=0)
{
Serial.println("In");
//Switch on the relay if a 1 was received as first character
if ((char)payload[11] == '0')
{
Serial.println("LOW");
digitalWrite(relay_pin, LOW); // Turn the Relay Off
deviceStatus = 0;
}
if ((char)payload[11] == '1')
{
Serial.println("HIGH");
digitalWrite(relay_pin, HIGH); // Turn the Relay on by making the voltage HIGH
deviceStatus = 1;
}
}
}
void relayOffOn()
{
if(deviceStatus == 0)
{
Serial.println("HIGH");
digitalWrite(relay_pin, HIGH); // Turn the Relay on by making the voltage HIGH
deviceStatus = 1;
}else if(deviceStatus == 1)
{
Serial.println("LOW");
digitalWrite(relay_pin, LOW); // Turn the Relay Off
deviceStatus = 0;
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
client.publish("connect", "switch0000 connected");
client.subscribe("switch");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
int buttonStatus = 1;
void loop() {
if (!client.connected()) {
reconnect();
}
int pinValue = digitalRead(btn_pin);
delay(10); // quick and dirty debounce filter
if (buttonStatus != pinValue) {
if(buttonStatus == 0)
{
relayOffOn();
}
buttonStatus = pinValue;
Serial.println(buttonStatus);
}
client.loop();
}
I've attached an image of what it loops when I have both devices powered on.