Home / Home Automation / Domoticz / MySensors 2.0: ESP8266 WiFi Gateway with Domoticz

MySensors 2.0: ESP8266 WiFi Gateway with Domoticz

We continue the series of articles on the library MySensors 2.0.In this article we will learn how to make a WiFi Gateway based on a ESP8266 module and connect it to Domoticz. The ESP8266 modules are supported since version 1.5 through the work of Yveaux. You can follow discussions on the subject here.

How to prepare the ESP8266 WiFi gateway

In this article, we will build the WiFi gateway with a nRF24L01 radio module. It will serve us for a later tutorial.

List of materials

No special specifications exists. Probably you can use one ESP-01 module, but i have not tried. I advice you to take one ESP-12 with a usb connector. It is easier to program and not much more expensive. For this article i used a Wemos D1 Mini.

ThenRF24L01 are not compatible with our breadboard. So, you can use a socket adapter plate to wire the nRF24L01 module. This socket integrate a power regulator to reduce from 5V to 3.3V.

Circuit

The circuit is simple. If you don’t have a 3.3V (or the power if bad), you can use a step down power supply module (from 5V).

Gateway Wifi ESP8266 + reed switch jeedom domoticz mqtt

Programming: what gateway choose?

There are two types of ESP8266 Gateway :

  • The Network Gateway. The Gateway will be accessible from its IP address. The default port is 5003. The home servers compatible with MySensors library will then be able to communicate with objects connected to the network MySensors.
  • MQTT client. The Gateway enables communication via a MQTT server (broker). The exchanges are bidirectional. Choose this solution if you home automation software (for example) is not compatible with MySensors.

In the examples, you will also find an OTA Gateway that allows the update “in the air” program. I have not tried yet.

The choice is quite simple, if you plan to integrate connected objects MySensors on a home automation server that provides a dedicated plugin (it works and take into account the functions of MySensors you need), build a Network Gateway, otherwise opt for the MQTT client.

Prepare the gateway program

mysensors arduino gateway mqtt client

Open the gateway program : Examples -> MySensors -> GatewayESP8266 or ESP8266MQTTClient.

Then

  • Modify your SSID and WiFi password (MY_ESP8266_SSID, MY_ESP8266_PASSWORD).
  • Set a static ip, MY_IP_ADDRESS (if you want). In this case, you need to set the dhcp address (MY_IP_GATEWAY_ADDRESS, MY_IP_SUBNET_ADDRESS).

That’s all

mysensors v2 arduino gateway ip static

If you build a MQTT Client gateway :

  • Uncomment and modify the address of your MQTT broker
  • If you want, you can modify the prefix of the in/out topics

Add the reed switch to the test

Copy this code inside the gateway.

#include <ESP8266WiFi.h>
#include <MySensors.h>

#define CHILD_ID_CONTACTEUR 0
#define CONTACTEUR_PIN D3          // Reed switch Pin 
boolean etatPrecedent = false;         // previous state
MyMessage msgContacteur(CHILD_ID_CONTACTEUR, V_TRIPPED);

void setup() {
  pinMode(CONTACTEUR_PIN, INPUT);
  digitalWrite(CONTACTEUR_PIN, HIGH);
}

void presentation() {
  sendSketchInfo("Reed switch Test", "1.0");
  present(CHILD_ID_CONTACTEUR, S_DOOR); 
}


void loop() {
  uint8_t etat;
  // Read the state of the reed switch
  etat = digitalRead( CONTACTEUR_PIN );  
  if (etat != etatPrecedent) {
    etatPrecedent = etat;
  
    Serial.print("Switch state ");
    Serial.println(etat ? "Open " : "Close" );
    send( msgContacteur.set( etat ? "1" : "0" ) );
  }  
  delay(50);
}

Some explanations.

We define a child for the node (the gateway is also a node).

#define CHILD_ID_CONTACTEUR 0

Reed switch is connected to the pin D3 and to ground (G)

#define CONTACTEUR_PIN D3

A boolean variable will test the previous state and send a MQTT messages in case of change of the switch state.

etatPrecedent boolean = false;

We create a MyMessage object with V_TRIPPED type

MyMessage msgContacteur(CHILD_ID_CONTACTEUR, V_TRIPPED);

Now, we present the sensor

void presentation() {
  sendSketchInfo("Reed switch Test", "1.0");
  present(CHILD_ID_CONTACTEUR, S_DOOR); 
}

The loop runs every 50ms. Send the value only if the reed switch state change.

send( msgContacteur.set( etat ? "1" : "0" ) );

Connect this MySensors IoT to Domoticz

Domoticz version used for this article : v3.4834

To add the Gateway, go into the settings and equipment. In the list, select Gateway MySensors with Lan interface.

domoticz mysensor ajout choix gateway

Fill in the IP address (static ip is better) and port (5003 by default). Save.

2. domoticz mysensor ajout gateway esp8266

Click Settings and wait at least one minute. Domoticz need this time to collect data and childs.

3. domoticz mysensor ajout dispositifs noeuds et fils

Add a device

4. domoticz mysensor ajout appareil

When Domoticz can make mistakes when it create devices (probably because i use the version 2). It is not important, we will change that after.

Click the blue arrow and write a name for this device.

5. Domoticz ajout dispositif

Go to switches

6. Domoticz dispositif mysensors

Click Edit. In the switch type choose Door Lock and save.
7. Domoticz changement type interrupteur
Click the star if you want to add this device on the dashboard.

8. Domoticz dispositif mysensors configuré