This is a prototype device for smart farming. The smart farm has a DHT11 sensor that measures temperature and humidity, a light-dependent resistor that measures the light intensity and a soil moisture sensor that measures soil moisture content. We have an irrigation system and fan system symbolized by a solid-state relay. When the soil moisture is low, the eris dev kit automatically starts the irrigation system to raise the soil moisture content. If the soil moisture rises until it reaches a certain level, the irrigation system is closed. When the temperature is high, the fan and irrigation system is turned on to reduce the microclimate temperature. If it drops to a certain level, the two systems are stopped. When the humidity is low, the irrigation system overhead the crops is started to raise the humidity to optimum levels. If it is high, the irrigation system is stopped. For light intensity, we are tracking the records to make better decisions in the future, maybe by data analysis.
The microcontroller generally measures all the parameters after every minute and sends it to Africa’s Talking IoT API. Meanwhile, the date is also shown on an OLED screen on the eris dev kit. For sending the data we are using the MQTT protocol and we are sending it via Safaricom network. This means that the eris dev kit has a pre-configured sim card to send data to Africa’s Talking API.
Who the article is for?
This article targets python and Arduino developers who would want to build a smart farm application system that allows farmers to monitor the temperature, humidity, light intensity and soil moisture of their farm and be able to access the data via SMS or USSD services provided by Africa’s Talking.
What the article is going to cover?
This project will be able to help the reader get started with connecting the hardware then connecting the firmware with Africa’s Talking USSD, SMS and MQTT APIs in order to publish the data collected to the users.
The application is built using python and Arduino programming.
- Some knowledge of Python programming language
- Arduino programming
- Africa’s Talking USSD, SMS and MQTT API
- Basic electronics
For the hardware, we are using the eris dev kit v1 as our micro-controller.
What we are going to configure:
- DHT11 sensor
- Soil moisture sensor
- Light-dependent resistor
- Solid-state relay
- OLED screen
For the wiring, we are connecting the dht11 sensor pin to a digital input, pin PA6, to receive temperature and humidity data. The Soil moisture sensor pin is also connected to a digital input pin, pin PB1. The Light-dependent resistor power pin is also connected to a digital input pin, pin PB0. The solid-state relay is connected to a digital output pin, pin PA7. The OLED screen is connected via the Inter-Integrated Circuit, or I2C on pin PB8 and PB9 for SCL and SDA respectively.
- SDA (Serial Data) – The line for the master and slave to send and receive data.
- SCL (Serial Clock) – The line that carries the clock signal.
I2C is a serial communication protocol, so data is transferred bit by bit along a single wire (the SDA line). I2C is synchronous, so the output of bits is synchronized to the sampling of bits by a clock signal shared between the master and the slave. The master always controls the clock signal.
We will look at some of the key points to take home. You can find others at this GitHub repository
Before we start anything we need to define our apn to connect to Safaricom before pushing data to the Africa Talking MQTT server.
We also need to config our MQTT to be able to push data to it. We need an MQTT username, password. With africastalking, we also need the device id to identify your device and the respective MQTT topics to publish and subscribe to.
All the functions we will be using are defined here.
An example of reading humidity data from the DHT11 sensor is outlined here. We use the DHT library to be able to get humidity data from it.
We need to connect to the Safaricom GSM endpoint since our sim card is Safaricom based. After connecting to GSM we will now use the sim card features e.g using the internet to push data to africastalking MQTT server.
We also need to connect to our MQTT server to publish and subscribe to topics.
Since we will be publishing messages to the topics, our function takes in the data you want to publish and the topic you want to publish.
To send data to our MQTT broker, we use this function. This is an example of sending humidity data to our africastalking endpoint.
To handle all our incoming data, we use this function. It checks if the incoming message is saying
off . This is to start and stop our relay since it is used as our irrigation system.
This function prints the topic and data to the OLED screen
The Arduino sketch can be found at here
This is the callback application that listens to all the incoming and is also responsible for outgoing messages. This application has USSD and SMS services for farming. In the future, I will implement a voice application. The USSD app is responsible for displaying the data of the farm to the user. It has a wide range of use cases spanning from getting data from any date to starting or stoping irrigation. The SMS service sends a daily average at the end of the day and sends alert on any extreme conditions.
Check out the Demo on this youtube video.
Build the Arduino file
|MQTT Client(Eclipse)||Handling MQTT Client Connection||Eclipse.org|
|Tiny GSM||Handling GSM Module connection||TinyGSM|
|DHT||High level wrapper for DHT sensors||Adafruit|
|U8x8lib||High level wrapper for OLED display||olikraus/u8g2|
- Go to
Hardware/hardwareappthen create a
config.hfile. This will hold your config Variables
- Inside the
config.hadd this line of code
- Reset the board and the build
Install Docker and NGROK
- Create a
broker.envfile with the following configs
2. Start Postgres database as a docker container
This starts a docker Postgres server in your local machine at port
3. Install requirements
This installs also your backend app requirements need for this to run.
4. Export configs
This exports your database URI and hardware app configs
5. Initialize the flask app
This initializes your database, migrates it then upgrades it before running your server.
6. Run ngrok
This is to expose your backend callback app to the internet.
7. Copy your callback url you will use it later
Setup Africa’s Talking Account
- In your Sandbox account, navigate to the USSD blade and click on “Create Channel”
2. In the channel creation form, add a USSD shortcode number, and in the callback URL field, add your app URL and append
ussd. For example, if your Heroku app URL in 4 above is
https://mycoolapp.herokuapp.com/ then your USSD callback should be
3. Save and in the end, you should have something like shown above
- Inside your AT IoT account, Click on the
Actionstab for the device group you’d like to configure the callback for and update the callback URL to
iot. For example, if your Heroku app url above is
https://mycoolapp.herokuapp.com/then your IoT callback should be
The Simulator (Sandbox)
- Navigate to the AT Sandbox Simulator page. Enter a valid phone number.
- Click on the USSD option.
3. Dial your USSD code. If your shortcode above was 1000, enter
*384*1000# and press the
Call button for magic!
4. Find the demo here
6. SMS Alerts
7. Data in my database:
You should now have a basic understanding of how Africa’s Talking USSD, SMS and MQTT A.P.I. works and how to implement them in Python. The article not only provides a guide on how to implement APIs on web applications but also on other applications that Python and Arduino can be applied to.
Further improvements could be done to the project. Finally, you can find the code snippets in this @repo.