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. My Project
  3. My 2AA battery sensor

My 2AA battery sensor

Scheduled Pinned Locked Moved My Project
90 Posts 27 Posters 106.6k 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.
  • YveauxY Yveaux

    @m26872 Just a suggestion: once the node dies you could take the raw data and run some filtering on it like @Anticimex suggested.
    It'll give you a quick way to experiment with different filter settings.
    Once you find an optimal filter you can implement it in the node(s).

    m26872M Offline
    m26872M Offline
    m26872
    Hardware Contributor
    wrote on last edited by
    #67

    @Yveaux You're right. I expect a good correlation between "spike depth" and the load profile just prior to sample. By controling this it should be quite repeatable.

    1 Reply Last reply
    0
    • mntlvrM Offline
      mntlvrM Offline
      mntlvr
      wrote on last edited by
      #68
      This post is deleted!
      m26872M 1 Reply Last reply
      0
      • mntlvrM mntlvr

        This post is deleted!

        m26872M Offline
        m26872M Offline
        m26872
        Hardware Contributor
        wrote on last edited by
        #69

        @mntlvr Interesting discussion but why in this thread?

        mntlvrM 1 Reply Last reply
        0
        • m26872M m26872

          @mntlvr Interesting discussion but why in this thread?

          mntlvrM Offline
          mntlvrM Offline
          mntlvr
          wrote on last edited by
          #70
          This post is deleted!
          1 Reply Last reply
          0
          • A Offline
            A Offline
            ahhk
            Hardware Contributor
            wrote on last edited by
            #71

            i think it would be better to open a new thread for this, instead to discuss two topics in one thread....the 2xaa battery sensor is very interesting ;)

            1 Reply Last reply
            0
            • m26872M m26872

              I declare the 6 months criteria easily passed. And the chinese step up regulator to be good value for money. Next target; 12 months.
              Nod105106_maj09.png

              m26872M Offline
              m26872M Offline
              m26872
              Hardware Contributor
              wrote on last edited by m26872
              #72

              The 12 months target was done a few days ago. Today's status 105: 63% and 106: 27%
              I had a thought to celebrate their birthday by retrieve old Vera data and make nice plot, but that has to wait. Next, 18 months.

              Just to clarify that these are no "sleeping" nodes; they transmit 100-150 messages each every day.

              1 Reply Last reply
              3
              • m26872M Offline
                m26872M Offline
                m26872
                Hardware Contributor
                wrote on last edited by
                #73

                Battery level status report after 18 months:
                Node 105: 51%
                Node 106 (with used batteries): Died early January.

                Next 24 months.

                1 Reply Last reply
                5
                • rhuehnR Offline
                  rhuehnR Offline
                  rhuehn
                  wrote on last edited by
                  #74

                  Hey @m26872 Congrats on the testing and results! Looks great! I was wondering if I could get your feedback on something. I'm trying to build a very low powered ( 2 x AA ) Battery powered 3 in 1 sensor. ( Platform is a Pro Mini 8Mhz, 3.3v, with an PIR HC-SR501, NRF24L01, and a DHT 11 Temp and Hum sensor. The sensor should also report battery power periodically ideally using the vcc library ( not implemented yet ). Because these components use power hungry devices ( PIR, DHT ), I have already moded the PIR to work at 3.3, and have removed the regulator / LED from the pro mini. Would it make sense to use a step up / down regulator, or power directly from the VCC pin? I can't decide what would be more effective in prolonging battery life? Also, in terms of reporting battery state. I'm not sure what would be best also, hence the question around the VCC library?

                  Below is my sketch. It is really taken from various posts on this forum and stitched together. If you could have a peak at it, and see if it is "optimal", as I don't believe it is today. ie: Perhaps I could only report temp / hum if it changes +/- a degree? Or send everything in a single radio announcement, then "Deep Sleep"? Lots of questions, and of course I am sure there are lots of answers. I'm just trying to build the most optimal battery efficient ( 2 x AA ) powered sensor no problem right! :) Below is my current code:

                   #define MY_GW_ID 31
                  // Enable debug prints
                  #define MY_DEBUG
                  
                  // Enable and select radio type attached
                  #define MY_RADIO_NRF24
                  //#define MY_RADIO_RFM69
                  
                  #include <SPI.h>
                  #include <MySensor.h>  
                  #include <DHT.h>  
                  
                  int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                  
                  //Constants
                  #define SKETCH_NAME           "3 in 1 Sensor"
                  #define SKETCH_VERSION        "1.0"
                  #define CHILD_ID_HUM 0                    // Child id for Humidity
                  #define CHILD_ID_TEMP 1                   // Child id for Temperature
                  #define CHILD_ID_MOT 2                   // Id of the sensor child
                  #define HUMIDITY_SENSOR_DIGITAL_PIN 7    // Where is my DHT22 data pin connected to
                  #define DIGITAL_INPUT_SENSOR 3           // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
                  #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
                  
                  //Misc. variables
                  uint8_t switchState = 2;
                  unsigned long SLEEP_TIME = 820000;      // Sleep time between reads (in milliseconds) (close to 15')
                  unsigned long SLEEP_TIME2 = 275000;     // Sleep time after int. (in milliseconds) (close to 5')
                  uint8_t cycleInProgress = 0;
                  uint8_t firstReportDone = 0;
                  uint8_t firstReportWithZeroDone = 0;
                  int oldBatteryPcnt = 0;
                  
                  MySensor gw;
                  DHT dht;
                  float lastTemp = 0 ;
                  float lastHum = 0 ;
                  boolean lastTripped = false ;
                  boolean metric = true; 
                  MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                  MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                  MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
                  
                  void setup()  
                  { 
                     // use the 1.1 V internal reference
                  #if defined(__AVR_ATmega2560__)
                     analogReference(INTERNAL1V1);
                  #else
                     analogReference(INTERNAL);
                  #endif
                    gw.begin();
                    dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
                  
                    // Send the Sketch Version Information to the Gateway
                    gw.sendSketchInfo("3 in 1 Sensor", "1.0");
                  
                  
                   pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
                   
                    // Register all sensors to gw (they will be created as child devices)
                    gw.present(CHILD_ID_HUM, S_HUM);
                    gw.present(CHILD_ID_TEMP, S_TEMP);
                    gw.present(CHILD_ID_MOT, S_MOTION);
                     
                    metric = gw.getConfig().isMetric;
                  }
                  
                  void loop()      
                  
                  {  
                  
                     delay(dht.getMinimumSamplingPeriod());
                  
                    float temperature = dht.getTemperature();
                    float humidity = dht.getHumidity();
                    
                    if (isnan(temperature)) {
                        Serial.println("Failed reading temperature from DHT");
                    } else if (temperature != lastTemp) {
                      lastTemp = temperature;
                      if (!metric) {
                        temperature = dht.toFahrenheit(temperature);
                      }
                      gw.send(msgTemp.set(temperature, 1));
                      Serial.print("T: ");
                      Serial.println(temperature);
                    }
                    
                    if (isnan(humidity)) {
                        Serial.println("Failed reading humidity from DHT");
                    } else if (humidity != lastHum) {
                        lastHum = humidity;
                        gw.send(msgHum.set(humidity, 1));
                        Serial.print("H: ");
                        Serial.println(humidity);
                    }
                    
                    // get the battery Voltage
                     int sensorValue = analogRead(BATTERY_SENSE_PIN);
                     #ifdef 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
                     float batteryV  = sensorValue * 0.003363075;
                     int batteryPcnt = sensorValue / 10;
                  
                     #ifdef DEBUG
                     Serial.print("Battery Voltage: ");
                     Serial.print(batteryV);
                     Serial.println(" V");
                  
                     Serial.print("Battery percent: ");
                     Serial.print(batteryPcnt);
                     Serial.println(" %");
                     #endif
                  
                    // Read digital motion value
                    boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
                    
                    if (tripped != lastTripped ) {      
                      Serial.println(tripped);
                      gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
                    }
                    
                     if (oldBatteryPcnt != batteryPcnt) {
                       // Power up radio after sleep
                       gw.sendBatteryLevel(batteryPcnt);
                       oldBatteryPcnt = batteryPcnt;
                     }
                    
                    // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
                    gw.sleep(INTERRUPT, CHANGE, 0 );
                  }
                  

                  thanks very much!

                  AWIA 1 Reply Last reply
                  0
                  • rhuehnR rhuehn

                    Hey @m26872 Congrats on the testing and results! Looks great! I was wondering if I could get your feedback on something. I'm trying to build a very low powered ( 2 x AA ) Battery powered 3 in 1 sensor. ( Platform is a Pro Mini 8Mhz, 3.3v, with an PIR HC-SR501, NRF24L01, and a DHT 11 Temp and Hum sensor. The sensor should also report battery power periodically ideally using the vcc library ( not implemented yet ). Because these components use power hungry devices ( PIR, DHT ), I have already moded the PIR to work at 3.3, and have removed the regulator / LED from the pro mini. Would it make sense to use a step up / down regulator, or power directly from the VCC pin? I can't decide what would be more effective in prolonging battery life? Also, in terms of reporting battery state. I'm not sure what would be best also, hence the question around the VCC library?

                    Below is my sketch. It is really taken from various posts on this forum and stitched together. If you could have a peak at it, and see if it is "optimal", as I don't believe it is today. ie: Perhaps I could only report temp / hum if it changes +/- a degree? Or send everything in a single radio announcement, then "Deep Sleep"? Lots of questions, and of course I am sure there are lots of answers. I'm just trying to build the most optimal battery efficient ( 2 x AA ) powered sensor no problem right! :) Below is my current code:

                     #define MY_GW_ID 31
                    // Enable debug prints
                    #define MY_DEBUG
                    
                    // Enable and select radio type attached
                    #define MY_RADIO_NRF24
                    //#define MY_RADIO_RFM69
                    
                    #include <SPI.h>
                    #include <MySensor.h>  
                    #include <DHT.h>  
                    
                    int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                    
                    //Constants
                    #define SKETCH_NAME           "3 in 1 Sensor"
                    #define SKETCH_VERSION        "1.0"
                    #define CHILD_ID_HUM 0                    // Child id for Humidity
                    #define CHILD_ID_TEMP 1                   // Child id for Temperature
                    #define CHILD_ID_MOT 2                   // Id of the sensor child
                    #define HUMIDITY_SENSOR_DIGITAL_PIN 7    // Where is my DHT22 data pin connected to
                    #define DIGITAL_INPUT_SENSOR 3           // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
                    #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
                    
                    //Misc. variables
                    uint8_t switchState = 2;
                    unsigned long SLEEP_TIME = 820000;      // Sleep time between reads (in milliseconds) (close to 15')
                    unsigned long SLEEP_TIME2 = 275000;     // Sleep time after int. (in milliseconds) (close to 5')
                    uint8_t cycleInProgress = 0;
                    uint8_t firstReportDone = 0;
                    uint8_t firstReportWithZeroDone = 0;
                    int oldBatteryPcnt = 0;
                    
                    MySensor gw;
                    DHT dht;
                    float lastTemp = 0 ;
                    float lastHum = 0 ;
                    boolean lastTripped = false ;
                    boolean metric = true; 
                    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                    MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
                    
                    void setup()  
                    { 
                       // use the 1.1 V internal reference
                    #if defined(__AVR_ATmega2560__)
                       analogReference(INTERNAL1V1);
                    #else
                       analogReference(INTERNAL);
                    #endif
                      gw.begin();
                      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
                    
                      // Send the Sketch Version Information to the Gateway
                      gw.sendSketchInfo("3 in 1 Sensor", "1.0");
                    
                    
                     pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
                     
                      // Register all sensors to gw (they will be created as child devices)
                      gw.present(CHILD_ID_HUM, S_HUM);
                      gw.present(CHILD_ID_TEMP, S_TEMP);
                      gw.present(CHILD_ID_MOT, S_MOTION);
                       
                      metric = gw.getConfig().isMetric;
                    }
                    
                    void loop()      
                    
                    {  
                    
                       delay(dht.getMinimumSamplingPeriod());
                    
                      float temperature = dht.getTemperature();
                      float humidity = dht.getHumidity();
                      
                      if (isnan(temperature)) {
                          Serial.println("Failed reading temperature from DHT");
                      } else if (temperature != lastTemp) {
                        lastTemp = temperature;
                        if (!metric) {
                          temperature = dht.toFahrenheit(temperature);
                        }
                        gw.send(msgTemp.set(temperature, 1));
                        Serial.print("T: ");
                        Serial.println(temperature);
                      }
                      
                      if (isnan(humidity)) {
                          Serial.println("Failed reading humidity from DHT");
                      } else if (humidity != lastHum) {
                          lastHum = humidity;
                          gw.send(msgHum.set(humidity, 1));
                          Serial.print("H: ");
                          Serial.println(humidity);
                      }
                      
                      // get the battery Voltage
                       int sensorValue = analogRead(BATTERY_SENSE_PIN);
                       #ifdef 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
                       float batteryV  = sensorValue * 0.003363075;
                       int batteryPcnt = sensorValue / 10;
                    
                       #ifdef DEBUG
                       Serial.print("Battery Voltage: ");
                       Serial.print(batteryV);
                       Serial.println(" V");
                    
                       Serial.print("Battery percent: ");
                       Serial.print(batteryPcnt);
                       Serial.println(" %");
                       #endif
                    
                      // Read digital motion value
                      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
                      
                      if (tripped != lastTripped ) {      
                        Serial.println(tripped);
                        gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
                      }
                      
                       if (oldBatteryPcnt != batteryPcnt) {
                         // Power up radio after sleep
                         gw.sendBatteryLevel(batteryPcnt);
                         oldBatteryPcnt = batteryPcnt;
                       }
                      
                      // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
                      gw.sleep(INTERRUPT, CHANGE, 0 );
                    }
                    

                    thanks very much!

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

                    @rhuehn It looks like you are repeating the conversation in this thread. Its better to continue the discussion there.

                    1 Reply Last reply
                    1
                    • mfalkviddM Offline
                      mfalkviddM Offline
                      mfalkvidd
                      Mod
                      wrote on last edited by
                      #76

                      I agree with AWI, but would like to add one thing: By using the DHT11 you are really fighting an uphill battle. There are several sensors that are more suitable for battery-powered sensors. See the study referenced by @AWI in https://forum.mysensors.org/topic/3801/witch-temp-hum-sensor-is-best-to-use-for-battery-powered-sensors/2

                      1 Reply Last reply
                      0
                      • rhuehnR Offline
                        rhuehnR Offline
                        rhuehn
                        wrote on last edited by
                        #77

                        Thanks @mfalkvidd Starting to realize that..... I might swap to the HTU21D, but keep in mind I'm also using a PIR..... Is there a reliable, low voltage PIR sensor that works well? I haven't seen one yet ( 2 x AA BATT's, under 2.8 V )?

                        Thanks

                        1 Reply Last reply
                        0
                        • rhuehnR Offline
                          rhuehnR Offline
                          rhuehn
                          wrote on last edited by
                          #78

                          I have updated the original post if anyone can offer any suggestions. it is located Here

                          Any feedback is greatly appreciated

                          1 Reply Last reply
                          0
                          • N Offline
                            N Offline
                            Nicklas Starkel
                            wrote on last edited by
                            #79

                            @EasyIoT , how are your sensors doing, its been 2 years :)
                            And also, @m26872 , the node 105. Is it alive?

                            m26872M 1 Reply Last reply
                            1
                            • N Nicklas Starkel

                              @EasyIoT , how are your sensors doing, its been 2 years :)
                              And also, @m26872 , the node 105. Is it alive?

                              m26872M Offline
                              m26872M Offline
                              m26872
                              Hardware Contributor
                              wrote on last edited by
                              #80

                              @Nicklas-Starkel I've been busy for few months moving to a new house and have had my sensor network offline until now because of this. Node 105 was kept on and handled with care, but I was pretty sure it would be dead when I started my gateway yesterday. BUT, it was ALIVE ?! at 14% battery level. Amazing! 24 months with such poor and simple hardware and software. :exclamation: :two_hearts: :star: :musical_note: :dancers: :clap: :muscle: Same batteries and only one restart (due to my controller change last year).
                              One little remark though, it doesn't seem to send temperature och pressure, only humidity and battery level. Maybe an issue of voltage recovery time between transmissions.

                              1 Reply Last reply
                              6
                              • S Offline
                                S Offline
                                sineverba
                                Hardware Contributor
                                wrote on last edited by
                                #81
                                This post is deleted!
                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  sineverba
                                  Hardware Contributor
                                  wrote on last edited by
                                  #82

                                  Sorry for the noob question...
                                  I would re-create your setup. At which point of it you insert the multimeter to measure the current?

                                  Is it ok to "interrupt" the ground (proximity to the step up) and measure the current there?

                                  Thank you

                                  m26872M 1 Reply Last reply
                                  0
                                  • S sineverba

                                    Sorry for the noob question...
                                    I would re-create your setup. At which point of it you insert the multimeter to measure the current?

                                    Is it ok to "interrupt" the ground (proximity to the step up) and measure the current there?

                                    Thank you

                                    m26872M Offline
                                    m26872M Offline
                                    m26872
                                    Hardware Contributor
                                    wrote on last edited by
                                    #83

                                    @sineverba Just cut one of the battery wires of the finished assembly. Solder together again when your done measuring.

                                    1 Reply Last reply
                                    1
                                    • P Offline
                                      P Offline
                                      palmerfarmer
                                      wrote on last edited by
                                      #84

                                      Has anyone got a V2 version working for this?

                                      F 1 Reply Last reply
                                      0
                                      • P palmerfarmer

                                        Has anyone got a V2 version working for this?

                                        F Offline
                                        F Offline
                                        flopp
                                        wrote on last edited by
                                        #85

                                        @palmerfarmer said in My 2AA battery sensor:

                                        Has anyone got a V2 version working for this?

                                        Do you mean running with MySensors 2.3?

                                        1 Reply Last reply
                                        0
                                        • P Offline
                                          P Offline
                                          palmerfarmer
                                          wrote on last edited by
                                          #86

                                          Any Ver 2.xx,
                                          So I have cobbled together/hacked this and it seem to work, battery has stayed at 97% for 2 weeks now. Feel free to Hack about and improve i have this mounted on an easyPCB board, x2AA batteries, NRF radio, BME280 with a 3.3v convertor .

                                          /*   
                                          * use mysensors library 2.2.0
                                          * ALL WORKING AND TESTED ON EASY PCB BOARD WITH BATTERY SAVING CODE 29/11/18
                                          *
                                          * Time SET to 15 mins
                                          * also added BME280.writeMode (smSleep); to reduce quisecent to current to 40uA
                                          * BME280 sensor connections: 3.3v, 0V, SCL to A5, SDA to A4, BAT to A0
                                          **/
                                          
                                          //Enable debug prints to serial monitor
                                          #define MY_DEBUG 
                                          
                                          // Enable and select radio type attached
                                          #define MY_RADIO_NRF24
                                          #define MY_RF24_PA_LEVEL RF24_PA_MIN //settings are MIN, LOW, HIGH, MAX
                                          #define MY_NODE_ID 18
                                          //#define MY_RADIO_RFM69
                                          
                                          #include <SPI.h>
                                          #include <MySensors.h>  
                                          #include <Wire.h>
                                          #include <BME280_MOD-1022.h>
                                          
                                          // BME280 libraries and variables
                                          // Bosch BME280 Embedded Adventures MOD-1022 weather multi-sensor Arduino code
                                          // Written originally by Embedded Adventures
                                          // https://github.com/embeddedadventures/BME280
                                          
                                          
                                          #define BARO_CHILD 0
                                          #define TEMP_CHILD 1
                                          #define HUM_CHILD 2
                                          
                                          
                                          int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                                          
                                          unsigned long SLEEP_TIME = 15*60000;  // sleep time between reads (seconds x*1000 milliseconds)
                                          //unsigned long SLEEP_TIME = 20000;  // Debug test time 20 seconds
                                          int oldBatteryPcnt = 0;
                                          
                                              
                                          long interval = 1000;                // 1 min interval at which to send (milliseconds)
                                          long previousMillis = interval;        // will store last time data was sent
                                          
                                          const float ALTITUDE = 0; // <-- adapt this value to your location's altitude (in m). Use your smartphone GPS to get an accurate value!
                                          
                                          const char *weather[] = { "stable", "sunny", "cloudy", "unstable", "thunderstorm", "unknown" };
                                          enum FORECAST
                                          {
                                            STABLE = 0,     // "Stable Weather Pattern"
                                            SUNNY = 1,      // "Slowly rising Good Weather", "Clear/Sunny "
                                            CLOUDY = 2,     // "Slowly falling L-Pressure ", "Cloudy/Rain "
                                            UNSTABLE = 3,   // "Quickly rising H-Press",     "Not Stable"
                                            THUNDERSTORM = 4, // "Quickly falling L-Press",    "Thunderstorm"
                                            UNKNOWN = 5     // "Unknown (More Time needed)
                                          };
                                          
                                          
                                          const int LAST_SAMPLES_COUNT = 5;
                                          float lastPressureSamples[LAST_SAMPLES_COUNT];
                                          
                                          
                                          // this CONVERSION_FACTOR is used to convert from Pa to kPa in the forecast algorithm
                                          // get kPa/h by dividing hPa by 10 
                                          #define CONVERSION_FACTOR (1.0/10.0)
                                          
                                          int minuteCount = 0;
                                          bool firstRound = true;
                                          // average value is used in forecast algorithm.
                                          float pressureAvg;
                                          // average after 2 hours is used as reference value for the next iteration.
                                          float pressureAvg2;
                                          
                                          float dP_dt;
                                          boolean metric;
                                          MyMessage tempMsg(TEMP_CHILD, V_TEMP);
                                          MyMessage humMsg(HUM_CHILD, V_HUM);
                                          MyMessage pressureMsg(BARO_CHILD, V_PRESSURE);
                                          
                                          float getLastPressureSamplesAverage()
                                          {
                                            float lastPressureSamplesAverage = 0;
                                            for (int i = 0; i < LAST_SAMPLES_COUNT; i++)
                                            {
                                              lastPressureSamplesAverage += lastPressureSamples[i];
                                            }
                                            lastPressureSamplesAverage /= LAST_SAMPLES_COUNT;
                                          
                                            return lastPressureSamplesAverage;
                                          }
                                          
                                          
                                          // Algorithm found here
                                          // http://www.freescale.com/files/sensors/doc/app_note/AN3914.pdf
                                          // Pressure in hPa -->  forecast done by calculating kPa/h
                                          int sample(float pressure)
                                          {
                                            // Calculate the average of the last n minutes.
                                            int index = minuteCount % LAST_SAMPLES_COUNT;
                                            lastPressureSamples[index] = pressure;
                                          
                                            minuteCount++;
                                            if (minuteCount > 185)
                                            {
                                              minuteCount = 6;
                                            }
                                          
                                            if (minuteCount == 5)
                                            {
                                              pressureAvg = getLastPressureSamplesAverage();
                                            }
                                            else if (minuteCount == 35)
                                            {
                                              float lastPressureAvg = getLastPressureSamplesAverage();
                                              float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
                                              if (firstRound) // first time initial 3 hour
                                              {
                                                dP_dt = change * 2; // note this is for t = 0.5hour
                                              }
                                              else
                                              {
                                                dP_dt = change / 1.5; // divide by 1.5 as this is the difference in time from 0 value.
                                              }
                                            }
                                            else if (minuteCount == 65)
                                            {
                                              float lastPressureAvg = getLastPressureSamplesAverage();
                                              float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
                                              if (firstRound) //first time initial 3 hour
                                              {
                                                dP_dt = change; //note this is for t = 1 hour
                                              }
                                              else
                                              {
                                                dP_dt = change / 2; //divide by 2 as this is the difference in time from 0 value
                                              }
                                            }
                                            else if (minuteCount == 95)
                                            {
                                              float lastPressureAvg = getLastPressureSamplesAverage();
                                              float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
                                              if (firstRound) // first time initial 3 hour
                                              {
                                                dP_dt = change / 1.5; // note this is for t = 1.5 hour
                                              }
                                              else
                                              {
                                                dP_dt = change / 2.5; // divide by 2.5 as this is the difference in time from 0 value
                                              }
                                            }
                                            else if (minuteCount == 125)
                                            {
                                              float lastPressureAvg = getLastPressureSamplesAverage();
                                              pressureAvg2 = lastPressureAvg; // store for later use.
                                              float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
                                              if (firstRound) // first time initial 3 hour
                                              {
                                                dP_dt = change / 2; // note this is for t = 2 hour
                                              }
                                              else
                                              {
                                                dP_dt = change / 3; // divide by 3 as this is the difference in time from 0 value
                                              }
                                            }
                                            else if (minuteCount == 155)
                                            {
                                              float lastPressureAvg = getLastPressureSamplesAverage();
                                              float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
                                              if (firstRound) // first time initial 3 hour
                                              {
                                                dP_dt = change / 2.5; // note this is for t = 2.5 hour
                                              }
                                              else
                                              {
                                                dP_dt = change / 3.5; // divide by 3.5 as this is the difference in time from 0 value
                                              }
                                            }
                                            else if (minuteCount == 185)
                                            {
                                              float lastPressureAvg = getLastPressureSamplesAverage();
                                              float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
                                              if (firstRound) // first time initial 3 hour
                                              {
                                                dP_dt = change / 3; // note this is for t = 3 hour
                                              }
                                              else
                                              {
                                                dP_dt = change / 4; // divide by 4 as this is the difference in time from 0 value
                                              }
                                              pressureAvg = pressureAvg2; // Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past.
                                              firstRound = false; // flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop.
                                            }
                                          
                                            int forecast = UNKNOWN;
                                            if (minuteCount < 35 && firstRound) //if time is less than 35 min on the first 3 hour interval.
                                            {
                                              forecast = UNKNOWN;
                                            }
                                            else if (dP_dt < (-0.25))
                                            {
                                              forecast = THUNDERSTORM;
                                            }
                                            else if (dP_dt > 0.25)
                                            {
                                              forecast = UNSTABLE;
                                            }
                                            else if ((dP_dt > (-0.25)) && (dP_dt < (-0.05)))
                                            {
                                              forecast = CLOUDY;
                                            }
                                            else if ((dP_dt > 0.05) && (dP_dt < 0.25))
                                            {
                                              forecast = SUNNY;
                                            }
                                            else if ((dP_dt >(-0.05)) && (dP_dt < 0.05))
                                            {
                                              forecast = STABLE;
                                            }
                                            else
                                            {
                                              forecast = UNKNOWN;
                                            }
                                          
                                            return forecast;
                                          }
                                          
                                          
                                          
                                          void setup() 
                                          
                                          {
                                             // use the 1.1 V internal reference
                                          #if defined(__AVR_ATmega2560__)
                                             analogReference(INTERNAL1V1);
                                          #else
                                             analogReference(INTERNAL);
                                          #endif
                                          
                                            metric = getControllerConfig().isMetric;  // was getConfig().isMetric; before MySensors v2.1.1
                                            Wire.begin(); // Wire.begin(sda, scl)
                                            // use the 1.1 V internal reference
                                            #if defined(__AVR_ATmega2560__)
                                            analogReference(INTERNAL1V1);
                                            #else
                                            analogReference(INTERNAL);
                                            #endif
                                          }
                                          
                                          void presentation()  {
                                            // Send the sketch version information to the gateway and Controller
                                            sendSketchInfo("BME280_BAT_V2", "V2e.");
                                          
                                            // Register sensors to gw (they will be created as child devices)
                                            present(BARO_CHILD, S_BARO);
                                            present(TEMP_CHILD, S_TEMP);
                                            present(HUM_CHILD, S_HUM);
                                          
                                          }
                                          
                                          // Loop
                                          void loop() {
                                          
                                          {
                                             // get the battery Voltage
                                             int sensorValue = analogRead(BATTERY_SENSE_PIN);
                                             #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("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);
                                          }
                                          
                                          
                                          unsigned long currentMillis = millis();  
                                          
                                          if(currentMillis - previousMillis > interval) {
                                              // save the last time sent the data
                                              previousMillis = currentMillis;
                                          
                                            analogReference(INTERNAL);
                                            wait(500);
                                            
                                            // need to read the NVM compensation parameters
                                            BME280.readCompensationParams();
                                          
                                            // Normal mode for regular automatic samples
                                            BME280.writeStandbyTime(tsb_0p5ms);         // tsb = 0.5ms
                                            BME280.writeFilterCoefficient(fc_16);       // IIR Filter coefficient 16
                                            BME280.writeOversamplingPressure(os16x);    // pressure x16
                                            BME280.writeOversamplingTemperature(os8x);  // temperature x8
                                            BME280.writeOversamplingHumidity(os8x);     // humidity x8
                                            
                                            BME280.writeMode(smNormal);
                                          
                                              // Just to be sure, wait until sensor is done mesuring  
                                              while (BME280.isMeasuring()) {
                                            }
                                          
                                            // Read out the data - must do this before calling the getxxxxx routines
                                            BME280.readMeasurements();
                                          
                                            float temperature = BME280.getTemperatureMostAccurate();                    // must get temp first
                                            float humidity = BME280.getHumidityMostAccurate();
                                            float pressure_local = BME280.getPressureMostAccurate();                    // Get pressure at current location
                                            float pressure = pressure_local/pow((1.0 - ( ALTITUDE / 44330.0 )), 5.255); // Adjust to sea level pressure using user altitude
                                            int forecast = sample(pressure);
                                          
                                            if (!metric) 
                                            {
                                              // Convert to fahrenheit
                                              temperature = temperature * 9.0 / 5.0 + 32.0;
                                            }
                                          //**/
                                            Serial.println();
                                            Serial.print("Temperature = ");
                                            Serial.print(temperature);
                                            Serial.println(metric ? " °C" : " °F");
                                            Serial.print("Humidity = ");
                                            Serial.print(humidity);
                                            Serial.println(" %");
                                            Serial.print("Pressure = ");
                                            Serial.print(pressure);
                                            Serial.println(" hPa");
                                            Serial.print("Forecast = ");
                                            Serial.println(weather[forecast]);
                                            Serial.println();
                                          //*/
                                          
                                              send(tempMsg.set(temperature, 1));
                                          wait(50);
                                              send(humMsg.set(humidity, 1));
                                          wait(50);
                                              send(pressureMsg.set(pressure, 2));
                                          wait(50);
                                          
                                          BME280.writeMode (smSleep);
                                          sleep(SLEEP_TIME);
                                          
                                          }
                                          
                                          }
                                          
                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          6

                                          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