Green House Monitoring/Alarm System With ESP8266 & Ubidots

by UbiMaker in Circuits > Electronics

5147 Views, 13 Favorites, 0 Comments

Green House Monitoring/Alarm System With ESP8266 & Ubidots

Untitled drawing (1).jpg

Is your green-thumb purple? If so, this is tutorial will prove essential in your next attempt to grow healthy plants. And if you are anything like me, changing temperatures, too much water, too little water, and these effects on the soil are all battles your plants have to fight, daily, and seemingly alone. With the advent and growing adoption of IoT monitoring systems, the plants fight for life can become a second thought as this GreenHouse Monitoring System with Intelligent Alarm is designed to help you grow your favorite herbs, flowers, or weeds – yielding more, by doing less, but “less” intelligently!
This plant monitoring system is exists with a user friendly design and setup and will incorporate historical records with Ubidots IoT Application Development Platform. Like other monitoring systems which only send and receive data to various clouds, this system will feature a simple to build and easy-to-use web-based dashboard display with programmable intelligent alarm notifications based on your plant’s health. The intelligent alarm system in this tutorial will use a Buzzer to notify horticulturist when a plants moisture has exceeded a desired saturation range. This intelligent alarm system will give your plants a voice to alert you when they are thirsty or if they are drowning. Now let’s get started...

Requirements

Step-By-Step

1. Hardware Setup

2. Firmware Setup

3. Ubidots Setup

4. Summary

Hardware Setup

HTB1BUP_NXXXXXc.XVXXq6xXFXXXI.jpg
ESP8266-NodeMCU-Pinout.png
Untitled1.png
Untitled2.png
Untitled3.png
Untitled drawing (2).jpg
20180703_090534.jpg

1. Begin by connecting your NodeMCU ESP8266 to the NodeMCU Base.

2. Connect the assembled device (shown above) with the micro USB cable to your computer's USB port to begin configuring the device.

Sensor Wiring and Casing

Soil Temperature/Moisture Sensor – SHT10

This digital sensor works with 3 or 5V logic and any SHT-1X code for a microcontroller will work. The sensor includes a 1 meter long extension and has four wires: Red = VCC (3-5VDC), Black or Green = Ground, Yellow = Clock, Blue = Data.

Wire setup - you will need to connect the 10K resistor from the Blue Data wire to the Red VCC wire. This ensures that the data line is at a defined logic level, and limits interference from electrical noise.

Board assembly- Following the table below, make the correct connections with the jumper wires between the SHT10 and the NodeMCU base. Remember that your Red and Blue wires will be connected via the resistor.

Light Sensor

This analog/digital sensor works with 3v logic and has 4 output pins – VCC+, GND, A0 (Analog Pin), D0 (Digital Pin).

Follow the table to make the correct connections with the jumper wires between the Light Sensor and the NodeMCU base. For this project the analog pin (A0) is being used and the digital pin (D0) suspended.

Buzzer

This sensor works with 3v logic and has three pins: VCC, GND, I/O (Data pin).

Follow the table to make the correct connections with the jumper wires between the Buzzer and the NodeMCU base.

After each sensor is properly connected to the NodeMCU base, assemble all the devices components/wires into a protective case. The final device for this project, without the transparent protective cover

Firmware Setup

1.png
2.png
3.png
j.png
k.png
l.png
m.png
plant1.png
plant2.png
plant3.png
Untitled1.png

The NodeMCU with the ESP8266 chipset makes use of the Arduino environment for programming and managing firmware. If you do not already have the most recent Arduino IDE installed, please download and install Arduino onto your computer prior to beginning the following Firmware setup.
1. We will begin our firmware setup by adding the ESP8266 package properties to the Arduino IDE. Open the Arduino IDE, and select File > Preferences and enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field.

IMPORTANT NOTE:
MAC users must install and upload this driver to your NodeMCU device.

Window users must install and upload this driver to your NodeMCU device.

2. Next, we will install the ESP8266 board modules to the Arduino IDE. From the toolbar, select Board > Boards Manager and install ESP8266 platform from the ESP8266 Community. To simply find the correct modules, search “ESP8266” within the search bar.

3. When found, install ESP8266, if not already installed, then close.

