Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Announcements
  3. Converting a sketch from 1.5.x to 2.0.x

Converting a sketch from 1.5.x to 2.0.x

Scheduled Pinned Locked Moved Announcements
52 Posts 21 Posters 31.9k Views 26 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • hekH Offline
    hekH Offline
    hek
    Admin
    wrote on last edited by Anticimex
    #1

    2.0.x allow configuration (old MyConfig stuff) to be done directly in the sketch using simple c #defines. You can still make modification in MyConfig if you want to override the default values once and for all.

    It is important to do these defines before including MySensors.h, otherwise they will go unnoticed when the library evaluates which parts to include.

    The most common thing you need to do is to decide which radio to use (or not to use). We currently support three different transport layers. Only activate ONE transport per node.

    // Activate one of these
    
    #define MY_RADIO_NRF24
    #define MY_RADIO_RFM69
    #define MY_RS485
    

    In addition to the transport you can also allow the node to be a gateway (communicating to the controller). We currently offer a few variants here as well. Note that ESP8266 is always considered to be a gateway node (but it can still communicate with a sub-network of none-esp sensor nodes). If you want to enable gateway functionality enable one of these:

    // Enable serial gateway
    #define MY_GATEWAY_SERIAL
    
    // Enable gateway ethernet for a ENC28J60 module 
    #define MY_GATEWAY_ENC28J60
    
    // Enable gateway for a W5100 ethernet module 
    #define MY_GATEWAY_W5100
    
    // Enable gateway for a ENC28J60 ethernet for a  module 
    #define MY_GATEWAY_ENC28J60
    
    // Enable a MQTT client gateway for W5100 or ESP node
    #define MY_GATEWAY_MQTT_CLIENT
    
    

    Each type of gateway has additional configuration available (ip settings etc). Please look at the prepared example for more details.

    To upgrade a sketch old 1.5 sketch to 2.0 there is just a few steps to go through.

    • Remove the MySensors gw; class initialization and gw.begin()-call. It's no longer needed. Use #defines to set radio channels or other settings. They are all documented in MyConfig.h.

    • The library now calls process() automatically. So you must remove it from you main loop().

    • Remove the gw. part before any library call. It's no longer needed as library functions is available in the main scoop. For instance gw.send(msg) should just be send(msg).

    • The controller can now re-request presentation information from a node. This means we have to move all node presentation to a new special function presentation() {} . This will be called at node startup or when controller requests a new presentation.

    So if your old node had this:

    void setup()  
    { 
      gw.sendSketchInfo("Distance Sensor", "1.0");
      gw.present(CHILD_ID, S_DISTANCE);
    }
    

    Do like this instead:

    void presentation()  
    { 
      sendSketchInfo("Distance Sensor", "1.0");
      present(CHILD_ID, S_DISTANCE);
    }
    

    If you need to do initialization before the MySensors library starts up, define a

    void before() {
        // This will execute before MySensors starts up
    }
    

    You can still use setup() which is executed AFTER mysensors has been initialised.

    void setup() {
    
    }
    

    To handle received messages, define the following function in your sketch

    void receive(const MyMessage &message) {
        // Handle incoming message
    }
    

    If your node requests time using requestTime(). The following function is used to pick up the response.

    void receiveTime(unsigned long ts) {
    }
    

    Here follows a small sample of the defines you can use in your sketch

    #define MY_DEBUG    // Enables debug messages in the serial log
    #define MY_REPEATER_FEATURE  // Enables repeater functionality for a radio node
    #define MY_BAUD_RATE  9600 // Sets the serial baud rate for console and serial gateway
    #define MY_NODE_ID 42 // Sets a static id for a node
    #define MY_PARENT_NODE_ID xx // Sets a static parent node id
    #define MY_OTA_FIRMWARE_FEATURE  // Enables OTA firmware updates
    
    #define MY_RF24_CE_PIN 9    // Radio specific settings for RF24
    #define MY_RF24_CS_PIN 10 // Radio specific settings for RF24 (you'll find similar config for RFM69)
    
    #define MY_INCLUSION_MODE_FEATURE // Enables inclusion mode (for a gateway)
    #define MY_INCLUSION_BUTTON_FEATURE // Eables inclusion mode button (for a gateway)
    #define MY_LEDS_BLINKING_FEATURE // Enables transmission led feature for a node or gateway
    
    #define MY_SIGNING_ATSHA204 // Enables hardware signing using ATSHA204
    #define MY_SIGNING_SOFT // Enables software signing
    

    Security related settings (HMAC and AES keys among other things) are now configured using the SecurityPersonalizer sketch. Secrets are stored in eeprom for all software based security features (including rfm69 encryption).

    There are many more things you can tweak using defines and we've tried to include most of them in relevant examples. The MyConfig.h/keywords.txt shows you the full list of defines available.

    1 Reply Last reply
    9
    • chrlyraC Offline
      chrlyraC Offline
      chrlyra
      wrote on last edited by
      #2

      Hi,

      How does this work when your gateway also have local sensors? I tried to merge the Humidity sensor (DHT11) with the SerialGateway but it seems that presentation is not called after uploading the sketch. This is what I have:

      void setup() { 
        // Setup locally attached sensors
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
        metric = getConfig().isMetric;
      }
      
      void presentation() {
       // Present locally attached sensors 
       sendSketchInfo("My Gateway", "1.0");
       present(CHILD_ID_HUM, S_HUM);
       present(CHILD_ID_TEMP, S_TEMP);
      }
      

      And the output is:

      0;255;3;0;14;Gateway startup complete.
      0;255;3;0;9;No registration required
      0;255;3;0;9;Init complete, id=0, parent=0, distance=0, registration=1
      0;1;1;0;0;28.0
      0;0;1;0;1;59.0 
      

      If I put the "present" lines at the setup() then I got the expected lines:

      0;255;3;0;14;Gateway startup complete.
      0;255;3;0;11;My Gateway
      0;255;3;0;12;1.0
      0;0;0;0;7;
      0;1;0;0;6;
      0;255;3;0;9;No registration required
      0;255;3;0;9;Init complete, id=0, parent=0, distance=0, registration=1
      0;1;1;0;0;28.0
      0;0;1;0;1;59.0 
      

      Is this expected or I missed something? I'm trying to use this gateway with homeassistant and it seems that homeassistant expect those presentation lines. There's a issue with the use of node "0" id too, but i should leave it to another topic.

      1 Reply Last reply
      0
      • rollercontainerR Offline
        rollercontainerR Offline
        rollercontainer
        wrote on last edited by rollercontainer
        #3

        Maybe related to this problem?
        https://github.com/mysensors/MySensors/issues/449

        My gateway does the same. No presentation, but sending data.

        chrlyraC 1 Reply Last reply
        0
        • hugowsH Offline
          hugowsH Offline
          hugows
          wrote on last edited by hugows
          #4

          The API looks very clean and simple, I look forward to use it more.
          Does Arduino/MySensors uses C++ features (namespaces?) to prevent name collision? In typical C projects I've seen the functions all beginning with some prefix (mysensors_).

          1 Reply Last reply
          0
          • rollercontainerR rollercontainer

            Maybe related to this problem?
            https://github.com/mysensors/MySensors/issues/449

            My gateway does the same. No presentation, but sending data.

            chrlyraC Offline
            chrlyraC Offline
            chrlyra
            wrote on last edited by
            #5

            @rollercontainer Where should the "if (presentation).." be? At the setup()?

            YveauxY 1 Reply Last reply
            0
            • chrlyraC chrlyra

              @rollercontainer Where should the "if (presentation).." be? At the setup()?

              YveauxY Offline
              YveauxY Offline
              Yveaux
              Mod
              wrote on last edited by
              #6

              @chrlyra there is no such thing as if(presentation)...
              The presentation function implementation can be anywhere in your sketch.

              http://yveaux.blogspot.nl

              chrlyraC 1 Reply Last reply
              0
              • YveauxY Yveaux

                @chrlyra there is no such thing as if(presentation)...
                The presentation function implementation can be anywhere in your sketch.

                chrlyraC Offline
                chrlyraC Offline
                chrlyra
                wrote on last edited by
                #7

                @Yveaux rollercontainer pointed to this issue: https://github.com/mysensors/MySensors/issues/449, where hek asked him to add two lines of code, the first one being "if (presentation)".

                YveauxY 1 Reply Last reply
                0
                • chrlyraC chrlyra

                  @Yveaux rollercontainer pointed to this issue: https://github.com/mysensors/MySensors/issues/449, where hek asked him to add two lines of code, the first one being "if (presentation)".

                  YveauxY Offline
                  YveauxY Offline
                  Yveaux
                  Mod
                  wrote on last edited by
                  #8

                  @chrlyra That's what happens if you deviate from the topic... Anyway, thanks for the hint!

                  http://yveaux.blogspot.nl

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Rolo6442u
                    wrote on last edited by
                    #9

                    Great work ! This version looks a lot cleaner and the support for the RFM69 is more integrated. Thanks for that. One thing I'm missing and that's the specification of the encryption key for the RFM69. This was done in version 1.5.x by a user defined 16 bytes string. Now i see only a #define MY_RFM69_ENABLE_ENCRYPTION.
                    How and where is the key specified ? How does this make my setup unique ?

                    Thanks.

                    AnticimexA 1 Reply Last reply
                    0
                    • scalzS Offline
                      scalzS Offline
                      scalz
                      Hardware Contributor
                      wrote on last edited by
                      #10

                      @Rolo6442u
                      If I remember well, it is handled by SecurityPersonalizer.ino It is sketch for signing options, and i think you can set/generate your rf aes key there. It store it in eeprom. Then you upload the new sketch with encryption define enabled. For me it's ok, as i use signing which needs to use the personalizer.
                      I think this works like this, not sure as i have not enabled encryption yet. Am I right @Anticimex ?

                      1 Reply Last reply
                      0
                      • R Rolo6442u

                        Great work ! This version looks a lot cleaner and the support for the RFM69 is more integrated. Thanks for that. One thing I'm missing and that's the specification of the encryption key for the RFM69. This was done in version 1.5.x by a user defined 16 bytes string. Now i see only a #define MY_RFM69_ENABLE_ENCRYPTION.
                        How and where is the key specified ? How does this make my setup unique ?

                        Thanks.

                        AnticimexA Offline
                        AnticimexA Offline
                        Anticimex
                        Contest Winner
                        wrote on last edited by
                        #11

                        @Rolo6442u
                        @scalz is correct. Details are in the doxygen documentation for usage. Link is on the GitHub readme. Look under the signing module there.

                        Do you feel secure today? No? Start requiring some signatures and feel better tomorrow ;)

                        R 1 Reply Last reply
                        1
                        • AnticimexA Anticimex

                          @Rolo6442u
                          @scalz is correct. Details are in the doxygen documentation for usage. Link is on the GitHub readme. Look under the signing module there.

                          R Offline
                          R Offline
                          Rolo6442u
                          wrote on last edited by
                          #12

                          @Anticimex
                          @scalz
                          Thanks, I got it working. By setting it to "soft" this sketch writes all key's to eeprom and will not look for a hardware siging module. The RFM69 uses the EAS key for encryption. I defined my own key again in the sketch.
                          Nice solution !

                          AnticimexA 1 Reply Last reply
                          1
                          • R Rolo6442u

                            @Anticimex
                            @scalz
                            Thanks, I got it working. By setting it to "soft" this sketch writes all key's to eeprom and will not look for a hardware siging module. The RFM69 uses the EAS key for encryption. I defined my own key again in the sketch.
                            Nice solution !

                            AnticimexA Offline
                            AnticimexA Offline
                            Anticimex
                            Contest Winner
                            wrote on last edited by
                            #13

                            @Rolo6442u precisely. Glad to hear that the docs are helping :) and thanks!

                            Do you feel secure today? No? Start requiring some signatures and feel better tomorrow ;)

                            1 Reply Last reply
                            0
                            • pascalgauthierP Offline
                              pascalgauthierP Offline
                              pascalgauthier
                              wrote on last edited by
                              #14

                              How can i call the process() inside a while in the loop() ?

                              Thanks

                              nielsokkerN 1 Reply Last reply
                              0
                              • pascalgauthierP pascalgauthier

                                How can i call the process() inside a while in the loop() ?

                                Thanks

                                nielsokkerN Offline
                                nielsokkerN Offline
                                nielsokker
                                wrote on last edited by
                                #15

                                @pascalgauthier

                                I think the function "wait()" might be useful. It is like a sleep, but now it calls "process()"

                                pascalgauthierP 1 Reply Last reply
                                0
                                • nielsokkerN nielsokker

                                  @pascalgauthier

                                  I think the function "wait()" might be useful. It is like a sleep, but now it calls "process()"

                                  pascalgauthierP Offline
                                  pascalgauthierP Offline
                                  pascalgauthier
                                  wrote on last edited by
                                  #16

                                  @nielsokker said:

                                  @pascalgauthier

                                  I think the function "wait()" might be useful. It is like a sleep, but now it calls "process()"

                                  I was thinking of using it with wait(0) but I was wondering if there was any way to call the process directly.

                                  nielsokkerN 1 Reply Last reply
                                  0
                                  • pascalgauthierP pascalgauthier

                                    @nielsokker said:

                                    @pascalgauthier

                                    I think the function "wait()" might be useful. It is like a sleep, but now it calls "process()"

                                    I was thinking of using it with wait(0) but I was wondering if there was any way to call the process directly.

                                    nielsokkerN Offline
                                    nielsokkerN Offline
                                    nielsokker
                                    wrote on last edited by
                                    #17

                                    @pascalgauthier

                                    I'm not sure. I think the developers will know.

                                    1 Reply Last reply
                                    0
                                    • scalzS Offline
                                      scalzS Offline
                                      scalz
                                      Hardware Contributor
                                      wrote on last edited by
                                      #18

                                      @pascalgauthier why do you need to call process ? It's already done internally by the lib. Or is it for inside some longtime loop? you still can use process but now it's _process()
                                      Wait is not the same as sleep.
                                      Wait : wait for a time and call _process
                                      sleep: does not call _process. it sleeps. for a time if set

                                      pascalgauthierP 1 Reply Last reply
                                      0
                                      • scalzS scalz

                                        @pascalgauthier why do you need to call process ? It's already done internally by the lib. Or is it for inside some longtime loop? you still can use process but now it's _process()
                                        Wait is not the same as sleep.
                                        Wait : wait for a time and call _process
                                        sleep: does not call _process. it sleeps. for a time if set

                                        pascalgauthierP Offline
                                        pascalgauthierP Offline
                                        pascalgauthier
                                        wrote on last edited by
                                        #19

                                        @scalz
                                        Because i have while() that increment motion detected in a 30sec timeframe. And i would like to be sure that i'm not missing any cmd from the gateway. Do i need to define any additional library to use the _process() ?

                                        AWIA 1 Reply Last reply
                                        0
                                        • pascalgauthierP pascalgauthier

                                          @scalz
                                          Because i have while() that increment motion detected in a 30sec timeframe. And i would like to be sure that i'm not missing any cmd from the gateway. Do i need to define any additional library to use the _process() ?

                                          AWIA Offline
                                          AWIA Offline
                                          AWI
                                          Hero Member
                                          wrote on last edited by
                                          #20

                                          @pascalgauthier I would recommend making your while loop non-blocking i.s.o. hacking into a MySensors function.

                                          1 Reply Last reply
                                          1
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          14

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular