wemos mega R3 Wifi ESP8266 - WifiEsp Timeout

Hi Guy’s,

I don’t really know if this would be the right place for my question, and have been looking everywhere for a possible solution.

Now the case is that ‘what seems’ I have an exotic board from banggood :

The ESP8266 part (or as it seems on the board → Eco Plugs B rev1.1 ) has just the AT firmware on it

and the Mega part has all the code, which works ‘for a small amount of time’

Controlling 16 relays with it over MQTT ( later on, i wish to add some sensors to it for a fully automated greenhouse system)

Problem : It reacts quite slowly, and for now clear reason it loses connection, then reconnects, and than ultimately get stuck , or in other cases just reboots.

Using following Code on the Mega (left out most of the relays because i else exceed the max allowed message length)

any help is welcome :slight_smile:

#include <WiFiEsp.h>
#include <PubSubClient.h>


void callback(char* topic, byte* payload, unsigned int length);

//EDIT THESE LINES TO MATCH YOUR SETUP
#define MQTT_SERVER "192.168.0.117"  //you MQTT IP Address
const char* ssid = "********";
const char* password = "********";
 

const int switchPin1 = 22;
const int switchPin2 = 23;
const int switchPin3 = 24;
const int switchPin4 = 25;


char const* switchTopic1 = "/h/g/s1/";
char const* switchTopic2 = "/h/g/s2/";
char const* switchTopic3 = "/h/g/s3/";
char const* switchTopic4 = "/h/g/s4/";


HardwareSerial & dbgTerminal = Serial; 
HardwareSerial & espSerial = Serial3; 
WiFiEspClient espClient;
PubSubClient client(MQTT_SERVER, 1883, callback, espClient);

void setup() {

  //initialize the switch as an output and set to LOW (off)
  pinMode(switchPin1, OUTPUT); // Relay Switch 1
  digitalWrite(switchPin1, HIGH);

  pinMode(switchPin2, OUTPUT); // Relay Switch 2
  digitalWrite(switchPin2, HIGH);

  pinMode(switchPin3, OUTPUT); // Relay Switch 3
  digitalWrite(switchPin3, HIGH);

  pinMode(switchPin4, OUTPUT); // Relay Switch 4
  digitalWrite(switchPin4, HIGH);

  
  
// initialize serial for debugging
Serial.begin(115200);
espSerial.begin(115200);
  // initialize ESP module
  WiFi.init(&espSerial);

  //start wifi subsystem
  WiFi.begin(ssid, password);
  //attempt to connect to the WIFI network and then connect to the MQTT server
  reconnect();

  //wait a bit before starting the main loop
      delay(500);
}


void loop(){

  //reconnect if connection is lost
  if (!client.connected() && WiFi.status() == 3) {
    reconnect();
    }

  //maintain MQTT connection
  client.loop();
 delay(500);

}

void callback(char* topic, byte* payload, unsigned int length) {

  //convert topic to string to make it easier to work with
  String topicStr = topic; 
  //EJ: Note:  the "topic" value gets overwritten everytime it receives confirmation (callback) message from MQTT

  //Print out some debugging info
   Serial.println("Callback update.");
   Serial.print("Topic: ");
   Serial.println(topicStr);

   if (topicStr == "/h/g/s1/") 
    {

     //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
     if(payload[0] == '1'){
       digitalWrite(switchPin1, LOW);
       client.publish("/h/g/r1/", "1");
       }

      //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
     else if (payload[0] == '0'){
       digitalWrite(switchPin1, HIGH);
       client.publish("/h/g/r1/", "0");
       }
     }

     // EJ: copy and paste this whole else-if block, should you need to control more switches
     else if (topicStr == "/h/g/s2/") 
     {
     //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
     if(payload[0] == '1'){
       digitalWrite(switchPin2, LOW);
       client.publish("/h/g/r2/", "1");
       }

      //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
     else if (payload[0] == '0'){
       digitalWrite(switchPin2, HIGH);
       client.publish("/h/g/r2/", "0");
       }
     }
     else if (topicStr == "/h/g/s3/") 
     {
     //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
     if(payload[0] == '1'){
       digitalWrite(switchPin3, LOW);
       client.publish("/h/g/r3/", "1");
       }

      //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
     else if (payload[0] == '0'){
       digitalWrite(switchPin3, HIGH);
       client.publish("/h/g/r3/", "0");
       }
     }
     else if (topicStr == "/h/g/s4/") 
     {
     //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
     if(payload[0] == '1'){
       digitalWrite(switchPin4, LOW);
       client.publish("/h/g/r4/", "1");
       }

      //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
     else if (payload[0] == '0'){
       digitalWrite(switchPin4, HIGH);
       client.publish("/h/g/r4/", "0");
       }
     }

}


void reconnect() {

  //attempt to connect to the wifi if connection is lost
  if(WiFi.status() != WL_CONNECTED){
    //debug printing
   Serial.print("Connecting to ");
   Serial.println(ssid);

    //loop while we wait for connection
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
    Serial.print(".");
    }

    //print out some more debug once connected
    Serial.println("");
    Serial.println("WiFi connected");  
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }

  //make sure we are connected to WIFI before attemping to reconnect to MQTT
  if(WiFi.status() == WL_CONNECTED){
  // Loop until we're reconnected to the MQTT server
    while (!client.connected()) {
      Serial.print("Attempting MQTT connection...");


      //if connected, subscribe to the topic(s) we want to be notified about
      //EJ: Delete "mqtt_username", and "mqtt_password" here if you are not using any 
      if (client.connect("arduino")) {  //EJ: Update accordingly with your MQTT account 
       Serial.print("\tMQTT Connected");
        client.publish("/command/","hello");
        client.subscribe(switchTopic1);
        client.subscribe(switchTopic2);
        client.subscribe(switchTopic3);
        client.subscribe(switchTopic4);


        
        //EJ: Do not forget to replicate the above line if you will have more than the above number of relay switches
      }

      //otherwise print failed for debugging
      else{Serial.println("\tFailed."); abort();}
    }
  }
}