Solar Powered Soil Moisture Sensor



  • I have build an Solar panel-powered Soil moisture sensor.

    Photos

    I bought the lamps on Jula.
    Lamp1 Battery 2/3 AAA 100 mAh, 10 SEK, 1 euro
    Lamp2 Battery 2/3 AA 200 mAh, 5 SEK, 0,5 euro
    Lamp3 Battery LR44 40 mAh, 10 SEK, 1 euro, NOT TESTED YET

    I removed all the electronic on the PCB and used the PCB as a connection board.
    The solar panel gives around 1,4 V during a very sunny day.
    I had to add a step-up(0,8->3,3V) to be able to run a ATMEGA. My first idea was to connect 2 batteries in series, but that was too much work. Now everything fits in the parts that is included.

    Lamp1 is using a Pro Mini(fake), glued soil sensor on "arrow"
    Lamp2 is using my designed PCB with ATMEGA328p, soil sensor just put in soil, not glued at all.
    Both MCU are using Optiboot, https://forum.mysensors.org/topic/3018/tutorial-how-to-burn-1mhz-8mhz-bootloader-using-arduino-ide-1-6-5-r5.
    Pro Mini:Power-on LED removed and also step-down. Activity-LED is still in use.
    My designed PCB:https://oshpark.com/shared_projects/F7esJEMY, also with LED for startup and send OK

    I am using @mfalkvidd sketch, thank you. I have only add a few rows(LED and resend function)
    It sends data(Humidity/Volt/VoltPercent every 10 second, just for testing the hardware later on I will send maybe twice an hour or once an hour.
    I am measuring the battery voltage on a analog input.

    #include <SPI.h>
    #include <MySensor.h>
    
    #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
    #define N_ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
    
    #define CHILD_ID_MOISTURE 0
    #define CHILD_ID_BATTERY 1
    #define SLEEP_TIME 10000 // Sleep time between reads (in milliseconds)
    #define THRESHOLD 1.1 // Only make a new reading with reverse polarity if the change is larger than 10%.
    #define STABILIZATION_TIME 1000 // Let the sensor stabilize before reading
    default BOD settings.
    const int SENSOR_ANALOG_PINS[] = {A4, A5}; // Sensor is connected to these two pins. Avoid A3 if using ATSHA204. A6 and A7 cannot be used because they don't have pullups.
    
    MySensor gw;
    MyMessage msg(CHILD_ID_MOISTURE, V_HUM);
    MyMessage voltage_msg(CHILD_ID_BATTERY, V_VOLTAGE);
    long oldvoltage = 0;
    byte direction = 0;
    int oldMoistureLevel = -1;
    float batteryPcnt;
    float batteryVolt;
    int LED = 5;
    
    void setup()
    {
      pinMode(LED, OUTPUT);
      digitalWrite(LED, HIGH);
      delay(200);
      digitalWrite(LED, LOW);
      delay(200);
      digitalWrite(LED, HIGH);
      delay(200);
      digitalWrite(LED, LOW);
      
      gw.begin();
    
      gw.sendSketchInfo("Plant moisture w solar", "1.0");
    
      gw.present(CHILD_ID_MOISTURE, S_HUM);
      delay(250);
      gw.present(CHILD_ID_BATTERY, S_MULTIMETER);
      for (int i = 0; i < N_ELEMENTS(SENSOR_ANALOG_PINS); i++) {
        pinMode(SENSOR_ANALOG_PINS[i], OUTPUT);
        digitalWrite(SENSOR_ANALOG_PINS[i], LOW);
      }
    }
    
    void loop()
    {
      int moistureLevel = readMoisture();
    
      // Send rolling average of 2 samples to get rid of the "ripple" produced by different resistance in the internal pull-up resistors
      // See http://forum.mysensors.org/topic/2147/office-plant-monitoring/55 for more information
      if (oldMoistureLevel == -1) { // First reading, save current value as old
        oldMoistureLevel = moistureLevel;
      }
      if (moistureLevel > (oldMoistureLevel * THRESHOLD) || moistureLevel < (oldMoistureLevel / THRESHOLD)) {
        // The change was large, so it was probably not caused by the difference in internal pull-ups.
        // Measure again, this time with reversed polarity.
        moistureLevel = readMoisture();
      }
      gw.send(msg.set((moistureLevel + oldMoistureLevel) / 2.0 / 10.23, 1));
      oldMoistureLevel = moistureLevel;
      
      int sensorValue = analogRead(A0);
      Serial.println(sensorValue);
      float voltage=sensorValue*(3.3/1023);
      Serial.println(voltage);
      batteryPcnt = (sensorValue - 248) * 0.72;
      batteryVolt = voltage;
      gw.sendBatteryLevel(batteryPcnt);
      resend((voltage_msg.set(batteryVolt, 3)), 10);
    
      digitalWrite(LED, HIGH);
      delay(200);
      digitalWrite(LED, LOW);
      
      gw.sleep(SLEEP_TIME);
    }
    
    void resend(MyMessage &msg, int repeats)
    {
      int repeat = 1;
      int repeatdelay = 0;
      boolean sendOK = false;
    
      while ((sendOK == false) and (repeat < repeats)) {
        if (gw.send(msg)) {
          sendOK = true;
        } else {
          sendOK = false;
          Serial.print("Error ");
          Serial.println(repeat);
          repeatdelay += 500;
        } repeat++; delay(repeatdelay);
      }
    }
    
    
    int readMoisture() {
      pinMode(SENSOR_ANALOG_PINS[direction], INPUT_PULLUP); // Power on the sensor
      analogRead(SENSOR_ANALOG_PINS[direction]);// Read once to let the ADC capacitor start charging
      gw.sleep(STABILIZATION_TIME);
      int moistureLevel = (1023 - analogRead(SENSOR_ANALOG_PINS[direction]));
    
      // Turn off the sensor to conserve battery and minimize corrosion
      pinMode(SENSOR_ANALOG_PINS[direction], OUTPUT);
      digitalWrite(SENSOR_ANALOG_PINS[direction], LOW);
    
      direction = (direction + 1) % 2; // Make direction alternate between 0 and 1 to reverse polarity which reduces corrosion
      return moistureLevel;
    }
    
    

    0_1465299264398_IMG_1337.JPG
    0_1465928822164_SolarPanel (2).png


  • Mod

    Very nice @flopp! Have a few lamps from Jula myself but haven't gotten round to use them yet.



  • New schematic. I forgot the step-up



  • @mfalkvidd
    I hope this can help you to use them.
    It takes maybe 1 hour to one lamp.



  • update
    I've let it be outdoor in the sun during 2-3 days now and it works well.
    yesterday I took one of them and placed it in the garage, totally black whole day not even a lamp.
    red line is when I moved it to the garage.
    it is sending every 10 second. After ~20 hours the battery was to low to be able to run Pro Mini
    0_1465505284492_volt.png



  • this is genious



  • Do not let it run out of battery. I had to disconnect the power from solar panel to step-up and charge it(sun)for a few hours to get it to work again, or you could use some kind of connectors to disconnect. I was "smart" and solder it directly to step-up.


  • Plugin Developer

    @flopp

    I like this design. Do you think it will survive the Swedish winter days with their limited sun hours?



  • @martinhjelmare said:

    @flopp

    I like this design. Do you think it will survive the Swedish winter days with their limited sun hours?

    Yes, I think so. Today I send data every 10 seconds so if I send it once an hour it should not be a problem during winter. It will test to simulate rain and see if it is sealed enough.


  • Hero Member

    What kind of soil moisture sensor is it?


  • Mod



  • I love this concept. This kind of setup could be used for outdoor temp, light, humidity, and other things and not having to worry about power with it being solar. It also keeps the arduino shielded from weather and such also which is nice. I may use this design for some of my new sensors.



  • @dbemowsk
    If you have the solar in the sun and the sensors in the shadow and protected from rain that will work.
    My idea is to use a solar for all my outdoor sensors but have a bigger solar panel and a bigger(more mah) that feeds my nodes, rain, temp, hum, pressure, light, UV and in future lightning.


  • Hero Member

    @mfalkvidd
    Thanks for posting that link! Even though I don't believe in these types of sensors, I used your link to buy some anyway just because they're so darn cheap!


  • Mod

    @NeverDie
    If you just want to know when it is time to water regular home plants (0.5-2 times per week normally) , they are more than good enough.

    If you want to maximize growth in a farming situation, you'll probably need to go for more advanced measurement methods.


  • Hero Member

    Good to know! I'll give it a try--I guess two months from now after they arrive. 😉 My wife has two dozen house plants, and so it would be nice if I could automate the monitoring.



  • I have noisy measurement on analog input for battery voltage.

    Anyone that know how I can solve that?

    All pictures during night so there shouldn't be any sun that create power. See schematic above how it is connected.

    First Node
    No cap. Measures every 10 seconds, picture is 5 minute average.
    0_1465806103475_volt.png

    Second Node.
    10uF electrolyte cap between GND and A0. Measure every 30 minute. Picture is 5 min average.
    0_1465806108764_volt2.png

    Third Node
    No cap. Measure every 30 minute. Picture is 5 min average.
    0_1465806821657_volt3.png

    I see a slightly better measurement on Seconds Node but can it be better? This is power directly from the battery and I think the battery should be more stable than this. I have other nodes were I measure the VCC on Arduino and that measurement is extremely stable. Maybe the analog input isn't better than what I get in the pictures?



  • Could you show better scetch how you connect electicaly everything? how you sending data from arduino pro mini to Domoticz?

    In your scetch I see only power->battery->step-up->MCU


  • Hero Member

    @flopp I had exactly the same problem with a solar powered node. I solved it by moving the battery measurement around in the sketch. The analog measurement is sensitive..



  • @Huczas said:

    Could you show better scetch how you connect electicaly everything? how you sending data from arduino pro mini to Domoticz?

    In your scetch I see only power->battery->step-up->MCU

    Electric connection as the attached schematic and NRF you connect according to MySensors instruction.
    Data is sent through the NRF with attached sketch



  • @AWI
    Ok I will try to put it somewhere else and also with some delay before reading analog.
    Thanks



  • @AWI said:

    @flopp I had exactly the same problem with a solar powered node. I solved it by moving the battery measurement around in the sketch. The analog measurement is sensitive..
    Do you use voltage divider or direct from battery to analog input?
    I use direct from battery.



  • Updated electric sketch with Connectors. This is useful if the battery gets empty then you need to disconnect Arduino so the battery can get charged.


  • Hero Member

    @flopp a voltage divider as I use a rechargeable Li -cell (3.7 v)



  • @flopp Maybe i miss something but I think the connector should be mounted between the battery and the Vin on the step-up. You mounted it on the A0, it´s just the voltage check port.

    In the original lamp there was what i think is a charge regulator between the solar cell and the batteri, did you keep that one or you just put the solar cell to the battery ?

    Thanks for a nice idé to use this lamp.



  • @pettib said:

    @flopp Maybe i miss something but I think the connector should be mounted between the battery and the Vin on the step-up. You mounted it on the A0, it´s just the voltage check port.

    Thanks, I was to quick when adding the connectors

    In the original lamp there was what i think is a charge regulator between the solar cell and the batteri, did you keep that one or you just put the solar cell to the battery ?

    I removed the small IC, YX8108. Below is a schematic for YX8108
    0_1465929144673_yx8108.png



  • @flopp Is it the led or the solar cell that is working as light sensor ? If i remove the led and measure on the pins there is always power, even if it´s in daylight. So it should be possible to get the power to the step-up Vin from the LED pin, This way we can keep the on/off switch and the (YX8018) Mine is marked HW012.



  • @pettib said:

    @flopp Is it the led or the solar cell that is working as light sensor ?

    Solar.
    First I used the YX8108 but I noticed as long as there is power out from solar(sunshine) the LED doesn't get any power, when the power is low(~0,3V) the LED gets power.



  • @flopp I found this link. https://ez.analog.com/community/university-program/blog/2014/11/14/hacking-an-led-solar-garden-light
    The strange thing is that i can´t get the power on the led pin to go low. But i will remove the IC anyway. I made one light 2 days ago but the battery was empty just the day after and i was not able to get it fully charged again. Maybe some bad connection. I will try a new one today.



  • @pettib
    Did you remove the power from battery to Arduino when you tried to charge it again?
    I tried to charge r battery for 1-2 days when I had arduino connected, didn't work. After I removed it and out the "lamp" in the sun for 5 hours I could connect the Arduino again and then I was on track again.



  • @flopp I think it´s was some bad connections. The new one is working very good.



  • @pettib
    Nice



  • @flopp

    Hi I know its a bit off topic but what controller are you using to create the graph?


  • Hero Member

    @doug Not my turn 😊 but the Graphs are std. Domoticz..



  • side thought

    why not write your sleep setting as a variable on the arduino side that is dependent on battery voltage?

    if v>(full charge) sleep x hours
    if v>(half charge) sleep for y hours
    if v<(close to terminal voltage) sleep for z hours/days



  • @punter9 said:

    side thought

    why not write your sleep setting as a variable on the arduino side that is dependent on battery voltage?

    if v>(full charge) sleep x hours
    if v>(half charge) sleep for y hours
    if v<(close to terminal voltage) sleep for z hours/days
    I don't see that it is needed. It will be no problem to send data every 30 min even when the sun is not shining. During a grey day I had vintage around 1.0-1.2.

    The idea is useful if you think/know that the solar will not charge your battery



  • @flopp
    sure is, It looks like this sketch already has already picked a set single point sleep time that achieved the discharge gradient being less than the charge gradient looking at the angles on the 7 day graph.

    My question is why wouldn't you write an algorithm for your sleep setting so you don't have to mess with it if you get a cloudy week etc? You have variable charge gradients depending on weather so why not have a simple variable discharge gradient?

    A simple one would be case A - sunny day discharge gradient B- very cloudy day discharge gradient C- emergency charge/top off battery

    you could write an equation based off your existing data but honestly you probably wouldn't get much out of it past the simple set listed above that cover 99% of conditions you will see.



  • @punter9 said:

    @flopp
    sure is, It looks like this sketch already has already picked a set single point sleep time that achieved the discharge gradient being less than the charge gradient looking at the angles on the 7 day graph.

    My question is why wouldn't you write an algorithm for your sleep setting so you don't have to mess with it if you get a cloudy week etc? You have variable charge gradients depending on weather so why not have a simple variable discharge gradient (or complex if you want to make an algorithm off the charted data) ?

    At the moment I don't need it. But if I see that the battery gets drained to much, because of bad weather, I may be use your idea.



  • ha you are on it! caught me editing!

    anyways I love your idea, I can guarantee I am looking for these little lights to be on sale every time I go out. This maxes out the clever scale.



  • The "lamp"(silver) that I run out of battery seems to be damaged, two days ago it stopped sending data, last reported voltage was 1.13V which is good enough to work.
    Maybe the battery got damaged, it is a NiMh and I think they have some kind of memory?
    But why doesn't it get damage when you use the lamp as a lamp?
    Normally the lamp is charging the battery during the day and when it is dark it power on a LED which will lit until the battery is empty or maybe until the IC(YX8018) measure a low voltage.

    Today I change my second "lamp"(black) to a 1.2v AAA NiMh2 800mAh. I think this will help to not getting to low voltage during night.
    The battery that was included was mounted in the lid, I removed it and placed the new battery in the stick.
    I also made the stick little bit shorter, but don't make it too short because then you will measure the moisture at the top of the soil.
    The black round stick is 9 cm and the whole thing, when put togehter, is 28 cm from top(solar) to bottom(tip)
    0_1466357169260_20160619_134452210_iOS.jpg



  • To have in mind

    If you use the lamp with silver stick, it is metal, then remember to protect the battery if you but the battery in the stick.

    Use hot glue at the top around the solar otherwise rain will stay between the solar and the place where it is mounted. I don't get water inside where the electronics are but you never know what will happen after a few month.

    I have some problem with my black lamp it is rust on the positive connection, today it stopped working because if this, I had to remove the connection and put it back.
    Recommend to solder the cable instead of using connectors, which I do0_1467323921262_image.jpeg


  • Hardware Contributor

    First, thank you very much for making these tests, I found some similar garden lamps in a local store and thought they were cheap so I bought one to have a look. Seems they were not so cheap as I paid nearly 3$ despite living in a developing country 😄
    I will make a test with it as soon as I find a step up accepting the 1.xV of the battery, but I'm not sure if it will really work as the battery inside mine is a tiny V80h with a very limited 60mAh capacity, not sure if it will survive the sending of data...

    @flopp said:

    I have some problem with my black lamp it is rust on the positive connection, today it stopped working because if this, I had to remove the connection and put it back.
    Recommend to solder the cable instead of using connectors, which I do!

    I would put some hot glue there too, no ? When all connections are checked and the board is running fine you could even sink the whole electronic in hot glue to solve these problems "forever" ?



  • @Nca78 said:

    First, thank you very much for making these tests, I found some similar garden lamps in a local store and thought they were cheap so I bought one to have a look. Seems they were not so cheap as I paid nearly 3$ despite living in a developing country 😄

    Thank you. $3 is not bad but expensive compared to mine 🙂

    I will make a test with it as soon as I find a step up accepting the 1.xV of the battery, but I'm not sure if it will really work as the battery inside mine is a tiny V80h with a very limited 60mAh capacity, not sure if it will survive the sending of data...

    This is what I use as step-up, search for that product you will find cheaper than $1.49
    http://www.ebay.com/itm/DC-DC-0-8-3V-to-3-3V-Step-UP-Boost-Power-Board-Module-Converter-Voltage-RF-/282029936915?hash=item41aa4b5113:g:3GAAAOSw1DtXLS7Q

    I would put some hot glue there too, no ? When all connections are checked and the board is running fine you could even sink the whole electronic in hot glue to solve these problems "forever" ?

    Yes, why not, sounds like a good idea. But keep TX, RX, RESET and GND available to be able to update the sketch



  • @flopp you attached your sensors by two differant methods. Which methods seems to be working the best?



  • @gbgent_nc
    The sensor itself is connected the same way, A4 and A5 on both. @gbgent_nc if you mean that one is glued, that is difficult to say if it makes any difference. I tried lamp1 and 2 in same soil but they always had ~10% difference, I also tried same sensor both glued in same soil still difference. You have to measure until the flower/vegetable is dry and check your percentage after this you can set an alarm on that percentage. E.g. I have spices with 30% when it is dry and a flower that right now is 20% and not dry, maybe different soil etc.
    I recommend to glue the sensor on the arrow, otherwise it is very difficult to remove the lamp from soil, the sensor with stay in soil and you have to dig it out 🙂
    I had a test with a cap connected to my voltage measurement A0, but it didn't make any difference. No need to use Cap.
    I use other battery on one "lamp" only to be able to measure many days without sun.

    Today I have 3 lamps, yesterday number 3 was put in the soil. I now measure every hour, before every 30 minutes.



  • I have a couple soil sensors running with simple bolts in the ground. No two sensors will read the same in the same soil, but that is because they are just a relative setup not an accurate setup. Doing what is recommended above is necessary. Determine what % correlates to watering time on the plants. Once this is done it is set and forget.

    Also, on your batteries
    "The phenomenon itself is very real. If a NiCd battery is repeatedly charged after it has only been partially discharged it will develop a lower voltage and a lower capacity. Fortunately, this effect is reversible by conditioning NiCds."

    So I vote to help your poor cheap lamp batteries out by writing logic into your arduino that varies by battery voltage. You can write it in ranges

    battery > X volts report every Y minutes
    battery > Z volts report every P minutes
    battery > .....etc

    also to keep your battery in good shape you may consider every so many days ( 2 - 5 ) writing logic that says
    every Q days report every 20 seconds (or another interval that will drain battery) until battery voltage = min
    then
    sleep until battery voltage = max



  • Hello. What should i do, when i have this problems:

    czujnik:12: error: expected unqualified-id before 'default'

    default BOD settings.

    ^

    C:\Users\Kompek\Downloads\arduino-1.6.9\czujnik\czujnik.ino: In function 'void setup()':

    czujnik:43: error: 'SENSOR_ANALOG_PINS' was not declared in this scope

    for (int i = 0; i < N_ELEMENTS(SENSOR_ANALOG_PINS); i++) {

                                  ^
    

    C:\Users\Kompek\Downloads\arduino-1.6.9\czujnik\czujnik.ino:5:35: note: in definition of macro 'N_ELEMENTS'

    #define N_ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))

                                   ^
    

    C:\Users\Kompek\Downloads\arduino-1.6.9\czujnik\czujnik.ino: In function 'int readMoisture()':

    czujnik:102: error: 'SENSOR_ANALOG_PINS' was not declared in this scope

    pinMode(SENSOR_ANALOG_PINS[direction], INPUT_PULLUP); // Power on the sensor

           ^
    

    exit status 1
    expected unqualified-id before 'default'



  • I would have try to use Ardunio IDE 1.6.8



  • it's misstaken source code - line 11 should be deleted and then all is compiling well, it's just end of comment from line 10, [enter] is unnecessery there.



  • @flopp
    I've been thinking along the same lines in terms of powering all my outdoor sensors with solar. Have you had a chance to try your ideas out yet? I'd be very interested in what you my have found. Should we start a new thread about that though? i'm very new to the forum so not sure (just joined tonight!).



  • @breimann
    It have been in use since I write this post.
    I have had many problems with the nodes. I don't know if the problem is with my repeater node/GW.
    Time to time is stop sending and then suddenly without restarting the node, it start to send again.
    I have restarted the nodes sometimes also.
    Maybe it is to "small" solar panel so it takes time for it to recharge the battery?
    If you will do a solar panel node, go for a big panel with high mA output and also a big battery(1000mA), I am using 1.2 v battery. I think it is much better to use at least 2.5 volt battery then you don't need the step-up.


  • Hardware Contributor

    I think the problem is the nimh battery, this kind of partial charge/discharge cycles is more suitable for a li-ion battery. It's ok when used as a garden light because the battery will discharge completely during the night and will not have a memory effect, but with the low power usage of a sensor it will lose capacity quickly.

    Also if the solar panel is 1.2V like one your picture I don't understand how it could have enough voltage to charge the battery after the voltage drop of the diode ? On the garden light I bought the solar panel is 2V so it's possible to charge the battery to 100% at 1.4V+



  • @Nca78
    Yes it can be the NiMh battery, but it is actually run for days maybe weeks with out any problem.
    Today I disconnect the power to ATmega and put it back directly and it started to work.
    If the power goes below 0.8-0.9 it seems that I have to disconnect the step-up otherwise the solar panel cannot charge the battery.

    Solar panel is 1.4V and battery is 1.2V it seems to work and I have around 1.2-1.3 V during night



  • @flopp
    Thank you for your reply. That's all helpful info.



  • A funny thing,
    Yesterday I power off/on both my solar nodes and now the second one sent the data, haha strange??!! It was about 20 hours ago I powered it off/on



  • @flopp

    guys seriously this is just yet another reason to write your reporting interval based on battery voltage and time and not on time alone.

    You are using extremely cheap systems with cheap batteries of unknown age that are likely very prone to incur reduced capacity over shorter times and memory from discharge cycles.

    See the posts above. This really is one of the greatest ideas I have seen for monitoring, just needs a tweak

    With these findings I would set a floor voltage around 1-1.1 V for the arduino to go into sleep mode (find your own floor voltage by looking at your sleep discharge rate and length of night time).



  • Hello.
    Do you have any idea how to add to this code auto reset for instance every one hour? i have a problem with my sensor, because sometimes it is not responding, and i dont have any idea how to reset arduino automaticly



  • Hi all, I was so impressed by this thread that I decided to build my own. I must have spent maybe £6 or so - I really pushed the boat out. 😃

    It's been running successfully for a few weeks now, so I thought I'd share my code and a few pics. I upgraded the original code to v2.0. I've kept the update frequency high and it's running just fine, but we'll see how it goes in winter with less sun.

    Pics!
    0_1477923890152_IMG_20161016_151754 (Small).jpg
    Here you can see the boost converter and the Arduino pro mini.

    0_1477923898665_IMG_20161016_154217 (Small).jpg
    Here you can see the finished product in its natural environment. The RF radio sits in the plastic area, as I figured the metal collar at the top (where the arduino + battery sits) would have blocked/reduced the RF transmission.

    Code to follow.



  • I meant to mention that the wires coming out of the bottom are the wires that go to the soil moisture probes.

    Here's the code in case anyone else would like it:

    // Updated to v2.0 of Mysensors
    
    // Enable debug prints
    #define MY_DEBUG
    #define MY_RADIO_NRF24
    
    #include <MySensors.h>
    #include <SPI.h>
    
    #define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
    #define N_ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
    
    #define CHILD_ID_MOISTURE 0
    #define CHILD_ID_BATTERY 1
    #define SLEEP_TIME 10000 // Sleep time between reads (in milliseconds), was 10000
    #define THRESHOLD 1.1 // Only make a new reading with reverse polarity if the change is larger than 10%.
    #define STABILIZATION_TIME 1000 // Let the sensor stabilize before reading default BOD settings
    const int SENSOR_ANALOG_PINS[] = {A4, A5}; // Sensor is connected to these two pins. Avoid A3 if using ATSHA204. A6 and A7 cannot be used because they don't have pullups.
    
    // MySensor gw;   //removed for v2.0
    MyMessage msg(CHILD_ID_MOISTURE, V_HUM);
    MyMessage voltage_msg(CHILD_ID_BATTERY, V_VOLTAGE);
    long oldvoltage = 0;
    byte direction = 0;
    int oldMoistureLevel = -1;
    float batteryPcnt;
    float batteryVolt;
    int LED = 5;
    
    void setup()
    {
      pinMode(LED, OUTPUT);
      digitalWrite(LED, HIGH);
      delay(200);
      digitalWrite(LED, LOW);
      delay(200);
      digitalWrite(LED, HIGH);
      delay(200);
      digitalWrite(LED, LOW);
      
      //  gw.begin(); //Removed for v2.0
      for (int i = 0; i < N_ELEMENTS(SENSOR_ANALOG_PINS); i++) {
        pinMode(SENSOR_ANALOG_PINS[i], OUTPUT);
        digitalWrite(SENSOR_ANALOG_PINS[i], LOW);
      }
    }
    
    void presentation(){  //created for v2.0
      sendSketchInfo("Plant moisture w solar", "1.0");
      present(CHILD_ID_MOISTURE, S_HUM);
      delay(250);
      present(CHILD_ID_BATTERY, S_MULTIMETER);
    }
    
    
    void loop()
    {
      int moistureLevel = readMoisture();
    
      // Send rolling average of 2 samples to get rid of the "ripple" produced by different resistance in the internal pull-up resistors
      // See http://forum.mysensors.org/topic/2147/office-plant-monitoring/55 for more information
      if (oldMoistureLevel == -1) { // First reading, save current value as old
        oldMoistureLevel = moistureLevel;
      }
      if (moistureLevel > (oldMoistureLevel * THRESHOLD) || moistureLevel < (oldMoistureLevel / THRESHOLD)) {
        // The change was large, so it was probably not caused by the difference in internal pull-ups.
        // Measure again, this time with reversed polarity.
        moistureLevel = readMoisture();
      }
      send(msg.set((moistureLevel + oldMoistureLevel) / 2.0 / 10.23, 1));
      oldMoistureLevel = moistureLevel;
     
      int sensorValue = analogRead(A0);
      Serial.print("--Sensor value:");Serial.println(sensorValue);
      float voltage=sensorValue*(3.3/1023);
      Serial.print("--Voltage:");Serial.println(voltage);
      batteryPcnt = (sensorValue - 248) * 0.72;
      Serial.print("--Battery %:");Serial.println(batteryPcnt);
      batteryVolt = voltage;
      sendBatteryLevel(batteryPcnt);
      resend((voltage_msg.set(batteryVolt, 3)), 10);
      //send(voltage_msg.set(batteryVolt), 3);
    
      //flash led to indicate send
      digitalWrite(LED, HIGH);
      delay(200);
      digitalWrite(LED, LOW);
      
      sleep(SLEEP_TIME);
    }
    
    void resend(MyMessage &msg, int repeats)
    {
      int repeat = 1;
      int repeatdelay = 0;
      boolean sendOK = false;
    
      send(msg);
    /*
      while ((sendOK == false) and (repeat < repeats)) {
        if (send(msg)) {
          sendOK = true;
        } else {
          sendOK = false;
          Serial.print("Error ");
          Serial.println(repeat);
          repeatdelay += 500;
        } repeat++; delay(repeatdelay);
      }*/
    }
    
    
    int readMoisture() {
      pinMode(SENSOR_ANALOG_PINS[direction], INPUT_PULLUP); // Power on the sensor
      analogRead(SENSOR_ANALOG_PINS[direction]);// Read once to let the ADC capacitor start charging
      sleep(STABILIZATION_TIME);
      int moistureLevel = (1023 - analogRead(SENSOR_ANALOG_PINS[direction]));
    
      // Turn off the sensor to conserve battery and minimize corrosion
      pinMode(SENSOR_ANALOG_PINS[direction], OUTPUT);
      digitalWrite(SENSOR_ANALOG_PINS[direction], LOW);
    
      direction = (direction + 1) % 2; // Make direction alternate between 0 and 1 to reverse polarity which reduces corrosion
      return moistureLevel;
    }
    
    

    My thanks to flopp for the cool idea and to everyone else on the thread for the contributions.


  • Hero Member

    Interesting project. To what degree, if any, has corrosion been a problem after you switched to soldered connections? Obviously the operating environment (near the ground outdoors) can be intrinsically humid.

    Also, can someone please post a larger photo of how the sensor is attached at the base? The area of interest on the photo provided is miniscule, and it's too grainy if I try to enlarge it to a better size:
    alt text


  • Hero Member

    By the way, I notice this guy has a rather interesting soil moisture sensor that appears to go beyond measuring mere electrical conductance: https://www.tindie.com/products/Power_Modules/fdr-100mhz-plant-soil-sensor-mineral-transparency/


  • Hero Member

    @flopp Can you please include the pictures in your posting as the "tinypic.com" is rather intrusive, thanks...



  • @NeverDie said:

    Interesting project. To what degree, if any, has corrosion been a problem after you switched to soldered connections? Obviously the operating environment (near the ground outdoors) can be intrinsically humid.

    I have not checked how the sensor look like now, but I have only run it for 6 months. I have always used soldered connections.

    Also, can someone please post a larger photo of how the sensor is attached at the base?

    1_1479545970922_20160606_131714652_iOS.jpg 0_1479545970922_20160606_131710115_iOS.jpg


  • Hero Member

    @flopp Just copy the area's of intrest with a "snipping tool" (screen print) and copy in the post. Or, use one of the less intrusive services of the big cloud names...



  • @NeverDie
    Interesting, but would make the project a little pricey.


  • Hero Member

    @flopp
    Perfect! Thanks a bunch. I like it. Very clever.

    I think you were wise to cover the insulated wire with the silicon. Despite the appearance of being waterproof, I've noticed that regular wire insulation isn't waterproof/vaporproof, and regular wires used outdoors don't survive well (especially "copper" wires from China, which tend to have a high iron content and thus literally rust their way to failure). I don't know that silicon is sufficient, but if it isn't it might at least slow down the degradation process. It takes some effort to put these things together, so plainly you want them to last as long as possible.

    Nice work!


  • Hardware Contributor

    @NeverDie said:
    I don't know that silicon is sufficient, but if it isn't it might at least slow down the degradation process.

    Silicon is water proof, but not vapor proof. Else, it would not cure 😉



  • I have put glue on top of the soldering



  • WARNING!!!
    I opened one of my items which didn't worked since many weeks ago.
    I putted it on a table and should just open the stuff when I saw some brown water coming out from the pole.
    My first guess was that it was water mixed with mud but the smell was strange. It can be that the battery has leaked.

    If you will build this item please seal the battery to 100%. I just put the battery in the pole but unfortunately water came in and what I think destroyed the battery!

    Be careful



  • Just wanna update you all how the battery has been since start.
    No restart since 1st September.
    A few days ago we moved the plant indoor and I forgot to take out the sensor, I moved the sensor outdoor the day after

    0_1481267772602_chart.png



  • @flopp is your code should work with new version of gateway 2.1.1?
    Just maked using this tutorial my gateway https://www.mysensors.org/build/raspberry and I'm getting nothing in debug...

    pi@raspberrypi:~/MySensors $ sudo ./bin/mysgw -d
    mysgw: Starting gateway...
    mysgw: Protocol version - 2.1.1
    mysgw: MCO:BGN:INIT GW,CP=RNNG---,VER=2.1.1
    mysgw: TSM:INIT
    mysgw: TSF:WUR:MS=0
    mysgw: !TSM:INIT:TSP FAIL
    mysgw: TSM:FAIL:CNT=1
    mysgw: TSM:FAIL:PDT
    mysgw: TSM:FAIL:RE-INIT
    

    In node I used your newest code for gw2.0 - is this because version mismatch or something different in my setup?
    Could you update your code to work with new version?



  • @Huczas
    I don't use 2.1, yet. I only use 1.5.1.
    I don't have any knowledge of 2.x, so I cannot update the code, sorry



  • @flopp just release that @mrwomble was posting that updated code sorry. @mrwomble can you help?



  • Hello All,
    Wondering if any one of the successful implenters would be able to share their soil moisture graphs,both for a day and over months..



  • To solve the battery discharge memory issue, could a second battery be added with logic to alternate between batteries and fully discharge one at a time?


  • Mod

    Charge memory is a thing of the past, lithium batteries don't have it (if I understood what you meant).


  • Hero Member

    @gohan said in Solar Powered Soil Moisture Sensor:

    Charge memory is a thing of the past, lithium batteries don't have it (if I understood what you meant).

    Except that, if I'm not mistaken, he's presently re-using the NiCd's that came with the original solar garden light.

    [Edit: correction: I think maybe (?) he switched to Nimh. I didn't think those had a memory problem. Do they? ]

    [Edit2: Maybe he'll "upgrade" to a supercap. 🙂 A soil moisture meter would be a good application for one. ]


  • Mod

    NiMh still have some charge memory but not as bad as the NiCd. I got the moisture sensors yesterday I have to test them the next days.
    https://www.aliexpress.com/item/Soil-Moisture-Sensor/32532715392.html



  • Hi,
    I started building this great idea of solar powered soil moisture sensor. Not integrated yet in a solar light as above, but still on my desk.
    I tested the sensor in salted water and I get a report of 78% moisture : based on your experience, is it normal ? Then I pushed the sensor in a very humid plant pot and read 71%. I would have expected mroe differenc ecompared to salted water.
    So, would you advise to caliber ? I guess I could modify the calculation formula in the script..

    Precisely, is it possible with Mysensor technology and Domoticz to push to a sensor a new setting (for instance to caliber using Domoticz each different sensor) ?

    And another question.. Is it possible to ask Domoticz to show the result of moisure reading in the Weather or Measures category rather than in the Temperature menu ?

    br,
    Ricorico94


Log in to reply
 

Suggested Topics

  • 8
  • 90
  • 1
  • 1
  • 3
  • 44

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts