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. Troubleshooting
  3. Temperature sensor WITH battery monitor

Temperature sensor WITH battery monitor

Scheduled Pinned Locked Moved Troubleshooting
10 Posts 4 Posters 5.0k Views 3 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.
  • H Offline
    H Offline
    Hausner
    wrote on last edited by
    #1

    Hi,

    I'm new to all of this arduino stuff, but I've managed to get a Serial GW and 2 tempsensors going.

    Now I would like to add the battery monitor to the temp sensor sketch, to monitor the 3,7V LiPo batteries.

    What I've done is, merged the battery sketch setup with the temp sketch setup, and also done the same with the Void sections.

    No errors on upload - weee!

    Problem is now, that I only get the Volt reading from the Node and no temperature reading anymore. :(

    So where did I go wrong?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hausner
      wrote on last edited by
      #2

      Here's my sketch for the tempnode:

      // Example sketch showing how to send in OneWire temperature readings
      #include <MySensor.h>
      #include <SPI.h>
      #include <DallasTemperature.h>
      #include <OneWire.h>

      #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected
      #define MAX_ATTACHED_DS18B20 16
      unsigned long SLEEP_TIME = 120000; // Sleep time between reads (in milliseconds)
      OneWire oneWire(ONE_WIRE_BUS);
      DallasTemperature sensors(&oneWire);
      MySensor gw;
      float lastTemperature[MAX_ATTACHED_DS18B20];
      int numSensors=0;
      boolean receivedConfig = false;
      boolean metric = true;
      // Initialize temperature message
      MyMessage msg(0,V_TEMP);

      int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
      int oldBatteryPcnt = 0;

      void setup()
      {
      // Startup OneWire
      sensors.begin();

      // 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.0");

      // 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);
      }

      // use the 1.1 V internal reference
      analogReference(INTERNAL);
      }

      void loop()
      {
      // Process incoming messages (like config from server)
      gw.process();

      // Fetch temperatures from Dallas sensors
      sensors.requestTemperatures();

      // 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 (lastTemperature[i] != temperature && temperature != -127.00) {
      
        // Send in the new temperature
        gw.send(msg.setSensor(i).set(temperature,1));
        lastTemperature[i]=temperature;
      }
      

      }

      // get the battery Voltage
      int sensorValue = analogRead(BATTERY_SENSE_PIN);
      Serial.println(sensorValue);
      // 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
      float batteryV = sensorValue * 0.004106;
      int batteryPcnt = sensorValue / 10;
      Serial.print("Battery Voltage: ");
      Serial.print(batteryV);
      Serial.println(" V");
      Serial.print("Battery percent: ");
      Serial.print(batteryPcnt);
      Serial.println(" %");
      if (oldBatteryPcnt != batteryPcnt) {
      // Power up radio after sleep
      gw.sendBatteryLevel(batteryPcnt);
      oldBatteryPcnt = batteryPcnt;
      }

      gw.sleep(SLEEP_TIME);
      }

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Hausner
        wrote on last edited by
        #3

        DOH!!! Stupid me!

        Forgot the temp only transmits on temp change...

        it's working now :)

        1 Reply Last reply
        0
        • A Offline
          A Offline
          abalazs
          wrote on last edited by
          #4

          Perhaps related to the topic. This program was used for monitoring battery level, but can not read the battery level. :(

          mqtt="<[mysensor:MyMQTT/21/255/V_BATTERY_LEVEL:state:default]"

          So I tried to read, the program opanhab, but not getting value. Any ideas what went wrong.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Markus.
            wrote on last edited by
            #5

            Hi
            I know this is an old post but I try to convert this Sketch to use it with Version 2.1.and I am not sure If this can be work because I am tottaly new in Arduino and Mysenseors

            // Enable debug prints to serial monitor
            //#define MY_DEBUG 
            
            // Enable and select radio type attached
             #define MY_RADIO_NRF24
             
            // Example sketch showing how to send in OneWire temperature readings
             #include <MySensors.h>
             #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 = 300000; // Sleep time between reads (in milliseconds)
             OneWire oneWire(ONE_WIRE_BUS);
             DallasTemperature sensors(&oneWire);
             //MySensors gw;
             float lastTemperature[MAX_ATTACHED_DS18B20];
             int numSensors=0;
             boolean receivedConfig = false;
             boolean metric = true;
             // Initialize temperature message
             MyMessage msg(0,V_TEMP);
            
            int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
             int oldBatteryPcnt = 0;
            
            void setup()
             {
             // Startup OneWire
             sensors.begin();
            
            // Send the sketch version information to the gateway and Controller
             sendSketchInfo("Temperature Sensor", "1.0");
            
            // 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);
             }
            
            // use the 1.1 V internal reference
             analogReference(INTERNAL);
             }
            
            void loop()
             {
             
            // Fetch temperatures from Dallas sensors
             sensors.requestTemperatures();
            
            // 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 (lastTemperature[i] != temperature && temperature != -127.00) {
            
              // Send in the new temperature
              send(msg.setSensor(i).set(temperature,1));
              lastTemperature[i]=temperature;
            }
            }
            
            // get the battery Voltage
             int sensorValue = analogRead(BATTERY_SENSE_PIN);
             Serial.println(sensorValue);
             // 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
             float batteryV = sensorValue * 0.004106;
             int batteryPcnt = sensorValue / 10;
             Serial.print("Battery Voltage: ");
             Serial.print(batteryV);
             Serial.println(" V");
             Serial.print("Battery percent: ");
             Serial.print(batteryPcnt);
             Serial.println(" %");
             if (oldBatteryPcnt != batteryPcnt) {
             // Power up radio after sleep
             sendBatteryLevel(batteryPcnt);
             oldBatteryPcnt = batteryPcnt;
             }
            
            sleep(SLEEP_TIME);
             }
            

            Would be great If someone confirm Ifthis is correct so far...

            Many thanks

            Markus

            AWIA 1 Reply Last reply
            0
            • M Markus.

              Hi
              I know this is an old post but I try to convert this Sketch to use it with Version 2.1.and I am not sure If this can be work because I am tottaly new in Arduino and Mysenseors

              // Enable debug prints to serial monitor
              //#define MY_DEBUG 
              
              // Enable and select radio type attached
               #define MY_RADIO_NRF24
               
              // Example sketch showing how to send in OneWire temperature readings
               #include <MySensors.h>
               #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 = 300000; // Sleep time between reads (in milliseconds)
               OneWire oneWire(ONE_WIRE_BUS);
               DallasTemperature sensors(&oneWire);
               //MySensors gw;
               float lastTemperature[MAX_ATTACHED_DS18B20];
               int numSensors=0;
               boolean receivedConfig = false;
               boolean metric = true;
               // Initialize temperature message
               MyMessage msg(0,V_TEMP);
              
              int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
               int oldBatteryPcnt = 0;
              
              void setup()
               {
               // Startup OneWire
               sensors.begin();
              
              // Send the sketch version information to the gateway and Controller
               sendSketchInfo("Temperature Sensor", "1.0");
              
              // 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);
               }
              
              // use the 1.1 V internal reference
               analogReference(INTERNAL);
               }
              
              void loop()
               {
               
              // Fetch temperatures from Dallas sensors
               sensors.requestTemperatures();
              
              // 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 (lastTemperature[i] != temperature && temperature != -127.00) {
              
                // Send in the new temperature
                send(msg.setSensor(i).set(temperature,1));
                lastTemperature[i]=temperature;
              }
              }
              
              // get the battery Voltage
               int sensorValue = analogRead(BATTERY_SENSE_PIN);
               Serial.println(sensorValue);
               // 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
               float batteryV = sensorValue * 0.004106;
               int batteryPcnt = sensorValue / 10;
               Serial.print("Battery Voltage: ");
               Serial.print(batteryV);
               Serial.println(" V");
               Serial.print("Battery percent: ");
               Serial.print(batteryPcnt);
               Serial.println(" %");
               if (oldBatteryPcnt != batteryPcnt) {
               // Power up radio after sleep
               sendBatteryLevel(batteryPcnt);
               oldBatteryPcnt = batteryPcnt;
               }
              
              sleep(SLEEP_TIME);
               }
              

              Would be great If someone confirm Ifthis is correct so far...

              Many thanks

              Markus

              AWIA Offline
              AWIA Offline
              AWI
              Hero Member
              wrote on last edited by
              #6

              @Markus. I would say: give it a go. It looks fine at first instance. It's rather hard to say it's correct without running it through a compiler. For readability's sake I would advice you to look at the coding guidelines for reference.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Markus.
                wrote on last edited by
                #7

                Its working so far.:-) But the battery voltage is only available as serial print. How must i change the file to get also the voltage send to the gateway? Simply by adding the send command before the sleep?

                Thx

                M.

                AWIA 1 Reply Last reply
                0
                • M Markus.

                  Its working so far.:-) But the battery voltage is only available as serial print. How must i change the file to get also the voltage send to the gateway? Simply by adding the send command before the sleep?

                  Thx

                  M.

                  AWIA Offline
                  AWIA Offline
                  AWI
                  Hero Member
                  wrote on last edited by
                  #8

                  @Markus. You need to send the voltage explicit with a V_VOLTAGE (S_MULTIMETER) message.

                  M 1 Reply Last reply
                  0
                  • AWIA AWI

                    @Markus. You need to send the voltage explicit with a V_VOLTAGE (S_MULTIMETER) message.

                    M Offline
                    M Offline
                    Markus.
                    wrote on last edited by
                    #9

                    @AWI Hope you can give me an example because it seems that the requiered programming Level currently are over my Knowledge ;-)

                    THX
                    Markus

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Markus.
                      wrote on last edited by
                      #10

                      Anyone here an idea how i can get the voltage send tomthe gateway? Works great so far except the send of the voltage ....:-(

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


                      13

                      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