Have you ever played Farmville? β Thatβs the social network game where you basically manage a simulated farm, you can either do this in a single player or in a multiplayer setting. Now what if that farm was real β then you would say well that would be just real life farming β but bear with me here, what if you could still be online, play as a single player or a multiplayer to manage this real life farm? Then that would be something more than βjust farmingβ right.
Well I donβt have a farm, I live in an apartment in an urban area and have a small garden, but the above idea was my inspiration behind creating an Internet of things (IoT) community garden which could be managed remotely by a group of like-minded people using Telegram app group chat. For those unfamiliar with Telegram, it is a messaging application similar to Whatsapp, but it allows any user to create a bot using an API, a bot is simply some code which responds to commands in an automated fashion. So I implemented my idea by coding up a Telegram Bot which runs on a Rasberry Pi and accepts commands from users β to water plants, take a picture, turn lights on and off. I plan to further expand this in the future by adding temperature and moisture sensors, but for now I simply pull the weather data using weather API for dark sky. On April 4th 2019 I posted about the bot on Facebook groups and soon had random people from around the world join my Telegram group to play with my bot. Currently as I write this article I have about 30-35 people who interact with the bot daily.
Letβs take a small pause β if at this moment you want to test out our community IoT garden yourself, then download the Telegram app on your smart phone and simply look for our chat group called: βiotgardenβ, Β or click the link :Β https://t.me/iotgarden from your mobile device.Β Another option is to look directly for: ZenofallBot or click the link: https://t.me/ZenofallbotΒ from your mobile device.Β
Β What are the differences between the two?Β
- IotgardenΒ is a Telegram chat group βΒ there are multiple users in this group, the bot called Zenofallbot is added to the group similar to a real user, more importantly everyone can see each otherβs interaction with the bot. The idea here is to build a community where we can not only interact with the bot as a community, but also discuss ideas for improvement to the project, other DIY projects or have general tech discussions. Also you can directly interact with me here and ask questions, I am the admin of this group.
- Zenofallbot Β is the actual Telegram Bot β if you want to privately interact with the bot without being part of the community then directly search its name β no one else will be able to see your interaction.
Once you have initiated a chat with the chat group or directly with the bot, just type the command β/startβ, hit send, a menu should pop-up with buttons for actions.
As far as our IoT garden is concerned, management only implies watering the plants at regular intervals, analyzing the plant health βby monitoring photosynthesis activity using Infrared imaging (NDVI), and finally turning led string lights on or off β the last one doesnβt really affect plant health but is purely for garden decoration. It can also be a lot of fun to flip the lights on/off around the garden and take pictures or videos to check if everything worked as designed, this by itself is totally incredible to me β as you could potentially be flipping the lights on/off for my garden from any part of the world.Β
As I mentioned before live in an apartment and have a small garden, but if you have a big back yard or have wild animals or rodents roaming around that eat your garden produce β then you could potentially consider expanding the application by introducing a motion detection system to the same setup β and turn on flood lights or some other form of deterrent when motion is detected. The possibilities of what such an integrated IoT system can do are only limited by oneβs imagination. TheΒ concept can be further extended to manage an actual farm as long as the Raspberry Pi can be connected to internet β one could use low power Xbee sensors in the farm to monitor environmental activity.
SetupI built this using simple inexpensive components such as:
- Β Raspberry Pi β the brain that runs the bot
- Β Raspberry Pi NoIR camera with a blue filter
- Β 433Mhz radio transmitter chip
- Β 433Mhz Christmas light switches β Receivers
- Β Small aquarium pump
- Β Led String Lights
This project requires a power supply and wifi connectivity, this wasnβt a problem for me as I live in an apartment and my garden is 10ft away from my router.
Drip Irrigation
The drip irrigation system mainly consists of a small aquarium pump and tubes, the pump is connected to 433Mhz radio outlet from etekcity, which is in turn connected to a power source. The pump is immersed in a bucket of water. The main reason for choosing the 433Mhz power outlet is that we can use a transmitter chip with Raspberry Pi to turn the outlet On/Off. I have explained how to do this in a previous blog post.
The pump and the tubing I purchased were both from amazon:
Pump :Β https://www.amazon.com/PonicsPumps-PP21105-Submersible-Hydroponics-Aquaponics
Tubing: https://www.amazon.com/gp/product/B01DEYGBO2/
There are two main things to consider while choosing the pump and pipes:
- Β The power of the pump β for my application I needed a lift of about 6 to 12 inches, as the lift increases the water delivery rate decreases i.e. the gallons per hour specification. The pump I chose delivered 211gph at zero lift, and about 192gph at 12β lift. As my garden is small this was sufficient for me.Β I only planned to run the pump for 1min when I watered my plants β this was supposed to deliver approximately 192gph / 60 mins = 3.2gallons of water in 1 min, but empirically I realized it delivered close to 0.5 gallon every 1min I ran the pump. This was still good enough for me.
- The tubing should fit the pump.<pan>This was the main problem I encountered while setting up the irrigation system was that the smallest connector that came with the pump was ΒΌβ internal diameter (ID), but the tubing was ΒΌβ outside diameter (OD). I knew the two were incompatible just based on specification of products before I made the purchase, but I had to make the purchase because I was unable to find drip irrigation pipes in different size or the pump with a smaller nozzle. I solved the problem by buying an elbow connector from home depot, with the right size diameters at both ends
I have also put up some decorative led string lights around my garden, which are again connected to a 433Mhz power outlet, same as the irrigation pump. As mentioned above the 433Mhz outlet can be turned on/off from a Raspberry Pi using a 433Mhz transmitter chip. The calibration steps for getting the outlet on/off codes is mentioned in a previous blogΒ post.
CameraI use the Raspberry Pi NoIR camera, with a thin blue filter taped to it, this helps record photosynthesis activity using the Normalized Difference Vegetation Index β NDVI.Β I will explain the logic behind NDVI in a later section. Β The difference between a regular camera and noIR camera is that NoIR camera does not have an infrared light blocking filter i.e. it captures infrared light.
The details on how to connect the camera to Raspberry Pi are shown in a previous post.
Plant Health Monitoring & Control β Digital Image NDVI, Weather and WateringAs mentioned earlier I use Raspberry Pi NoIR camera with a thin blue filter taped to it, this thin blue filter actually comes in the Raspberry Pi NoIR camera box. More on this can be found here.
A digital image is captured by a camera in terms of pixels and each pixel has <R, G, B> i.e. Red, Green and Blue colors, also called RGB channels. Thus an image in digital form is just an array defined as [height-index, width-index, [r, g, b]], for example letβs say we have an image A.jpg of 1280 x 960 dimension, the first pixel can be found at A[0, 0] and will have some [r, g, b] values: [12, 40, 22] (the range of each of R, G, B is 0-255) and last pixel will be given at A[959, 1279] and will have some other [r, g, b] values and together the whole multi-dimensional array makes an image. i.e. [pixel-row, pixel-col, R, G, B]
The basic principle of NDVI is that during photosynthesis plants absorb visible spectrum of light, but reflect the Infrared wavelengths, so a measure of the reflected infrared light should indicate photosynthesis. Typically the red channel CMOS sensor is also sensitive to infrared wavelengths, and in modern cameras there is a special filter to filter out these infrared wavelengths β although a NoIR camera simply does not have this filter and the red channel indirectly records the infrared wavelengths.
If a physical blue film is taped to the camera it will theoretically capture more Blue light and Infrared light and filter out the visible spectrum R and G wavelengths, allowing for easy calculation of NDVI measure from vectorized image data.
Thus NDVI can then be calculated as: (NIR-Blue)/(NIR+Blue) and the image can be recomposed.Β
The physical setup involves connecting the camera to the Pi and then taping the thin blue film filter (that comes in the camera packaging box) on top of it.
The other part of our monitoring system involves keeping an eye on the weather β we use dark sky api and Python libraries to check the weather, dark sky is a weather monitoring service which gives weather data by gps coordinates. One could also get this data by using a temperature humidity sensor directly connected to the Raspberry Pi, but I realized that sensor readings are prone to error, so rather than spending money on the sensor we decided to use an online service.
The photosynthesis monitoring plus the weather data sufficiently captures the monitoring aspect.
Finally we move to the control β which is in terms of making a decision as to whether we water our plants are not based on weather and last time of watering β I have built some restrictions around this, which I will explain in the next section.
Before we go any further, please consider subscribing to our mailing list if you like our article so far and to keep up to date with our news.
Software Functions β Telegram BotThe software is mainly implemented as a Python script β I have kept it simple and not focused on writing a very good OOP design pattern, as this wasnβt my goal. It is very functional and works. I prefer to use an application called βtmuxβ on raspberry pi which create a standalone session and allows me to close out of my remote ssh client without stopping the session and killing the Python code after I have kicked off the Python script. To install tmux:
sudo apt-get install tmuxΒ
When you log into raspberry pi remotely, first start tmux and then kickoff the script:
tmux
python3 gardenBot_publish_v1.py &
To detach from session press Cntrl+B and then press D. Now you can close your remote ssh client such as putty. The Python script implements a Telegram bot and communicates as a control client with Telegram server, the commands from users go to the server and the Python code which is running on raspberry pi polls for the commands. In a previous articleΒ I have explained in detail how to create your own Telegram Bot. For the script to work you will need some pre-requisites: Β Darksky Python library install by typing:
pip3 install darksky
Utility called MP4box used for video conversion.Install by typing:
sudo apt-get install gpac
*gpac is a collection of utilities, mp4box is one of them.
- Telegram Bot API key
- Telegram Bot Python library Install by typing:
pip3 install python-telegram-bot
(refer https://python-telegram-bot.org/)
Since I was planning to allow other users to monitor and control my garden, I wanted to make sure that I as admin had full control at all times β i.e. be able to remove users, stop addition of more users, etc. This is actually very easy to do by just using the Telegram user id. Every user that uses the Telegram app for messaging is assigned a unique user-id, you can find your unique user id by simply messaging your bot and reading the message using the web-api.Β This user-id is assigned by Telegram to your phone, so cannot be spoofed by software.
How to Find Your User ID?Open telegram app on your iOS or Android device, search for your newly created bot and send it a message β anything like βHiβType the following in following command in your web-browser:
https://api.telegram.org/bot<TOKEN>/getUpdates(here the <token> is your token assigned during bot creation, also do not include < >, just copy-paste the key)
The bot on server records this as a simple post request and displays it on browser:
User FunctionsOnce you have this id, you can use this id and implement your bot such that the bot responds to only this user. In my case I wanted others to be able to use my bot and monitor/control my garden but also be in full control, so I wrote functions accordingly to give me admin privileges.
The main function that I implemented, with which any user begins their interaction with the bot was:
/start
This function responds with basic instructions, adds the new user to IoT control and pops up a command menu, which the user can use to perform actions.
A common user can only use the functions available through this menu, but admin has more functions at their disposal.Β
Admin FunctionsAs Raspberry Pi is resource constrained, watering interval and light flip intervals need to be controlled, this is implemented using some control variables in the code which restrict usage.
Example:
- Watering can only be done at set intervals β every 4hrs or 6hrs
- Lights can be flipped every 10mins but not more than that
- Pics can be taken every 2mins, video can be taken every 5min
Basically the admin can set these controls, this brings us to admin control functions.
There are some special functions which only the admin can use:
/stop: kill the bot gracefully
/fetch: get list of current IoT users
/add: add users
/rm: remove users
/disable {args} : disable menu button functions and autoAdd
/setexp {arg} : set exposure for camera
/setawb {arg}: set white balance for camera
/setLimit {arg}: set time limits for pic, watering, lights, weather, video
Disabling autoAdd gives admin full control on which users can be added manually to manage the garden- after disabling auto add the admin can use /fetch, /add and /rm for user management.Β
Also once /stop is executed the bot code will end, and to restart one has to log into raspberry pi and start it.
Further instructions are provided in the Python code.
The codeFinally what you all have been waiting for, my actual Community IoT Garden bot code. There are three files for this:
1. gardenBot_publish_v1.py : This is telegram bot wrapper and main file to run
2. iotcontrol.py : This is where the actual user functions are implemented
3. NDVI.py : This is for NDVI pics
You can download all three from my website in a zip, after registration.
Disclaimer: The Python source code below is provided by Zen Of All LLC for free upon registration, the users (downloaders) of the code are free to modify it as they see fit. Zen Of All LLC assumes no legal responsibility or liability towards the code, its maintenance, usage or any modifications by the users (downloaders).
In English: I wrote this code very fast for testing my proof of concept idea β feel free to read it carefully and edit as per your own needs, I am not responsible if something breaks.Β
You will need to create a login for this website to download the code.Β Β
Please contact: contact@zenofall.com for any questions.
Hope you enjoyed the article and followed along, thanks for reading, Β donβt forget to subscribe to our mailing list for updates.
Comments