How to use transportInit() in 2.0.0 to re-initialize radio



  • I'm working on a sensor node that is set up to be able to turn off power to all of the peripherals (sensors, radio) when the arduino goes to sleep to minimize power consumption. When it wakes up it re-energizes the bus and I need to re-initialize the radio which I thought was what calling transportInit() does. It doesn't seem to be working out though. Below is my code:

    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    #define MY_NODE_ID 3
    #define MY_RF24_CE_PIN 8
    
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <prescaler.h>
    
    #define SKETCH_NAME "Sensor Node"
    #define SKETCH_MAJOR_VER "1"
    #define SKETCH_MINOR_VER "0"
    
    #define TEMPERATURE_SENSOR 1
    
    const byte EN = 17; //turns on the boost converter to get 3.3v
    const byte PER = 14; //peripherals (DS18B20, nRF24L01)
    const byte Wake_pin = 2;  //wake from sleep mode powerdown
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(TEMPERATURE_SENSOR, V_TEMP);
    
    
    void setup()  
    {  
      pinMode(EN,OUTPUT);
      digitalWrite(EN,LOW);  //give us 3.3v
      
      pinMode(PER, OUTPUT);   //for turning on peripherals
      digitalWrite(PER,LOW);  //peripherals on - P Chan FET
      
      digitalWrite (Wake_pin, HIGH);  //will wake with falling edge
    }
    
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);
    
      // Register binary input sensor to sensor_node (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      present(TEMPERATURE_SENSOR, S_TEMP);  
     
    }
    
    // Loop will iterate on changes on the BUTTON_PINs
    void loop() 
    {
    
         send(msg.set(20));
    
    
        
        
        digitalWrite (PER, HIGH); //turn off peripherals - PER is connected to a P-Chan Fet
        // Sleep for 10 seconds (just for testing)
        sleep(10000);
        digitalWrite (PER, LOW); //turn on peripherals
        //delay(1000);  //tried instituting a delay in case it needed a moment for power to stabilize
        transportInit();  //re-initialize radio
    } 
    

    Looking at the library files it seems that transportInit() just calls RF24_Initialize() in the RF24.cpp library file and RF24_Initialize() appears to do everything it needs to do to get the radio back up to snuff. The DEBUG output on the arduino serial monitor has a lot of different things. Below is a sample of the various messages:

    Starting sensor (RNNNA-, 2.0.0)
    TSM:INIT
    !TSM:RADIO:FAIL
    !TSM:FAILURE
    TSM:PDT
    TSM:INIT
    TSM:RADIO:OK
    TSP:ASSIGNID:OK (ID=3)
    TSM:FPAR
    TSP:MSG:SEND 3-3-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSP:MSG:READ 0-0-0 s=0,c=0,t=0,pt=0,l=0,sg=0:
    !TSP:MSG:PVER mismatch
    
    TSP:MSG:READ 0-0-0 s=0,c=0,t=0,pt=0,l=0,sg=0:
    !TSP:MSG:PVER mismatch
    TSP:MSG:READ 0-0-0 s=0,c=0,t=0,pt=0,l=0,sg=0:
    !TSP:MSG:PVER mismatch
    
    TSM:FPAR
    TSP:MSG:SEND 3-3-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    
    TSM:PDT
    !TSP:SEND:TNR
    !TSP:SEND:TNR
    !TSP:SEND:TNR
    !TSP:SEND:TNR
    !TSP:SEND:TNR
    

    In the openhab log I get a lot of this:

    2016-07-23 15:50:51.139 [DEBUG] [.b.m.internal.MySensorsBinding] - I_LOG_MESSAGE: TSP:MSG:READ 3-3-255 s=255,c=3,t=7,pt=0,l=0,sg=0:
    2016-07-23 15:50:51.141 [DEBUG] [.b.m.internal.MySensorsBinding] - I_LOG_MESSAGE: TSP:MSG:BC
    2016-07-23 15:50:51.142 [DEBUG] [.b.m.internal.MySensorsBinding] - I_LOG_MESSAGE: TSP:MSG:FPAR REQ (sender=3)
    2016-07-23 15:50:51.143 [DEBUG] [.b.m.internal.MySensorsBinding] - I_LOG_MESSAGE: TSP:CHKUPL:OK
    2016-07-23 15:50:51.144 [DEBUG] [.b.m.internal.MySensorsBinding] - I_LOG_MESSAGE: TSP:MSG:GWL OK
    2016-07-23 15:50:51.974 [DEBUG] [.b.m.internal.MySensorsBinding] - I_LOG_MESSAGE: !TSP:MSG:SEND 0-0-3-3 s=255,c=3,t=8,pt=1,l=1,sg=0,ft=0,st=fail:0
    

    Any thoughts would be greatly appreciated!



  • A few additional data points to ponder. If I run that sketch above it gives the wacky output that I pasted above. If I roll back the sketch to a previously working version (i.e. just commenting out the lines that turn the radio off and back on again) like this:

    void loop() 
    {
    
         send(msg.set(20));
    
    
        
        
        //digitalWrite (PER, HIGH); //turn off peripherals
        // Sleep for 10 seconds (just for testing)
        sleep(10000);
        //digitalWrite (PER, LOW); //turn on peripherals
        //delay(1000);  //tried instituting a delay in case it needed a moment for power to stabilize
        //transportInit();  //re-initialize radio
    } 
    

    ..the sketch still doesn't work. It requires me resetting the actual gateway that is attached to my Raspi before it will start to work again. So basically this sketch:

    void loop() 
    {
    
         send(msg.set(20));
    
    
        
        
        digitalWrite (PER, HIGH); //turn off peripherals
        // Sleep for 10 seconds (just for testing)
        sleep(10000);
        digitalWrite (PER, LOW); //turn on peripherals
        //delay(1000);  //tried instituting a delay in case it needed a moment for power to stabilize
        transportInit();  //re-initialize radio
    } 
    

    ..causes the gateway to go haywire.


  • Hardware Contributor

    try to put your pin initialisation in before() instead of setup(), you will see that will work better ;).

    before() is executed before Mysensors initialisation. then setup()
    That's why your radio is not initialized looking at logs. nothing to matter with transportinit() which is far after the real problem I think. And I confirm transportinit() works ok as I use it 🙂



  • @scalz Well that seems to have fixed the radio init problem and the Gateway exploding problem although I'm not sure how. Seems like I should be able to initialize my i/o pins anywhere in the sketch. Of course I'm not much of a programmer so it's not uncommon for me to not know why or how something works 😉 Thank you for that!!!

    Now I have a second issue. The sketch that doesn't include the radio turn off functionality sends data to an Openhab MySensors binding every 10 seconds. When I add the radio turn off functionality the binding catches two data points and then nothing after although the arduino serial monitor shows that the transmissions are successful. Here is the Serial monitor output:

    TSP:MSG:SEND 3-3-0-0 s=1,c=1,t=0,pt=2,l=2,sg=0,ft=0,st=ok:20
    TSP:MSG:SEND 3-3-0-0 s=1,c=1,t=0,pt=2,l=2,sg=0,ft=0,st=ok:20
    TSP:MSG:SEND 3-3-0-0 s=1,c=1,t=0,pt=2,l=2,sg=0,ft=0,st=ok:20
    TSP:MSG:SEND 3-3-0-0 s=1,c=1,t=0,pt=2,l=2,sg=0,ft=0,st=ok:20
    TSP:MSG:SEND 3-3-0-0 s=1,c=1,t=0,pt=2,l=2,sg=0,ft=0,st=ok:20
    TSP:MSG:SEND 3-3-0-0 s=1,c=1,t=0,pt=2,l=2,sg=0,ft=0,st=ok:20
    TSP:MSG:SEND 3-3-0-0 s=1,c=1,t=0,pt=2,l=2,sg=0,ft=0,st=ok:20
    

    Here is the Openhab log:

    2016-07-23 17:05:27.921 [DEBUG] [.b.m.internal.MySensorsBinding] - I_LOG_MESSAGE: TSP:MSG:READ 3-3-0 s=1,c=1,t=0,pt=2,l=2,sg=0:20
    2016-07-23 17:05:27.923 [DEBUG] [.b.m.internal.MySensorsBinding] - DTank = 20
    2016-07-23 17:05:27.923 [DEBUG] [.b.m.internal.MySensorsBinding] - internalReceiveUpdate(DTank,20) is called!
    2016-07-23 17:05:30.107 [DEBUG] [.b.m.internal.MySensorsBinding] - Gateway Version: 2.0.0
    2016-07-23 17:05:31.236 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'sitemaps'
    2016-07-23 17:05:31.238 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'persistence'
    2016-07-23 17:05:31.240 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'rules'
    2016-07-23 17:05:31.242 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'scripts'
    2016-07-23 17:05:31.244 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'items'
    2016-07-23 17:05:39.976 [DEBUG] [.b.m.internal.MySensorsBinding] - I_LOG_MESSAGE: TSP:MSG:READ 3-3-0 s=1,c=1,t=0,pt=2,l=2,sg=0:20
    2016-07-23 17:05:39.978 [DEBUG] [.b.m.internal.MySensorsBinding] - DTank = 20
    2016-07-23 17:05:39.978 [DEBUG] [.b.m.internal.MySensorsBinding] - internalReceiveUpdate(DTank,20) is called!
    2016-07-23 17:05:41.246 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'sitemaps'
    2016-07-23 17:05:41.248 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'persistence'
    2016-07-23 17:05:41.250 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'rules'
    2016-07-23 17:05:41.252 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'scripts'
    2016-07-23 17:05:41.254 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'items'
    2016-07-23 17:05:46.365 [DEBUG] [.b.m.internal.MySensorsBinding] - I_LOG_MESSAGE: TSP:SANCHK:OK
    2016-07-23 17:05:51.257 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'sitemaps'
    2016-07-23 17:05:51.259 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'persistence'
    2016-07-23 17:05:51.261 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'rules'
    2016-07-23 17:05:51.263 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'scripts'
    2016-07-23 17:05:51.265 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'items'
    2016-07-23 17:06:01.268 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'sitemaps'
    2016-07-23 17:06:01.270 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'persistence'
    2016-07-23 17:06:01.272 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'rules'
    2016-07-23 17:06:01.274 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'scripts'
    2016-07-23 17:06:01.276 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'items'
    2016-07-23 17:06:11.278 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'sitemaps'
    2016-07-23 17:06:11.280 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'persistence'
    2016-07-23 17:06:11.282 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'rules'
    2016-07-23 17:06:11.284 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'scripts'
    2016-07-23 17:06:11.286 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'items'
    2016-07-23 17:06:21.288 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'sitemaps'
    2016-07-23 17:06:21.290 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'persistence'
    2016-07-23 17:06:21.292 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'rules'
    2016-07-23 17:06:21.294 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'scripts'
    2016-07-23 17:06:21.295 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'items'
    2016-07-23 17:06:30.109 [DEBUG] [.b.m.internal.MySensorsBinding] - Gateway Version: 2.0.0
    2016-07-23 17:06:31.297 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'sitemaps'
    2016-07-23 17:06:31.299 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'persistence'
    2016-07-23 17:06:31.301 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'rules'
    2016-07-23 17:06:31.303 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'scripts'
    2016-07-23 17:06:31.305 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'items'
    2016-07-23 17:06:41.307 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'sitemaps'
    2016-07-23 17:06:41.309 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'persistence'
    2016-07-23 17:06:41.311 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'rules'
    2016-07-23 17:06:41.313 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'scripts'
    2016-07-23 17:06:41.315 [DEBUG] [.o.m.c.i.folder.FolderObserver] - Refreshing folder 'items'
    2016-07-23 17:06:46.342 [DEBUG] [.b.m.internal.MySensorsBinding] - I_LOG_MESSAGE: TSP:SANCHK:OK
    

    You can see that it catches two data transmissions and then nothing after. It works fine without transportInit() being called so I am a bit confused by it.


  • Hardware Contributor

    of course you can initialize your io where you want 🙂 except that you were enabling radio in setup. And between before() and setup(), mysensors lib was trying to init but no power on radio...so the logs.

    for your problem with openhab, i can't tell more..i don't know this controller. maybe try to add a sleep(100) or wait(100) between your sendings if it helps. you could increment value too, so you can see a bit more when it happens etc..
    though, what's your "sketch that doesn't include the radio turn off functionality"?



  • @scalz Oh ya that makes sense regarding the before() and setup().

    Regarding the sketch that doesn't include the radio turn off....I was just referring to the one that had the relevant lines commented out so:

    void loop() 
    {
    
         send(msg.set(20));
    
    
        
        
        //digitalWrite (PER, HIGH); //turn off peripherals
        // Sleep for 10 seconds (just for testing)
        sleep(10000);
        //digitalWrite (PER, LOW); //turn on peripherals
        //delay(1000);  //tried instituting a delay in case it needed a moment for power to stabilize
        //transportInit();  //re-initialize radio
    } 
    

    instead of:

    void loop() 
    {
    
         send(msg.set(20));
    
    
        
        
        digitalWrite (PER, HIGH); //turn off peripherals
        // Sleep for 10 seconds (just for testing)
        sleep(10000);
        digitalWrite (PER, LOW); //turn on peripherals
        //delay(1000);  //tried instituting a delay in case it needed a moment for power to stabilize
        transportInit();  //re-initialize radio
    }
    

 

432
Online

7.9k
Users

8.8k
Topics

93.8k
Posts