Mixing Electronics & Water

Enter Your Electronics & Design Project for Your Chance to Win a $100 Shopping Cart!

Back to The Project14 homepage

Project14 Home
Monthly Themes
Monthly Theme Poll

 

[ Final Post - Updated 2nd April ]

 

After some thinking, my project is going to be along the lines of watering plants using a smart system rather than a standard timer. I'm thinking that if I had to rely on using a water barrel to irrigate crops how annoying it would be if it rained the next day anyway, or perhaps the plants could even hang on for two days at the most. In these situations the water might be wasted instead of being held back for the days when it didn't rain. Also watering at night time and directly into the roots would reduce the amount of water lost as run off/through or evaporation (that would be the simple timing part). Lastly I think an override button would be good to give a timed burst should the farmer/gardener notice the plants have wilted.

 

I've never experimented with the following so this is a bit of trial and error: I know there are software interfaces to many of the large companies and organisations gathering useful data so looked at the main one here in the UK - the MetOffice. They do an Application Programming Interface (API) that can get weather detail for the next few days via an http request to their server. I've signed up and got a personal key that I can use to make data enquiries which are returned as either XML or JSON format - I guess I can then extract the bits I need. Normally I'd use C or C++ for software dev, but whilst learning the API stuff I might as well also give Python a go (it is meant for this sort of thing I believe).

 

Therefore I need a computer, internet access, real time clock and ability to drive at least a 12v solenoid liquid valve and perhaps have a few other indicators or moisture/temperature sensors to make the system even smarter. I think my Raspberry Pi 2 should fit the Bill Of Materials (BOM) really well.

 

Tonight I'm downloading PyCharm and seeing what library is available to make reading the XML or JSON a bit easier.

 

As for a project name I've used HOPPY as a very loose interpretation of H2O Py.

 

Progress 24th Feb 2018

I managed to pull a sample file from the MetOffice website and saved that as Testfile.json.

 

I then spent quite a while getting to grips with Python and JSON format before being able to read the various sections from the file. This following code now prints the precipitation for the next day/first period as a percentage.

 

import json
input_file = 'Testfile.json'output_file = open('test.json', 'w')

with open(input_file,'r+') as f:
  dic=json.load(f)

   for key,value in dic.items():
print("Digging:", dic['SiteRep']['DV']['Location']['Period'][0]['Rep'][0]['Pp'])

 

 

Progress - Sunday 25th Feb 2018

I've learnt some more Python today, getting used to the syntax and error messages. I'm quite happy with the code for now which runs well on my PC. It pulls data for Heathrow, London and uses that for its prediction of weather - I need to work on that algorithm some more. The code is likely to be a right insult to hardened Python programmers so any comments to improve the layout/order are gratefully appreciated.

 

