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. Slim Node Si7021 sensor example

Slim Node Si7021 sensor example

Scheduled Pinned Locked Moved My Project
137 Posts 18 Posters 65.2k Views 20 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.
  • L LastSamurai

    @rsachoc Yes, I actually just tried it out and its working.
    Here is my code for now (just for testing):
    I basically just switched libraries

    /* Sketch with Si7021 and battery monitoring.
    by m26872, 20151109 
    Changed by LastSamurai
    15052016
    */
    #include <MySensor.h>  
    #include <Wire.h>
    #include <SPI.h>
    #include <SparkFunHTU21D.h>
    #include <RunningAverage.h>
    
    //#define DEBUG
    
    #ifdef DEBUG
    #define DEBUG_SERIAL(x) Serial.begin(x)
    #define DEBUG_PRINT(x) Serial.print(x)
    #define DEBUG_PRINTLN(x) Serial.println(x)
    #else
    #define DEBUG_SERIAL(x)
    #define DEBUG_PRINT(x) 
    #define DEBUG_PRINTLN(x) 
    #endif
    
    #define NODE_ID 100             // <<<<<<<<<<<<<<<<<<<<<<<<<<<   Enter Node_ID
    #define CHILD_ID_TEMP 0
    #define CHILD_ID_HUM 1
    // #define SLEEP_TIME 15000 // 15s for DEBUG
    #define SLEEP_TIME 300000   // 5 min
    #define FORCE_TRANSMIT_CYCLE 36  // 5min*12=1/hour, 5min*36=1/3hour 
    #define BATTERY_REPORT_CYCLE 2016   // Once per 5min   =>   12*24*7 = 2016 (one report/week)
    #define VMIN 1900
    #define VMAX 3300
    #define HUMI_TRANSMIT_THRESHOLD 3.0  // THRESHOLD tells how much the value should have changed since last time it was transmitted.
    #define TEMP_TRANSMIT_THRESHOLD 0.5
    #define AVERAGES 2
    
    int batteryReportCounter = BATTERY_REPORT_CYCLE - 1;  // to make it report the first time.
    int measureCount = 0;
    float lastTemperature = -100;
    int lastHumidity = -100;
    
    RunningAverage raHum(AVERAGES);
    HTU21D humiditySensor;
    
    MySensor gw;
    MyMessage msgTemp(CHILD_ID_TEMP,V_TEMP); // Initialize temperature message
    MyMessage msgHum(CHILD_ID_HUM,V_HUM);
    
    void setup() {
      DEBUG_SERIAL(9600);    // <<<<<<<<<<<<<<<<<<<<<<<<<< Note BAUD_RATE in MySensors.h
      DEBUG_PRINTLN("Serial started");
      
      DEBUG_PRINT("Voltage: ");
      DEBUG_PRINT(readVcc()); 
      DEBUG_PRINTLN(" mV");
    
      delay(500);
      DEBUG_PRINT("Internal temp: ");
      DEBUG_PRINT(GetInternalTemp()); // Probably not calibrated. Just to print something.
      DEBUG_PRINTLN(" *C");
    
      delay(500); // Allow time for radio if power useed as reset
      gw.begin(NULL, NODE_ID);
      gw.sendSketchInfo("TempHumTest2", "1.0 17052016"); 
      gw.present(CHILD_ID_TEMP, S_TEMP);   // Present sensor to controller
      gw.present(CHILD_ID_HUM, S_HUM);
      DEBUG_PRINT("Node and "); DEBUG_PRINTLN("2 children presented.");
      
      raHum.clear();
      humiditySensor.begin();  
    }
    
    void loop() { 
    
      measureCount ++;
      batteryReportCounter ++;
      bool forceTransmit = false;
      
      if (measureCount > FORCE_TRANSMIT_CYCLE) {
        forceTransmit = true; 
      }
      sendTempHumidityMeasurements(forceTransmit);
    /*
      // Read and print internal temp
      float temperature0 = static_cast<float>(static_cast<int>((GetInternalTemp()+0.5) * 10.)) / 10.;
      DEBUG_PRINT("Internal Temp: "); DEBUG_PRINT(temperature0); DEBUG_PRINTLN(" *C");        
    */
      // Check battery
      if (batteryReportCounter >= BATTERY_REPORT_CYCLE) {
        long batteryVolt = readVcc();
        DEBUG_PRINT("Battery voltage: "); DEBUG_PRINT(batteryVolt); DEBUG_PRINTLN(" mV");
        uint8_t batteryPcnt = constrain(map(batteryVolt,VMIN,VMAX,0,100),0,255);   
        DEBUG_PRINT("Battery percent: "); DEBUG_PRINT(batteryPcnt); DEBUG_PRINTLN(" %");
        gw.sendBatteryLevel(batteryPcnt);
        batteryReportCounter = 0;
      }
      
      gw.sleep(SLEEP_TIME);
    }
    
    // function for reading Vcc by reading 1.1V reference against AVcc. Based from http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
    // To calibrate reading replace 1125300L with scale_constant = internal1.1Ref * 1023 * 1000, where internal1.1Ref = 1.1 * Vcc1 (per voltmeter) / Vcc2 (per readVcc() function) 
    long readVcc() {
      // set the reference to Vcc and the measurement to the internal 1.1V reference
      ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
      delay(2); // Wait for Vref to settle
      ADCSRA |= _BV(ADSC); // Start conversion
      while (bit_is_set(ADCSRA,ADSC)); // measuring
      uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH  
      uint8_t high = ADCH; // unlocks both
      long result = (high<<8) | low;
      result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
      return result; // Vcc in millivolts
    }
    // function for reading internal temp. From http://playground.arduino.cc/Main/InternalTemperatureSensor 
    double GetInternalTemp(void) {  // (Both double and float are 4 byte in most arduino implementation)
      unsigned int wADC;
      double t;
      // The internal temperature has to be used with the internal reference of 1.1V. Channel 8 can not be selected with the analogRead function yet.
      ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));   // Set the internal reference and mux.
      ADCSRA |= _BV(ADEN);  // enable the ADC
      delay(20);            // wait for voltages to become stable.
      ADCSRA |= _BV(ADSC);  // Start the ADC
      while (bit_is_set(ADCSRA,ADSC));   // Detect end-of-conversion
      wADC = ADCW;   // Reading register "ADCW" takes care of how to read ADCL and ADCH.
      t = (wADC - 88.0 ) / 1.0;   // The default offset is 324.31.
      return (t);   // The returned temperature in degrees Celcius.
    }
    
    /*********************************************
     * * Sends temperature and humidity from Si7021 sensor
     * Parameters
     * - force : Forces transmission of a value (even if it's the same as previous measurement)
     *********************************************/
    void sendTempHumidityMeasurements(bool force) {
      bool tx = force;
      
      float temperature = humiditySensor.readTemperature();
      DEBUG_PRINT("T: ");DEBUG_PRINTLN(temperature);
    
      float diffTemp = abs(lastTemperature - temperature);
      DEBUG_PRINT(F("TempDiff :"));DEBUG_PRINTLN(diffTemp);
    
      if (diffTemp > TEMP_TRANSMIT_THRESHOLD || tx) {
        gw.send(msgTemp.set(temperature,1));
        lastTemperature = temperature;
        measureCount = 0;
        DEBUG_PRINTLN("T sent!");
      }
      
      int humidity = humiditySensor.readHumidity();
      DEBUG_PRINT("H: ");DEBUG_PRINTLN(humidity);
    
      raHum.addValue(humidity);
      humidity = raHum.getAverage();  // MA sample imply reasonable fast sample frequency
      float diffHum = abs(lastHumidity - humidity);
      DEBUG_PRINT(F("HumDiff  :"));DEBUG_PRINTLN(diffHum); 
    
      if (diffHum > HUMI_TRANSMIT_THRESHOLD || tx) {
        gw.send(msgHum.set(humidity));
        lastHumidity = humidity;
        measureCount = 0;
        DEBUG_PRINTLN("H sent!");
      }
    
    }
    
    rsachocR Offline
    rsachocR Offline
    rsachoc
    wrote on last edited by
    #91

    @LastSamurai thanks, progress, I'm getting this now, which means it's working! Finally, a working node from a complete rookie like me!

    Serial started
    Voltage: 3389 mV
    Internal temp: 266.00 *C
    send: 100-100-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
    send: 100-100-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.4
    send: 100-100-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
    read: 0-0-100 s=255,c=3,t=15,pt=0,l=2,sg=0:
    sensor started, id=100, parent=0, distance=1
    send: 100-100-0-0 s=255,c=3,t=11,pt=0,l=12,sg=0,st=fail:TempHumTest2
    send: 100-100-0-0 s=255,c=3,t=12,pt=0,l=12,sg=0,st=fail:1.0 17052016
    send: 100-100-0-0 s=0,c=0,t=6,pt=0,l=0,sg=0,st=fail:
    send: 100-100-0-0 s=1,c=0,t=7,pt=0,l=0,sg=0,st=fail:
    Node and 2 children presented.
    T: 998.00
    TempDiff :1098.00
    send: 100-100-0-0 s=0,c=1,t=0,pt=7,l=5,sg=0,st=fail:998.0
    T sent!
    H: 118
    HumDiff  :218.00
    send: 100-100-0-0 s=1,c=1,t=1,pt=2,l=2,sg=0,st=ok:118
    H sent!
    Battery voltage: 3359 mV
    Battery percent: 104 %
    send: 100-100-0-0 s=255,c=3,t=0,pt=1,l=1,sg=0,st=ok:104
    

    I think that the readings are not accurate though, not sure why, but I'll have a go at figuring our why anyway!

    GertSandersG 1 Reply Last reply
    1
    • rsachocR rsachoc

      @LastSamurai thanks, progress, I'm getting this now, which means it's working! Finally, a working node from a complete rookie like me!

      Serial started
      Voltage: 3389 mV
      Internal temp: 266.00 *C
      send: 100-100-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
      send: 100-100-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.4
      send: 100-100-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
      read: 0-0-100 s=255,c=3,t=15,pt=0,l=2,sg=0:
      sensor started, id=100, parent=0, distance=1
      send: 100-100-0-0 s=255,c=3,t=11,pt=0,l=12,sg=0,st=fail:TempHumTest2
      send: 100-100-0-0 s=255,c=3,t=12,pt=0,l=12,sg=0,st=fail:1.0 17052016
      send: 100-100-0-0 s=0,c=0,t=6,pt=0,l=0,sg=0,st=fail:
      send: 100-100-0-0 s=1,c=0,t=7,pt=0,l=0,sg=0,st=fail:
      Node and 2 children presented.
      T: 998.00
      TempDiff :1098.00
      send: 100-100-0-0 s=0,c=1,t=0,pt=7,l=5,sg=0,st=fail:998.0
      T sent!
      H: 118
      HumDiff  :218.00
      send: 100-100-0-0 s=1,c=1,t=1,pt=2,l=2,sg=0,st=ok:118
      H sent!
      Battery voltage: 3359 mV
      Battery percent: 104 %
      send: 100-100-0-0 s=255,c=3,t=0,pt=1,l=1,sg=0,st=ok:104
      

      I think that the readings are not accurate though, not sure why, but I'll have a go at figuring our why anyway!

      GertSandersG Offline
      GertSandersG Offline
      GertSanders
      Hardware Contributor
      wrote on last edited by
      #92

      @rsachoc
      Congrats !!!

      m26872M 1 Reply Last reply
      0
      • GertSandersG GertSanders

        @rsachoc
        Congrats !!!

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

        @GertSanders Thx a lot for your help! I had no clue about the HTU21D lib.

        1 Reply Last reply
        0
        • badmannenB Offline
          badmannenB Offline
          badmannen
          wrote on last edited by
          #94
          This post is deleted!
          1 Reply Last reply
          0
          • siodS Offline
            siodS Offline
            siod
            wrote on last edited by
            #95

            so with the correct library there is no need to solder those 3 spots together!?

            still learning...

            GertSandersG 1 Reply Last reply
            0
            • siodS siod

              so with the correct library there is no need to solder those 3 spots together!?

              GertSandersG Offline
              GertSandersG Offline
              GertSanders
              Hardware Contributor
              wrote on last edited by
              #96

              @siod
              The "spots" are there to connect the pullup resistors to the SDA and SCL lines. This is always needed, regardless of the library of the board onto which you connect the sensor board does not provide pullup resistors itself. On a minimal board like the one in this thread, this is not the case, so the pull up resistors are needed.

              1 Reply Last reply
              0
              • siodS Offline
                siodS Offline
                siod
                wrote on last edited by siod
                #97

                ok, thanks for making it clear for me Gert!!

                update:

                finally found time to build my sensor and it seems to work fine, will do more testing but for now my results are:

                7.5 uA when sleeping, 15,3 mA when working (every 15 minutes for about 2seconds, but I think my cheap multimeter gives bad readings on those spikes, I guess it´s only around 1 mA)

                seems to be okay, isn´t it?

                edit:

                I have 2 reed switches attached. I recognized that I have +0,10 mA for each reed switch when activated. So when reed switches are both touching their counterpart I have a sleeping consumption of 0,20 mA. Why is that??

                still learning...

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jacikaas
                  wrote on last edited by jacikaas
                  #98

                  Hello,

                  I'm trying to convert existing Si7021 sensor code from 1.5 to 2.0.0 library. I change the code and then I was getting an error which said:

                  Si7021_sensor_MySensors_2.0.0:158: error: 'si7021_env' has no member named 'humidityPercent'
                     int humidity = data.humidityPercent;
                                         ^
                  exit status 1
                  'si7021_env' has no member named 'humidityPercent'
                  

                  Then I chech the Si7021 library, and how I saw there wasn't member 'humidityPercent', but there was a member 'humidityBasisPoints'.

                  So I change line from:

                    int humidity = data.humidityPercent;
                  

                  ...to:

                    int humidity = data.humidityBasisPoints;
                  

                  Then code compiles and I upload it to Slim Node Si7021. I leave sensor and today I checked it log on Domoticz. I have this:
                  0_1471539568477_chart.jpeg

                  Temperature looks ok, but Humidity value jumping a lot, thats why I don't want to believe it is true.

                  Could someone, who has a 2.0.0 library and Si7021 sensor chech is it same situation? Becose I have only one Si7021 sensor.
                  Maybe someone could look also on the code and say if I convert it right?

                  Converted code:

                  /* Sketch with Si7021 and battery monitoring.
                  by m26872, 20151109 
                  */
                  
                  #define MY_DEBUG
                  #define MY_RADIO_NRF24
                  #define MY_NODE_ID 5             // <<<<<<<<<<<<<<<<<<<<<<<<<<<   Enter Node_ID
                  #define MY_BAUD_RATE 115200
                  
                  #include <MySensors.h>  
                  #include <Wire.h>
                  #include <SI7021.h>
                  #include <SPI.h>
                  #include <RunningAverage.h>
                  
                  //#define DEBUG
                  
                  #ifdef DEBUG
                  #define DEBUG_SERIAL(x) Serial.begin(x)
                  #define DEBUG_PRINT(x) Serial.print(x)
                  #define DEBUG_PRINTLN(x) Serial.println(x)
                  #else
                  #define DEBUG_SERIAL(x)
                  #define DEBUG_PRINT(x) 
                  #define DEBUG_PRINTLN(x) 
                  #endif
                  
                  #define CHILD_ID_TEMP 0
                  #define CHILD_ID_HUM 1
                  // #define SLEEP_TIME 15000 // 15s for DEBUG
                  #define SLEEP_TIME 300000   // 5 min
                  #define FORCE_TRANSMIT_CYCLE 36  // 5min*12=1/hour, 5min*36=1/3hour 
                  #define BATTERY_REPORT_CYCLE 2880   // Once per 5min   =>   12*24*7 = 2016 (one report/week)
                  #define VMIN 1900
                  #define VMAX 3300
                  #define HUMI_TRANSMIT_THRESHOLD 3.0  // THRESHOLD tells how much the value should have changed since last time it was transmitted.
                  #define TEMP_TRANSMIT_THRESHOLD 0.5
                  #define AVERAGES 2
                  
                  int batteryReportCounter = BATTERY_REPORT_CYCLE - 1;  // to make it report the first time.
                  int measureCount = 0;
                  float lastTemperature = -100;
                  int lastHumidity = -100;
                  
                  RunningAverage raHum(AVERAGES);
                  SI7021 humiditySensor;
                  
                  MyMessage msgTemp(CHILD_ID_TEMP,V_TEMP); // Initialize temperature message
                  MyMessage msgHum(CHILD_ID_HUM,V_HUM);
                  
                  
                  void presentation()  
                  { 
                    sendSketchInfo("EgTmpHumBat5min", "1.0 151106");
                    present(CHILD_ID_TEMP, S_TEMP);   // Present sensor to controller
                    present(CHILD_ID_HUM, S_HUM);
                  }
                  
                  
                  void setup() {
                    DEBUG_SERIAL(115200);    // <<<<<<<<<<<<<<<<<<<<<<<<<< Note BAUD_RATE in MySensors.h
                    DEBUG_PRINTLN("Serial started");
                    
                    DEBUG_PRINT("Voltage: ");
                    DEBUG_PRINT(readVcc()); 
                    DEBUG_PRINTLN(" mV");
                  /*
                    delay(500);
                    DEBUG_PRINT("Internal temp: ");
                    DEBUG_PRINT(GetInternalTemp()); // Probably not calibrated. Just to print something.
                    DEBUG_PRINTLN(" *C");
                  */  
                    delay(500); // Allow time for radio if power useed as reset
                    DEBUG_PRINT("Node and "); DEBUG_PRINTLN("2 children presented.");
                    
                    raHum.clear();
                    
                  }
                  
                  void loop() { 
                  
                    measureCount ++;
                    batteryReportCounter ++;
                    bool forceTransmit = false;
                    
                    if (measureCount > FORCE_TRANSMIT_CYCLE) {
                      forceTransmit = true; 
                    }
                    sendTempHumidityMeasurements(forceTransmit);
                  /*
                    // Read and print internal temp
                    float temperature0 = static_cast<float>(static_cast<int>((GetInternalTemp()+0.5) * 10.)) / 10.;
                    DEBUG_PRINT("Internal Temp: "); DEBUG_PRINT(temperature0); DEBUG_PRINTLN(" *C");        
                  */
                    // Check battery
                    if (batteryReportCounter >= BATTERY_REPORT_CYCLE) {
                      long batteryVolt = readVcc();
                      DEBUG_PRINT("Battery voltage: "); DEBUG_PRINT(batteryVolt); DEBUG_PRINTLN(" mV");
                      uint8_t batteryPcnt = constrain(map(batteryVolt,VMIN,VMAX,0,100),0,255);   
                      DEBUG_PRINT("Battery percent: "); DEBUG_PRINT(batteryPcnt); DEBUG_PRINTLN(" %");
                      sendBatteryLevel(batteryPcnt);
                      batteryReportCounter = 0;
                    }
                    
                    sleep(SLEEP_TIME);
                  }
                  
                  // function for reading Vcc by reading 1.1V reference against AVcc. Based from http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
                  // To calibrate reading replace 1125300L with scale_constant = internal1.1Ref * 1023 * 1000, where internal1.1Ref = 1.1 * Vcc1 (per voltmeter) / Vcc2 (per readVcc() function) 
                  long readVcc() {
                    // set the reference to Vcc and the measurement to the internal 1.1V reference
                    ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
                    delay(2); // Wait for Vref to settle
                    ADCSRA |= _BV(ADSC); // Start conversion
                    while (bit_is_set(ADCSRA,ADSC)); // measuring
                    uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH  
                    uint8_t high = ADCH; // unlocks both
                    long result = (high<<8) | low;
                    result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
                    return result; // Vcc in millivolts
                  }
                  // function for reading internal temp. From http://playground.arduino.cc/Main/InternalTemperatureSensor 
                  double GetInternalTemp(void) {  // (Both double and float are 4 byte in most arduino implementation)
                    unsigned int wADC;
                    double t;
                    // The internal temperature has to be used with the internal reference of 1.1V. Channel 8 can not be selected with the analogRead function yet.
                    ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));   // Set the internal reference and mux.
                    ADCSRA |= _BV(ADEN);  // enable the ADC
                    delay(20);            // wait for voltages to become stable.
                    ADCSRA |= _BV(ADSC);  // Start the ADC
                    while (bit_is_set(ADCSRA,ADSC));   // Detect end-of-conversion
                    wADC = ADCW;   // Reading register "ADCW" takes care of how to read ADCL and ADCH.
                    t = (wADC - 88.0 ) / 1.0;   // The default offset is 324.31.
                    return (t);   // The returned temperature in degrees Celcius.
                  }
                  
                  /*********************************************
                   * * Sends temperature and humidity from Si7021 sensor
                   * Parameters
                   * - force : Forces transmission of a value (even if it's the same as previous measurement)
                   *********************************************/
                  void sendTempHumidityMeasurements(bool force) {
                    bool tx = force;
                  
                    si7021_env data = humiditySensor.getHumidityAndTemperature();
                    
                    float temperature = data.celsiusHundredths / 100.0;
                    DEBUG_PRINT("T: ");DEBUG_PRINTLN(temperature);
                    float diffTemp = abs(lastTemperature - temperature);
                    DEBUG_PRINT(F("TempDiff :"));DEBUG_PRINTLN(diffTemp);
                    if (diffTemp > TEMP_TRANSMIT_THRESHOLD || tx) {
                      send(msgTemp.set(temperature,1));
                      lastTemperature = temperature;
                      measureCount = 0;
                      DEBUG_PRINTLN("T sent!");
                    }
                    
                    int humidity = data.humidityPercent;
                    DEBUG_PRINT("H: ");DEBUG_PRINTLN(humidity);
                    raHum.addValue(humidity);
                    humidity = raHum.getAverage();  // MA sample imply reasonable fast sample frequency
                    float diffHum = abs(lastHumidity - humidity);  
                    DEBUG_PRINT(F("HumDiff  :"));DEBUG_PRINTLN(diffHum); 
                    if (diffHum > HUMI_TRANSMIT_THRESHOLD || tx) {
                      send(msgHum.set(humidity));
                      lastHumidity = humidity;
                      measureCount = 0;
                      DEBUG_PRINTLN("H sent!");
                    }
                  
                  }
                  

                  Thank You!

                  AWIA 1 Reply Last reply
                  0
                  • J jacikaas

                    Hello,

                    I'm trying to convert existing Si7021 sensor code from 1.5 to 2.0.0 library. I change the code and then I was getting an error which said:

                    Si7021_sensor_MySensors_2.0.0:158: error: 'si7021_env' has no member named 'humidityPercent'
                       int humidity = data.humidityPercent;
                                           ^
                    exit status 1
                    'si7021_env' has no member named 'humidityPercent'
                    

                    Then I chech the Si7021 library, and how I saw there wasn't member 'humidityPercent', but there was a member 'humidityBasisPoints'.

                    So I change line from:

                      int humidity = data.humidityPercent;
                    

                    ...to:

                      int humidity = data.humidityBasisPoints;
                    

                    Then code compiles and I upload it to Slim Node Si7021. I leave sensor and today I checked it log on Domoticz. I have this:
                    0_1471539568477_chart.jpeg

                    Temperature looks ok, but Humidity value jumping a lot, thats why I don't want to believe it is true.

                    Could someone, who has a 2.0.0 library and Si7021 sensor chech is it same situation? Becose I have only one Si7021 sensor.
                    Maybe someone could look also on the code and say if I convert it right?

                    Converted code:

                    /* Sketch with Si7021 and battery monitoring.
                    by m26872, 20151109 
                    */
                    
                    #define MY_DEBUG
                    #define MY_RADIO_NRF24
                    #define MY_NODE_ID 5             // <<<<<<<<<<<<<<<<<<<<<<<<<<<   Enter Node_ID
                    #define MY_BAUD_RATE 115200
                    
                    #include <MySensors.h>  
                    #include <Wire.h>
                    #include <SI7021.h>
                    #include <SPI.h>
                    #include <RunningAverage.h>
                    
                    //#define DEBUG
                    
                    #ifdef DEBUG
                    #define DEBUG_SERIAL(x) Serial.begin(x)
                    #define DEBUG_PRINT(x) Serial.print(x)
                    #define DEBUG_PRINTLN(x) Serial.println(x)
                    #else
                    #define DEBUG_SERIAL(x)
                    #define DEBUG_PRINT(x) 
                    #define DEBUG_PRINTLN(x) 
                    #endif
                    
                    #define CHILD_ID_TEMP 0
                    #define CHILD_ID_HUM 1
                    // #define SLEEP_TIME 15000 // 15s for DEBUG
                    #define SLEEP_TIME 300000   // 5 min
                    #define FORCE_TRANSMIT_CYCLE 36  // 5min*12=1/hour, 5min*36=1/3hour 
                    #define BATTERY_REPORT_CYCLE 2880   // Once per 5min   =>   12*24*7 = 2016 (one report/week)
                    #define VMIN 1900
                    #define VMAX 3300
                    #define HUMI_TRANSMIT_THRESHOLD 3.0  // THRESHOLD tells how much the value should have changed since last time it was transmitted.
                    #define TEMP_TRANSMIT_THRESHOLD 0.5
                    #define AVERAGES 2
                    
                    int batteryReportCounter = BATTERY_REPORT_CYCLE - 1;  // to make it report the first time.
                    int measureCount = 0;
                    float lastTemperature = -100;
                    int lastHumidity = -100;
                    
                    RunningAverage raHum(AVERAGES);
                    SI7021 humiditySensor;
                    
                    MyMessage msgTemp(CHILD_ID_TEMP,V_TEMP); // Initialize temperature message
                    MyMessage msgHum(CHILD_ID_HUM,V_HUM);
                    
                    
                    void presentation()  
                    { 
                      sendSketchInfo("EgTmpHumBat5min", "1.0 151106");
                      present(CHILD_ID_TEMP, S_TEMP);   // Present sensor to controller
                      present(CHILD_ID_HUM, S_HUM);
                    }
                    
                    
                    void setup() {
                      DEBUG_SERIAL(115200);    // <<<<<<<<<<<<<<<<<<<<<<<<<< Note BAUD_RATE in MySensors.h
                      DEBUG_PRINTLN("Serial started");
                      
                      DEBUG_PRINT("Voltage: ");
                      DEBUG_PRINT(readVcc()); 
                      DEBUG_PRINTLN(" mV");
                    /*
                      delay(500);
                      DEBUG_PRINT("Internal temp: ");
                      DEBUG_PRINT(GetInternalTemp()); // Probably not calibrated. Just to print something.
                      DEBUG_PRINTLN(" *C");
                    */  
                      delay(500); // Allow time for radio if power useed as reset
                      DEBUG_PRINT("Node and "); DEBUG_PRINTLN("2 children presented.");
                      
                      raHum.clear();
                      
                    }
                    
                    void loop() { 
                    
                      measureCount ++;
                      batteryReportCounter ++;
                      bool forceTransmit = false;
                      
                      if (measureCount > FORCE_TRANSMIT_CYCLE) {
                        forceTransmit = true; 
                      }
                      sendTempHumidityMeasurements(forceTransmit);
                    /*
                      // Read and print internal temp
                      float temperature0 = static_cast<float>(static_cast<int>((GetInternalTemp()+0.5) * 10.)) / 10.;
                      DEBUG_PRINT("Internal Temp: "); DEBUG_PRINT(temperature0); DEBUG_PRINTLN(" *C");        
                    */
                      // Check battery
                      if (batteryReportCounter >= BATTERY_REPORT_CYCLE) {
                        long batteryVolt = readVcc();
                        DEBUG_PRINT("Battery voltage: "); DEBUG_PRINT(batteryVolt); DEBUG_PRINTLN(" mV");
                        uint8_t batteryPcnt = constrain(map(batteryVolt,VMIN,VMAX,0,100),0,255);   
                        DEBUG_PRINT("Battery percent: "); DEBUG_PRINT(batteryPcnt); DEBUG_PRINTLN(" %");
                        sendBatteryLevel(batteryPcnt);
                        batteryReportCounter = 0;
                      }
                      
                      sleep(SLEEP_TIME);
                    }
                    
                    // function for reading Vcc by reading 1.1V reference against AVcc. Based from http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
                    // To calibrate reading replace 1125300L with scale_constant = internal1.1Ref * 1023 * 1000, where internal1.1Ref = 1.1 * Vcc1 (per voltmeter) / Vcc2 (per readVcc() function) 
                    long readVcc() {
                      // set the reference to Vcc and the measurement to the internal 1.1V reference
                      ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
                      delay(2); // Wait for Vref to settle
                      ADCSRA |= _BV(ADSC); // Start conversion
                      while (bit_is_set(ADCSRA,ADSC)); // measuring
                      uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH  
                      uint8_t high = ADCH; // unlocks both
                      long result = (high<<8) | low;
                      result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
                      return result; // Vcc in millivolts
                    }
                    // function for reading internal temp. From http://playground.arduino.cc/Main/InternalTemperatureSensor 
                    double GetInternalTemp(void) {  // (Both double and float are 4 byte in most arduino implementation)
                      unsigned int wADC;
                      double t;
                      // The internal temperature has to be used with the internal reference of 1.1V. Channel 8 can not be selected with the analogRead function yet.
                      ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));   // Set the internal reference and mux.
                      ADCSRA |= _BV(ADEN);  // enable the ADC
                      delay(20);            // wait for voltages to become stable.
                      ADCSRA |= _BV(ADSC);  // Start the ADC
                      while (bit_is_set(ADCSRA,ADSC));   // Detect end-of-conversion
                      wADC = ADCW;   // Reading register "ADCW" takes care of how to read ADCL and ADCH.
                      t = (wADC - 88.0 ) / 1.0;   // The default offset is 324.31.
                      return (t);   // The returned temperature in degrees Celcius.
                    }
                    
                    /*********************************************
                     * * Sends temperature and humidity from Si7021 sensor
                     * Parameters
                     * - force : Forces transmission of a value (even if it's the same as previous measurement)
                     *********************************************/
                    void sendTempHumidityMeasurements(bool force) {
                      bool tx = force;
                    
                      si7021_env data = humiditySensor.getHumidityAndTemperature();
                      
                      float temperature = data.celsiusHundredths / 100.0;
                      DEBUG_PRINT("T: ");DEBUG_PRINTLN(temperature);
                      float diffTemp = abs(lastTemperature - temperature);
                      DEBUG_PRINT(F("TempDiff :"));DEBUG_PRINTLN(diffTemp);
                      if (diffTemp > TEMP_TRANSMIT_THRESHOLD || tx) {
                        send(msgTemp.set(temperature,1));
                        lastTemperature = temperature;
                        measureCount = 0;
                        DEBUG_PRINTLN("T sent!");
                      }
                      
                      int humidity = data.humidityPercent;
                      DEBUG_PRINT("H: ");DEBUG_PRINTLN(humidity);
                      raHum.addValue(humidity);
                      humidity = raHum.getAverage();  // MA sample imply reasonable fast sample frequency
                      float diffHum = abs(lastHumidity - humidity);  
                      DEBUG_PRINT(F("HumDiff  :"));DEBUG_PRINTLN(diffHum); 
                      if (diffHum > HUMI_TRANSMIT_THRESHOLD || tx) {
                        send(msgHum.set(humidity));
                        lastHumidity = humidity;
                        measureCount = 0;
                        DEBUG_PRINTLN("H sent!");
                      }
                    
                    }
                    

                    Thank You!

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

                    @jacikaas where did you get the si7021 library? Take a look at the sensebender sketch for a good reference.

                    J 1 Reply Last reply
                    0
                    • AWIA AWI

                      @jacikaas where did you get the si7021 library? Take a look at the sensebender sketch for a good reference.

                      J Offline
                      J Offline
                      jacikaas
                      wrote on last edited by
                      #100

                      @AWI
                      I download Si7021 library from here: https://github.com/LowPowerLab/SI7021

                      I was heard about Sensbender node, but never look deeper what is in it. I will look at it and try to understand whats in there. I just learning about all that stuff, so it will be good practice :blush:

                      AWIA 1 Reply Last reply
                      0
                      • J jacikaas

                        @AWI
                        I download Si7021 library from here: https://github.com/LowPowerLab/SI7021

                        I was heard about Sensbender node, but never look deeper what is in it. I will look at it and try to understand whats in there. I just learning about all that stuff, so it will be good practice :blush:

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

                        @jacikaas that's the way to go. The sensebender sketch contains some rather nice best practices for low power. You best replace the si library with the one in the MySensors examples on github.

                        J 1 Reply Last reply
                        1
                        • AWIA AWI

                          @jacikaas that's the way to go. The sensebender sketch contains some rather nice best practices for low power. You best replace the si library with the one in the MySensors examples on github.

                          J Offline
                          J Offline
                          jacikaas
                          wrote on last edited by jacikaas
                          #102

                          @AWI I try to replace library from this one https://github.com/LowPowerLab/SI7021 to this one https://github.com/mysensors/MySensorsArduinoExamples

                          to work it I need to return variable name from:

                            int humidity = data.humidityBasisPoints;
                          

                          to:

                            int humidity = data.humidityPercent;
                          

                          I think this library https://github.com/LowPowerLab/SI7021 was updated, so it is not suitable for this one code. Now I test it again and everything looks fine:
                          0_1471719934242_chart (1).jpeg

                          Temperature and humidity node unregister or stop working repeatedly for about 2 hours (2h25min, 2.20, 1.50...). But after that it start to send data again, for some time and then goes around again. :) I will try to understand why it happening too.

                          1 Reply Last reply
                          0
                          • W Offline
                            W Offline
                            wergeld
                            wrote on last edited by
                            #103

                            I am trying to build the code using the HTU21D sensors. This built fine under 1.4 and 1.5 but now under v2 of MySensors I am getting this error:

                            C:\Users\wergeld\Documents\Arduino\SlimNodeSi7021\SlimNodeSi7021.ino:4:24: fatal error: MySensor.h: No such file or directory
                            
                             #include <MySensor.h>  
                            
                                                    ^
                            
                            compilation terminated.
                            
                            exit status 1
                            Error compiling for board APM Optiboot internal 1MHz noBOD 9600baud.
                            

                            Okay, so I change my reference to:

                            #include <MySensors.h>
                            

                            And I get this lovely error:

                            In file included from C:\Users\wergeld\Documents\Arduino\SlimNodeSi7021\SlimNodeSi7021.ino:4:0:
                            
                            C:\Users\wergeld\Documents\Arduino\libraries\MySensors-master/MySensors.h:287:4: error: #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless.
                            
                               #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless.
                            
                                ^
                            
                            exit status 1
                            Error compiling for board APM Optiboot internal 1MHz noBOD 9600baud.
                            

                            So, other than slamming my head against my desk how can I solve this?

                            1 Reply Last reply
                            0
                            • W Offline
                              W Offline
                              wergeld
                              wrote on last edited by
                              #104

                              Well, I solved it by modifying the code to 2.0 spec. Good lord I need sleep. :anguished:
                              Working on debugging temp outputs now to Ethernet Gateway running on my Uno. Or I will sleep...

                              1 Reply Last reply
                              0
                              • W Offline
                                W Offline
                                wergeld
                                wrote on last edited by
                                #105

                                Okay, I now have a soldered node and serial gateway setup to my laptop for testing. It presents the sensor node! The name and available sensors is correct. Howevr, it never sends any message payload with temp/hum or battery values. I am using the HTU21D without soldering those 3 pads on it together. I need to get myself a 3.3v FTDI cable so I can serially debug I guess. But, still! Progress.

                                1 Reply Last reply
                                0
                                • W Offline
                                  W Offline
                                  wergeld
                                  wrote on last edited by
                                  #106

                                  After soldering the 3 pads on the HTU21D I am now reading data and it is being sent to my test serial gateway. Need to measure current usage but at least it is functional.

                                  1 Reply Last reply
                                  0
                                  • W Offline
                                    W Offline
                                    wergeld
                                    wrote on last edited by
                                    #107

                                    Okay, seeing something odd. The force transmit does not appear to be working.
                                    Here is my current sketch:

                                    #define MY_RADIO_NRF24
                                    
                                    #include <MyConfig.h>
                                    #include <MySensors.h>
                                    
                                    /* Sketch with Si7021 and battery monitoring.
                                    by m26872, 20151109 
                                    */
                                    //#include <MySensors.h>  
                                    #include <Wire.h>
                                    #include <SparkFunHTU21D.h>
                                    #include <SPI.h>
                                    #include <RunningAverage.h>
                                    
                                    //#define DEBUG
                                    
                                    #ifdef DEBUG
                                    #define DEBUG_SERIAL(x) Serial.begin(x)
                                    #define DEBUG_PRINT(x) Serial.print(x)
                                    #define DEBUG_PRINTLN(x) Serial.println(x)
                                    #else
                                    #define DEBUG_SERIAL(x)
                                    #define DEBUG_PRINT(x) 
                                    #define DEBUG_PRINTLN(x) 
                                    #endif
                                    
                                    #define NODE_ID 132             // <<<<<<<<<<<<<<<<<<<<<<<<<<<   Enter Node_ID
                                    #define CHILD_ID_TEMP 0
                                    #define CHILD_ID_HUM 1
                                    // #define SLEEP_TIME 15000 // 15s for DEBUG
                                    #define SLEEP_TIME 300000   // 5 min
                                    #define FORCE_TRANSMIT_CYCLE 36  // 5min*12=1/hour, 5min*36=1/3hour 
                                    #define BATTERY_REPORT_CYCLE 2880   // Once per 5min   =>   12*24*7 = 2016 (one report/week)
                                    #define VMIN 1900
                                    #define VMAX 3300
                                    #define HUMI_TRANSMIT_THRESHOLD 3.0  // THRESHOLD tells how much the value should have changed since last time it was transmitted.
                                    #define TEMP_TRANSMIT_THRESHOLD 0.5
                                    #define AVERAGES 2
                                    
                                    int batteryReportCounter = BATTERY_REPORT_CYCLE - 1;  // to make it report the first time.
                                    int measureCount = 0;
                                    float lastTemperature = -100;
                                    int lastHumidity = -100;
                                    
                                    RunningAverage raHum(AVERAGES);
                                    HTU21D humiditySensor;
                                    
                                    //MySensor gw;
                                    MyMessage msgTemp(CHILD_ID_TEMP,V_TEMP); // Initialize temperature message
                                    MyMessage msgHum(CHILD_ID_HUM,V_HUM);
                                    
                                    void presentation()  
                                    { 
                                      sendSketchInfo("HTU21D", "1.0"); 
                                      present(CHILD_ID_TEMP, S_TEMP);   // Present sensor to controller
                                      present(CHILD_ID_HUM, S_HUM);
                                    }
                                    
                                    void setup() {
                                      DEBUG_SERIAL(9600);    // <<<<<<<<<<<<<<<<<<<<<<<<<< Note BAUD_RATE in MySensors.h
                                      DEBUG_PRINTLN("Serial started");
                                      
                                      DEBUG_PRINT("Voltage: ");
                                      DEBUG_PRINT(readVcc()); 
                                      DEBUG_PRINTLN(" mV");
                                    /*
                                      delay(500);
                                      DEBUG_PRINT("Internal temp: ");
                                      DEBUG_PRINT(GetInternalTemp()); // Probably not calibrated. Just to print something.
                                      DEBUG_PRINTLN(" *C");
                                    */  
                                      delay(500); // Allow time for radio if power useed as reset
                                      //gw.begin(NULL,NODE_ID);
                                      //sendSketchInfo("HTU21D", "1.0"); 
                                      //present(CHILD_ID_TEMP, S_TEMP);   // Present sensor to controller
                                      //present(CHILD_ID_HUM, S_HUM);
                                      DEBUG_PRINT("Node and "); DEBUG_PRINTLN("2 children presented.");
                                      
                                      raHum.clear();
                                      
                                    }
                                    
                                    void loop() { 
                                    
                                      measureCount ++;
                                      batteryReportCounter ++;
                                      bool forceTransmit = false;
                                      
                                      if (measureCount > FORCE_TRANSMIT_CYCLE) {
                                        forceTransmit = true; 
                                      }
                                      sendTempHumidityMeasurements(forceTransmit);
                                    /*
                                      // Read and print internal temp
                                      float temperature0 = static_cast<float>(static_cast<int>((GetInternalTemp()+0.5) * 10.)) / 10.;
                                      DEBUG_PRINT("Internal Temp: "); DEBUG_PRINT(temperature0); DEBUG_PRINTLN(" *C");        
                                    */
                                      // Check battery
                                      if (batteryReportCounter >= BATTERY_REPORT_CYCLE) {
                                        long batteryVolt = readVcc();
                                        DEBUG_PRINT("Battery voltage: "); DEBUG_PRINT(batteryVolt); DEBUG_PRINTLN(" mV");
                                        uint8_t batteryPcnt = constrain(map(batteryVolt,VMIN,VMAX,0,100),0,255);   
                                        DEBUG_PRINT("Battery percent: "); DEBUG_PRINT(batteryPcnt); DEBUG_PRINTLN(" %");
                                        sendBatteryLevel(batteryPcnt);
                                        batteryReportCounter = 0;
                                      }
                                      
                                      sleep(SLEEP_TIME);
                                    }
                                    
                                    // function for reading Vcc by reading 1.1V reference against AVcc. Based from http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
                                    // To calibrate reading replace 1125300L with scale_constant = internal1.1Ref * 1023 * 1000, where internal1.1Ref = 1.1 * Vcc1 (per voltmeter) / Vcc2 (per readVcc() function) 
                                    long readVcc() {
                                      // set the reference to Vcc and the measurement to the internal 1.1V reference
                                      ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
                                      delay(2); // Wait for Vref to settle
                                      ADCSRA |= _BV(ADSC); // Start conversion
                                      while (bit_is_set(ADCSRA,ADSC)); // measuring
                                      uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH  
                                      uint8_t high = ADCH; // unlocks both
                                      long result = (high<<8) | low;
                                      result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
                                      return result; // Vcc in millivolts
                                    }
                                    // function for reading internal temp. From http://playground.arduino.cc/Main/InternalTemperatureSensor 
                                    double GetInternalTemp(void) {  // (Both double and float are 4 byte in most arduino implementation)
                                      unsigned int wADC;
                                      double t;
                                      // The internal temperature has to be used with the internal reference of 1.1V. Channel 8 can not be selected with the analogRead function yet.
                                      ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));   // Set the internal reference and mux.
                                      ADCSRA |= _BV(ADEN);  // enable the ADC
                                      delay(20);            // wait for voltages to become stable.
                                      ADCSRA |= _BV(ADSC);  // Start the ADC
                                      while (bit_is_set(ADCSRA,ADSC));   // Detect end-of-conversion
                                      wADC = ADCW;   // Reading register "ADCW" takes care of how to read ADCL and ADCH.
                                      t = (wADC - 88.0 ) / 1.0;   // The default offset is 324.31.
                                      return (t);   // The returned temperature in degrees Celcius.
                                    }
                                    
                                    /*********************************************
                                     * * Sends temperature and humidity from Si7021 sensor
                                     * Parameters
                                     * - force : Forces transmission of a value (even if it's the same as previous measurement)
                                     *********************************************/
                                    void sendTempHumidityMeasurements(bool force) {
                                      bool tx = force;
                                      
                                      float temperature = humiditySensor.readTemperature();
                                      DEBUG_PRINT("T: ");DEBUG_PRINTLN(temperature);
                                    
                                      float diffTemp = abs(lastTemperature - temperature);
                                      DEBUG_PRINT(F("TempDiff :"));DEBUG_PRINTLN(diffTemp);
                                    
                                      if (diffTemp > TEMP_TRANSMIT_THRESHOLD || tx) {
                                        send(msgTemp.set(temperature,1));
                                        lastTemperature = temperature;
                                        measureCount = 0;
                                        DEBUG_PRINTLN("T sent!");
                                      }
                                      
                                      int humidity = humiditySensor.readHumidity();
                                      DEBUG_PRINT("H: ");DEBUG_PRINTLN(humidity);
                                    
                                      raHum.addValue(humidity);
                                      humidity = raHum.getAverage();  // MA sample imply reasonable fast sample frequency
                                      float diffHum = abs(lastHumidity - humidity);
                                      DEBUG_PRINT(F("HumDiff  :"));DEBUG_PRINTLN(diffHum); 
                                    
                                      if (diffHum > HUMI_TRANSMIT_THRESHOLD || tx) {
                                        send(msgHum.set(humidity));
                                        lastHumidity = humidity;
                                        measureCount = 0;
                                        DEBUG_PRINTLN("H sent!");
                                      }
                                    }
                                    

                                    I am at 1+ hours since last transmission.

                                    m26872M 1 Reply Last reply
                                    0
                                    • W wergeld

                                      Okay, seeing something odd. The force transmit does not appear to be working.
                                      Here is my current sketch:

                                      #define MY_RADIO_NRF24
                                      
                                      #include <MyConfig.h>
                                      #include <MySensors.h>
                                      
                                      /* Sketch with Si7021 and battery monitoring.
                                      by m26872, 20151109 
                                      */
                                      //#include <MySensors.h>  
                                      #include <Wire.h>
                                      #include <SparkFunHTU21D.h>
                                      #include <SPI.h>
                                      #include <RunningAverage.h>
                                      
                                      //#define DEBUG
                                      
                                      #ifdef DEBUG
                                      #define DEBUG_SERIAL(x) Serial.begin(x)
                                      #define DEBUG_PRINT(x) Serial.print(x)
                                      #define DEBUG_PRINTLN(x) Serial.println(x)
                                      #else
                                      #define DEBUG_SERIAL(x)
                                      #define DEBUG_PRINT(x) 
                                      #define DEBUG_PRINTLN(x) 
                                      #endif
                                      
                                      #define NODE_ID 132             // <<<<<<<<<<<<<<<<<<<<<<<<<<<   Enter Node_ID
                                      #define CHILD_ID_TEMP 0
                                      #define CHILD_ID_HUM 1
                                      // #define SLEEP_TIME 15000 // 15s for DEBUG
                                      #define SLEEP_TIME 300000   // 5 min
                                      #define FORCE_TRANSMIT_CYCLE 36  // 5min*12=1/hour, 5min*36=1/3hour 
                                      #define BATTERY_REPORT_CYCLE 2880   // Once per 5min   =>   12*24*7 = 2016 (one report/week)
                                      #define VMIN 1900
                                      #define VMAX 3300
                                      #define HUMI_TRANSMIT_THRESHOLD 3.0  // THRESHOLD tells how much the value should have changed since last time it was transmitted.
                                      #define TEMP_TRANSMIT_THRESHOLD 0.5
                                      #define AVERAGES 2
                                      
                                      int batteryReportCounter = BATTERY_REPORT_CYCLE - 1;  // to make it report the first time.
                                      int measureCount = 0;
                                      float lastTemperature = -100;
                                      int lastHumidity = -100;
                                      
                                      RunningAverage raHum(AVERAGES);
                                      HTU21D humiditySensor;
                                      
                                      //MySensor gw;
                                      MyMessage msgTemp(CHILD_ID_TEMP,V_TEMP); // Initialize temperature message
                                      MyMessage msgHum(CHILD_ID_HUM,V_HUM);
                                      
                                      void presentation()  
                                      { 
                                        sendSketchInfo("HTU21D", "1.0"); 
                                        present(CHILD_ID_TEMP, S_TEMP);   // Present sensor to controller
                                        present(CHILD_ID_HUM, S_HUM);
                                      }
                                      
                                      void setup() {
                                        DEBUG_SERIAL(9600);    // <<<<<<<<<<<<<<<<<<<<<<<<<< Note BAUD_RATE in MySensors.h
                                        DEBUG_PRINTLN("Serial started");
                                        
                                        DEBUG_PRINT("Voltage: ");
                                        DEBUG_PRINT(readVcc()); 
                                        DEBUG_PRINTLN(" mV");
                                      /*
                                        delay(500);
                                        DEBUG_PRINT("Internal temp: ");
                                        DEBUG_PRINT(GetInternalTemp()); // Probably not calibrated. Just to print something.
                                        DEBUG_PRINTLN(" *C");
                                      */  
                                        delay(500); // Allow time for radio if power useed as reset
                                        //gw.begin(NULL,NODE_ID);
                                        //sendSketchInfo("HTU21D", "1.0"); 
                                        //present(CHILD_ID_TEMP, S_TEMP);   // Present sensor to controller
                                        //present(CHILD_ID_HUM, S_HUM);
                                        DEBUG_PRINT("Node and "); DEBUG_PRINTLN("2 children presented.");
                                        
                                        raHum.clear();
                                        
                                      }
                                      
                                      void loop() { 
                                      
                                        measureCount ++;
                                        batteryReportCounter ++;
                                        bool forceTransmit = false;
                                        
                                        if (measureCount > FORCE_TRANSMIT_CYCLE) {
                                          forceTransmit = true; 
                                        }
                                        sendTempHumidityMeasurements(forceTransmit);
                                      /*
                                        // Read and print internal temp
                                        float temperature0 = static_cast<float>(static_cast<int>((GetInternalTemp()+0.5) * 10.)) / 10.;
                                        DEBUG_PRINT("Internal Temp: "); DEBUG_PRINT(temperature0); DEBUG_PRINTLN(" *C");        
                                      */
                                        // Check battery
                                        if (batteryReportCounter >= BATTERY_REPORT_CYCLE) {
                                          long batteryVolt = readVcc();
                                          DEBUG_PRINT("Battery voltage: "); DEBUG_PRINT(batteryVolt); DEBUG_PRINTLN(" mV");
                                          uint8_t batteryPcnt = constrain(map(batteryVolt,VMIN,VMAX,0,100),0,255);   
                                          DEBUG_PRINT("Battery percent: "); DEBUG_PRINT(batteryPcnt); DEBUG_PRINTLN(" %");
                                          sendBatteryLevel(batteryPcnt);
                                          batteryReportCounter = 0;
                                        }
                                        
                                        sleep(SLEEP_TIME);
                                      }
                                      
                                      // function for reading Vcc by reading 1.1V reference against AVcc. Based from http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
                                      // To calibrate reading replace 1125300L with scale_constant = internal1.1Ref * 1023 * 1000, where internal1.1Ref = 1.1 * Vcc1 (per voltmeter) / Vcc2 (per readVcc() function) 
                                      long readVcc() {
                                        // set the reference to Vcc and the measurement to the internal 1.1V reference
                                        ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
                                        delay(2); // Wait for Vref to settle
                                        ADCSRA |= _BV(ADSC); // Start conversion
                                        while (bit_is_set(ADCSRA,ADSC)); // measuring
                                        uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH  
                                        uint8_t high = ADCH; // unlocks both
                                        long result = (high<<8) | low;
                                        result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
                                        return result; // Vcc in millivolts
                                      }
                                      // function for reading internal temp. From http://playground.arduino.cc/Main/InternalTemperatureSensor 
                                      double GetInternalTemp(void) {  // (Both double and float are 4 byte in most arduino implementation)
                                        unsigned int wADC;
                                        double t;
                                        // The internal temperature has to be used with the internal reference of 1.1V. Channel 8 can not be selected with the analogRead function yet.
                                        ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));   // Set the internal reference and mux.
                                        ADCSRA |= _BV(ADEN);  // enable the ADC
                                        delay(20);            // wait for voltages to become stable.
                                        ADCSRA |= _BV(ADSC);  // Start the ADC
                                        while (bit_is_set(ADCSRA,ADSC));   // Detect end-of-conversion
                                        wADC = ADCW;   // Reading register "ADCW" takes care of how to read ADCL and ADCH.
                                        t = (wADC - 88.0 ) / 1.0;   // The default offset is 324.31.
                                        return (t);   // The returned temperature in degrees Celcius.
                                      }
                                      
                                      /*********************************************
                                       * * Sends temperature and humidity from Si7021 sensor
                                       * Parameters
                                       * - force : Forces transmission of a value (even if it's the same as previous measurement)
                                       *********************************************/
                                      void sendTempHumidityMeasurements(bool force) {
                                        bool tx = force;
                                        
                                        float temperature = humiditySensor.readTemperature();
                                        DEBUG_PRINT("T: ");DEBUG_PRINTLN(temperature);
                                      
                                        float diffTemp = abs(lastTemperature - temperature);
                                        DEBUG_PRINT(F("TempDiff :"));DEBUG_PRINTLN(diffTemp);
                                      
                                        if (diffTemp > TEMP_TRANSMIT_THRESHOLD || tx) {
                                          send(msgTemp.set(temperature,1));
                                          lastTemperature = temperature;
                                          measureCount = 0;
                                          DEBUG_PRINTLN("T sent!");
                                        }
                                        
                                        int humidity = humiditySensor.readHumidity();
                                        DEBUG_PRINT("H: ");DEBUG_PRINTLN(humidity);
                                      
                                        raHum.addValue(humidity);
                                        humidity = raHum.getAverage();  // MA sample imply reasonable fast sample frequency
                                        float diffHum = abs(lastHumidity - humidity);
                                        DEBUG_PRINT(F("HumDiff  :"));DEBUG_PRINTLN(diffHum); 
                                      
                                        if (diffHum > HUMI_TRANSMIT_THRESHOLD || tx) {
                                          send(msgHum.set(humidity));
                                          lastHumidity = humidity;
                                          measureCount = 0;
                                          DEBUG_PRINTLN("H sent!");
                                        }
                                      }
                                      

                                      I am at 1+ hours since last transmission.

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

                                      @wergeld It looks like it's set to 3 hrs.

                                      W 1 Reply Last reply
                                      0
                                      • m26872M m26872

                                        @wergeld It looks like it's set to 3 hrs.

                                        W Offline
                                        W Offline
                                        wergeld
                                        wrote on last edited by
                                        #109

                                        @m26872
                                        Indeed it does. Just was throwing me off with the wording. I read it as "1/2 hour" or "1/3 hour". So far I am prepping up more nodes. Have 4 more 328s to programs and build out. I am thinking at the end I will have:

                                        1. Gateway (serial or the direct connect nrf+ once it supports v2)
                                        2. Temp/Hum node using HTU21D for back porch sending every 5 minutes and force send every 30 minutes.
                                        3. Temp/Hum node using HTU21D inside using same send times.
                                        4. Parking sensor in garage hardwired to mains.
                                          5 & 6 I am not sure yet.

                                        Lots of fun so far.

                                        1 Reply Last reply
                                        0
                                        • W Offline
                                          W Offline
                                          wergeld
                                          wrote on last edited by
                                          #110

                                          I am seeing 5.0 μA at sleep and 12.4 mA when transmitting. Seems kind of high. I did the poor man's test as well - put an LED across the HTU21D power pins and it lit up. Same when I did it across the nrf's power pins. Shouldn't the MySensors code turn off the power rails during sleep?

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


                                          21

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 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