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

mysensors version2 esp8266 mqtt gateway nrf24l01PNA
Table of Contents

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.

mysensors v2 ide arduino library manager

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 :

  • A gateway can be a sensor node too.
  • Two network gateway can communicate together without radio module
  • We can build WiFi gateway based on the ESP8266 module and a MQTT client gateway.
  • You can define a static ip (or use a dhcp)

New cool functions

  • before(): include the code you want to execute before the MySensors initialization. 
  • receive(const MyMessage &message): retrieves the messages received by the object
  • receiveTime(unsigned long ts): retrieves the time on the gateway. Time is asked with the function requestTime().

New sensors and formats

Version 2 includes new sensor types :

  • S_INFO (36) – Gisplay text on LCD screen, uses V_TEXT
  • S_GAS (37) – Gas meter, uses V_FLOW and V_VOLUME
  • S_GPS (38) – GPS Sensor, uses V_POSITION
  • S_WATER_QUALITY (39) – Water quality sensor, uses V_TEMP, V_PH, V_ORP, V_EC and V_STATUS
  • S_LIGHT is deprecated. Uses S_BINARY instead of V_LIGHT

New variable type associated

  • V_TEXT (47) – For S_INFO. Text message to display on LCD or controller device

  • V_CUSTOM (48) – Custom messages 
  • V_POSITION (49) – GPS position and altitude. Payload: latitude;longitude;altitude(m)
  • V_IR_RECORD (50) – Record IR codes S_IR for playback
  • V_PH (51) – For S_WATER_QUALITY, water PH
  • V_ORP (52) – For S_WATER_QUALITY, water ORP : redox potential in mV
  • V_EC (53) – For S_WATER_QUALITY, water electric conductivity μS/cm (microSiemens/cm)
  • V_VAR (54) – For S_POWER, Reactive power: volt-ampere reactive (var)
  • V_VA (55) – For S_POWER, Apparent power: volt-ampere (VA)
  • V_POWER_FACTOR (56) – For S_POWER, Ratio of real power to apparent power: floating point value in the range [-1,..,1]
  • V_DIMMER is deprecated. Uses V_PERCENTAGE
  • V_HEATER is deprecated. Uses V_HVAC_FLOW_STATE
  • V_LIGHT is deprecated. Uses V_STATUS
Click to rate this post!
[Total: 0 Average: 0]

Thanks for your reading

Did you like this project ? Don't miss any more projects by subscribing to our weekly newsletter!

Are you having a problem with this topic?

Maybe someone has already found the solution, visit the forum before asking your question
We will be happy to hear your thoughts

Leave a Reply

Read more
DIY Projects
DIY Projects