GitHub vs GitLab vs BitBucket Server (Formerly Stash ...

I've also added my code to Github here and will push new code to that repository as I develop it further.

 

To run the code you need to get an API key by signing up to the MetOffice site (free) and saving that in an external file called MyMetOfficeAPIKey.txt which I did so that my key is kep private; the MetOffice have a fair use policy. Alternatively feel free to branch the code and modify it to work with any other weather API

 

Next: I probably need to move code across to my Raspberry Pi, check the code runs on that. Then add GPIO and some hardware. I guess I should buy an actual solenoid valve as well.

 

Update 30th March 2018

 

I was successful in getting the code to run on my Raspberry Pi and are now developing it further directly on that, using SSH from my PC to interface to the RPi as I run it headless....and using Nano to edit the code . Currently I'm hardcoding the time that I wish watering to commence into the Python script - so I'll move that into an argument when the code is called to make it more flexible. Once the time triggers the code pulls the JSON data shown below from the MetOffice website and saves some specific values to local variables in the code - specifically the precipitation data, the temperature and weather values for the current day, next day and the day after that.....from those nine values I will be able to create an algorithm for watering my plants. This is the part that anyone reusing this project may like to concentrate on further to apply their own logic. It may also be prudent to do some simulation of the code against a testbench to see how it performs against a range of data.

The Algorithm For Watering

Now the part that will make statisticians cringe....I added the percentage probabilities (so can get a result greater than 100%) but weighted the following days with nominal factors of 0.7 and 0.5 (values I just chose at random to indicate the uncertainty in those values).

 

My logical though here, again using some nominal random values, is now:

 

1.    Is there a reasonable chance it might rain (eg >40%) over the next few days AND have we watered the plants recently anyway.....we can probably get away with not wasting water again.

 

2.    If we haven't watered the plants recently they will really need a drink....but if there is a high probability of rain (>90%) then we also hold off.

 

Testing the Code So Far

Manually testing the code is actually quite awkward given the timescales that it operates over combined with the API data not changing much - again perhaps one for a scripted testbench to solve?

 

Hardware

I didn't purchase a solenoid valve for this project - partly because at the time we had snow and it detracted me from thinking about the hot summer ahead. However I do need to add something so I'll add in the GPIO code to activate a pin and illuminate an LED or switch a relay.

 

Next: add the relay/LED, add watering time as argument, test out code via testbench.

 

Update 2nd April 2018

 

Hardware Modifications

I didn't add a relay but used an LED to show when the watering valve would be initiated.

 

I added a push switch and a timing function that allows that to be tested throughout the timing period rather than locking it out for the duration. This enables the user to manually turn the valve on and off.

 

Code Changes

I made a copy of the latest v1.8 Python code available on my Github site here should anyone want to replicate the work or branch off to make their own interpretation of it.

 

I also added several command line arguments that can be added when calling the program. These are:

  • -t      sets the time of day that the watering is to start. Defaults to 8pm
  • -d    sets the duration of watering in seconds. Defaults to 60 seconds
  • -l      set the location as used by the MetOffice API. Defaults to 3772 (which is Heathrow UK)
  • -a    select if API is to get called/debug mode. Default=0 where API is used. 1 & 2 are used as various debug modes. 3 tests out the GPIO functions.

 

Hardware

Videos of Operation

Warning these aren't the most inspiring of videos!

 

Normal Operation

The first viseo shows HOPPY working normally, launching it a few seconds before the required watering period. At 17:38 the watering tests are undertaken and you will see the live API data being grabbed. Nothing happens though as the plants have previously been watered (that is the default but might be better forcing to indicate they haven't been watered). At the very end the timer ticks on to 17:39. In another 24 hours the test will be undertaken again.

 

Manual Override

In these two videos I push the switch to manually override the code. There is a delay afterwards of 250ms to stop the switch bounce being used.

 

NB: ESP8266 module is not part of HOPPY.

 

Arguments and Debug

Here I start the code using the arguments for time, delay and debug. The latter uses smaller delays whilst waiting for the time to increment. It also holds off checking the API (which is useful as you are only allowed a certain number of API requests per hour via the MetOffice fair usage policy). Lastly the criteria are forced to imply the plants need watering - and so the valve is opened for the given duration (in this case 5 seconds).

 

What Have I Learnt

I've learnt to do some coding in Python using PyCharm, most of which went well although the finished code probably could do with a decent tidyup. The variable and function names were not to PyCharm's liking but I didn't change them.

 

I learn to pull JSON data from an external API. Which was really interesting to read about and do. I know there are lots of other API available so might have to utilise a few other sites in future projects.

 

I also downloaded and used Fritzing for the first time - that is such a great little program!

 

Things to be Improved

The finished product's algorithm could likely be improved drastically - I didn't even use the temperatures or weather settings that I pulled from the API. I would use the temperature to prevent watering during the colder days. The hardware setup still needs an external water valve and driver circuit.

 

Reading vlaine recent review, I think a GraspIo board would easily be incorporated to this design to add an interesting user interface (currently I'm using SSH to access the Python code remotely).

 

As mentioned in the comments by mcb1 genebren a soil humidity/moisture sensor could also be incorporated and would make an essential fail safe should my algorithm fail.

 

Summary

The final HOPPY project is aimed to provide smarter usage of water when the supply is limited i.e. from a rain barrel rather than directly off the mains water supply. I've had lots of fun doing this project and it gave me an excuse to try out some areas of technology I haven't used before. It should be noted that the code is prototype and likely contains a few issues and bugs (testing over long time periods was not undertaken) - so perhaps don't use it to water your competition pumpkin whilst away on your holidays . Hopefully others will find the code useful and can base their designs on something similar.

 

Happy to try and answer any questions....

 

Rod