Home | IoT - Connected Objects | MySensors v2 – Discover, news, migrate old sketchs (1.4 or 1.5)

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

The Arduino MySensors library received a major update this summer. In this article we will discover what is new, how to write your new programs and how to migrate your old sketches.

Before to start, you have to know that you can now install the library directly with the Arduino IDE Library Manager.

mysensors v2 ide arduino library manager

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

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.

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.
Now, we can create a child. It simply a number from 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).
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.

The standard Arduino loop allows us to make UV measurement

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

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

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