Building an agriculture sensor - mitra42/frugal-iot GitHub Wiki
Building an Agriculture Sensor
This document gives you an idea for how you could build a simple agriculture sensor.
The sensor measures soil temperature & moisture; and ambient temperature & humidity; it should be trivial to add a light sensor.
This version is appropriate for running in a garden, or somewhere else close to infrastructure because it depends on WiFi, though it would also be possible to build using LoRa dev boards for more dispersed use cases (Mitra will help if you choose to try this).
Power is presumed to come from a small solar panel, though a simpler version could be powered from a USB power source.
I have built four versions of this sensor so far, and will include photos of them, the sensor Graam Disha built as part of the Varta project is essentially the same. The design is easy to adapt to different dev boards, enclosures etc.
Construction of the sensor
The construction involves the following steps.
- Sensor selection
- Board selection
- Adding sensors to the board
- Adding a power solution
- Building into an enclosure
Sensor selection
I use the following sensors in both designs.
Prices include shipping (to Australia) and are at 10x quantity where this is cheaper; note that prices change frequently, and it is worth shopping around for similar products.
| Sensing | Sensor | Connection | Housing | Cost |
|---|---|---|---|---|
| Temperature and Humidity | SHT30 or SHT40 in 1M tube | I2C | One metre cord+tube | €2.53 |
| Soil Temperature | DS18B20 | OneWire | One metre cord+tube | €0.73 |
| Soil Moisture | Capacitive | Analog | 100mm blade | €0.69 |
See below about possibly adding a light sensor
Temperature and Humidity
The differences between SHT40 and SHT30 are not significant in this application, so you can go on price. There are cheaper sensors, either bare, or in different housing, but I strongly recommend this version as its hard to come up with a housing that allows humidity testing, and is reasonably weather proof.
Prices have almost doubled recently, shop around
Soil Temperature
You need a fully waterproof housing, as this will be inserted directly into the soil. These DS18B20 sensors are available from many suppliers, again - I recommend the sealed version on a cord.
Soil Moisture
It is a little tricky to measure soil moisture, resistive sensors are available, but have a reputation of corroding rapidly. I've used these capacitive sensors, but not yet for long enough to be sure of their longevity. Resistive sensors certainly last longer if only powered up when taking readings, and this may be the same for capacitive.
They work much better in some soils/materials than others, for example trying to use them in a compost pile (or BSF pile) was a complete failure.
Selection of a development board
The sensors above require: an I2C port, or two digital pins; one digital pin for OneWire to the DS18B20; one analog pin for the soil moisture. In addition you'll need one analog pin for the battery sensor.
Because it will be solar powered it needs to use Deep Sleep, this works much better on ESP32 (any version) than on ESP8266 (any version).
The requirement for two analog pins means you can't use the ESP8266 which only has one analog.
For better power response, it is better to power the sensors from digital pins, so that they can be powered down between readings. This requires at least one more digital pin, but connecting both positive and ground to pins dramatically simplifies the wiring. So a board with 6 more pins (for a total of 2 analog and 9 digital) is ideal.
Power choice also determines board choice.
An onboard battery works well, as power requirements are low since it will be in deep sleep most of the time. The only ESP32 board I know of (other than LoRa boards) is the C3 Pico for about €5.50 each + €5 so €6 at 10x.
Alternatively an external power bank - designed for IoT use, (not an off the shelf one for charging phones), will work. See Power Design. Note that the power-bank design may actually be less expensive since it makes it possible to use almost any dev board. The prototype below uses the Lolin S2 €1.50-€2. One of the prototypes was built using the minimalist C3 SuperMini which also worked fine.
Construction of dev + sensors
Its fairly straightforward to build one of these.
There are two complications which make the soldering tricky.
- The DS18b20 needs an approximate 4.7kΩ resistor between 3.3V and data. Some have this already built in, but most do not.
- The battery sensor needs a voltage divider, typically a pair of 100k resistors.
DS18b20 pullup resistor
The easiest way to add the pullup is inline while adding a plug on the sensor - the red line is positive, and yellow is data. A 4k7Ω resistor is recommended.
Battery sensor Voltage divider
For the voltage divider, the best place to put it is on the Input of the booster (not the output, which should always be 5V) because the soldering on the dev board is already crowded. I used 100kΩ resistors, but I've used 220kΩ on another board.
Four different approaches to prototypes
I went through anumber of designs, and will probably evolve it more, but this is the current version and is replicable enough to recommend others to try!
The previous versions - and what to avoid:
- Wires soldered direct to a Lolin C3 Pico - multiple wires on the 3v3 and Gnd where hard to debug
- Daughter board (proto board) attached by a ribbon, still hard to debug
- Powered sensors from digital pins, removed the debug challenges, but sensors plugged onto header pins on board, made it hard to fit in a low profile box.
- Plug/socket on board BUT too much stiffness on each sensor (sensor cable -> heat shrink -> plug -> socket -> wire -> board) so hard to enclose
- Sensors direct to board, as in the first, but now powering from digital pins.
- As #5, but with strain releaf on adjecent holes.
Construction of power options
For the first prototype I used a Lolin C3 Pico, so the power is a small (RC aircraft) battery plugged directly into it. Check the polarity on the batteries it is not standardised. If your battery is wired the opposite way around then cables can be switched around, though its a little tricky.
TODO make little video on editing a cable
This prototype used a 5V plug in solar panel.
Prototype 2 used a Lolin S2 mini, which does not have battery charging on board. This used a separate IoT power bank (not the same thing as a power bank for phones), see Power Design for instructions to build one.
Prototype 3 is USB powered, with no onboard battery.
Prototypes 4 and 5 have a power bank similar to Prototype 2, except I salvaged a battery from a Vape, this seems to be working fine.
Enclosure
With an included battery, the most cost effective solution I have found is using plumbing pipe with end caps, they are easily available and avoid the cost of shipping which usually exceeds the cost of any box.
See the image above, in this particular case the end pipes were a little loose, and we added some tape.
Programming
Configuration and installation
Follow the instructions at Programming and Configuring a Device on PlatformIO or Arduino IDE to set it up.
The example to clone for this sensor is examples/agri
Check the pins in examples/agri/main.cpp and platform.ini match the pins where you have soldered your sensors (especially the I2C)
Ways to adapt and expand this ....
Controlling irrigation, etc
An obvious use for this sensor is to pair it with a Sonoff and use that to control (mains powered) irrigation or a fan. If your irrigation is 12V then a relay board could be constructed but that will need a separate write up. (TODO)
Data to Google Sheets
While you can view data on the platform, it can also be sent to a Google Sheets. This works well, though it requires a minor step by Mitra until its integrated into admin. A write up is needed, but please prompt for it if this is something you want. (TODO)