Problem with dallastemperature sensor, node id.



  • So I'm having some issues with a newly built dallastemperature sensor. I'm using an arduino Nano, nrf24l01+ and 2 ds18b20. Used the latest library, with Arduino IDE 1.6.6 on Mac OSx.

    Other side is a raspberry pi with a standard serial gateway (arduino nano, nrf24l01+ high power). Im running home-assistant on the raspberry. Home-assistant can see the serial gateway and it starts up as it should, the sensor-node however doesn't seem to report as it should.

    The mysensor.json file that home-assistant creates just contain:
    {"1": {"sketch_name": "Temperature Sensor", "type": 17, "children": {}, "sketch_version": "1.1", "battery_level": 0, "sensor_id": 1}}

    And the log output of home-assistant/serial-gateway is:

    Feb 02 18:23:41 raspberrypi hass[420]: INFO:mysensors.mysensors:n:0 c:0 t:3 s:9 p:read: 1-1-0 s=255,c=0,t=17,pt=0,l=5,sg=0:1.5.3
    Feb 02 18:23:41 raspberrypi hass[420]: INFO:homeassistant.components.mysensors:update sensor_update: node 1
    Feb 02 18:23:42 raspberrypi hass[420]: INFO:mysensors.mysensors:n:0 c:0 t:3 s:9 p:read: 1-1-0 s=255,c=3,t=6,pt=1,l=1,sg=0:0
    Feb 02 18:23:42 raspberrypi hass[420]: INFO:mysensors.mysensors:n:0 c:0 t:3 s:9 p:send: 0-0-1-1 s=255,c=3,t=6,pt=0,l=1,sg=0,st=ok:M
    Feb 02 18:23:43 raspberrypi hass[420]: INFO:mysensors.mysensors:n:0 c:0 t:3 s:9 p:read: 1-1-0 s=255,c=3,t=11,pt=0,l=18,sg=0:Temperature Senso
    Feb 02 18:23:43 raspberrypi hass[420]: INFO:homeassistant.components.mysensors:update sensor_update: node 1
    Feb 02 18:23:43 raspberrypi hass[420]: INFO:mysensors.mysensors:n:0 c:0 t:3 s:9 p:read: 1-1-0 s=255,c=3,t=12,pt=0,l=3,sg=0:1.1
    Feb 02 18:23:43 raspberrypi hass[420]: INFO:homeassistant.components.mysensors:update sensor_update: node 1
    

    I'm hoping that someone can point me in the right direction on this. It is for whatever reason not showing up in home-assistant at all. And I'm unsure if i've made a mistake somewhere.

    If this is posted in the wrong category I'm sorry.

    Cheers
    Henrik


  • Plugin Developer

    @heggrik

    Hi!

    Have you read the guide on how to present a sensor to home assistant here?

    https://home-assistant.io/components/mysensors/

    If not, try those steps. If still no success, post your home assistant log after start and your sensor sketch here, and I'll troubleshoot it together with you. I'll be back later tonight.

    You can tag the topic with home assistant, or post under that category, next time.



  • Hi,

    I read the guide, the only thing i couldn't figure out was the "manual node id, or AUTO". Im not entirely sure how to define this. I used the mysensors-library standard dallastemperature-sketch

    Sketch:

    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define COMPARE_TEMP 0 // 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
    unsigned long SLEEP_TIME = 10000; // Sleep time between reads (in milliseconds)
    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. 
    MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);
    
    void setup()  
    { 
      // Startup up the OneWire library
      sensors.begin();
      // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
    
      // Startup and initialize MySensors library. Set callback for incoming messages. 
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.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++) {   
         gw.present(i, S_TEMP);
      }
    }
    
    
    void loop()     
    {     
      // Process incoming messages (like config from server)
      gw.process(); 
    
      // 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)
      gw.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>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(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
          gw.send(msg.setSensor(i).set(temperature,1));
          // Save new temperatures for next compare
          lastTemperature[i]=temperature;
        }
      }
      gw.sleep(SLEEP_TIME);
    }
    

    What i managed to miss was a warning in the home-assistant.log 16-02-01 19:40:03 mysensors.mysensors: Error decoding message from gateway, probably received bad byte.

    Not sure what that means exactly.
    At startup it shows:Feb 02 19:34:48 raspberrypi hass[31732]: INFO:homeassistant.components.http:Starting web interface at http://0.0.0.0:8123 Feb 02 19:34:48 raspberrypi hass[31732]: INFO:mysensors.mysensors:Trying to connect to /dev/ttyUSB0 Feb 02 19:34:48 raspberrypi hass[31732]: INFO:homeassistant.core:Timer:starting Feb 02 19:34:48 raspberrypi hass[31732]: INFO:homeassistant.components.mysensors:update persistence: node 1 Feb 02 19:34:48 raspberrypi hass[31732]: INFO:netdisco.service:Scanning Feb 02 19:34:50 raspberrypi hass[31732]: INFO:homeassistant.components.media_player:Updating media_player entities Feb 02 19:34:50 raspberrypi hass[31732]: INFO:plexapi:GET http://10.0.1.11:32400/clients Feb 02 19:34:50 raspberrypi hass[31732]: INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 10.0.1.11 Feb 02 19:34:50 raspberrypi hass[31732]: INFO:plexapi:GET http://10.0.1.11:32400/status/sessions Feb 02 19:34:50 raspberrypi hass[31732]: INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 10.0.1.11 Feb 02 19:34:51 raspberrypi hass[31732]: INFO:mysensors.mysensors:/dev/ttyUSB0 is open... Feb 02 19:34:51 raspberrypi hass[31732]: INFO:mysensors.mysensors:Connected to /dev/ttyUSB0 Feb 02 19:34:51 raspberrypi hass[31732]: INFO:mysensors.mysensors:n:0 c:0 t:3 s:9 p:gateway started, id=0, parent=0, distance=0 Feb 02 19:35:00 raspberrypi hass[31732]: INFO:homeassistant.components.sensor:Updating sensor entities


  • Plugin Developer

    @heggrik

    First, looking at the log, that looks good! Btw, you can post multiline code like this in this forum:

    123
    4567890123456789
    

    The second last line in the log tells you that the gateway has started and is waiting for messages.

    If you get the "bad byte" warning at startup it's good to restart home assistant, and make sure you get the confirmation from the gateway.

    That warning tells you there's some issue with the serial connection, but a restart usually solves that. If you always get the warning you should try a different version of pyserial.

    Moving on...

    Your sketch has a couple of problems. In the setup function, you should present all the sensors. In your case this is done in a for loop. But right now your endpoint is the same as the start point, 0. You should change the variable that defines the number of sensors from 0 to how many 1wire sensors you have. Try with 1 for example.

    For each sensor you present, you have to have a corresponding message setup. That's done before the setup function, so that the message variable(s) can be used everywhere in the sketch after they are declared.

    Right now, you only declare one message, but at the same time use a for loop that could present multiple sensors. So there will be a problem if you have more than one sensor. You should setup one message per sensor. Either you do this one line per message, or you use another for loop and an array to store the message variables. I've posted an example how to do the latter, in another topic about home assistant. I can find it for you in a bit.

    The last thing to change, is to send at least one value per sensor in the end of the setup function. See the serial API for example.

    I can post more examples if you need, in a bit, when I get home. On my cell right now.

    I forgot... Regarding setting node id, that is done in the gw.begin() function call. The default value is AUTO, which will ask the controller for a node id. Look at the API for this function. I can give you more info in a bit.


  • Plugin Developer

    @heggrik

    Sorry, one correction, the number of sensors are actually set in the setup function by calling the 1wire library, so you don't have to change that.

    And another correction... it seems the sensor of the message is set right before the value is set, so you might not neee to do multiple initializations of the messages.


  • Plugin Developer

    @heggrik

    Sorry for blabbering on, without reading the code properly. I can blame my cell phone, maybe. 😄

    So, all in all, the only thing I can see wrong with the sketch is, you need to send the initial values, one per sensor, in the end of the setup function.

    void setup()  
    { 
    ...
      // Present all sensors to controller
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
        gw.present(i, S_TEMP);
        gw.send(msg.setSensor(i).set(0.0,1));
      }
    }
    

    Regarding setting node id. This is the full default call of gw.begin():
    gw.begin(NULL, AUTO, false)

    The first argument (NULL) is for a callback function for incoming messages. Since this is a pure sensor sketch, you probably don't expect incoming messages. NULL means you don't declare a callback. The second arg (AUTO) is for node id. AUTO requests a node id from controller. You can set a manual node id, by changing AUTO to an integer digit, for example: gw.begin(NULL, 1, false). The third arg (false) is to enable or disable repeater mode on the node. To enable repeater mode, change false for true.

    Make sure you see the gateway started message, then power up your node, and keep watching the log output from home assistant. It should look like your first post, but after that you should also see the presentation messages and the set value messages, one per sensor.

    Edit
    There's actually a fourth argument to gw.begin(): parentNodeId
    You can set a static parent node id, that your node will always try to communicate through, instead of trying to find the shortest route to the gateway automatically. I would say this is for advance use, when you are sure of what you are doing.



  • Wow, this was a lot of information at once 🙂

    I'm going to go through the code tomorrow and actually figure out whats going on. I'm contemplating actually writing my own code, instead of relying on someone else's. Might make things easier (or harder 😛 ) but in the end i think i might benefit from it.

    i changed the code i had with the changes you suggested, and i can see the messages coming through to the gateway, but i still can't actually get home-assistant to display them. Trying to figure out if it's the code on the sensor, the code on the gateway or somewhere in-between that the message gets lost.

    i really appreciate all your help, i'll get back as soon as i've revised my code and (knock on wood) got it to work 🙂

    Again, thanks for all the help!


Log in to reply
 

Suggested Topics

  • 2
  • 3
  • 10
  • 4
  • 2
  • 6

217
Online

9.7k
Users

10.3k
Topics

106.4k
Posts