Presentation fails for one sensor but works for another



  • Hi, I have a node that measures temperature and also reports battery level. For some reason it fails to present the temp sensor properly but is able to present the battery sensor. What could possibly cause this?

    Here is my sketch:

    /**
     * Report temperature from attached DS18B20(s) on pin ONE_WIRE_BUS.
     * Sleep SLEEP_TIME (millisecs) between each reading.
     * Also report battery level using the Vcc lib.
     */
    
    #define MY_OTA_FIRMWARE_FEATURE
    
    #define MY_NODE_ID AUTO
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <Vcc.h>
    #include <SPI.h>
    #include <MySensors.h>  
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    
    #define SLEEP_TIME 600000 // Report every 10 mins
    //#define SLEEP_TIME 60000 // Report every 1 min
    
    // Battery measurement 
    const float VccCorrection = 1.0/1.0;  // Measured Vcc by multimeter divided by reported Vcc
    Vcc vcc(VccCorrection);
    float oldBatteryVolts = 0.0;
    #define BATT_SENSOR_ID 100
    // Initialize battery message
    MyMessage battmsg(BATT_SENSOR_ID,V_VOLTAGE);
    
    // One wire/temp
    OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    bool receivedConfig = false;
    bool metric = true;
    
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);
    
    void before()
    {
      // Startup up the OneWire library
      sensors.begin();
    }
    
    void setup()  
    { 
      // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
    }
    
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Temperature Sensor", "1.1");
    
      // Fetch the number of attached temperature sensors  
      numSensors = sensors.getDeviceCount();
    
      // Present all sensors to controller
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
         Serial.println("Presenting temp sensor");
         present(i, S_TEMP);
      }
      Serial.println("Presenting battery sensor");
      present(BATT_SENSOR_ID, S_MULTIMETER);
    }
    
    void loop()     
    {     
      // Check battery first
      float batteryVolts = round(vcc.Read_Volts()*100.0)/100.0;
      if (oldBatteryVolts != batteryVolts) {
        send(battmsg.set(batteryVolts, V_VOLTAGE));
        oldBatteryVolts = batteryVolts;
      }
      
      // Fetch temperatures from Dallas sensors
      sensors.requestTemperatures();
    
      // query conversion time and sleep until conversion completed
      int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
      // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
      sleep(conversionTime);
    
      // Read temperatures and send them to controller 
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
     
        // Fetch and round temperature to one decimal
        float temperature = static_cast<float>(static_cast<int>(sensors.getTempCByIndex(i) * 10.)) / 10.;
     
        // Only send data if temperature has changed and no error
        #if COMPARE_TEMP == 1
        if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
        #else
        if (temperature != -127.00 && temperature != 85.00) {
        #endif
     
          // Send in the new temperature
          send(msg.setSensor(i).set(temperature,1));
          // Save new temperatures for next compare
          lastTemperature[i]=temperature;
        }
      }
      smartSleep(SLEEP_TIME);
    }
    

    And here's the serial log when it starts:

    0 MCO:BGN:INIT NODE,CP=RNONA--,VER=2.1.1
    4 MCO:BGN:BFR
    65 TSM:INIT
    65 TSF:WUR:MS=0
    73 TSM:INIT:TSP OK
    75 TSF:SID:OK,ID=233
    77 TSM:FPAR
    114 TSF:MSG:SEND,233-233-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    2123 !TSM:FPAR:NO REPLY
    2125 TSM:FPAR
    2162 TSF:MSG:SEND,233-233-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    3092 TSF:MSG:READ,0-0-233,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    3098 TSF:MSG:FPAR OK,ID=0,D=1
    4171 TSM:FPAR:OK
    4171 TSM:ID
    4173 TSM:ID:OK
    4175 TSM:UPL
    4182 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    4292 TSF:MSG:READ,0-0-233,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    4298 TSF:MSG:PONG RECV,HP=1
    4302 TSM:UPL:OK
    4304 TSM:READY:ID=233,PAR=0,DIS=1
    4311 TSF:MSG:SEND,233-233-0-0,s=255,c=4,t=0,pt=6,l=10,sg=0,ft=0,st=OK:FFFFFFFFFFFFFFFF0300
    4321 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    4608 TSF:MSG:READ,0-0-233,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    4616 TSF:MSG:SEND,233-233-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.1.1
    4626 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    5048 TSF:MSG:READ,0-0-233,s=255,c=3,t=6,pt=0,l=6,sg=0:Metric
    5056 TSF:MSG:ACK REQ
    5060 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=6,pt=0,l=6,sg=0,ft=0,st=OK:Metric
    5070 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=11,pt=0,l=18,sg=0,ft=0,st=OK:Temperature Sensor
    5083 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.1
    Presenting temp sensor
    5126 !TSF:MSG:SEND,233-233-0-0,s=0,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=NACK:
    Presenting battery sensor
    5136 TSF:MSG:SEND,233-233-0-0,s=100,c=0,t=30,pt=0,l=0,sg=0,ft=1,st=OK:
    5144 MCO:REG:REQ
    5181 !TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=NACK:2
    5861 TSF:MSG:READ,0-0-233,s=255,c=3,t=6,pt=0,l=6,sg=0:Metric
    7190 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=1,st=OK:2
    7266 TSF:MSG:READ,0-0-233,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    7272 MCO:PIM:NODE REG=1
    7274 MCO:BGN:STP
    7276 MCO:BGN:INIT OK,TSP=1
    7282 TSF:MSG:SEND,233-233-0-0,s=100,c=1,t=38,pt=7,l=5,sg=0,ft=0,st=OK:3.30999990
    7292 MCO:SLP:MS=750,SMS=0,I1=255,M1=255,I2=255,M2=255
    7299 MCO:SLP:TPD
    7301 MCO:SLP:WUP=-1
    7333 TSF:MSG:SEND,233-233-0-0,s=0,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:24.5
    7342 MCO:SLP:MS=600000,SMS=1,I1=255,M1=255,I2=255,M2=255
    7350 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=22,pt=5,l=4,sg=0,ft=0,st=OK:74
    7698 TSF:MSG:READ,0-0-233,s=255,c=3,t=6,pt=0,l=1,sg=0:M
    7847 TSF:MSG:READ,0-0-233,s=255,c=3,t=6,pt=0,l=1,sg=0:M
    7858 MCO:SLP:TPD
    

    As you can see, the presentation for sensor id 0 fails - this is the temp sensor. Sending a value for the same sensor works fine though.



  • I worked around it by checking the return value of present() to see if the presentation was successful or not, and retrying if it wasn't.

    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Temperature Sensor", "1.1");
    
      // Fetch the number of attached temperature sensors  
      numSensors = sensors.getDeviceCount();
    
      // Present all sensors to controller
      bool presented;
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
         presented = false;  
         while (!presented) {
          Serial.println("Presenting temp sensor");
          presented = present(i, S_TEMP);
          if (!presented) {
            Serial.println("Presentation failed - retrying");
            sleep(1000);
          }
         }
      }
      Serial.println("Presenting battery sensor");
      presented = false;  
      while (!presented) {
        presented = present(BATT_SENSOR_ID, S_MULTIMETER);
        if (!presented) {
          Serial.println("Presentation failed - retrying");
          sleep(1000);
        }
      }
    }
    

    Here's the log with the above change:

    0 MCO:BGN:INIT NODE,CP=RNONA--,VER=2.1.1
    4 MCO:BGN:BFR
    65 TSM:INIT
    65 TSF:WUR:MS=0
    73 TSM:INIT:TSP OK
    75 TSF:SID:OK,ID=233
    77 TSM:FPAR
    114 TSF:MSG:SEND,233-233-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    2123 !TSM:FPAR:NO REPLY
    2125 TSM:FPAR
    2162 TSF:MSG:SEND,233-233-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    2967 TSF:MSG:READ,0-0-233,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    2973 TSF:MSG:FPAR OK,ID=0,D=1
    4171 TSM:FPAR:OK
    4171 TSM:ID
    4173 TSM:ID:OK
    4175 TSM:UPL
    4179 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    4292 TSF:MSG:READ,0-0-233,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    4298 TSF:MSG:PONG RECV,HP=1
    4302 TSM:UPL:OK
    4304 TSM:READY:ID=233,PAR=0,DIS=1
    4311 TSF:MSG:SEND,233-233-0-0,s=255,c=4,t=0,pt=6,l=10,sg=0,ft=0,st=OK:FFFFFFFFFFFFFFFF0300
    4321 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    4608 TSF:MSG:READ,0-0-233,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    4616 TSF:MSG:SEND,233-233-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.1.1
    4626 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    5052 TSF:MSG:READ,0-0-233,s=255,c=3,t=6,pt=0,l=6,sg=0:Metric
    5058 TSF:MSG:ACK REQ
    5064 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=6,pt=0,l=6,sg=0,ft=0,st=OK:Metric
    5074 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=11,pt=0,l=18,sg=0,ft=0,st=OK:Temperature Sensor
    5087 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.1
    Presenting temp sensor
    5130 !TSF:MSG:SEND,233-233-0-0,s=0,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=NACK:
    Presentation failed - retrying
    5138 MCO:SLP:MS=1000,SMS=0,I1=255,M1=255,I2=255,M2=255
    5146 MCO:SLP:TPD
    5148 MCO:SLP:WUP=-1
    Presenting temp sensor
    5152 TSF:MSG:SEND,233-233-0-0,s=0,c=0,t=6,pt=0,l=0,sg=0,ft=1,st=OK:
    Presenting battery sensor
    5163 TSF:MSG:SEND,233-233-0-0,s=100,c=0,t=30,pt=0,l=0,sg=0,ft=0,st=OK:
    5171 MCO:REG:REQ
    5175 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    5500 TSF:MSG:READ,0-0-233,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    5507 MCO:PIM:NODE REG=1
    5509 MCO:BGN:STP
    5511 MCO:BGN:INIT OK,TSP=1
    5517 TSF:MSG:SEND,233-233-0-0,s=100,c=1,t=38,pt=7,l=5,sg=0,ft=0,st=OK:3.32999990
    5527 MCO:SLP:MS=750,SMS=0,I1=255,M1=255,I2=255,M2=255
    5533 MCO:SLP:TPD
    5535 MCO:SLP:WUP=-1
    5570 TSF:MSG:SEND,233-233-0-0,s=0,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:25.5
    5578 MCO:SLP:MS=600000,SMS=1,I1=255,M1=255,I2=255,M2=255
    5586 TSF:MSG:SEND,233-233-0-0,s=255,c=3,t=22,pt=5,l=4,sg=0,ft=0,st=OK:75
    6096 MCO:SLP:TPD
    


  • Do you use some kind of battery booster / step up regulator?


  • Mod

    @maghac I guess you're flooding the gateway with messages during startup.

    Could you give this a try?

      // Present all sensors to controller
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
         wait(200);     // wait for 200ms between presenting sensors
         Serial.println("Presenting temp sensor");
         present(i, S_TEMP);
      }
    


  • @xydix No, nothing at all, just the radio and a DS18b20, powered by 2xAA batteries.



  • @Yveaux That's possible. When I apply the workaround above, it works on the second try, which is after a 1000ms delay.

    I'll try your suggestion and see if it helps.


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.