WeMos WiFi MQTT Config

For the most IoT Projects i used MQTT with a WeMos or ESP-12. So I build a library to handel the basic WiFi and MQTT Config via Webinterface. 


The HandlerCloud library provides some basic functions to create IoT Projects with a ESP or a devopler board with a ESP like the WeMos D1 mini. It handles WiFi and MQTT config via a Webfrontend and allows you as developer to subscribe or publish informations.

If you include and setup the library it contains the following features:

  • If there is no saved WiFi connection open a WiFi Access Point to config the System
  • Connect to a saved WiFi connection
  • Provides a HTML Interface to setup the WiFi (ssid, password) and MQTT connections (username, password, server) in both WiFi modus (connectet or Access Point)
  • Connect to the MQTT Server (used the PubSubClient)
  • Subscribe to MQTT-Topics
  • Publish to MQTT-Topics
  • Reconnect to WiFi if the connection is lost
  • Reconnect to MQTT if the connection is lost

 Webinterface

The Webinterface works with the WiFi Access Point or if the device is connectet to a WiFi network. Only the font for the Interface is loaded from the Internet. If you use the Interface at the Access Point Modus or without internet connection it contains a default font from the OS. The HTML and Style is included in the Library.

 

Documentation

Setup

If you want to use all featues of the Lib you can make a setup with just one line of code:

hCloud.setupAll(true);

 The setupAll Function require one parameter which enabled or disabled the Serial Console outputs. If you just want to use parts of the library you can setup each feature you want to use and ignore the other ones:

hCloud.setupDebug(); //Enabled the Debug output
hCloud.setupWifi(); //Connect to WiFi or create Access Point
hCloud.setupWebserver(); //Start the Webserver to config the System
hCloud.setupMQTT(); //Connect to the MQTT Server

If you use MQTT or the Webserver you need to call some functions in the loop. 

hCloud.handleClient(); //Handle Webserver Requests
hCloud.handleMQTT(); //Handle MQTT incomming Messages
hCloud.checkAll(); //Reconnect to WiFi and MQTT if the connection is lost

 Subscribe to a MQTT-Topic

To Subscribe a MQTT-Topic you need to add the topic befor you run the setupMQTT(); function. To subscribe the topic "test/test" you can use the following code:

hCloud.addTopic("soeren/lichts/bad/mirrow");
hCloud.setupMQTT();

If the library reconnect to the MQTT-Server it will also re-subscribe this topic. If you get incomming Messages you need to create a "callback" function to get the informations:

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.println("Incomming Clalback");
  String msg = "";
  for (int i = 0; i < length; i++) {
    msg = msg + String((char)payload[i]);
  }
  String t = String(topic);
}

In this function you can check the topic (in this example in the String t) and the message (in this example in the String msg). 

Publish a MQTT Message

It's really easy to publich a Message to the MQTT-Broker:

hCloud.publishMessage("topic/test", "Juhu from ESP");

Full Example

Here is a full example to controll a Relay (How to connect a Relay to a WeMos) with this Library:

#include "HandlerCloud.h"

HandlerCloud hCloud;

void setup() {
   Serial.begin(115200);
   Serial.println("Start");

  hCloud.setupDebug();
  hCloud.setupWifi();
  hCloud.setupWebserver();
  hCloud.addTopic("soeren/lights/bad/mirrow");
  
  hCloud.setupMQTT();
  hCloud.setCallback(callback);
  hCloud.publishMessage("soeren/test2", "Juhu from ESP");
  Serial.println("Ende Setup");
  pinMode(D1, OUTPUT);
  digitalWrite(D1, HIGH);
  
}

// Callback with data from MQTT
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.println("Incomming Clalback");
  String msg = "";
  for (int i = 0; i < length; i++) {
    msg = msg + String((char)payload[i]);
  }
  String t = String(topic);
  if(t=="soeren/lights/bad/mirrow"){
    if(msg=="1")
    {
      digitalWrite(D1, LOW);
      Serial.println("Licht an");
    }
    else
    {
      digitalWrite(D1, HIGH);
      Serial.println("Licht aus");
    }
  }
  Serial.println(topic);
  Serial.println(msg);
}

void loop() {
  hCloud.handleClient();
  hCloud.handleMQTT();
  hCloud.checkAll();
  delay(100);
}

 Download

You can get the current Version from Github