Reader small image

You're reading from  Hands-On Embedded Programming with C++17

Product typeBook
Published inJan 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781788629300
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Maya Posch
Maya Posch
author image
Maya Posch

Maya Posch is a senior C++ developer with more than 15 years of experience. Discovering the joys of programming early on, and later the joys of electronics, she has always expressed a profound interest in technology, a passion that she gladly shares with others. Describing herself as a C developer who happens to like C++ and Ada, she likes to seek the limits of what can be done with the minimum of code and hardware to accomplish everything that is cool, new, and exciting. She also enjoys FPGA development, AI, and robotics research, in addition to creative writing, music, and drawing.
Read more about Maya Posch

Right arrow

Chapter 5. Example - Soil Humidity Monitor with Wi-Fi

Keeping indoor plants alive is no small feat. The example project in this chapter will show you how to create a Wi-Fi-enabled soil humidity monitor with actuator options for a pump or similar, like a valve and gravity-fed water tank. Using the built-in web server, we will be able to use its browser-based UI for monitoring the plant health and control system features, or integrate it into a larger system using its HTTP-based REST API.

The topics covered in this chapter are as follows:

  • Programming an ESP8266 microcontroller
  • Connecting sensors and actuators to an ESP8266
  • Implementing an HTTP server on this platform
  • Developing a web-based UI for monitoring and control
  • Integrating the project into a larger network

Keeping plants happy


To keep plants alive, you need a number of things:

  • Nutrients
  • Light
  • Water

Of these, the first two are usually handled by nutrient-rich soil and putting the plant in a well-lit place, respectively. The main issue with keeping plants alive after satisfying those two points is usually the third point, as this has to be handled on a daily basis.

Here, it's not just a simple matter of keeping the water topped up, but instead of staying within the range where the soil has enough but not too much water. The presence of too much water in the soil affects how much oxygen the plant can absorb via its roots. As a result, with too much water in the soil, the plant will wither and die.

On the other hand, too little water means that the plant cannot take up enough water to compensate for the water that is being evaporated through its leaves, nor can it get the nutrients into its roots. In this case, the plant will also wither and die.

When manually watering plants, we tend to use rough estimates...

Our solution


To keep things simple, we will just be building something that can take care of a single plant. This will provide us with the most flexibility in terms of placement, as we would just have a single system next to each plant, no matter whether it's on a windowsill, table, or terrace somewhere.

In addition to measuring the soil moisture level, we would also want to be able to have the system automatically water the plant at set trigger levels and for us to be able to monitor this process. This requires some kind of network access, preferably wireless so that we don't have to run any more cables than the power connector.

This makes the ESP8266 MCU very attractive, with the NodeMCU development board an attractive target for developing and debugging the system. We'd hook up a soil moisture sensor to it, along with a peristaltic pump.

By connecting to the ESP8266 system's IP address using a web browser, we would see the current status of the system, with the soil humidity level and optionally...

The hardware


Our ideal solution would have the most accurate sensor, without breaking the bank. This means that we pretty much have to use a capacitance sensor, as we saw earlier in this chapter. These sensors can be obtained as capacitive soil moisture sensors for little more than a few euros or dollars for a simple 555 timer IC-based design such as these:

You would simply stick these into the soil up to the point where the circuitry begins, then connect it with a power source as well as the connection to the analog to digital converter of the MCU.

Most peristaltic pumps one can purchase require 12V. This means that we need to either have a power supply that can provide both 5V and 12V, or use a so-called boost converter to convert the 5V to 12V. Either way, we would also need to have some method to turn the pump on or off. With the boost converter, we can use its enable pin to turn its output on or off using a GPIO pin on our MCU.

For prototyping, we can use one of these common 5V to 12V...

The firmware


For this project, we will be implementing a module for the same firmware that we will be using in Chapter 9, Example - Building Monitoring and Control. Therefore, this chapter will only cover the parts that are unique to this plant-watering module.

Before we can start with the firmware itself, we first have to set up the development environment. This involves the installation of the ESP8266 SDK and the Sming framework.

Setting up Sming

The Sming-based ESP8266 development environment can be used on Linux, Windows, and macOS. You want to preferably use the development branch of Sming, however, which is where using it on Linux (or in a Linux VM, or Windows 10's Windows Subsystem for Linux (WSL)) is the easiest way, and definitely recommended. On Linux installing in the /opt folder is recommended for consistency with the Sming quick start guide.

This quick start guide for Linux can be found at https://github.com/SmingHub/Sming/wiki/Linux-Quickstart.

On Linux, we can use the Open SDK...

Taking it further


You also need to have the following considerations:

  • At this point, you can further refine the system by implementing plant-watering profiles to add dry periods or to adjust for certain soil types. You can add new RGB LED modes to make full use of the color choices available.
  • The entire hardware could be built into an enclosure, to make it blend into the background, or maybe to make it more visible.
  • The web interface could be extended to allow for controlling the trigger point and such from the browser, instead of having to use an MQTT client.
  • In addition to the moisture sensor, you could also add a brightness sensor, a temperature sensor, and so on, to measure more aspects that affect the plant's health.
  • For bonus points, you could automate the applying of (liquid) fertilizer to the plant.

Complications


One possible complication you may encounter with the ESP8266's ADC is that on the NodeMCU boards, the first reserved (RSV) pin that is right next to the ADC pin is directly connected to the ESP8266 module's ADC input. This can potentially cause issues with electrostatic discharge ESD exposure. Essentially the discharging of a high voltage, but low current, into the MCU. Adding a small capacitor on this RSV pin to ground can help reduce this risk.

One thing that this system obviously cannot help with is to keep your plants pest-free. This means that though the watering may be automated, that doesn't mean that you can just ignore the plants. The regular checking of the plants for any issues, as well as the system for any issues that may be developing (disconnected tubing, things that have fallen over due to cats, and so on) remains an important task.

 

 

 

 

 

 

 

 

Summary


In this chapter, we looked at how to take a simple ESP8266-based project from theory and simple requirements to a functioning design with a versatile firmware and a collection of input and output options, using which we can ensure that a connected plant gets just the right amount of water to stay healthy. We also saw how to set up a development environment for the ESP8266.

The reader should now be able to create projects for the ESP8266, program the MCU with new firmware, and have a solid grasp on both the strengths and limitations of this development platform.

In the next chapter, we will be looking at how to test embedded software written for SoCs and other large, embedded platforms.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Embedded Programming with C++17
Published in: Jan 2019Publisher: PacktISBN-13: 9781788629300
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Maya Posch

Maya Posch is a senior C++ developer with more than 15 years of experience. Discovering the joys of programming early on, and later the joys of electronics, she has always expressed a profound interest in technology, a passion that she gladly shares with others. Describing herself as a C developer who happens to like C++ and Ada, she likes to seek the limits of what can be done with the minimum of code and hardware to accomplish everything that is cool, new, and exciting. She also enjoys FPGA development, AI, and robotics research, in addition to creative writing, music, and drawing.
Read more about Maya Posch