Building a Smart Garden With Raspberry Pi 3B+

Building a Smart Garden With Raspberry Pi 3B+

A few months ago, a very generous friend of mine gave me a fantastic gift, my very own Raspberry Pi 3B+ For those who are unfamiliar, a Raspberry Pi is a small and affordable computer which can also be connected to sensors and other electronics.

The first challenge was deciding what to do. I’m always looking to extend my skills as a software developer, and I’d been toying with the idea of growing some vegetables. Receiving the Raspberry Pi was exactly the impetus I needed to start a project: a smart hydroponic garden system, with automated alerts, data analysis and maintenance.

Spoiler Alert: If you’re only looking to grow food, this project isn’t particularly efficient.Applying computing to a small home garden is time consuming, sometimes costly, and doesn’t necessarily improve yield. However, if you’re interested in IoT, cloud computing and how technology can be applied to agriculture, then read on, and maybe even try it yourself to learn some new skills!

A Crash Course in Hydroponics

Yep, I know what you’re thinking. But it’s not just that which is grown hydroponically. An increasingly large amount of food is also grown hydroponically, and there are plenty of benefits to consider: less water, little to no chemicals, less land and fast all year round growth (but much more energy consumed if you’re using lighting!). In a world where around 11% of all land is used for agriculture, access to clean water is a major issue, and renewable energy is becoming more effective, there’s a lot to be said for hydroponics.

The type of system I built is called Deep Water Culture. It’s fairly simple, so I made a diagram to explain how it works.

Building the System

Assembling the system involved a visit to a hydroponics shop, hardware shop various purchases online and some creative re-use of materials I already had on hand.

All up I spent around $250 AUD on the hydroponics aspect. The most expensive part was the light, and I could have done have saved more by purchasing everything online. I ended up purchasing almost 20 different items, and could still think of more things to buy. Marginal costs of growing are low though, so it would work out well over time.

After some DIY action, this was the end result. The tube leading into the box connects to the air pump out of view. The frame was definitely overkill but there’s space for a second system if I decide to upgrade and it was mostly already built from a previous project.

If you would be interested in a detailed article on how to make a system like this then leave a comment (or Google, there’s a lot of information out there).

Making it Smart

This was already pretty cool so far as I was concerned, and the plants were growing nicely. It was clearly time to add some unwarranted complexity. I saw four main elements to this:

  1. Collecting and storing and making it accessible
  2. Automated alerts
  3. Web based interface to monitor/control the system and analyze data
  4. Physical automation (Think pH balancing, dynamic light cycles based on power prices and more!)

Currently I’ve finished #1, completed a fair bit of #2 and have laid the initial groundwork for #3. Physical automation is definitely an exciting prospect but it will also require another round of spending and I wanted to get everything else settled first.

Collecting and Storing the Data

I have two sensors running at the moment, a DS18B20 waterproof temperature sensor and a DHT11 humidity and air temperature sensor. I also have a water sensor which I’ve been meaning to add in. They connect to the breadboard which is in turn connected Raspberry Pi.

The first question was where to store the data. I wanted to try out designing a NoSQL schema, and DynamoDB is free for up to 25GB which is more than enough for my purposes. As well as storing sensor readings the DynamoDB table also stores information about the system configuration. I optimized the schema for retrieving the system configuration and retrieving the data for a specific sensor for a specified time period, because I knew these would be my two main use cases.

I then wrote a simple python script to post the sensor readings into my database. So far, so good. However, this wasn’t contrived enough for my liking.

So I decided to write a GraphQL endpoint, deployed with the Serverless framework and Node.js 8.10 Lambda. This was a bit more complicated than I needed right now, but these were all things I’d been wanting to try, and I plan in the future to use third party weather and power price APIs in the future so GraphQL will provide more value at that point. Serverless is a really enjoyable way to work with Lambda (it also supports other providers!), and you should definitely try it if you haven’t yet.

Automated Alerts

At this point I had a pretty solid setup for logging, storing and accessing data. It was time to put that to good use by setting up alerts.

I wanted to be alerted for when:

  • The sensor readings were outside of the ‘safe’ range.
  • The sensors stopped sending data (this could mean that there was an issue with my Raspberry Pi).
  • Either of the above issues persisted for an extended period of time.
  • Either of the above issues resolved (i.e. the temperature dropped back into the ‘safe’ range’.

For this I turned to Amazon Simple Notification Service. I can send 1000 emails a month for free, and it’s very straightforward to publish a notification from a Lambda function. I wrote another Serverless project with functions that are run every 20 minutes. I stored information regarding the state of an alert (i.e. that this issue has been persisting since 10PM) in my DynamoDB table.

I also plan to add alerts to remind me to do tasks like check the pH and add nutrients.

Web Interface

A web interface for the system could have all sorts of features. Reviewing the current state of the system, analyzing historical data, controlling automation and configuring alerts are just a few ideas. So far I’ve created an exceedingly bare bones Next.js app which uses Apollo Client to fetch data from the GraphQL endpoint I created earlier. I haven’t had time to work on this aspect too much yet, but with the data retrieval in place I can start building UI soon.

Additionally, it seems a bit of a waste to be storing more than a few weeks of time series data in DynamoDB, seeing as only the new data gets accessed frequently. To that end I plan to combine AWS Glue, S3, and Athena to transfer, store, and query long term data. Here’s an AWS blog which explains what that would involve.

Physical Automation and Other Cool (Expensive) Stuff

Unfortunately there’s nothing to show here yet. The first step will be buying a 433mHz transceiver and a remote control socket. I’ll use this to control the light, enabling features like automatically minimizing power expenses.

Other useful features would be likely to involve peristaltic pumps and pH sensors, which I would probably need to order from China. I might also buy an Arduino to have easy access to analog interfaces, seeing as that’s inconvenient with the Raspberry Pi. Another option could be adding a camera to the Raspberry Pi. Software wise I might try my hand at writing the next service in Go. Leave a comment if you have any ideas on what I should try and do next.

In Summary…

I’ve succeeded in growing food, learnt a lot of new things, and crammed an exceptionally high number of tech buzzwords into one post. Hopefully this wasn’t too long and has maybe inspired you to get started on a project of your own. I also made plenty of mistakes along the way (you might notice my kale is very small), so next cycle I aim to do better. Even a setup like this takes a lot of thought, and it makes you appreciate the complexity of food production which we mostly take for granted. I’ve also spent more than $300 AUD so it shows how capital intensive ‘high tech’ farming is.

Further Information

If combining technology and agriculture interests you, then have a look at this video which shows commercial vertical farming. If you’d like to try hydroponics in a very simple and cost effective way then have a look at this video. Epic Gardening has a range of informative articles on hydroponics and other gardening topics. If you decide to have a go then you can ask questions on r/Hydroponics or Hydroponics for Beginners. All of the technologies used have extensive documentation and community support, so you should be able to find whatever you need.

Leave a comment if you have a suggestion for what I should do next, or if there’s a specific element of the project which you think I should write a more in depth post about. I’ll probably write a more technical post once I’ve progressed further. You can also message me on LinkedIn if you want to discuss anything!

30s ad

From 0 to 1: Raspberry Pi and the Internet of Things

Hands on Zigbee using Raspberry Pi

Introduction to Internet of Things(IoT) using Raspberry Pi 2

Homebaked | Raspberry Pi + Django Home Server

Raspberry Pi 4 on the Raspberry Pi 4 - Computerphile

Raspberry Pi 4 on the Raspberry Pi 4 - Computerphile

A quick tour of the Raspberry Pi 4 edited on the Raspberry Pi 4. Dr Steve Bagley gets out his knife.dll to unbox Sean's purchases! ☞ [I created a home IoT setup with AWS, Raspberry...

A quick tour of the Raspberry Pi 4 edited on the Raspberry Pi 4. Dr Steve Bagley gets out his knife.dll to unbox Sean's purchases!

I created a home IoT setup with AWS, Raspberry Pi

Benchmarking the Raspberry Pi 4

The easy way to set up Docker on a Raspberry Pi – freeCodeCamp.org

Creating a Rogue Wi-Fi Access Point using a Raspberry Pi

Building a Smart Garden With Raspberry Pi 3B+

Learn Raspberry Pi for Image Processing Applications

Learn Raspberry Pi for Image Processing Applications

New to the newly launched Raspberry Pi 3? Learn all the components of Raspberry Pi, connecting components to Raspberry Pi, installation of NOOBS operating system, basic Linux commands, Python programming and building Image Processing applications on Raspberry Pi. At just $9.

Description
Image Processing Applications on Raspberry Pi is a beginner course on the newly launched Raspberry Pi 3 and is fully compatible with Raspberry Pi 2 and Raspberry Pi Zero.

The course is ideal for those who are new to the Raspberry Pi and want to explore more about it.

You will learn the components of Raspberry Pi, connecting components to Raspberry Pi, installation of NOOBS operating system, basic Linux commands, Python programming and building Image Processing applications on Raspberry Pi.

This course will take beginners without any coding skills to a level where they can write their own programs.

Basics of Python programming language are well covered in the course.

Building Image Processing applications are taught in the simplest manner which is easy to understand.

Users can quickly learn hardware assembly and coding in Python programming for building Image Processing applications. By the end of this course, users will have enough knowledge about Raspberry Pi, its components, basic Python programming, and execution of Image Processing applications in the real time scenario.

The course is taught by an expert team of Electronics and Computer Science engineers, having PhD and Postdoctoral research experience in Image Processing.

Anyone can take this course. No engineering knowledge is expected. Tutor has explained all required engineering concepts in the simplest manner.

The course will enable you to independently build Image Processing applications using Raspberry Pi.

This course is the easiest way to learn and become familiar with the Raspberry Pi platform.

By the end of this course, users will build Image Processing applications which includes scaling and flipping images, varying brightness of images, perform bit-wise operations on images, blurring and sharpening images, thresholding, erosion and dilation, edge detection, image segmentation. User will also be able to build real-world Image Processing applications which includes real-time human face eyes nose detection, detecting cars in video, real-time object detection, human face recognition and many more.

The course provides complete code for all Image Processing applications which are compatible on Raspberry Pi 3/2/Zero.

Who is the target audience?

Anyone who wants to explore Raspberry Pi and interested in building Image Processing applications

To read more:

Raspberry Pi: Dummy tutorial on port forwarding and SSH

Raspberry Pi: Dummy tutorial on port forwarding and SSH

Raspberry Pi: Dummy tutorial on port forwarding and SSH .This is a continuation of my series on setting up Raspberry Pi to be a remote jupyter notebook code editor. In the last chapter Raspberry Pi was set up and could be accessed by SSH at your home network

This is a continuation of my series on setting up Raspberry Pi to be a remote jupyter notebook code editor. In the last chapter Raspberry Pi was set up and could be accessed by SSH at your home network. In this chapter I will guide you how to set up port forwarding and access the Pi remotely from the internet, so you could write command under CLI just like at home.

First time Pi user and first time reader? Check out the first part here.

But once your Pi is accessible from the internet, you need to be 100x cautious about potential security vulnerability. Most of the content you could find on Google Search should guide you to a term “Port Forwarding”, while in the Raspberry Pi official documentation it explicitly told you that port forwarding exposes a known security problem and you should consider altnerative ways to do it. I am going to introduce both ways in this series. And I promise we will get to host website soon, but let’s make sure it’s safe to do so first.

Table of Content
  • Increase your security before exposing your Pi to the internet
  • Port Fowarding
  • What’s next: Cloud Proxy Connections
Increase your security before exposing your Pi to the internet

There are two things you should do before exposing your Pi to the internet.

  1. Change the password of default user Pi

To change the password, simply type in passwd and follow the text instructions by re-typing your old password, your new password and re-tpying your new password.

  1. Create a new user without sudo access and use that user to access your pi remotely

sudo: similar to system admin preveilage in windows, which user can run command that is defined as superuser level, including rebooting your computer and installing any software. You can check out more here.
A good practice, for example, is to use sudo at home to install the software needed and give normal user read/write access to only some sub-folders, then use normal user to remotely login the Pi and only work on these sub-folders.

Let’s create a new user without sudo access, type in sudo adduser normal_user.

You will be asked a series of questions, including the password, basic information of the user and enter Y to confirm all information is correct.

Now we have created a new user normal_user.

To check whether this user have sudo access, we could simply type in sudo -l -U normal_user. And the text should show you ‘User normal_user is not allowed to run sudo on raspberrypi.’

So next time, you could try out SSH connection by not using pi as the login, but normal_user .

Want to know more about user access and organizing user? Check out the tutorial on digitalocean.

Port Forwarding

First, I want to reiterate that setting up port forwarding without any safety add-on to block malicious traffic is not recommended, and this article does not provide enough guidance to set that up. What I would recommend is to set up a cloud proxy server, which we will go through in the next chapter.

With all said, let’s go through a dummy example.

Your home network is known as Private LAN in that no external device could connect to your devices at home. This is controlled by firewall, which by default denies all incoming traffic.

But you don’t always spend your life in Raspberry Pi (me neither, just to be clear), you also love to play Diablo II, a classic rpg game which you could coop with other players. And you play it on another home device, PC. Turns out Diablo II is an old game which requires a direct connection between you and other players in order to hack-and-slash-and-loot the monsters. When you guys are in the game, data package including player’s location, level and action will need to be continuously streamed between players.

Your home PC has an internal IP address of 192.168.1.4. Diablo II automatcially configures a port 1033 for other players to get your data pacakge.

As I mentioned in last chatper, each device at your home will be automatically assigned an IP address by the router (e.g. my Pi’s address is at 192.168.1.50) and each internet application will use up one port number (e.g. 22). A quick refresher example: 192.168.1.50:22 represents <my Raspberry Pi>:<SSH Application>.

Finally, you do a google search and know that your external IP address is 50.247.207.5.

Turns out, if you were able to open a port on the external IP address, and map(point) that port to one set of <Internal IP Address>:<Port>, then you can tell your friends to enter your <External IP Address>:<Port your opened> to route their connection to your <PC's internal IP Address>:<Dialbo II data package application (1033)>.

This is what referred as Port Fowarding and you basically pinhole on your firewall to finally allow incoming traffic to a specific internal IP address and port.

Risk with Port Fowarding

Before I teach you how to forward a port, I want to crystalize what could happen at the worst case.

First, Port Forward won’t expose all your devices at your home. It only allow external user to connect to device you’ve pointed it at. In Diablo II, that’s perfectly fine. As the only application you opened is for other player to receive data package from your game. But for Raspberry Pi, the SSH application represents full access to your Pi’s CLI, and able to run any command including communication with your other home devices. And this is why opening Port for Diablo and XBox are generally safe and trivial but for Raspberry Pi it could be very dangerous.

The chance for any hacker to hide in the brush and wait til you open a port is nearly impossible, let alone to say that hacker needs to know a user / password pair in order to login. So it isn’t really a huge security problem for most of home users. But the possibility is there.

Port Forwarding your Pi to external network

Let’s clarify things we know before we started. From last chatper, we know how to check the internal IP address of our Pi, external IP address of our network and we also know that port 22 is opened for SSH from our Pi.

1~ Enter your router configuration page

Router configuration page is usually a website hosted inside your home network. The url should be printed on the router itself, and definitely shown in the router manual. If you still cannot find it, simply try http://192.168.1.1/ as most of the routers occupied this IP as their configuration page.

2~ Go to port forwarding configuration page

The page location will vary for different brands. Try your best to look for keywords including

  • Advanced Setting, Security Setting, Port Forwading, Virtual Server, Wan Setting

And you should be able to get to the port forwarding page.

3~ Configure rules

For some routers, they will require an extra step to enable port forwarding. Make sure to enable that!

Then, you will probably see a list of inputs, including

  • Service Name: text to describle the port foward service.
  • Source Target [optional]: Whitelist of external IP. Only IPs that are whitelisted could be able to connect to this port. Therefore, if you already know the IP where you are going to make the connection. Adding the whitelist IP here could tremendously increase your security. But this is only available for newer models of router. Leave it blank if you allow all IP to make connection.
  • Port Range: Port that is opened up from your router. In the above example, I open a port on 10300. Let’s say my external IP is 50.247.207.5, using 50.247.207.5:10300 could redirect me to the Pi later.
  • Local IP: IP address of your Pi in your private LAN. Check out last chapter if you don’t know how to find it. In this case it’ 192.168.1.50.
  • Port: Port / Application you want to open from the device specified in the local IP. In this case its 22, since Pi configure port 22 as the SSH application.
  • Protocol: TCP / UDP are usually available. You should also be able to select both. If only one of them can be selected. Please select TCP.

Once you apply the change, you are able to ssh to your Pi anywhere in the world! In this example, we can access our Pi by visiting 50.247.207.5:10300.

Accessing your Pi with windows

Same as last chatper, we could use Putty to connect to our Pi. But this time you could finally try this in a safe network (aka not Starbucks) outside your home wifi.

On Putty, type in your external IP address and Port you open up from the port forwarding setting. In this example it is 50.247.207.5:10300. Keep the connection type as SSH.

And you should be able to login with the CLI pop-up! This time let’s try out our normal_user username and password, so even if hackers get your credential, it’s just a non-sudo user.

And…. we are in! Now you can write and run any program in your Pi from anywhere!

What’s next: Cloud Proxy Connections

Port forwarding is always associated with risk. And we should consider other options like cloud proxy connections. Unfortunately on Raspberry Pi documentation there is no concreted instruction on how to do so, though it does recommend some free services we could use.

In next story, I am going to demonstrate how to use one of the cloud proxy connection service listed in the official documentation to connect to your Pi with SSH, remotely and securely. Stay tuned!