Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Announcements
  3. 💬 Temperature Sensor

💬 Temperature Sensor

Scheduled Pinned Locked Moved Announcements
171 Posts 40 Posters 54.7k Views 36 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mfalkviddM Offline
    mfalkviddM Offline
    mfalkvidd
    Mod
    wrote on last edited by
    #115

    Ok. I give up. If the node isn't executing the serial prints I know no way of troubleshooting.

    DickD 1 Reply Last reply
    0
    • mfalkviddM mfalkvidd

      Ok. I give up. If the node isn't executing the serial prints I know no way of troubleshooting.

      DickD Offline
      DickD Offline
      Dick
      wrote on last edited by
      #116

      @mfalkvidd I ony can tell "thanksfo the support ad you time" . Have a nice wekend!!!

      1 Reply Last reply
      1
      • skywatchS Offline
        skywatchS Offline
        skywatch
        wrote on last edited by
        #117

        I have been frustrated with this for weeks now. Today I notice that void loop is not closed with a curly brace and if I add one it does not compile.
        So what black magic is going on here?

        I have been trying to get sensor readings every 2 mins and battery every half hour. But whatever I try it still gives temp every minute and battery at once a minute too. No wonder my batteries are draining so fast.

        mfalkviddM 1 Reply Last reply
        0
        • skywatchS skywatch

          I have been frustrated with this for weeks now. Today I notice that void loop is not closed with a curly brace and if I add one it does not compile.
          So what black magic is going on here?

          I have been trying to get sensor readings every 2 mins and battery every half hour. But whatever I try it still gives temp every minute and battery at once a minute too. No wonder my batteries are draining so fast.

          mfalkviddM Offline
          mfalkviddM Offline
          mfalkvidd
          Mod
          wrote on last edited by
          #118

          @skywatch the curly braces were discussed and explained earlier in this thread.

          Could you post your sketch and the debug output of your node?

          1 Reply Last reply
          0
          • skywatchS Offline
            skywatchS Offline
            skywatch
            wrote on last edited by
            #119

            @mfalkvidd

            Thanks for fast reply! See code below. If you don't see problem there I will debug tomorrow. The battery time is set for 90sec to test, but is taking 18mins between sends!

            
            
            // Enable debug prints to serial monitor
            //#define MY_DEBUG
            
            // Enable and select radio type attached
            #define MY_RADIO_NRF24
            //#define MY_RADIO_RFM69
            #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
            #define MY_RF24_PA_LEVEL (RF24_PA_LOW)
            #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
            #define MAX_ATTACHED_DS18B20 3
            #include <SPI.h>
            #include <MySensors.h>
            #include <DallasTemperature.h>
            #include <OneWire.h>
            int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
            int batteryPcnt = 0;
            unsigned long SLEEP_TIME = 117000; // 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;
            const long interval = 90000;
            unsigned long previousMillis = 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);
              analogReference(INTERNAL);
            }
            
            void presentation() {
              // Send the sketch version information to the gateway and Controller
              sendSketchInfo("HW/CH", "0.4");
            
              // 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);
              }
            }
            
            void loop()
            {
              // get the battery Voltage
              unsigned long currentMillis = millis();
            
              if (currentMillis - previousMillis >= interval) {
                previousMillis = currentMillis;
                int sensorValue = analogRead(BATTERY_SENSE_PIN);
                batteryPcnt = sensorValue / 10;
                sendBatteryLevel(batteryPcnt);
              }
            
              // 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;
                }
              }
            
              int8_t sleep(1, FALLING, SLEEP_TIME);
            }```
            mfalkviddM 1 Reply Last reply
            0
            • skywatchS skywatch

              @mfalkvidd

              Thanks for fast reply! See code below. If you don't see problem there I will debug tomorrow. The battery time is set for 90sec to test, but is taking 18mins between sends!

              
              
              // Enable debug prints to serial monitor
              //#define MY_DEBUG
              
              // Enable and select radio type attached
              #define MY_RADIO_NRF24
              //#define MY_RADIO_RFM69
              #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
              #define MY_RF24_PA_LEVEL (RF24_PA_LOW)
              #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
              #define MAX_ATTACHED_DS18B20 3
              #include <SPI.h>
              #include <MySensors.h>
              #include <DallasTemperature.h>
              #include <OneWire.h>
              int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
              int batteryPcnt = 0;
              unsigned long SLEEP_TIME = 117000; // 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;
              const long interval = 90000;
              unsigned long previousMillis = 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);
                analogReference(INTERNAL);
              }
              
              void presentation() {
                // Send the sketch version information to the gateway and Controller
                sendSketchInfo("HW/CH", "0.4");
              
                // 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);
                }
              }
              
              void loop()
              {
                // get the battery Voltage
                unsigned long currentMillis = millis();
              
                if (currentMillis - previousMillis >= interval) {
                  previousMillis = currentMillis;
                  int sensorValue = analogRead(BATTERY_SENSE_PIN);
                  batteryPcnt = sensorValue / 10;
                  sendBatteryLevel(batteryPcnt);
                }
              
                // 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;
                  }
                }
              
                int8_t sleep(1, FALLING, SLEEP_TIME);
              }```
              mfalkviddM Offline
              mfalkviddM Offline
              mfalkvidd
              Mod
              wrote on last edited by mfalkvidd
              #120

              @skywatch doing what you want is a bit hard since millis won't update while the node is in power save mode (sleeping). But this should do it:
              set SLEEP_TIME = 2 * 60 * 1000 // 2 minutes
              and change

              void loop()
              {
                // get the battery Voltage
                unsigned long currentMillis = millis();
              
                if (currentMillis - previousMillis >= interval) {
                  previousMillis = currentMillis;
                  int sensorValue = analogRead(BATTERY_SENSE_PIN);
                  batteryPcnt = sensorValue / 10;
                  sendBatteryLevel(batteryPcnt);
                }
              ...
              

              to

              unsigned int batteryReportFactor = 30*60*1000ul/SLEEP_TIME; // Only report battery every x SLEEP times (x=15 with current values)
              unsigned int timesSlept = 0;
              void loop()
              {
                if (timesSlept < batteryReportFactor) {
                  timesSlept++;
                } else {
                  // get the battery Voltage
                  timesSlept = 0;
                  int sensorValue = analogRead(BATTERY_SENSE_PIN);
                  batteryPcnt = sensorValue / 10;
                  sendBatteryLevel(batteryPcnt);
                }
              ...
              

              I don't understand why you're using interrupt to sleep though.

              skywatchS 1 Reply Last reply
              0
              • mfalkviddM mfalkvidd

                @skywatch doing what you want is a bit hard since millis won't update while the node is in power save mode (sleeping). But this should do it:
                set SLEEP_TIME = 2 * 60 * 1000 // 2 minutes
                and change

                void loop()
                {
                  // get the battery Voltage
                  unsigned long currentMillis = millis();
                
                  if (currentMillis - previousMillis >= interval) {
                    previousMillis = currentMillis;
                    int sensorValue = analogRead(BATTERY_SENSE_PIN);
                    batteryPcnt = sensorValue / 10;
                    sendBatteryLevel(batteryPcnt);
                  }
                ...
                

                to

                unsigned int batteryReportFactor = 30*60*1000ul/SLEEP_TIME; // Only report battery every x SLEEP times (x=15 with current values)
                unsigned int timesSlept = 0;
                void loop()
                {
                  if (timesSlept < batteryReportFactor) {
                    timesSlept++;
                  } else {
                    // get the battery Voltage
                    timesSlept = 0;
                    int sensorValue = analogRead(BATTERY_SENSE_PIN);
                    batteryPcnt = sensorValue / 10;
                    sendBatteryLevel(batteryPcnt);
                  }
                ...
                

                I don't understand why you're using interrupt to sleep though.

                skywatchS Offline
                skywatchS Offline
                skywatch
                wrote on last edited by skywatch
                #121

                @mfalkvidd said in 💬 Temperature Sensor:
                Thank you very much for helping out with this! - I will try it today if I can. I guess I could use the same principle to send sensor data every 2 minutes instead of every 1 minute and further reduce battery draw. I know the DS18B20 sensors are correctly going into sleep mode, but the node itself is still using too much juice.

                @skywatch doing what you want is a bit hard since millis won't update while the node is in power save mode (sleeping).

                There is nothing in the API about this, would be nice if it were added as I don't think I will be the only person with such requirements. Also, what does sleep actually do and are there further things I could do to reduce the power use?

                I don't understand why you're using interrupt to sleep though.

                That is because I haven't finished this node yet.... I am working on it systematically and in future a water leak detector will be added. But only when this part is working correctly and I have good battery life from the node.

                mfalkviddM 1 Reply Last reply
                0
                • skywatchS skywatch

                  @mfalkvidd said in 💬 Temperature Sensor:
                  Thank you very much for helping out with this! - I will try it today if I can. I guess I could use the same principle to send sensor data every 2 minutes instead of every 1 minute and further reduce battery draw. I know the DS18B20 sensors are correctly going into sleep mode, but the node itself is still using too much juice.

                  @skywatch doing what you want is a bit hard since millis won't update while the node is in power save mode (sleeping).

                  There is nothing in the API about this, would be nice if it were added as I don't think I will be the only person with such requirements. Also, what does sleep actually do and are there further things I could do to reduce the power use?

                  I don't understand why you're using interrupt to sleep though.

                  That is because I haven't finished this node yet.... I am working on it systematically and in future a water leak detector will be added. But only when this part is working correctly and I have good battery life from the node.

                  mfalkviddM Offline
                  mfalkviddM Offline
                  mfalkvidd
                  Mod
                  wrote on last edited by mfalkvidd
                  #122

                  @skywatch said in 💬 Temperature Sensor:

                  There is nothing in the API about this, would be nice if it were added as I don't think I will be the only person with such requirements.

                  Good point. I have added a note on https://www.mysensors.org/download/sensor_api_20#sleeping

                  Also, what does sleep actually do and are there further things I could do to reduce the power use?

                  It does a lot of things, but basically it puts the radio and the Arduino in power save mode and uses the Arduino's watchdog timer to roughly keep track of time (exact timing is not possible because exact timing requires power, and power save mode is all about saving power).

                  https://www.mysensors.org/build/battery has the official MySensors recommendations on how to save power in battery-operated nodes.

                  If you want to dive deeper, start with https://www.gammon.com.au/power

                  1 Reply Last reply
                  1
                  • gohanG Offline
                    gohanG Offline
                    gohan
                    Mod
                    wrote on last edited by
                    #123

                    I'd use the sleep time to 2 minutes and then simply set a counter that every 15 loops of the code sends battery status and resets the counter. Imho 2 minutes are quite often for battery life.

                    mfalkviddM 1 Reply Last reply
                    0
                    • gohanG gohan

                      I'd use the sleep time to 2 minutes and then simply set a counter that every 15 loops of the code sends battery status and resets the counter. Imho 2 minutes are quite often for battery life.

                      mfalkviddM Offline
                      mfalkviddM Offline
                      mfalkvidd
                      Mod
                      wrote on last edited by
                      #124

                      @gohan wasn't that what I did?

                      gohanG 1 Reply Last reply
                      0
                      • skywatchS Offline
                        skywatchS Offline
                        skywatch
                        wrote on last edited by
                        #125

                        This can't be right, can it???

                        (http://imagebucket.net/7mtp8rcnqss2/BattGraph.jpg)

                        mfalkviddM 1 Reply Last reply
                        0
                        • mfalkviddM mfalkvidd

                          @gohan wasn't that what I did?

                          gohanG Offline
                          gohanG Offline
                          gohan
                          Mod
                          wrote on last edited by
                          #126

                          @mfalkvidd I'm on mobile, I didn't read the code 😇

                          mfalkviddM 1 Reply Last reply
                          0
                          • skywatchS skywatch

                            This can't be right, can it???

                            (http://imagebucket.net/7mtp8rcnqss2/BattGraph.jpg)

                            mfalkviddM Offline
                            mfalkviddM Offline
                            mfalkvidd
                            Mod
                            wrote on last edited by
                            #127

                            @skywatch it looks normal. The precision is probably 1% and the real value close to 100.5%, so sometimes it will be rounded to 101% and sometimes to 100%.

                            1 Reply Last reply
                            0
                            • gohanG gohan

                              @mfalkvidd I'm on mobile, I didn't read the code 😇

                              mfalkviddM Offline
                              mfalkviddM Offline
                              mfalkvidd
                              Mod
                              wrote on last edited by
                              #128

                              @gohan hek has added a great feature that I use a lot on mobile. Click the code box once to maximize it. Super neat!

                              1 Reply Last reply
                              2
                              • skywatchS Offline
                                skywatchS Offline
                                skywatch
                                wrote on last edited by
                                #129

                                @mfalkvidd,

                                Did you notice the time line on the bottom of the image. I thought it was supposed to send data once every 30 mins. Seems that something isn't right or I am looking at it from the wrong angle.....?

                                mfalkviddM 1 Reply Last reply
                                0
                                • skywatchS skywatch

                                  @mfalkvidd,

                                  Did you notice the time line on the bottom of the image. I thought it was supposed to send data once every 30 mins. Seems that something isn't right or I am looking at it from the wrong angle.....?

                                  mfalkviddM Offline
                                  mfalkviddM Offline
                                  mfalkvidd
                                  Mod
                                  wrote on last edited by
                                  #130

                                  @skywatch I'm not that versed in c/c++ arithmetic, but I suspect the calculation might overflow. Could you add a debug print to see the value of batteryReportFactor?

                                  1 Reply Last reply
                                  0
                                  • skywatchS Offline
                                    skywatchS Offline
                                    skywatch
                                    wrote on last edited by
                                    #131

                                    OK I'll do that - seems to be sending battery info every minute still though- strange!

                                    mfalkviddM 1 Reply Last reply
                                    0
                                    • skywatchS skywatch

                                      OK I'll do that - seems to be sending battery info every minute still though- strange!

                                      mfalkviddM Offline
                                      mfalkviddM Offline
                                      mfalkvidd
                                      Mod
                                      wrote on last edited by mfalkvidd
                                      #132

                                      @skywatch could you post the entire sketch also, to check if something fishy has crept in?

                                      1 Reply Last reply
                                      0
                                      • skywatchS Offline
                                        skywatchS Offline
                                        skywatch
                                        wrote on last edited by
                                        #133

                                        Here is the sketch, debug will be later today I hope....

                                        
                                        
                                        // Enable debug prints to serial monitor
                                        //#define MY_DEBUG
                                        
                                        // Enable and select radio type attached
                                        #define MY_RADIO_NRF24
                                        //#define MY_RADIO_RFM69
                                        #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
                                        #define MY_RF24_PA_LEVEL (RF24_PA_LOW)
                                        #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
                                        #define MAX_ATTACHED_DS18B20 3
                                        #include <SPI.h>
                                        #include <MySensors.h>
                                        #include <DallasTemperature.h>
                                        #include <OneWire.h>
                                        int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                                        int batteryPcnt = 0;
                                        unsigned long SLEEP_TIME = 2 * 60 * 1000; // 2 minutes Sleep time between reads.
                                        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;
                                        const long interval = 90000;
                                        unsigned long previousMillis = 0;
                                        bool receivedConfig = false;
                                        bool metric = true;
                                        unsigned int batteryReportFactor = 30*60*1000/SLEEP_TIME; // Only report battery every x SLEEP times (x=15 with current values)
                                        unsigned int timesSlept = 0;
                                        // 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);
                                          analogReference(INTERNAL);
                                        }
                                        
                                        void presentation() {
                                          // Send the sketch version information to the gateway and Controller
                                          sendSketchInfo("HW/CH", "0.5");
                                        
                                          // 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);
                                          }
                                        }
                                        
                                        void loop()
                                        {
                                          // get the battery Voltage
                                          if (timesSlept < batteryReportFactor) {
                                            timesSlept++;
                                          } else {
                                            // get the battery Voltage
                                            timesSlept = 0;
                                            int sensorValue = analogRead(BATTERY_SENSE_PIN);
                                            batteryPcnt = sensorValue / 10;
                                            sendBatteryLevel(batteryPcnt);
                                          }
                                        
                                          // 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;
                                            }
                                          }
                                        
                                          int8_t sleep(1, FALLING, SLEEP_TIME);
                                        }```
                                        
                                        Thanks!
                                        mfalkviddM 1 Reply Last reply
                                        0
                                        • skywatchS skywatch

                                          Here is the sketch, debug will be later today I hope....

                                          
                                          
                                          // Enable debug prints to serial monitor
                                          //#define MY_DEBUG
                                          
                                          // Enable and select radio type attached
                                          #define MY_RADIO_NRF24
                                          //#define MY_RADIO_RFM69
                                          #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
                                          #define MY_RF24_PA_LEVEL (RF24_PA_LOW)
                                          #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
                                          #define MAX_ATTACHED_DS18B20 3
                                          #include <SPI.h>
                                          #include <MySensors.h>
                                          #include <DallasTemperature.h>
                                          #include <OneWire.h>
                                          int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                                          int batteryPcnt = 0;
                                          unsigned long SLEEP_TIME = 2 * 60 * 1000; // 2 minutes Sleep time between reads.
                                          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;
                                          const long interval = 90000;
                                          unsigned long previousMillis = 0;
                                          bool receivedConfig = false;
                                          bool metric = true;
                                          unsigned int batteryReportFactor = 30*60*1000/SLEEP_TIME; // Only report battery every x SLEEP times (x=15 with current values)
                                          unsigned int timesSlept = 0;
                                          // 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);
                                            analogReference(INTERNAL);
                                          }
                                          
                                          void presentation() {
                                            // Send the sketch version information to the gateway and Controller
                                            sendSketchInfo("HW/CH", "0.5");
                                          
                                            // 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);
                                            }
                                          }
                                          
                                          void loop()
                                          {
                                            // get the battery Voltage
                                            if (timesSlept < batteryReportFactor) {
                                              timesSlept++;
                                            } else {
                                              // get the battery Voltage
                                              timesSlept = 0;
                                              int sensorValue = analogRead(BATTERY_SENSE_PIN);
                                              batteryPcnt = sensorValue / 10;
                                              sendBatteryLevel(batteryPcnt);
                                            }
                                          
                                            // 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;
                                              }
                                            }
                                          
                                            int8_t sleep(1, FALLING, SLEEP_TIME);
                                          }```
                                          
                                          Thanks!
                                          mfalkviddM Offline
                                          mfalkviddM Offline
                                          mfalkvidd
                                          Mod
                                          wrote on last edited by
                                          #134

                                          @skywatch code looks good to me. What type of Arduino are you using?

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          7

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.0k

                                          Posts


                                          Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular