Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

IoT Project Tutorial: Smart Plant System

DZone's Guide to

IoT Project Tutorial: Smart Plant System

This IoT project tutorial describes how to build an IoT project that monitors plant health status.

· IoT Zone
Free Resource

This IoT project tutorial describes how to build an IoT project that monitors plant health status. We want to check some environment parameters like temperature, humidity and light intensity that have effects on the plant. Moreover, we want to retrieve the soil moisture.

All this information is sent by Arduino to the cloud using Ubidots IoT cloud platform. Previously, we talked already about IoT ecosystem and you know already what it means. You already know how to use IoT cloud platforms to store and retrieve data.

Just to recap briefly, Internet of things is defined as:

The network of physical objects — devices, vehicles, buildings and other items — embedded with electronics, software, sensors, and network connectivity that enables these objects to collect and exchange data. The IoT allows objects to be sensed and controlled remotely across existing network infrastructure.

An important aspect of IoT project is the IoT cloud platforms that store data coming from dev boards like Arduino, Raspberry and so on. Using this data, IoT cloud platforms build charts and have a built-in system to create some business rules on this information. Ubidots is an IoT cloud platform that not only stores data but enables users to create a dashboard to represent graphically the stored data. In the first part of this IoT project tutorial, we will explore how to use sensors to collect environment information using Arduino and how to send this information to the cloud.

In the second part of IoT project tutorial, we will explore how to enable triggers on the sensor values stored and send alert to user smartphone when some parameter value is out of the range.

Now it is time to describe the project in more detail. The image below shows the project at work:

IoT project tutorial

This IoT project tutorial uses Arduino Uno as dev board and a set of sensors:

  • DHT11: Temperature Humidity sensor -  a sensor to measure temperature and pressure. It is a cheap sensor and suitable for Arduino, you can use more accurate sensor but the way to use it is the same.
  • YL-38 + YL-69: Soil moisture sensor - a sensor to measure the soil moisture. It has to be inserted into the plant soil.
  • TEMT6000: Light intensity - a sensor used to measure the light intensity so that we can know how light the plant is receiving.

The wiring part is very simple as it is clear in the picture below:
 IoT sensors with Arduino

In the picture above, it is clear that Arduino uses an ethernet shield to connect to the network. You can use also a WIFI shield it is almost the same approach.

Now that it is clear how to connect sensors to Arduino the next step is reading the values to send them to the cloud.

The sketch is very simple:

void loop() {
float soilHum = analogRead(moisturePin);
soilHum = (1023 - soilHum) * 100 /1023;
Serial.println("Soil Humidty: " + String(soilHum));

// Read light 
float volts = analogRead(lightPin) * 5.0 / 1024.0;
float amps = volts /10000.0;
float microamps = amps * 1000000;
float lux = microamps * 2.0;
Serial.println("Lux: " + String(lux));

float h = dht.readHumidity();
float temp = dht.readTemperature();
...
}


Where the dht variable is defined as:

#define DHTTYPE DHT11
..
DHT dht(DHTPIN, DHTTYPE); // DHTPIN is the pin number connected to DHT11 data output


If we load the sketch into Arduino and run it you will know the values read by sensors using the serial monitor.

The next step is sending the data read to the cloud. In this tutorial, we use Ubidots. If you are new to this platform and don’t know how to use it, I suggest you read this tutorial named “Internet of things project: Connect Arduino to Ubidots and Android“. This project defines 4 variables holding values read from the sensor. Later using this variable we create the dashboard.
 Ubidots variables
 Once the variables are configured in Ubidots, we have the variable id:
 iot platform data.
 It is time to modify the Arduino sketch so that it sends the values to the Ubidots and these values are stored in these variables:

#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT11

String tempVarId = "575475df7625423fd9da9c36";
String humVarId = "575475f1762542406cb10c43";
String lightVarId = "575475fc762542410358a0c3";
String soilVarId = "5754760576254241593d4d47";
String token = "aIk7lh3ipJGRdgAWOwJwDlPFwCdQu6uoLWZcGsMelXVAF62ycFsanB9Yywdk";

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

char server[]="things.ubidots.com";

EthernetClient client;
IPAddress ip(192, 168, 1, 40); // Arduino IP Add
IPAddress myDns(8,8,8,8);
IPAddress myGateway(192,168,1,1);


int moisturePin = 0;
int lightPin = 3;
DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
Serial.print("Starting...");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac,ip,myDns,myGateway);
}
dht.begin();
}

void loop() {
float soilHum = analogRead(moisturePin);
soilHum = (1023 - soilHum) * 100 /1023;
Serial.println("Soil Humidty: " + String(soilHum));

// Read light 
float volts = analogRead(lightPin) * 5.0 / 1024.0;
float amps = volts /10000.0;
float microamps = amps * 1000000;
float lux = microamps * 2.0;

Serial.println("Lux: " + String(lux));

float h = dht.readHumidity();
float temp = dht.readTemperature();

Serial.println("Temp: " + String(temp,2));
Serial.println("Hum: " + String(h,2));
sendValue(temp, h, lux, soilHum);
delay (60000);
}

void sendValue(float tempValue, float humValue, float lux, float soil)
{
Serial.println("Sending data...");
// if you get a connection, report back via serial:
int bodySize = 0;
delay(2000);

// Post single value to single var
// String varString = "{\"value\":"+ String(tempValue) + "}";
String varString = "[{\"variable\": \"" + tempVarId + "\", \"value\":" + String(tempValue) + "}";
varString += ",{\"variable\": \"" + humVarId + "\", \"value\":" + String(humValue) + "}";
Serial.print(varString);
varString += ",{\"variable\": \"" + lightVarId + "\", \"value\":" + String(lux) + "}";
Serial.print(varString);
varString += ",{\"variable\": \"" + soilVarId + "\", \"value\":" + String(soil) + "}]";
Serial.print(varString);
bodySize = varString.length();

Serial.println("Connecting...");

if (client.connect(server,80))
{
client.println("POST /api/v1.6/collections/values HTTP/1.1");
Serial.println("POST /api/v1.6/collections/values HTTP/1.1");

client.println("Content-Type: application/json");
Serial.println("Content-Type: application/json");
client.println("Content-Length: "+String(bodySize));
Serial.println("Content-Length: "+String(bodySize));
client.println("X-Auth-Token: "+token);
Serial.println("X-Auth-Token: "+token);
client.println("Host: things.ubidots.com\n");
Serial.println("Host: things.ubidots.com\n");
client.print(varString);
}

else

{
// if you didn't get a connection to the server:
Serial.println("connection failed");
}

boolean sta = client.connected();
Serial.println("Connection ["+String(sta)+"]");
if (!client.connected())
{
Serial.println();
Serial.println("disconnecting.");
client.stop();
}

Serial.println("Reading..");
while (client.available())
{
char c = client.read();
Serial.print(c);
}
client.flush();
client.stop();
}

Running the sketch and accessing the Ubidots dashboard we have:
 IoT cloud dashboard

Hopefully, by the end of this tutorial, you've gained the knowledge about reading data sensors and sending the values to the cloud.

Topics:
cloud ,ecosystem ,light ,project ,sensor ,tutorial ,platform

Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}