DIY Projects

MySensors v2 – Discover, news, migrate old sketchs (1.4 or 1.5)

mysensors version2 esp8266 mqtt gateway nrf24l01PNA

The MySensors library has just received an important update and at the same time goes to version 2. This is an opportunity to discover (or rediscover) this library to create DIY connected objects developed by the Swedish Sensnology AB team. . This article will be followed by a series of tutorials to integrate connected objects in Jeedom and Domoticz.

Install the MySensors library on the Arduino IDE

There are already many explanations on the principle of the MySensors library on the internet. In this article we will focus on the changes made in version 2 of the library. Before going into the thick of the subject, the MySensors library is now available in the Arduino IDE, you can install it from Library Manager very easily.

The biggest change is under the hood. The Sensnology team did a lot of optimization work that reduced the size of the library by 20%, which is still very significant in an Arduino project. This optimization is accompanied by a change in the writing of the code. For example, it is no longer necessary to create a gw object (MySensor gw) then to call the desired method (for example gw.begin), we now call the desired method directly, for example sendSketchInfo () to send the name and the version of the object.

How to write an Arduino sketch with MySensor v2

The Sensnolgy Team has optimized the code of the library. Now, it is more simple to write. The code is more readable too. To write this article, i used the UVSensor example included in the library. This example use a UV Sensor (UVM-30Ato calculate the sun index. The sensor send only the value if the value change or every 5 minutes.

Activate the debug mode that allows to trace the program directly with the serial monitor

#define MY_DEBUG

Choose the correct radio module you use. MySensors support nRF24L01, RFM69 and RS485. Of course the most used is the nRF24L01. If you need long distance (or you have several walls), it is better to choose a nRF24L01+PA+LNA module. We will learn how to use it in a futur post.

You can continue to use the MyConfig.h file, but it is better to set the parameters directly inside the Arduino program with #define.

All parameters must be defined before to include the MySensors.h library.

#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
//#define MY_RS485

We need to include the SPI.h and MySensors.h libraries. Note the ‘s’ at the end to show the difference with the previous version.

#include <SPI.h>
#include <MySensors.h>

Now, we can create a child. It simply a number from 0.

#define CHILD_ID_UV 0

Now we can set an MyMessage object. This object will contain the value (in the correct format). In this case it is a UV Index (V_UV).

MyMessage uvMsg(CHILD_ID_UV, V_UV);

If you need to initialise output for example, you can do that in the standard Arduino function startup(){}.

Before to check the UV index, wWe need now to present the node on the MySensor network with the new presentation(){} function. You can see that now, we just need to call the MySensors function without gw. We don’t need to create the gw object and start anything, all is done behind the scene.

void presentation()  {
  // Send the sketch version information to the gateway and Controller
  sendSketchInfo("UV Sensor", "1.2");

  // Register all sensors to gateway (they will be created as child devices)
  present(CHILD_ID_UV, S_UV);

The standard Arduino loop allows us to make UV measurement

uint16_t uv = analogRead(UV_SENSOR_ANALOG_PIN);

Check if the value change (or every 5 minutes). So, use the send command to send the value. The MyMessage will prepare the value in the correct format. You can define the number of decimal in this case.


Finally the Arduino can sleep a little. For example 30 x 1000 ms in this case.


As you can see, it is really simple to use the MySensor with this new version. All is done behind the scene, we are just concentrated on the process.

How to convert old sketchs to version 2

Now with the version 2, you don’t need to use the MyConfig.h file (the config file already exist). Just use #define inside the Arduino program before to call the MySensors.h library.

Add a ‘s’ at like that

#include <MySensors.h>

Erase MySensor gw and gb.begin() in your code.

Erase all gw in front of all the MySensors library calls. For example gw.send() becomes send().

Move the presentation inside the new presentation() function like that

void presentation()  {
  sendSketchInfo("UV Sensor", "1.2");
  present(CHILD_ID_UV, S_UV);

That all !

Other new features in version 2

The library is lighter (20%), but this version comes with a large number of cool new features (all here). Probably the most significance :

New cool functions

New sensors and formats

Version 2 includes new sensor types :

New variable type associated

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