Navigation

    • Register
    • Login
    • OpenHardware.io
    • Categories
    • Recent
    • Tags
    • Popular
    1. Home
    2. ksga
    3. Posts
    • Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups

    Posts made by ksga

    • RE: 💬 NodeManager

      @user2684
      Tried again with adafruits library and got it working changing the address.
      But still can't get any useful data from it using NodeManager.

      Did a bit of copy/paste and got this code working (with adafruit and the alternative library):

      #define MY_RADIO_NRF24
      
      #include <MySensors.h>
      #include <SPI.h>
      #include <BH1750.h>
      #include <Wire.h>
      #include <BME280I2C.h>
      
      #define BARO_CHILD 0
      #define TEMP_CHILD 1
      #define HUM_CHILD 2
      #define CHILD_ID_LIGHT 3
      
      #define R1 687000
      #define R2 222000
      #define VMIN 3.30
      #define VMAX 4.18
      #define ADC_PRECISION 1023
      #define VREF 1.107
      
      unsigned long SLEEP_TIME = 300000;
      const float ALTITUDE = 10;
      
      BME280I2C bme;
      BH1750 lightSensor;
      
      MyMessage pressureMsg(BARO_CHILD, V_PRESSURE);
      MyMessage tempMsg(TEMP_CHILD, V_TEMP);
      MyMessage humMsg(HUM_CHILD, V_HUM);
      MyMessage lightMsg(CHILD_ID_LIGHT, V_LEVEL);
      
      float lastbaro = -1;
      float lasttemp = -1;
      float lasthum = -1;
      float lastlux = -1;
      float oldBatteryPcnt = -1;
      int BATTERY_SENSE_PIN = A0;
      
      void setup()
      {
        lightSensor.begin();
        bme.begin();
        analogReference(INTERNAL);
        pinMode(BATTERY_SENSE_PIN, INPUT);
        delay(100);
      }
      
      void presentation()
      {
        sendSketchInfo("BME280_BH1750", "1.1");
        present(BARO_CHILD, S_BARO);
        present(TEMP_CHILD, S_TEMP);
        present(HUM_CHILD, S_HUM);
        present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
      }
      
      void loop()
      {
        float baro_local = bme.pres() / 100.0;
        float baro = ( baro_local / pow((1.0 - ( ALTITUDE / 44330.0 )), 5.255));
        if (baro != lastbaro) {
            send(pressureMsg.set(baro,2));
            lastbaro = baro;
        }
        
        float temp = bme.temp();
        if (temp != lasttemp) {
            send(tempMsg.set(temp,1));
            lasttemp = temp;
        }
        
        float hum = bme.hum();
        if (hum != lasthum) {
            send(humMsg.set(hum,1));
            lasthum = hum;
        }
        
        float lux = lightSensor.readLightLevel();
        if (lux != lastlux) {
            send(lightMsg.set(lux,0));
            lastlux = lux;
        }
        float batteryPcnt = getBatteryPercentage();
        if (oldBatteryPcnt != batteryPcnt) {
        // Power up radio after sleep
        sendBatteryLevel(batteryPcnt);
        oldBatteryPcnt = batteryPcnt;
        }
        
        sleep(SLEEP_TIME);
        
      }
      
      float getBatteryPercentage()
      {
      delay(500);
      
      int inputValue = analogRead(BATTERY_SENSE_PIN);
      float voltageDividerFactor = (R1 + R2) / R2;
      float maxValue = voltageDividerFactor * VREF;
      float voltsPerBit = maxValue / ADC_PRECISION;
      float batteryVoltage = voltsPerBit * inputValue;
      float batteryPercentage = ((batteryVoltage-VMIN)/(VMAX-VMIN))*100;
      
      return batteryPercentage;
      }
      
      posted in OpenHardware.io
      ksga
      ksga
    • RE: 💬 NodeManager

      @user2684 Sorry about the delay - but life claimed some time (stupid life claiming time I want to use for stuff 😐 )

      Disabled MODULE_ANALOG_INPUT, MODULE_DIGITAL_INPUT and MODULE_DIGITAL_OUTPUT, and even tried without BH1750 - but still no luck.
      Node registers correctly and fires bogus data on one or two sensors...

      Pretty sure I have to use the other library - tried using Adafruit's examples, and it claims no BME is attached...

      I can make all the logic changes to NodeManager, but have not been able to initialize the sensor - maybe I could upload my edits to Git and someone could help me get the last bits together ???

      Made the changes here.

      posted in OpenHardware.io
      ksga
      ksga
    • RE: 💬 NodeManager

      @user2684 Yeah.. missed that it rebooted... Thanks.

      I cleared EEPROM again, recompiled with and without debug (no usable result without, so decided to add eyes again 🙂 )

      void before() part of the sketch looks like this:

      // before
      void before() {
        // setup the serial port baud rate
        Serial.begin(MY_BAUD_RATE);  
        /*
         * Register below your sensors
        */
        nodeManager.setSleep(SLEEP,1,MINUTES);
        nodeManager.registerSensor(SENSOR_BH1750);
        nodeManager.registerSensor(SENSOR_BME280);
        /*
         * Register above your sensors
        */
        nodeManager.before();
      }
      

      and config.h:

      /***********************************
       * NodeManager configuration
       */
      
      // if enabled, enable debug messages on serial port
      #define DEBUG 1
      //#define DEBUG 0
      
      // if enabled, will load the sleep manager library. Sleep mode and sleep interval have to be configured to make the board sleeping/waiting
      #define SLEEP_MANAGER 1
      // if enabled, enable the capability to power on sensors with the arduino's pins to save battery while sleeping
      #define POWER_MANAGER 0
      // if enabled, will load the battery manager library to allow the battery level to be reported automatically or on demand
      #define BATTERY_MANAGER 0
      // if enabled, allow modifying the configuration remotely by interacting with the configuration child id
      #define REMOTE_CONFIGURATION 1
      // if enabled, persist the remote configuration settings on EEPROM
      #define PERSIST 0
      
      // if enabled, send a SLEEPING and AWAKE service messages just before entering and just after leaving a sleep cycle and STARTED when starting/rebooting
      #define SERVICE_MESSAGES 0
      // if enabled, a battery sensor will be created at BATTERY_CHILD_ID and will report vcc voltage together with the battery level percentage
      #define BATTERY_SENSOR 0
      
      // Enable this module to use one of the following sensors: SENSOR_ANALOG_INPUT, SENSOR_LDR, SENSOR_THERMISTOR, SENSOR_MQ, SENSOR_ML8511
      #define MODULE_ANALOG_INPUT 1
      // Enable this module to use one of the following sensors: SENSOR_DIGITAL_INPUT
      #define MODULE_DIGITAL_INPUT 1
      // Enable this module to use one of the following sensors: SENSOR_DIGITAL_OUTPUT, SENSOR_RELAY, SENSOR_LATCHING_RELAY
      #define MODULE_DIGITAL_OUTPUT 1
      // Enable this module to use one of the following sensors: SENSOR_DHT11, SENSOR_DHT22
      #define MODULE_DHT 0
      // Enable this module to use one of the following sensors: SENSOR_SHT21
      #define MODULE_SHT21 0
      // Enable this module to use one of the following sensors: SENSOR_SWITCH, SENSOR_DOOR, SENSOR_MOTION
      #define MODULE_SWITCH 0
      // Enable this module to use one of the following sensors: SENSOR_DS18B20
      #define MODULE_DS18B20 0
      // Enable this module to use one of the following sensors: SENSOR_BH1750
      #define MODULE_BH1750 1
      // Enable this module to use one of the following sensors: SENSOR_MLX90614
      #define MODULE_MLX90614 0
      // Enable this module to use one of the following sensors: SENSOR_BME280
      #define MODULE_BME280 1
      
      #endif
      

      The serial output seems to show no reboot this time ;):

      REG I=1 P=18 P=16 T=37
      REG I=2 P=18 P=6 T=0
      REG I=3 P=18 P=7 T=1
      REG I=4 P=18 P=8 T=4
      NodeManager v1.4
      INT1 M=255
      INT2 M=255
      RADIO OK
      PRES I=200, T=23
      PRES I=1 T=16
      PRES I=2 T=6
      PRES I=3 T=7
      PRES I=4 T=8
      READY
      
      MY I=3 M=1
      BH1 I=1 L=-10924
      BME I=2 T=0.00
      SEND D=0 I=2 C=0 T=0 S= N=0 F=0.00
      BME I=3 H=0.00
      SEND D=0 I=3 C=0 T=1 S= N=0 F=0.00
      BME I=4 P=-793.67
      SLEEP 60s
      
      AWAKE
      BH1 I=1 L=-10924
      BME I=2 T=0.00
      SEND D=0 I=2 C=1 T=0 S= N=0 F=0.00
      BME I=3 H=0.00
      SEND D=0 I=3 C=1 T=1 S= N=0 F=0.00
      BME I=4 P=-793.67
      SLEEP 60s
      

      I would guess there should be enough memory on the arduino:

      Sketch uses 26.024 bytes (80%) of program storage space. Maximum is 32.256 bytes.
      Global variables use 1.262 bytes (61%) of dynamic memory, leaving 786 bytes for local variables. Maximum is 2.048 bytes.
      

      Definately not fixed 😞
      Maybe I could try adding support for the alternative BME library to NodeManager...

      posted in OpenHardware.io
      ksga
      ksga
    • RE: 💬 NodeManager

      Way too cool solution 👯

      I decided to give it a go with my Light/Baro/Hum/Temp node.
      Installed the dependencies from Adafruit, cleared EEPROM, compiled and flashed - no issues 👍

      The node reports the expected sensors to MyController.
      The BH1750 light sensor works like a charm, but I'm not getting anything from the BME280 except from a 0.0°C reading on temperature (which, even though spring is pretty lousy so far is a bit on the low side for my office).

      Output on serial looks like this:

      PWR G=6 V=7
      REG I=1 P=18 P=16 T=37
      REG I=2 P=18 P=6 T=0
      REG I=3 P=18 P=7 T=1
      REG I=4 P=18 P=8 T=4
      NodeManager v1.4
      INT1 M=255
      INT2 M=255
      RADIO OK
      PRES I=200, T=23
      PRES I=201, T=30
      BATT V=3.35 P=94
      SEND D=0 I=201 C=0 T=38 S= I=0 F=3.35
      PRES I=1 T=16
      PRES I=2 T=6
      PRES I=3 T=7
      PRES I=4 T=8
      READY
      
      MY I=1 M=1
      ON P=7
      BH1 I=1 L=2500
      SEND D=0 I=1 C=0 T=37 S= N=2500 F=0.00
      BME I=2 T=0.00
      SEND D=0 I=2 C=0 T=0 S= N=0 F=0.00
      BME I=3 H=69.25
      SEND D=0 I=3 C=0 T=1 S= N=0 F=69.25
      BME I=4 P=0.00
      SEND D=0 I=4 C=0 T=4 S= N=0 F=0.00
      OFF P=7
      SLEEP 300s
      
      AWAKE
      ON P=7
      BH@PWR G=6 V=7
      REG I=1 P=18 P=16 T=37
      REG I=2 P=18 P=6 T=0
      REG I=3 P=18 P=7 T=1
      REG I=4 P=18 P=8 T=4
      NodeManager v1.4
      INT1 M=255
      INT2 M=255
      RADIO OK
      PRES I=200, T=23
      PRES I=201, T=30
      BATT V=3.35 P=94
      SEND D=0 I=201 C=0 T=38 S= I=0 F=3.35
      PRES I=1 T=16
      PRES I=2 T=6
      PRES I=3 T=7
      PRES I=4 T=8
      READY
      
      MY I=1 M=1
      ON P=7
      BH1 I=1 L=9951
      SEND D=0 I=1 C=0 T=37 S= N=9951 F=0.00
      BME I=2 T=0.00
      SEND D=0 I=2 C=0 T=0 S= N=0 F=0.00
      BME I=3 H=69.25
      SEND D=0 I=3 C=0 T=1 S= N=0 F=69.25
      BME I=4 P=0.00
      SEND D=0 I=4 C=0 T=4 S= N=0 F=0.00
      OFF P=7
      SLEEP 300s
      

      The node was running fine for months using the same BH1750 library, but the BME280 library by Tyler Glenn.

      Any ideas?

      Should notice that I'm using this kind of BME280 from Ali

      posted in OpenHardware.io
      ksga
      ksga
    • RE: Help needed - SSR switch / SCR dimmer

      @Koresh
      No worries - all help is appreciated - fast or slow 🙂

      posted in General Discussion
      ksga
      ksga
    • RE: Help needed - SSR switch / SCR dimmer

      @Koresh
      Cool.. could you share the code?

      posted in General Discussion
      ksga
      ksga
    • RE: Help needed - SSR switch / SCR dimmer

      @FutureCow
      Looks like it. But no code published...
      Also - is the ESP8266 capable of handling the interrupts without messing up the timing when it is also responsible for communicating with the controller and the rest of the stuff??

      posted in General Discussion
      ksga
      ksga
    • RE: !TSM:FPAR: FAIL !TSM: FAILURE TSM:PDT

      I'm getting this error quite frequently when flashing new sketches to my nodes.
      Flash the EPROM clear sketch and reflash the sketch you want on the node should fix it.

      posted in Troubleshooting
      ksga
      ksga
    • RE: Help needed - SSR switch / SCR dimmer

      @Koresh
      Does your solution looks anything like this?

      posted in General Discussion
      ksga
      ksga
    • RE: Help needed - SSR switch / SCR dimmer

      @Koresh
      Sounds like a good plan. Have you shared the code/design anywhere?

      @stedew
      Thanks fo the link 🙂
      Browsed Ali and ebay, but shipping is killing me on these trafos.
      Got me wondering... a lot of cheap AC-DC transformers are flooding these sites in 5-12V variants.
      Why not just take one of those apart - salvage the trafo and scrap the rest?

      I only need a few hundred mA to run arduino, radio and relay...

      This is the kind of stuff I'm talking about (claims to be able to deliver 2A 5VDC - but that would only be if you had to start a fire...):
      0_1481269256312_IMG_20161209_083757.jpg
      0_1481269266253_IMG_20161209_083806.jpg

      posted in General Discussion
      ksga
      ksga
    • RE: Help needed - SSR switch / SCR dimmer

      @stedew
      But only if I have them before the ON/OFF relay 😉

      Okay... so when looking at the specs for the H11AA1 I can see that the maximum current on the input is 60mA.
      Looking at lthis I've reached the conclusion - that I believe you wanted me to - that I have to lower the ac current before the zero-cross detector using an AC/AC transformer.
      I'm having a bit of trouble finding a suitable (read - cheap) 240 / 6-0-6 AC/AC transformer. Any suggestions (prefer ebay or ali)??

      posted in General Discussion
      ksga
      ksga
    • RE: Help needed - SSR switch / SCR dimmer

      @stedew
      Hi Stefan, and thanks.

      I agree that I might have to crawl before I run 😉
      In the link you provided there are two 30k ½W resistors in front of the bridge rectifier. Would they not produce a lot of heat as well?
      He uses a bridge rectifier. As far as I know the only function of that is to convert AC to DC, but it would still be 240V and be in a waveform.
      The zero-point detector I ordered (based on the Arduino playground schematic) can handle a bi-directional input, as opposed to the 4n25 that is used on Instructables, so couldn't I just leave the rectifier out?

      posted in General Discussion
      ksga
      ksga
    • RE: 💬 Atmospheric Pressure Sensor

      @flopp
      That would be awesome. I have one in the mail and are trying to get things ready 😀

      It will replace my SI7021 on my solar powered node...

      posted in Announcements
      ksga
      ksga
    • RE: 💬 Atmospheric Pressure Sensor

      Anybody feeling like adopting this to the BME280 and including temperature and humidity readings?? Low cost single chip and power usage looks fairly low...

      posted in Announcements
      ksga
      ksga
    • Help needed - SSR switch / SCR dimmer

      So - I decided to enter the 21st century and get me some of those LED bulbs for an old chandelier (5 bulbs). Ordered these, dimmable 4W filament bulbs.
      This however means that I will have to replace my old iron core dimmer switch with a SCR-type dimmer (according to the specs on the LED bulbs I ordered from our old chap Ali).

      I've been reading a bit about the topic, and I have come to the conclusion that the solution could be something based of this.
      It would be pretty awesome if it could be combined with this, but for the moment it might be better just to do a perfboard version to prove the concept.

      I need a bit of help on the phase control circuit from Arduino playground:
      R1, R2 and R3: What dissipation is required 1/4, 1/2 or 1 W ? (I assume R4 on the DC side can be as small as I like it to be).
      Where to put the SSR-relay? Position 1. or 2.?
      0_1480928436712_AC_PhaseControl_SSR.PNG

      And then of course the fun part. How to implement the code from Arduino playground in this code?

      posted in General Discussion
      ksga
      ksga
    • RE: 💬 Heatpump / airconditioner controller

      Too cool - I can make my heatpump beep and stuff from my smartphone - live will surely never be the same again 🙂

      There is one more trick I would love to teach it though - it seems that the code is prepared for "fan swing" commands, but they don't seem to be implemented in either the Arduino code, or in the LUA script for Domoticz.

      Does anyone have some pointers on how to activate these bits?

      posted in OpenHardware.io
      ksga
      ksga
    • RE: Battery percentage - Help needed

      UPDATE:
      So I decided to go ahead and connect the ProMini and nrf24l01+

      Now the output from the terminal fluctuated between 80% and 105% with no real pattern.
      So I added a 0.22uf across R2

      Now the output is stable:

      Battery percent: 104 %
      

      I still only measure 0.63V across R2.
      I guess the remaining error could be written down to the 1.1V reference being off. I think I read somewhere that it can be 10% off!

      posted in Hardware
      ksga
      ksga
    • RE: Battery percentage - Help needed

      @m26872 Thank you for sticking with me.. I'm quite puzzled by this.
      Removed all caps. Added a 1k resistor in front of the LED.
      Checked resistance of wires - individually and mounted (all ~0.5 Ohm)

      Readings remain the same:
      0_1453755173541_IMG_20160125_214414.jpg

      Where does the last "0,63V" go - and should the fact that it is the same as the current over R2 teach me something?

      posted in Hardware
      ksga
      ksga
    • RE: Battery percentage - Help needed

      @m26872 I decided to trip everything from my breadboard and start over.
      First thing - the voltage divider:
      Simple Bat+ > pos.rail > R1 (1M, new specimen, verified resistance with multimeter) > R2(220k)/0.22uf capacitor > neg.rail > Bat-
      In paralel I added an LED to put a bit of load on the battery for more realistic current.

      The results are:
      Battery current: 4.03V
      Neg.rail to coupling point R1-R2: 0.61V
      Coupling point R1-R2 to Pos.rail: 2.78V

      Does that make any sense?
      I would expect 0.61V+2.78V=4.03V (or maybe I still haven't grasped the concept.)

      Hopefully this photo will make someone wiser than me point out my many mistakes 😉
      0_1453742475955_IMG_20160125_181636.jpg

      posted in Hardware
      ksga
      ksga
    • RE: Battery percentage - Help needed

      @m26872

      #include <SPI.h>
      #include <MySensor.h>  
      #include <DHT.h>  
      
      #define CHILD_ID_HUM 0
      #define CHILD_ID_TEMP 1
      #define HUMIDITY_SENSOR_DIGITAL_PIN 3
      
      // define values for the battery measurement
      #define R1 1012e3
      #define R2 220e3
      #define VMIN 3.2
      #define VMAX 4.2
      #define ADC_PRECISION 1023
      #define VREF 1.1
      
      MySensor gw;
      DHT dht;
      unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)
      float lastTemp;
      float lastHum;
      boolean metric = true; 
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      int oldBatteryPcnt = 0;
      int BATTERY_SENSE_PIN = A0;
      
      
      void setup()  
      { 
           // use the 1.1 V internal reference
       
           analogReference(INTERNAL);
       
      
        gw.begin();
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      
        // Send the Sketch Version Information to the Gateway
        gw.sendSketchInfo("Humidity", "1.0");
      
        // 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);
        
        metric = gw.getConfig().isMetric;
      }
      
      void loop()      
      {  
         int batteryPcnt = getBatteryPercentage();
      
         Serial.print("Battery percent: ");
         Serial.print(batteryPcnt);
         Serial.println(" %");
      
         if (oldBatteryPcnt != batteryPcnt) {
           // Power up radio after sleep
           gw.sendBatteryLevel(batteryPcnt);
           oldBatteryPcnt = batteryPcnt;
         }
        
        // totally random test values
        gw.send(msgTemp.set(20, 1));
        gw.send(msgHum.set(42, 1));
      
        gw.sleep(SLEEP_TIME); //sleep a bit
      }
      
      int getBatteryPercentage() {
      
        // read analog pin value
        int inputValue = analogRead(BATTERY_SENSE_PIN);
        
        // calculate the max possible value and therefore the range and steps
        float voltageDividerFactor = (R1 + R2) / R2;
        float maxValue = voltageDividerFactor * VREF;
        float voltsPerBit = maxValue / ADC_PRECISION;
      
        float batteryVoltage = voltsPerBit * inputValue;
        float batteryPercentage = ((batteryVoltage-VMIN)/(VMAX-VMIN))*100;
        //int batteryPercentage = map(batteryVoltage, 0, maxValue, 0, 100);
      
        return batteryPercentage;
      }
      
      posted in Hardware
      ksga
      ksga
    • RE: Battery percentage - Help needed

      @m26872 Thank you for the pointer.
      I used the sketch from the link, but still the readings are quite far off.

      I have tried checking the actual voltage at the division point. It reads 0.58V with a battery charge of 3.81V.
      I my math doesn't fail me, it should be 0.69V...

      I then looked at the actual resistance of R1 and R2.
      R1=1012e3
      R2=220e3

      Of course this changes things, but not too drastically - now it should be 0.68V.
      So I'm 15% wrong at my division point!

      Any suggestions?

      I should add that the serial output vary a bit at first, but then settles in after 30 seconds or so. I guess that could be because the capacitor is a bit on the large size...

      posted in Hardware
      ksga
      ksga
    • RE: Battery percentage - Help needed

      Update..
      Tried it like this:

        // 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)*10;   
          DEBUG_PRINT("Battery percent: "); DEBUG_PRINT(batteryPcnt); DEBUG_PRINTLN(" %");
          gw.sendBatteryLevel(batteryPcnt);
          batteryReportCounter = 0;
        }
      

      And got this result:

      Battery voltage: 3300 mV
      Battery percent: 100 %
      

      So at least it seems to work 💃
      Hoping voltage will drop like crazy through the night 😉

      posted in Hardware
      ksga
      ksga
    • RE: Battery percentage - Help needed

      @boozz
      Sounds like a plan 👯

      But where do I do it?
      Here, somewhere?

        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;
        }
      
      posted in Hardware
      ksga
      ksga
    • RE: Battery percentage - Help needed

      @ericvdb I replaced the resistors as suggested and added a capacitor (the smallest one I have a hand i 2.2uf - hope that's okay).
      Now it shows 10% at full charge:

      Serial started
      Voltage: 3309 mV
      //bla bla bla
      T: 24.41
      //more bla.
      H: 44
      //transmit bla bla bla
      Battery voltage: 3300 mV
      Battery percent: 10 %
      

      Actual current on battery is 4.19V

      posted in Hardware
      ksga
      ksga
    • RE: Battery percentage - Help needed

      Update:
      So I've digged a bit more into voltage division and off course it's pretty simple (ohms law and everything).
      I've added a voltage divider, using a 470k and 100k resistor, and connected it to A0.

      The controller now shows the battery percentage - but quite obviously the sketch needs altering since the battery is almost fully charged, but but the controller only shows it at 12%.

      The setup looks like this:
      0_1453368531505_solar_temp_bb_v2.png

      And the sketch like this:

      /* Sketch with Si7021 and battery monitoring.
      by m26872, 20151109 
      */
      #include <MySensor.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 NODE_ID 1             // <<<<<<<<<<<<<<<<<<<<<<<<<<<   Enter Node_ID
      #define CHILD_ID_TEMP 0
      #define CHILD_ID_HUM 1
      // #define SLEEP_TIME 15000 // 15s for DEBUG
      #define SLEEP_TIME 180000   // 3 min
      #define FORCE_TRANSMIT_CYCLE 10  // 3min*20=1/hour, 3min*10=1/2/hour 
      #define BATTERY_REPORT_CYCLE 20   // Once per 3min   =>   20*3 = 60 (one report/hour)
      #define VMIN 3200
      #define VMAX 4200
      #define HUMI_TRANSMIT_THRESHOLD 0.5  // 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;
      
      MySensor gw;
      MyMessage msgTemp(CHILD_ID_TEMP,V_TEMP); // Initialize temperature message
      MyMessage msgHum(CHILD_ID_HUM,V_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
        gw.begin(NULL,NODE_ID);
        gw.sendSketchInfo("EgTmpHumBat5min", "1.0 151106"); 
        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();
        
      }
      
      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;
      
        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) {
          gw.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) {
          gw.send(msgHum.set(humidity));
          lastHumidity = humidity;
          measureCount = 0;
          DEBUG_PRINTLN("H sent!");
        }
      
      }
      

      Any hints would be welcome.

      posted in Hardware
      ksga
      ksga
    • Battery percentage - Help needed

      Hi guys.

      So, I have to admit that I'm a total noob. But I hope some of you guys can spare me a minute.
      I'm working on a small battery powered temp+hum sensor based on a Pro Mini, NRF24L01+ and a Si7021 sensor.
      I would like to run it on a solar panel, and use a Li-Ion battery as storage for those rainy days (and nights off course).

      I have used m26872's the sketch from here, which seems to work fine:
      http://forum.mysensors.org/topic/2067/my-slim-2aa-battery-node/115

      I am, however, at a loss when it comes to monitoring the battery charge (I realize that voltage/charge is not linear, but at least it gives a bit of an idea where things are going).

      Can someone point me in the direction I need to go - both with hardware and sketch to monitor the Li-Ion charge and report it to the controller (I'm thinking about using Domoticz)?

      At the moment the setup looks like this:
      0_1453281555032_solar_temp_bb.png

      The current from the solar panel is regulated to 5V for the charging board.
      The battery will operate between 4.2V and 3.2V, and the current on the battery side is regulated to 3.3V for the Arduino, radio and sensor.

      I guess I will have to add a couple of resistors and modify the sketch, but I don't know were to start!

      posted in Hardware
      ksga
      ksga