Book Image

Intelligent IoT Projects in 7 Days

By : Agus Kurniawan
Book Image

Intelligent IoT Projects in 7 Days

By: Agus Kurniawan

Overview of this book

Intelligent IoT Projects in 7 days is about creating smart IoT projects in just 7 days. This book will help you to overcome the challenge of analyzing data from physical devices. This book aims to help you put together some of the most exciting IoT projects in a short span of time. You'll be able to use these in achieving or automating everyday tasks—one project per day. We will start with a simple smart gardening system and move on to a smart parking system, and then we will make our own vending machine, a smart digital advertising dashboard, a smart speaker machine, an autonomous fire fighter robot, and finally look at a multi-robot cooperation using swarm intelligence
Table of Contents (9 chapters)
8
Essential Hardware Components

Sensor devices for a smart gardening system

To build a smart gardening system, we need some sensors related to gardening and farming. In this section, we will review the main sensors that are used in gardening and farming. Soil moisture, temperature, and humidity are three parameters that we can use to build our smart gardening system.

Let's explore!

Soil moisture sensor

One of the parameters of gardening is soil moisture. We should measure soil moisture to ensure our plant grows well. There are many options when it comes to soil moisture sensors. You can use the SparkFun Soil Moisture Sensor, for example. You can find this module at https://www.sparkfun.com/products/13322 .

You can see the SparkFun Soil Moisture Sensor in the following image:

You can use other soil moisture sensors from cheap manufacturers in China. You can order them from Alibaba or Aliexpress.

To read soil moisture values, we can use analog I/O. To use analog I/O, you should be familiar with the following Sketch APIs:

  • analogRead() is for reading analog data from an analog pin on Arduino
  • analogWrite() is for writing analog data to an analog pin on Arduino

For demonstration, we'll connect an Arduino board to the SparkFun Soil Moisture sensor. The following is the wiring used:

  • VCC is connected to 5V on the Arduino
  • GND is connected to GND on the Arduino
  • SIG is connected to A0 on the Arduino

A complete wiring can be seen in the following figure:

Now you can open the Arduino software. If you haven't installed it yet, you can download and install the tool from https://www.arduino.cc. Once done, you can write a Sketch program. Create this script:

void setup() { 
Serial.begin(9600);
}

void loop() {
int val;
val = analogRead(A0);

Serial.print("Soil moisture: ");
Serial.print(val);

delay(3000);
}

The program starts by initializing a Serial object with a baud rate of 9600. In a looping program, we read soil moisture levels using the analogRead() method. Then the measurement is printed to the serial port.

Save this Sketch file and upload the program to the Arduino board. To see the output data, you can use the serial monitor in the Arduino software. You can find it by going to Tools | Serial Monitor. You should see a soil moisture reading in the Serial Monitor tool.

Temperature and humidity sensor

Temperature and humidity have significant impact on the growth of the crop. Keeping temperature and humidity within certain values also keeps crops healthy. To monitor temperature and humidity, we can use the DHT22 or DHT11 for Arduino and Raspberry Pi. These sensors, DHT22 and DHT11, are famous sensors and have a lot of resources available for development.

The RHT03 (also known as DHT22) is a low-cost humidity and temperature sensor with a single-wire digital interface. You can obtain this module from SparkFun (https://www.sparkfun.com/products/10167) and Adafruit (https://www.adafruit.com/products/393). You may also find it in your local online or electronics store.

You can see the DHT22 module in the following figure:

For further information about the DHT22 module, you can read the DHT22 datasheet at http://cdn.sparkfun.com/datasheets/Sensors/Weather/RHT03.pdf.

Now we connect the DHT22 module to the Arduino. The following is the wiring:

  • VDD (pin 1) is connected to the 3.3V pin on the Arduino
  • SIG (pin 2) is connected to digital pin 8 on the Arduino
  • GND (pin 4) is connected to GND on the Arduino

You can see the complete wiring in the following figure:

To access the DHT-22 on the Arduino, we can use the DHT sensor library from Adafruit: https://github.com/adafruit/DHT-sensor-library. We can install this library from the Arduino software. Go to Sketch | Include Library | Manage Libraries and you will get a dialog.

Search for dht in Library Manager. You should see the DHT sensor library by Adafruit. Install this library:

Now let's start to write our Sketch program. You can develop a Sketch program to read temperature and humidity using DHT22. You can write this Sketch program in the Arduino software:

#include "DHT.h" 

// define DHT22
#define DHTTYPE DHT22
// define pin on DHT22
#define DHTPIN 8

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
dht.begin();
}

void loop() {
delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();


// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.println(" *C ");
}

Save the program. Now you can compile and upload it to the Arduino board. Furthermore, you can open Serial Monitor to see sensor data from the serial port. You can see a sample program output in the following screenshot:

How it works

In the setup() function, we initialize the DHT module by calling dht.begin(). To read temperature and humidity, you can use dht.readTemperature() and dht.readHumidity(). You also can get a heat index using the dht.computeHeatIndex() function.