4. With the ESP8266 modules installed to the IDE, a variety of boards will be listed under the board manager to choose from – Reopen the toolbar and search for the NodeMCU 1.0 (ESP-12E module) board.

5. Also, to keep everything running fast and smooth, let's make sure the upload speed is optimized to 115200. Go to Tools > Upload Speed > 115200:

6. Next, we will be using MQTT to communicate our plants data with Ubidots. To ensure data communication, download Ubidots MQTT ESP Library and click the green "Clone or download" button to begin the "Download ZIP” process.

7. While in Github, also download the SH1X library to allow the sensors and NodeMCU board to understand one another. Click here to access the SH1X github and select the green button called "Clone or download" and select "Download ZIP". Note: The SH1X library is a general library which work with all the board supported by the Arduino IDE and not just for the NodeMCU board.

8. With the .ZIP libraries downloaded to your computer, let’s move back to the Arduino IDE and add the libraries to the hardware. With your Arduino IDE open, select Sketch > Include Library > Add .ZIP Library.

9. Select the .ZIP file of UbidotsMQTTESP and then “Accept” or “Choose”.

10. Repeat Step 8 above, this time add the .ZIP file of SH1X Master and then “Accept” or “Choose”.

11. With the libraries added, close and reboot the Arduino IDE.

Sending (POST) & Receiving (GET) Sensor Values to Ubidots

1. In the Arduino IDE, copy and paste the below sample code to POST your devices data to your Ubidots account. Be sure to update the TOKEN and WiFi credentials along the way.

Assign the following variables in the code below:

  • Your Ubidots TOKEN where indicated in the code:
#define TOKEN "............................." // Your Ubidots TOKEN
  • Your WIFINAME where indicated in the code:
#define WIFINAME "..................." //Your SSID
  • Your WIFIPASSWORD where indicated in the code:
#define WIFIPASS "...................." // Your Wifi Pass
  • Ubiclient.ubidotsSetBroker ("industrial.ubidots.com"). If you are using the Ubidots for education platform kindly change the endpoint to things.ubidots.com.

/****************************************   Include Libraries ******************************/

#include "UbidotsESPMQTT.h"

#include <SHT1x.h>

/****************************************   Define Constants ***********************************/#define TOKEN "............................." // Your Ubidots TOKEN
#define WIFINAME "..................." //Your SSID#
define WIFIPASS "...................." // Your Wifi Pass
/*****Not to be changed****/
#define DEVICE_LABEL "plantms"
#define VARIABLE_LABEL_1 "light-intensity"
#define VARIABLE_LABEL_2 "temperature"
#define VARIABLE_LABEL_3 "soilmoisture"
#define VARIABLE_LABEL_4 "buzzer"
#define DATAPIN D7
#define CLCKPIN D5
#define LIGHT_SENSOR A0
#define BUZZER D3
Ubidots ubiclient(TOKEN);
WiFiClient client;
SHT1x sht1x(DATAPIN, CLCKPIN);
/****************************************   Auxiliar Functions *********************************/void callback(char* topic, byte* payload, unsigned int length)
 {  Serial.print("Message arrived [");  
Serial.print(topic);  Serial.print("] ");  
for (int i = 0; i < length; i++) {   
 Serial.print((char)payload[i]);  }  
if ((char)payload[0] == '1') {    
digitalWrite(BUZZER, HIGH);  } 
 else {   
 digitalWrite(BUZZER, LOW); 
 }  Serial.println();}char* getUbidotsDevice(char* deviceLabel) 
{  char* data = (char *) malloc(sizeof(char) * 700);  char* response = (char *)
 malloc(sizeof(char) * 400); 
 sprintf(data, "GET /api/v1.6/devices/%s/", deviceLabel); 
 sprintf(data, "%s HTTP/1.1\r\n", data);
  sprintf(data, "%sHost: industrial.api.ubidots.com\r\nUser-Agent:esp8266/1.0\r\n", data);  
sprintf(data, "%sX-Auth-Token: %s\r\nConnection: close\r\n\r\n", data, TOKEN);
  if (client.connect("industrial.api.ubidots.com", 80))
 {    client.println(data); 
 } else {    return "e";  
}  free(data);  int timeout = 0;
  while (!client.available() && timeout < 5000) 
{    timeout++;    if (timeout >= 4999) {     
 return "e";    }   
 delay(1);  } 
 int i = 0;  
while (client.available()) {   
 response[i++] = (char)client.read(); 
   if (i >= 399) {      break;    } 
 }  char * pch;  char * statusCode;  
int j = 0;  pch = strtok (response, " "); 
 while (pch != NULL)
 {    if (j == 1 ) {      
statusCode = pch;    } 
   pch = strtok (NULL, " ");  
  j++;  } 
 free(response);  
return statusCode;}
/****************************************   Main Functions ****************************************/void setup() {  
// put your setup code here, to run once:  
pinMode(BUZZER, OUTPUT);  
ubiclient.ubidotsSetBroker("industrial.ubidots.com"); // Sets the broker properly for the business account  ubiclient.setDebug(true); 
// Pass a true or false bool value to activate debug messages  Serial.begin(115200);  ubiclient.wifiConnection(WIFINAME, WIFIPASS); 
 ubiclient.begin(callback);  if (!ubiclient.connected())
 {    ubiclient.reconnect();  }  
// Getting info of device  char* deviceStatus = getUbidotsDevice(DEVICE_LABEL);
  // verify if the device is created  if (strcmp(deviceStatus, "404") == 0) 
{    ubiclient.add(VARIABLE_LABEL_4, 0);
 //Insert your variable Labels and the value to be sent
    ubiclient.ubidotsPublish(DEVICE_LABEL);    
ubiclient.loop();  } 
 ubiclient.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL_4);
 //Insert the dataSource and Variable's Labels}void loop() {  
// put your main code here, to run repeatedly: 
 if (!ubiclient.connected()) {    ubiclient.reconnect();   
 ubiclient.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL_4);
 //Insert the dataSource and Variable's Labels  } 
 // Taking sensor readings  
int light_intesity_reading = analogRead(LIGHT_SENSOR);
  float tempc = sht1x.readTemperatureC(); 
 float soilmoisture = sht1x.readHumidity(); 
 // Publishing values to assigned variables 
 ubiclient.add(VARIABLE_LABEL_1, light_intesity_reading); 
 ubiclient.add(VARIABLE_LABEL_2, tempc);
  ubiclient.ubidotsPublish(DEVICE_LABEL);
  ubiclient.loop();  
ubiclient.add(VARIABLE_LABEL_3, soilmoisture);
  ubiclient.ubidotsPublish(DEVICE_LABEL); 
 ubiclient.loop();  
delay(1000);
}

2. With your code in the IDE and updated with WiFi and Security TOKEN, Upload the code with the”check-mark” icon and “right-arrow” icon found in the top-left of the IDE display.

3. When the code is complete with its upload, the following notification will report from the Arduino IDE.

4. To verify data is being sent from your device to Ubidots, select the com port tool. This tools is denoted by a “magnifying glass” icon located in the top-right corner of the IDE.

Ubidots Setup

6.png
7.png
8.png
a.png
b.png
n.png
o.png
p.png
Untitled.png
Untitled2.png
Untitled4.png
Untitled19.png
v.png
1.png
2.png
Untitle6.png
Untitled.png
Untitled1.png
Untitled3.png
Untitled4.png
Untitled5.png
Untitled7.png
Untitled8.png
Untitled9.png
Untitled10.png
Untitled11.png
Untitled.png
Untitled1.png
ezgif.com-crop (5).gif
soil.PNG

With Ubidots IoT Application Development platform you are the creator and administrator of your very own Apps. The first time your device sends its first dot to Ubidots, a new digital device in Ubidots’ user interface will be automatically created containing the variables that will be updated from the sample code.
In your Ubidots account you will find a new device with the name “plantms” short for Plant Monitoring System.

Click on this device to find the underlying variables of your sensors updating and displaying.

Intelligent Alarm System Setup

This intelligent alarm system features a buzzer alert that serves as a plants voice in your monitoring systems. The buzzer alert works as an alarm system that is triggered based on the increase/decrease of soil moisture content value above or below predetermined threshold. To create the intelligent alarm system, we will create the following events:

Event 1: Too Wet

When the "soil moisture" variable exceeds the threshold of 60, trigger the “buzzer” variable to become 1. Then the hardware will recognize, when variable “buzzer” is 1, the buzzer will sound.

Event 2: Good Condition

