Temperature sensor sketch only sending battery info to mqtt



  • I have a newbie-pcb board with a Dallas temperature sensor attached. I mixed some sketches together to make it work.
    When starting the sensor the first time I can see all the actions performed. Reading temperature and battery percentage. But than after sleeping it is only reading and sending battery percentage (no voltage)? No temperature action is visible any-more. So, there must be a fault in my sketch which makes it read the temperature part only once? Any advice on this? Thanks.

    [code]
    
    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0: Henrik EKblad
     * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz)
     * 
     * DESCRIPTION
     * This sketch provides an example of how to implement a humidity/temperature
     * 
     * Dallas temperature sensor DS18B20
     *  
     * For more information, please visit:
     * http://www.mysensors.org/build/humidity
     * 
     HTU21D Humidity Sensor
     Hardware Connections (Breakoutboard to Arduino):
     -VCC = 3.3V
     -GND = GND
     -data  = D3 met pcb 470 uf resistor
     */
    
     
    #define MY_NODE_ID 4
    #define MY_PARENT_NODE_ID 0
    #define MY_PARENT_NODE_IS_STATIC
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached 
    #define MY_RADIO_NRF24
    //#define MY_RF24_PA_LEVEL RF24_PA_HIGH
    //#define MY_RADIO_RFM69
    //#define MY_RS485
     
    #include <MySensors.h> 
    //#include <Wire.h>
    #include <SPI.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    
    
    // Force sending an update of the temperature after n sensor reads, so a controller showing the
    // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
    // the value didn't change since;
    // i.e. the sensor would force sending an update every 
    //UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
    //static const uint8_t FORCE_UPDATE_N_READS = 10;
    
    #define COMPARE_TEMP 0 // Send temperature only if changed? 1 = Yes 0 = No
    // arrays to hold device address
    DeviceAddress insideThermometer;
    
    #define ONE_WIRE_BUS 3 // Pin where dallas sensor is connected 
    #define MAX_ATTACHED_DS18B20 1
    unsigned long SLEEP_TIME = 30000; // 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. 
    
    
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    bool receivedConfig = true;
    bool metric = true;
    
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_BATTERY 2
    #define CHILD_ID_VOLT 3
    //#define CHILD_ID_PRCNT 3
    
    float lastTemp;
    //float lastHum;
    
    uint8_t nNoUpdatesTemp;
    //uint8_t nNoUpdatesHum;
    //boolean metric = true;
    
    
    
    
    
    void presentation()  
    { 
     // Send the Sketch Version Information to the Gateway                                                                                                                     
      sendSketchInfo("Temperature Sensor", "1.1");      
       
     // Register all sensors to gw (they will be created as child devices)                                                                                                     
    //  present(CHILD_ID_HUM, S_HUM);                                                                                                                                          
      present(CHILD_ID_TEMP, S_TEMP);
       present(CHILD_ID_BATTERY, S_MULTIMETER);                                                                                                                                         
      metric = getControllerConfig().isMetric;
    
    // 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++) {   
         present(i, S_TEMP);
      }
       
    }
    
    int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                                                                                                                                                      
    //unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)  60000                                                                                           
    static int oldBatteryPcnt = 0;                                                                                                                                                                        
                                                                                                                                                                   
                                                                                                                                                                            
    //Create an instance of the object                                                                                                                                          
    
      MyMessage msg(0,V_TEMP);                                                                                                                                                                                                                                                                                                                           
    //MyMessage msgHum(CHILD_ID_HUM, V_HUM);                                                                                                                                      
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);                                                                                                                                   
    MyMessage msgBattery(CHILD_ID_BATTERY, V_VOLTAGE);
    //MyMessage msgBattery(CHILD_ID_BATTERY, V_PRC);
    
    
    void setup() {
        // Startup up the OneWire library
      sensors.begin();
    
         // use the 1.1 V internal reference
    #if defined(__AVR_ATmega2560__)
        analogReference(INTERNAL1V1);
    #else
        analogReference(INTERNAL);
    #endif
    
    // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
    
    } 
                                                                                                                                                                                                                                                                                                                                                     
    void loop()                                                                                                                                                                 
    {                                                                                                                                                                           
    
    
    // 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>((getControllerConfig().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
          send(msg.setSensor(i).set(temperature,1));
          // Save new temperatures for next compare
          lastTemperature[i]=temperature;
        }
      }
    
    
    // some delay here
      //delay(500);
    
       // get the battery Voltage
         
        int sensorValue = analogRead(BATTERY_SENSE_PIN);
        delay(500);
    #ifdef MY_DEBUG
        Serial.println(sensorValue);
    #endif
    
        // 1M, 470K divider across battery and using internal ADC ref of 1.1V
        // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
        // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
        // 3.44/1023 = Volts per bit = 0.003363075
        int batteryPcnt = sensorValue / 10;
    
    #ifdef MY_DEBUG
        float batteryV  = sensorValue * 0.003363075;
        Serial.print("Child ID ");
        Serial.print(CHILD_ID_BATTERY);
        Serial.print("Battery Voltage: ");
        Serial.print(batteryV);
        Serial.println(" V");
    
        Serial.print("Battery Percent: ");
        Serial.print(batteryPcnt);
        Serial.println(" %");
    #endif
    
        if (oldBatteryPcnt != batteryPcnt) {
            // Power up radio after sleep
                  
            sendBatteryLevel(batteryPcnt);
            oldBatteryPcnt = batteryPcnt;
        } 
                                                                                                                                                                          
      sleep(SLEEP_TIME); //sleep a bit  
                                                                                                                                           
    }
    [/code]```
    
    
    This is in the serial monitor:
    
    [code]```
    16 MCO:BGN:INIT NODE,CP=RNNNA---,VER=2.2.0
    26 TSM:INIT
    28 TSF:WUR:MS=0
    34 TSM:INIT:TSP OK
    36 TSM:INIT:STATID=4
    38 TSF:SID:OK,ID=4
    40 TSM:FPAR
    43 TSM:FPAR:STATP=0
    45 TSM:ID
    47 TSM:ID:OK
    47 TSM:UPL
    1730 !TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=NACK:1
    3739 TSM:UPL
    5421 !TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=1,st=NACK:1
    7430 TSM:UPL
    7477 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=2,st=OK:1
    7497 TSF:MSG:READ,0-0-4,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    7503 TSF:MSG:PONG RECV,HP=1
    7507 TSM:UPL:OK
    7510 TSM:READY:ID=4,PAR=0,DIS=1
    7518 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    7526 TSF:MSG:READ,0-0-4,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    7536 TSF:MSG:SEND,4-4-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.2.0
    7557 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    11247 !TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=11,pt=0,l=18,sg=0,ft=0,st=NACK:Temperature Sensor
    12939 !TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=1,st=NACK:1.1
    14628 !TSF:MSG:SEND,4-4-0-0,s=1,c=0,t=6,pt=0,l=0,sg=0,ft=2,st=NACK:
    16318 !TSF:MSG:SEND,4-4-0-0,s=2,c=0,t=30,pt=0,l=0,sg=0,ft=3,st=NACK:
    16324 MCO:REG:REQ
    18010 !TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=4,st=NACK:2
    21698 !TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=5,st=NACK:2
    21706 !TSM:READY:UPL FAIL,STATP
    25389 !TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=NACK:2
    29079 !TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=1,st=NACK:2
    31088 MCO:BGN:STP
    31137 MCO:BGN:INIT OK,TSP=1
    31141 MCO:SLP:MS=94,SMS=0,I1=255,M1=255,I2=255,M2=255
    31148 TSF:TDI:TSL
    31150 MCO:SLP:WUP=-1
    31154 TSF:TRI:TSB
    903
    Child ID 2Battery Voltage: 3.04 V
    Battery Percent: 90 %
    31657 !TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=2,st=NACK:90
    31668 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
    31674 TSF:TDI:TSL
    31676 MCO:SLP:WUP=-1
    31678 TSF:TRI:TSB
    31682 MCO:SLP:MS=94,SMS=0,I1=255,M1=255,I2=255,M2=255
    31688 TSF:TDI:TSL
    31690 MCO:SLP:WUP=-1
    31692 TSF:TRI:TSB
    831
    Child ID 2Battery Voltage: 2.79 V
    Battery Percent: 83 %
    32202 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=3,st=OK:83
    32208 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
    32217 TSF:TDI:TSL```

  • Mod

    First of all there are too many NACKs in the log, meaning you have a serious communication problem and you need to resolve that.
    Battery voltage is not sent because you are only sending battery %



  • @gohan

    Well the NACKS are gone 🙂 by setting the RF_24_PA_HIGH but still I do not get any temperature after the first one while starting up:

    16 MCO:BGN:INIT NODE,CP=RNNNA---,VER=2.2.0
    26 TSM:INIT
    28 TSF:WUR:MS=0
    34 TSM:INIT:TSP OK
    36 TSM:INIT:STATID=4
    38 TSF:SID:OK,ID=4
    40 TSM:FPAR
    43 TSM:FPAR:STATP=0
    45 TSM:ID
    47 TSM:ID:OK
    47 TSM:UPL
    1730 !TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=NACK:1
    3739 TSM:UPL
    5421 !TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=1,st=NACK:1
    7430 TSM:UPL
    7452 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=2,st=OK:1
    7460 TSF:MSG:READ,0-0-4,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    7467 TSF:MSG:PONG RECV,HP=1
    7471 TSM:UPL:OK
    7473 TSM:READY:ID=4,PAR=0,DIS=1
    7491 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    7499 TSF:MSG:READ,0-0-4,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    7512 TSF:MSG:SEND,4-4-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.2.0
    7530 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    9545 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=11,pt=0,l=18,sg=0,ft=0,st=OK:Temperature Sensor
    9586 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.1
    9801 TSF:MSG:SEND,4-4-0-0,s=1,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
    9840 TSF:MSG:SEND,4-4-0-0,s=2,c=0,t=30,pt=0,l=0,sg=0,ft=0,st=OK:
    9848 MCO:REG:REQ
    9861 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    9869 TSF:MSG:READ,0-0-4,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    9875 MCO:PIM:NODE REG=1
    9877 MCO:BGN:STP
    9926 MCO:BGN:INIT OK,TSP=1
    911
    Child ID 2Battery Voltage: 3.06 V
    Battery Percent: 91 %
    10438 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:91
    10446 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
    10452 TSF:TDI:TSL
    10455 MCO:SLP:WUP=-1
    10457 TSF:TRI:TSB
    831
    Child ID 2Battery Voltage: 2.79 V
    Battery Percent: 83 %
    10967 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:83
    10975 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
    10981 TSF:TDI:TSL
    10983 MCO:SLP:WUP=-1
    10985 TSF:TRI:TSB
    830
    Child ID 2Battery Voltage: 2.79 V
    Battery Percent: 83 %
    11493 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
    11501 TSF:TDI:TSL
    11503 MCO:SLP:WUP=-1
    11505 TSF:TRI:TSB
    833
    Child ID 2Battery Voltage: 2.80 V
    Battery Percent: 83 %
    12013 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
    12021 TSF:TDI:TSL
    12023 MCO:SLP:WUP=-1
    12025 TSF:TRI:TSB
    830
    Child ID 2Battery Voltage: 2.79 V
    Battery Percent: 83 %
    12533 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
    12541 TSF:TDI:TSL```


  • You seem to send the temp messages under a different ChildID than it is presented:
    presentation takes "1", send uses "0". You have to allign that, e.g. by using

    send(msg.setSensor(i+CHILD_ID_TEMP).set(temperature,1));
    

    But as long as there's just one DS18B20 doing that dynamically doesn't really make sense. Using the defined message structure (MyMessage msgTemp...) could make things more transparent.

    Also there's no sending command for voltage. So why do you expect the controller to receive also voltage?


Log in to reply
 

Suggested Topics

  • 8
  • 16
  • 3
  • 5
  • 87
  • 12
  • 5

1
Online

11.2k
Users

11.1k
Topics

112.5k
Posts