Convert sketch from 1.3 to 1.4



  • @C4Vette

    "Are there any guidelines on how to convert a sketch that was made for the 1.3 libraries to 1.4?
    I would very much like to use the DimmableLED sketch but I know too little about coding. I found a few obvious differences but nevertheless did not succeed in making the sketch work.

    I found it here: http://forum.micasaverde.com/index.php/topic,23342.0.html and I know the maker blacey is also on this forum and think it is a very nice sketch. The only way I see how to get this to work is to downgrade to the 1.3 libraries but than I would have to start all over."

    <>
    It's not easy to cover all possibilities but here are a few notes and examples based on a temperature sensor plus some other info that might help with the DimmableSketch. I can re-edit this post if there are any corrections needed or something else needs to be added.

    AT THE START OF THE PROGRAM:

    #include <Sensor.h>  change to  #include <MySensor.h>
    #include <EEPROM.h>  <-- this line is no longer required  - delete
    #include <RF24.h>    <-- this line is no longer required  - delete
    Sensor gw;           change to -->  MySensor gw;
    Sensor gw(9,10);     change to -->  MySensor gw;
    
    // Initialize temperature message   <-- insert line
    MyMessage msg(0,V_TEMP);            <-- insert line
    

    IN void setup():

    // send the Sketch Version Information to the Gateway   <-- insert line
    gw.sendSketchInfo("My sketch name", "0.50");            <-- insert line
    
    gw.sendSensorPresentation(i, S_TEMP);  change to -->  gw.present(i, S_TEMP);
    
    metric = gw.isMetricSystem();  change to -->  metric = gw.getConfig().isMetric;
    

    IN void loop():

    // process incoming messages (like config from server)   <-- insert line
    gw.process();                                        <-- insert line
    
    gw.sendVariable(i, V_TEMP, temperature, 1);  change to -->  gw.send(msg.setSensor(i).set(temperature,1));  // send float with one decimal point
    

    SEARCH FOR THE FOLLOWING:

    gw.powerUp();   <-- this line is no longer required - delete if found
    
    sendSensorPresentation   change variable name to --> present
    

    If the following is found then this can be deleted:

    // Set RADIO_ID to something unique in your sensor network (1-254)
    // or set to AUTO if you want gw to assign a RADIO_ID for you.
    #define RADIO_ID AUTO
    
    If the #define RADIO_ID AUTO was found and deleted as above then change this also
    gw.begin( RADIO_ID ); change to --> gw.begin();
    

    OTHER EXAMPLE MESSAGES:

    // batteryPcnt is a uint8_t
    gw.sendBatteryLevel(batteryPcnt);
    
    // I've not checked this
    gw.sendVariable( 0, V_DIMMER, currentLevel ); change to --> gw.send(currentLevel);
    

    INITIALIZING THE SENSOR TO HANDLE A MESSAGE FROM THE MAIN GATEWAY:

    // handle incoming messages
    void incomingMessage(MyMessage message)
    {
       // handle your messages here
       // I've not checked this
       setDimmableLEDState(message);
    }
    
    // the call back incomingMessage receives any incoming messages
    gw.begin(incomingMessage);

  • Contest Winner

    @a-lurker

    I looked this over, thanks. This, and @hek's update on the new version are falling a bit short for me to convert some of my sketches.

    I guess I really need to see some better examples of converted code, before and after.

    I must be missing something, to me it should be more intuitive what to do here.


  • Admin

    @BulldogLowell

    You just have to compare the provided examples between 1.3<->1.4. They have all been converted.


  • Contest Winner

    @hek

    MyMessage msg(0,V_TEMP); 
    

    Is this needed for every child device??


  • Mod

    @BulldogLowell yes, if you declare a message for each child and initialize it once.
    You can also create a single message in loop () and change its properties on the fly (don't have my PC near right now, so no code example.... 😢


  • Contest Winner

    @Yveaux

    thanks, I guess I have tunnel vision here.

    I have 3 hours on a plane tonight, I'll fiddle with it then and see if I can't get a sketch to even compile!

    gw.loadState???

    where is all of this explained? Did I miss some document that has all of these?


  • Hero Member

    @hek So, all the examples provided on MySensors.org are already configured for the 1.4 Library including the Serial Gateway? It will probably be easier to simply make minor tweaks to the examples to suit my specific configs then.


  • Mod

    @BulldogLowell The 1.4 library is still under development and documentation is only present in the code. If you're unable to read the code then maybe you should delay the switch to 1.4 until released and stable...


  • Hero Member

    @Yveaux Good idea 🙂

    John


  • Contest Winner

    @Yveaux said:

    If you're unable to read the code then maybe you should delay the switch to 1.4 until released and stable...

    sort of goes without saying, right?!!

    I can read the code, I just have to spend the time deciphering the calls.

    I have built some more complex sketches that were using a lot of the richness of the MySensors capability, I am just trying to piece it all together from the bits that are in each (very lean) example.


  • Mod

    @BulldogLowell said:

    @Yveaux said:

    sort of goes without saying, right?!!

    Not necessarily... Some people just want it because it's new and probably better.
    Then they get frustrated when they run into problems.

    I'm not saying you're one of them and I also don't mean you cannot read code, I just want to stress that it might be better to wait unless you are prepared to run into troubles 😉


  • Contest Winner

    @Yveaux

    yeah, perhaps I'll go back to creating new stuff and wait for the dust to settle on the API.

    I may sneak n a bit of time on this, though. I cannot stand not knowing! 🙂

    thanks!!


  • Mod

    @BulldogLowell Just some stuff for in the plane tonight, should you decide to dive into 1.4 a little deeper.
    It shows how to create and send messages with the 1.4 library.

    The example below shows 3 possible scenarios, each with their own pros & cons. Combinations are also possible.
    It will depend on the application and your preference which one to choose.

    I prefer option 1.

    #include <MySensor.h>
    
    #define NODE_ID              (64)
    #define CHILD_ID_TEMP         (0)
    #define CHILD_ID_HUMIDITY     (1)
    #define CHILD_ID_LIGHT        (2)
    
    MySensor gw;
    
    void setup()
    {
      gw.begin( NULL, NODE_ID, false /*relay*/ );
      gw.sendSketchInfo("My 1.4 Sensor", "1.0");
      gw.present(CHILD_ID_TEMP, S_TEMP);
      gw.present(CHILD_ID_HUMIDITY, S_HUM);
      gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
    }
    
    void loop()
    {
      // ... fetch data from sensors ...
      double humi = SHT2x.GetHumidity();
      double temp = SHT2x.GetTemperature();
      int lightLevel = analogRead(LDR_PIN)/10.23; 
    
      // Option 1 -- compact, message on stack:
      // Create a new MyMessage instance for every message to send,
      // and set the value by chaining (using the reference to the
      // MyMessage instance returned by the constructor)
      gw.send( MyMessage(CHILD_ID_TEMP, V_TEMP).set(temp, 1) );
      gw.send( MyMessage(CHILD_ID_HUMIDITY, V_HUM).set(humi, 1) );
      gw.send( MyMessage(CHILD_ID_LIGHT, V_LIGHT_LEVEL).set(lightLevel) );
    
      // Option 2 -- longer, message in RAM:
      // Create the MyMessage instance once, and re-use it for every sensor
      static MyMessage msg;
      msg.setSensor(CHILD_ID_TEMP);
      msg.setType(V_TEMP);
      msg.set(temp, 1);
      gw.send(msg);
    
      msg.setSensor(CHILD_ID_HUMIDITY);
      msg.setType(V_HUM);
      msg.set(humi, 1);
      gw.send(msg);
      // etc.
    
      // Option 3 -- static msg for each sensor. More RAM, less CPU:
      // Create a MyMessage instance for each sensor once.
      static MyMessage msgTemp;
      static MyMessage msgHum;
      static MyMessage msgLight;
      static bool initDone = false;
      if (!initDone)   // Better to do the init once in setup(), it is only here for clarity.
      {
        msgTemp.setSensor(CHILD_ID_TEMP);
        msgTemp.setType(V_TEMP);
        msgTemp.setSensor(CHILD_ID_HUMIDITY);
        msgTemp.setType(V_HUM);
        msgTemp.setSensor(CHILD_ID_LIGHT);
        msgTemp.setType(V_LIGHT_LEVEL);
        initDone = true;
      }
      msgTemp.set(temp, 1);
      gw.send(msgTemp);
      msgHum.set(humi, 1);
      gw.send(msgHum);
      msgLight.set(light);
      gw.send(msgLight);
    
      // ... e.g. sleep the CPU & radio
    }

  • Contest Winner

    @Yveaux

    WOW! thanks!

    I appreciate that and will look it over. Hopefully I'll have something to share with you tomorrow (or a problem that I need help).

    thanks again.

    Jim


Log in to reply
 

Suggested Topics

  • 1
  • 2
  • 1
  • 5
  • 1
  • 2

9
Online

11.4k
Users

11.1k
Topics

112.7k
Posts