DIY Projects

How to make a MySensors v2.0 WiFi Gateway with an ESP8266. Test with Domoticz

Gateway Wifi ESP8266 reed switch jeedom domoticz mqtt

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).

Programming: what gateway choose?

There are two types of ESP8266 Gateway :

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

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

Then

That’s all

If you build a MQTT Client gateway :

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.

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

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

Add a device

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.

Go to switches

Click Edit. In the switch type choose Door Lock and save.

Click the star if you want to add this device on the dashboard.

Click to rate this post!
[Total: 1 Average: 3]
Exit mobile version