When the "soil moisture" variable is between the thresholds of 40-60, trigger the buzzer variable to become 0. Then our device will recognize, when variable buzzer is 0, then the buzzer will not sound.

Event 3: Too Dry

When the "soil moisture" variable falls below the threshold of 40, trigger the “buzzer” variable to become 1. Then the hardware will recognize, when variable “buzzer” is 1, then the buzzer will sound.

Event 4: Timer for buzzer sound

When the soil moisture for too wet/too dry the buzzer is activated. To avoid the headache of the buzzer, this event will also be programmed to switch off the buzzer after 1 minute of activation.

Event Setup

1. In your Ubidots account, select Device management > Events. Then, select CREATE EVENT or click the blue“+” icon in the top-right corner of your User Interface.

2. The events we will be creating will have the following logic:

  • Buzzer Notification for Too Wet
  • Variable Updated for Good Condition
  • Buzzer Notification for Too Dry
  • Timer for Buzzer sound

Buzzer Notification for Too Wet

At this point the intelligent notification system for our plants is partially complete. The next step is to configuring and assigning a notification when our plants in good condition. To complete the Buzzer Notification CREATE a New EVENT for good conditions as described below.

Variable Updated for Good Condition

Now the intelligent notification system for our plants is nearly complete. Let’s finish the job by configuring and assigning a notification when our plants too dry. To complete the Buzzer Notification for too dry. we are going to create another event with the following logic.

Buzzer Notification for Too Dry

The final step of our intelligent notification system for our plants is configuring the Timer for the Buzzer. To have the buzzer sounding while the soil is too wet or too dry is quite a headache. For this reason, we will add a final end to kill the buzzer sound after 1 minute. To complete the Timer for Buzzer, create one final event as shown below.

Timer for Buzzer sound

At this point the intelligent notification system for our plants is complete, resulting in the below 4 events.

Calibration Note: For the success of this tutorial, we simply used 60 and 40 as the high and low thresholds. Your plants will likely require different thresholds for too wet or too dry alarms. Below is a diagram to help set the event thresholds according to the type of soil being used. With this you can assign the following events according to the diagram above.

  • Event 1: Too Wet - At “Too Much Water”.
  • Event 2: Good Condition - At “Plant available water”.
  • Event 3: Too Dry - At “Unavailable Water”.

For example: If my soil is a loamy soil. I would set the “too wet” event to > 62, “good condition” event between 62 - 43 and “too dry” event < 43. Intelligent Alert System Expectations:

Going back to your device “plantms”. The “buzzer” variable should be 1 when the soil moisture is above 60 accompanied with a buzzer sound for 1 minute; 1 when the soil moisture is below 40 accompanied with a buzzer sound for 1 minute; and should be 0 between thresholds of 40 - 60 without any sound from the buzzer.

Dashboard Setup

Using a dashboard you can organise and present data to a user that can quickly be understood with the display of indicator, switches, graphs, and identifiers. To create your dashboard to look like ours, simply follow the below setup steps.

1. Create a Dashboard. To do this, from your Ubidots account, click on Device management > Dashboard > Create a new Dashboard.

2. Any name or identifying title can be assigned to the dashboard. As shown below, our sample dashboard is called PlantMS.

3. After the Dashboard is created follow the on screen instruction to Create the first Widget and begin visualizing and interacting with the data.

4. Our dashboard will contain a chart and control widget. To create the chart simply select: Chart widget > Line chart > Add Variable > then select temperature, soil moisture and light intensity. Click finish to complete the widget’s programming.

5. Next, create and Indicator Widget. This widget will alert you when a variable is registering as “1” or “0”. Create the widget using the following flow:

Indicator widget > On-Off > Add Variable > the buzzer (assigning 1 or 0 as “on” or “off” respectively), then click finish.

6. When complete you will be able to visualize and hear your data and the status of your precious plants.

Summary

Growing healthy plants is hard. Extreme temperatures, water availability, and changing soil conditions constantly beat up our precious plants. With this quick GreenHouse Monitoring System with Intelligent Alarm, your plants now have any ally in their fight for healthy growth and optimal conditions.
Other users also found useful:

1. Arduino Ide

2. Connect an ESP8266 NodeMCU 1.0 to Ubidots using MQTT

3. Types of Soil and Moisture content

4. Create Dashboards and Widget