Sunday, February 7, 2021

Smart Home Gardening solution with IOT


Food is an essential part of day-to-day life. So with agriculture, we can cultivate the plant and made food for a living. We have a very long history of farming techniques and tools we are able to make development in agriculture.   The age of information technology can contribute new technology for farming solutions.

And the answer is yes, Using the Smart Farming solution we can enable IoT solution can be part of Agriculture development.

Why IoT solution?

With the Internet of Things solution, we can use the new technology solution in the farming field. 

1. Gather the weather information using the weather sensor in the farm environment.
2. Use an IoT sensor to collect and analyze get the health parameters from the soil.
3. Farm data analysis using Cloud Technology for precious agriculture.
4. Automation for the Irrigation system and fertilization via Drone.

There are a few solutions we can implement using the IoT concept. 
  • Checking garden weather using IoT sensors.
  • The moisture level of soil for smart irrigation.
So let's do a small interesting project where we will monitor the weather forecast for our gardening with IoT Platform. Then use some of the sensors to collect the moisture level of soil so that we can check the water requirement for the garden.

Hardware Required:

1. ESP8266 NodeMCU 
2. DHT sensor.
3. Moisture Sensor.

Software Required:

1. Arduino 
2. Adafruit IO dashboard tool

Introduction:

This is my setup where I have used a smart gardening solution using IoT.  Here we are using the embedded microcontroller to collect the weather and soil information using the attached sensor. This microcontroller is ESP8266 Nodemcu which runs on a small battery voltage of 5 V. So can be attached to the power bank with a supported voltage range.  

This setup will have one DHT sensor which will get the Temperature and Humidity date around the plant. and another Moisture sensor that is inserted into the soil will get the moisture level of the soil, which then used to indicate whether the plant needs water or not.




This sensor information can be collected by a microcontroller can be sent over to internet service, where we can monitor the garden's health using a mobile device from anywhere.


This is a dashboard from the Adafruit  IO service. IO - Smart Garden (adafruit.com) which will show the live feed information from the home garden.


So now let's being with how this has been done step by step procedure.


ESP8266 NodeMCU :

ESP8266 NodeMCU is a single-board microcontroller and low-cost open-source IoT platform. NodeMCU included firmware that runs on the ESP8266 Wi-Fi SoC from Espressif Systems. ESP8266 is a bite-sized and low const WiFi-enabled microcontroller, it can monitor and control things from anywhere in the world and it just perfect for just about any IoT project.


Specification of ESP866 NodeMCU: 


  • ESP-12E Chip Tensilica Xtensa® 32-bit LX106
  • 80 to 160 MHz Clock Freq.
  • 128kB internal RAM.
  • 4MB external flash.
  • 802.11b/g/n Wi-Fi transceiver ()







Power to the ESP8266 NodeMCU is supplied via the on-board MicroB USB connector. Alternatively, if you have a regulated 5V voltage source, the VIN pin can be used to directly supply the ESP8266 and its peripherals.




The ESP8266 NodeMCU has a total of 30 pins that interface it to the outside world. The connections are as follows:





Sketch Diagram:

The below diagram shows the complete circuit connection for setting up the project.




You can download the sketch from the My GitHub Repository.

Once the connection is established we can connect ESP8266 Nodemcu using the USB connection.



Setting ESP8266 NodeMCU configuration using Arduino:

You can use the Arduino software to connect and configure the ESP8266. Arduino is IDE for software development for the microcontroller, which will enable us to program and run the program on a microcontroller without an easy interface.

To configure and setup ESP8266 we need to download and install the Arduino software. Please download and install the Arduino software from the link Arduino Software.


Click on the Highlighted portion to download the window installer. Open the downloaded installer and install the Arduino software with system admin settings. Once you successfully install Arduino you will be able to open Arduino Application from the start menu.



Once you installed the IDE successfully open the Arduino program from the start menu. Connect your ESP8255 Nodemcu microcontroller board to PC using USB-Serail Port. The matching COM port will be shown in Ardunio SOftware when you connect the ESP8266 Nodemcu with the PC using USB-Serial Cable.

Now you need to install the ESP8266 Board Package. 
  • Go to File -> Preferences from the Arduino Software
  • Add link http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field in the Arduino preferences. 
With this, we are adding this new ESP8266 microcontroller board to Arduino IDE.


  • Open Board manager to install the ESP8266 package. [Tools --> Board (esp8266 Module) -> Board Manager]. Make sure your ESP8266 is connected to PC using USB cable.
  • Install ESP8266 Nodemcu library using the "esp8266" key in the search bar.



  • After the install process, you should see that the esp8266 package is marked INSTALLED. 

Now we need to install the Adafruit MQTT library which will help in connecting the ESP8266 device to the Adafruit IO service Platform. To install the library we need to open the library manager
  • Go to   Libary Manager. with path [Tools --> Manager Libraries -> Libary  Manager].
  • Install the "Adafruit MQTT Library" Library using the below search key "Adafruit MQTT Library" in the search bar.
  • Close the Boards Manager window once the Libary installed successfully.
Blink Test :


We'll begin with the simple blink test so it will make sure that our connection to the ESP8266 board is correct and working fine.
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
}
// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level
  // but actually the LED is on; this is because
  // it is active low on the ESP-01)
  delay(1000);                      // Wait for a second
  digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
  delay(2000);                      // Wait for two seconds (to demonstrate the active low LED)
}

Build and upload the code to the connected ESP8266 Microcontroller. 


Once successful upload ESP8266 will start blinking its LED with a delay of 1 sec.

Adafruit IO Service:

Now we have configured ESP8288 with Arduino software we are ready for the next step to create a service using the Adafruit IO service. Adafruit IO is the easiest way to get your projects onto the Internet of Things. 




Using the Adafruit IO service you can connect your device our the internet service. Adafruit IO uses MQTT (message queue telemetry transport) as a transport protocol for device communication that Adafruit IO supports. 
Create the account and user login credentials you can able to access the IO service by creating the dashboard which will display your sensor data feed with graphical representation.






Adafruit IO: Feeds

Feeds are the core of Adafruit IO which takes part in exacting data communication to and from Adafruit IO service. This feed data can be used to control the sensor device using a microcontroller device like ESP8266. Using an MQTT library or client you can publish and subscribe to a feed to send and receive feed data. you can read more from the Adafruit IO site on how to create, edit, and delete your very own feed. 



Adafruit IO : Dashboard

The second part is Dashboards allow you to visualize and control Adafruit IO connected projects from any modern web browser. Widgets such as charts, sliders, and buttons are available to help you quickly get your project up and running without the need for any custom code.
Click on New Dashboard to create your own dashboard to add the sensor feed for garden data.


Adafruit IO Key:

To use the Adafruit service over the internet we need a few connection details when establishing the connection to the Adafruit IO service. use the following details to connect an MQTT client to Adafruit IO:
  • Host: io.adafruit.com
  • Port: 1883 or 8883 (for SSL encrypted connection)
  • Username: your Adafruit account username (see the accounts.adafruit.com page here to find yours)
  • Password: your Adafruit IO key (click the AIO Key button on a dashboard to find the key)
Note: Your Adafruit IO Key should be kept in a safe place and treated with the same care as your Adafruit username and password.


Below are the parameters which need to be set in the Arduino source config code. 
  #define IO_USERNAME  "account_username"
  #define IO_KEY       "aio_key"


Programming the ESP8266 with sensor information:

#define WLAN_SSID "ssid" // use wifi ssid #define WLAN_PASS "password" // use wifi password
  • Also below parameters that need to be set in the Arduino source code from the Adafruit service. 
  #define IO_USERNAME  "account_username"
  #define IO_KEY       "aio_key"

That's it,  your ESP8266 is connected to a PC you can compile and upload the program to ESP8266 using a serial connection that is already connected using the USB port.

Once the program uploaded successfully you can remove the USB connection from the PC and connect to the supported power bank. So that you can use the device set up in the garden place.


Now insert your Moisture sensor into the soil near to plant so that it can read the data from the soil. This will help to read the correct moisture from the soil and send the data to ESP8266 more accurately.

Now device program will start collecting the sensor data from the both DHT sensor and Moisture sensor and send to the Adafruit service via MQTT protocol over the internet. 

You can go to the Adafruit dashboard to see the data is received from the garden.
https://io.adafruit.com/sonwaneganesh/dashboards/smart-garden




 This will show the weather and soil data directly from your garden continuously. which will help in monitor the plant health regularly basic. Additionally, we can trigger the water pump which can be installed with the irrigation system. Once a certain level of moisture level gets down the pump will start a water pump which will irrigate the garden automatically.




That's how we can use technology to grow plants smart way. 


 Happy Gardening!

Friday, February 5, 2021

Object Detection using Tensorflow Lite on Raspberry Pi

 



In this blog, we are going to use one of the object detection mechanism inside our raspberry pi.  Using Object Detection we can identify the different objects within the image or video we captured from the camera. 


Based on the identified object we can create different real-time applications like home security, traffic signals where we use security cameras to detect or find suspicious objects or incidents over the video feed depend on the feature you wanted to use in your product.

Before going to start the practical use case let's understand what is Object Detection and how it will help us to find the object identification using our small embedded device.

We are going to use Tesnroflow machine learning platforms inside our raspberry pi to detector the different objects using the live camera from the device.  Here we will be using TensorFlow, which is an end-to-end open-source machine learning platform. Tensorflow also provides an open-source library for machine learning which runs the Object Detection API to perform object detection and identified the object from the live camera feed. 

Basically,  These object detection mechanisms use Artificial intelligence technology (AI), which refers to the simulation of human intelligence in machines that are programmed to think like humans and mimic their actions.  

Artificial Intelligence applies machine learning, deep learning, and other techniques to solve actual problems in the fields of finance, healthcare, eCommerce, engineering services, and so on.  

AI can perform tasks that can be used in the fields of robotics, control systems, scheduling, data mining, logistics, speech recognition, facial recognition, and many others.



AI uses machine learning techniques to solve the problem by learning from experience and improves itself automatically through a previous experience. That is why machine learning is called the branch of Artificial Intelligence. There are many other techniques to use the AI for solving the real-time complex task which can not be solved using existing code or algorithm.

Now let's begin with the practical implementation for one of the small use cases of machine learning using Tensorflow. 

Introduction: 

This guide provides step-by-step instructions for how to set up TensorFlow’s Object Detection API on the Raspberry Pi. By following the steps in this guide, you will be able to use your Raspberry Pi to perform object detection on live video feeds from a web camera. 



What you will need:

The guide walks through the following steps to set up the TensorFlow Lite on Raspberry Pi:
  1. Connecting Pi Camera to Raspberry Pi
  2. Remote connection to Raspberry Pi using SSH.
  3. Update raspberry pi.
  4. Install the Python 3.7 (or latest version)
  5. Install the OpenCV for Image processing Libraries.
  6. Protocol Buffer Compiler Installation
  7. Installation of TensorFlow on Arm package.
  8. Set up TensorFlow Directory Structure and PYTHONPATH Variable.
  9. Download SSL_Lite TensorFlow model for Object Detection.
  10. Remote Desktop Connection using VNC Viewer.
  11. Running Object Detection Program
Connect Pi Camera to Raspberry Pi:

Now you can connect the Pi Camera module to Raspberry Pi using PIN interface 



Connecting to Raspberry Pi using SSH:



Here we will connect our Raspberry Pi using an SSH remote connection. Hope your Raspberry Pi is connected to your WiFi network so that we can access using SSH. For more detail regarding setup up the Raspberry Pi to Wife and accessing using SSH connection you can check my blog How to set up WiFi and enable SSH on your Raspberry Pi. Once your Raspberry Pi is connected to Wifi Network we can use the IP address of Raspberry Pi to connect from the Remote PC in the same network.

Use the SSH console to connect Raspberry Pi using valid credentials.
 



Once you connected using an SSH session you will be able to access the Raspberry Pi terminal to run and install the prerequired software for setting up TensorFlow in Pi. So we will go step by step process by giving the commands which will configure and set up the device for running TensorFlow.

Update Raspberry Pi :

The first step is to update the raspberry pi which helps the device to keep up to date with a list of available packages and their versions with recent upgrades available.  This will take time depending on the system's recent update from last use.
pi@raspberrypi:~ $ sudo apt-get update
pi@raspberrypi:~ $ sudo apt-get upgrade
After a successful upgrade, it will install the latest available packet from the list and keep the system up to date. Now let's create the workspace for TensorFlow by creating a separate folder so that we can keep all the required configuration and installation files in the same directory.

pi@raspberrypi:~ $ mkdire tensorflow


once you created the directory we can use this directory for installing and setting up the TensorFlow for object detection API. Let's start with the installation of required software and dependencies before going to TensorFlow installation.


Install Python 3.7 or the latest version:

sudo apt-get install python3

The command may take time-based on available and new installation time so wait until it finished the successful installation. Once it is installed you can verify the installation version by checking the below command

pi@raspberrypi:~ $ python3 --version
Python 3.7.3
Installation of OpenCV:

We also need the OpenCV package which is need by object detection script to grab the image and position of the identified object from the TensorFlow result. 

For the installation of the OpenCV, we need to install the first dependent libraries. Please run the below dependencies using the command line.

pi@raspberrypi:~ $sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
pi@raspberrypi:~ $sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
pi@raspberrypi:~ $sudo apt-get install libxvidcore-dev libx264-dev
pi@raspberrypi:~ $sudo apt-get install qt4-dev-tools libatlas-base-dev
Now you can install the OpenCV using the below command.

pi@raspberrypi:~ $pip3 install opencv-python


Once successfully installed we will use the OpenCV library while running the Object Detection python script.
Alright, now OpenCV is installed, The nest is Protocol buffer Compiler (Protobuf). 

Installation Protobuf Compiler:

TensorFlow Object Detection API uses the Protobuf to generate the Protobuf file. Protobuf us google package which can be installed using the below command inside the console with easy installation. You can find the detail for the same here with Protocol Buffer Compiler Installation.
pi@raspberrypi:~ $sudo apt-get install protobuf-compiler

Accept the installation process by giving permission. This will install the Protobuf compiler which will help in compiling the Tensorflow proto files to python files.  
 
After successful installation, you can check if the Protobuf compiler is installed in Raspberry Pi.

pi@raspberrypi:~/tensorflow $ protoc --version
libprotoc 3.14.0
Installation of TensorFlow Lite:

Next, we’ll install TensorFlow by using the PIP package installer with python. To install please run the below command on Raspberry Console. 
pi@raspberrypi:~/tensorflow $sudo pip3 install tensorflow
So it may take a while because it will automatically fetch the compatible package from the TensorFlow Repository and start the installation process. 


Also, you can install TensorFlow manually with a specific version of the Tensorflow package from the TensorFlow Repository.  Prebuild raspberry Pi compatible WHL Python package files are available at TensorFlow on Arm git repository TensorFlow Repository.

Get the available recent link for the WHL package which can be downloaded to the TensorFlow directory based on the Version and Raspberry Pi model available.



And get the link for a package from available TensorFlow releases.


Once you found out the correct release and package you can copy the link below to download the package to Raspberry Pi.


Use the below command to download the package to TensorFlow directory created in Raspberry Pi.

pi@raspberrypi:~/tensorflow $ wget https://github.com/lhelontra/tensorflow-on-arm/releases/download/v2.4.0/tensorflow-2.4.0-cp37-none-linux_armv7l.whl


It will start the download process into the local directory. This may take time to download based on your internet speed.


On successful, we can install the TensorFlow using the PIP package installer for python. So run the below command with the package file path in the current directory.

pi@raspberrypi:~/tensorflow $sudo pip3 install tensorflow-2.4.0-cp37-none-linux_armv7l.whl

This will start the installation process of the Tensorflow package so wait to finish it successfully. 

So after successful TensorFlow installation, It also needs the LibAtlas package and a few dependencies. Do it install it by issuing the following command. 

pi@raspberrypi:~/tensorflow $sudo apt-get install libatlas-base-dev
pi@raspberrypi:~/tensorflow $sudo pip3 install pillow lxml jupyter matplotlib cython
pi@raspberrypi:~/tensorflow $sudo apt-get install python-tk
This will finish the TensorFlow installation process successfully. Now we have done with the installation process, the next step is to configure and run the TensorFlow model to perform the Object Detection process.

Set up TensorFlow Model Directory Structure:

Now that we’ve installed all the packages, we need to set up the TensorFlow directory currently we are in.  So now we Download the TensorFlow Model GitHub repository by issuing the below git command.

pi@raspberrypi:~/tensorflow $git clone --depth 1 https://github.com/tensorflow/models.git
*Hope you have installed git software in your raspberry pi before using the above git command.




Once you download the Model it will download the directory structure describe in the TensorFlow Model GitHub repository.

Next, we need to modify the PYTHONPATH environment variable to point at some directories inside the TensorFlow repository we just downloaded. To export this variable using the Below command.

pi@raspberrypi:~/tensorflow $export PYTHONPATH=$PYTHONPATH:/home/pi/tensorflow/models/research:/home/pi/tensorflow/models/research/slim
The same path can be verified using the printing PYTHONPATH environment variable setting.

pi@raspberrypi:~/tensorflow $echo $PYTHONPATH
 
we can set this variable  PYTHONPATH  for permanent by adding it to the .bashrc file so that it will already set every time we open a new terminal. Once the setting has been done we can close and open a new terminal check for the same variable.

The next step is to use the Protobuf compiler which we have already installed to use for compiling the Protocol Buffer (.proto)  files in models/research path.

So go to the "models/research" directory and run the below command.

pi@raspberrypi:~/tensorflow $cd /home/pi/tensorflow/models/research
pi@raspberrypi:~/tensorflow/models/research $protoc object_detection/protos/*.proto --python_out=.



This command converts all the "name".proto files to "name_pb2".py files. you can check the generated python files inside the "models/research/object_detection/protos/" directory.



Next, move into the object_detection directory "models/research/object_detection/".
pi@raspberrypi:~/tensorflow $cd /home/pi/tensorflow/models/object_detection
Download SSL_Lite TensorFlow model for Object Detection: 

TensorFlow Object Detection Model Zoo provides the collection of pre-trained detection models from the database. for more information about you can check with GitHub repository TensorFlow 2 Detection Model Zoo. Then select the model which will suit your optimal for your device.

Please use the below model link to download the SSD Lite model to the current directory.

http://download.tensorflow.org/models/object_detection/ssdlite_mobilenet_v2_coco_2018_05_09.tar.gz


Once download extracts the tar file using the below command.
pi@raspberrypi:~/tensorflow/models/research/object_detection $tar -xzvf ssdlite_mobilenet_v2_coco_2018_05_09.tar.gz



Now the model ssdlite_mobilenet_v2_coco_2018_05_09 is in this directory and ready to be used.


Now download the Python program from my GitHub repository Object-Detector-using-Tensorflow-Lite-on-Raspberry-Pi.

Please find the link for the python program for object detection.

You can download the source into the object_detection directory.


Run Object Detection using Pi Camera:

Now everything setup we can start the program which can run on Pi to perform the real-time object detection using the Raspberry Pi Camera. 

To run the Object Detection program with a GUI interface you need to login to Desktop mode or VNC mode. This will provide the output terminal with a GUI interface. So make sure you are in Desktop mode and then run the Below program.
Please check how to connect using a VNC server to get into a Desktop mode with below my blog.

Once you can login using VNC you can able to connect to Raspberry Pi with Desktop mode.




Before starting the program make sure that the Camera Pi interface is enabled in Raspberry Pi.
This can be enabled in the Raspberry Pi configuration menu from the bar.



Go to Interface Menu and enable the camera button.




Now you open the terminal from the VNC viewer and go to the  object_detection directory.
pi@raspberrypi:~ $cd /home/pi/tensorflow/models/research/object_detection




Then run the python program to start the object detection using Pi Camere.

pi@raspberrypi:~/tensorflow/models/research/object_detection $python3 Object_Detection_picamera.py



This will open the Object Detector window with the Live camera feed. It may take a bit of time to wait until your new window open. 

This will show the screen captured by the Pi Camera along with the Detected Object using TensorFlow.




Here Algorithm can identify the Person and cellphone inside my screenshot taken while running object detection.

Running the TensorFlow object detection algorithm on Raspberry Pi will give you very slow performance due to low computation power. So Algorithm will only be able to process images with slow with 0.86 FPS. To improve the performance we can run the same algorithm using the accelerators like coral to enhance the detection performance.

 

We can use such a TensorFlow object detection mechanism in different smart applications like home monitoring, Traffic signal and Security uses to detect the intruder with a spying mechanism.

Hope you find interesting this blog. 

Sunday, May 3, 2020

Reading connected Sensor Data from Google IoT Cloud



In the last blog  Sending Room Temperature and Humidity Data to Google Cloud, we have sent the room temperature and humidity data to google cloud using the Google IoT core platform. This makes us complete our first steps of connecting to the Google IoT cloud and send the sensor data from the device to the cloud. Now next step is to read those data from the Google cloud environment and put it into useful information or any kind of application that we wanted to control using these sensor data.

Here basic use case will be if the temperature of the room goes beyond a certain degree we can able to turn on the cooling devices.

Here we will use the ESP8266 Nodemcu microcontroller to read the sensor data from Google Cloud.
https://en.wikipedia.org/wiki/NodeMCU

Let's begin with the following sequence :
  • Installation of Arduino and ESP8266 configuration.
  • Library Configuration for Google IoT Cloud 
  • Hardware setup configuration.
  • Source code
  •  Demo
Hardware Requirement:
  • ESP8266 NodeMCU 
  • FAN
  • Relay Board (1 channel relay Board )
  • Breadboard
  • Jumper wire
Software Requirement:
  • Arduino IDE.
  • Fritzing Software 
Programming Language:
  • C/C++
  • Python
  • OpenSSL Command line

Introduction:

ESP8266 NodeMCU :

ESP8266 NodeMCU is a single-board microcontroller and low-cost open-source IoT platform. NodeMCU included firmware that runs on the ESP8266 Wi-Fi SoC from Espressif Systems. ESP8266 is a bite-sized and low const WiFi-enabled microcontroller, it can monitor and control things from anywhere in the world and it just perfect for just about any IoT project.


Specification of ESP866 NodeMCU: 


  • ESP-12E Chip Tensilica Xtensa® 32-bit LX106
  • 80 to 160 MHz Clock Freq.
  • 128kB internal RAM.
  • 4MB external flash.
  • 802.11b/g/n Wi-Fi transceiver ()









Power to the ESP8266 NodeMCU is supplied via the on-board MicroB USB connector. Alternatively, if you have a regulated 5V voltage source, the VIN pin can be used to directly supply the ESP8266 and its peripherals.




The ESP8266 NodeMCU has a total of 30 pins that interface it to the outside world. The connections are as follows:



LCD Display with I2C Interface: 

This I2C 16x2 Arduino LCD Screen is using an I2C communication interface. It means it only needs 4 pins for the LCD display: VCC, GND, SDA, SCL. We Will use the LCD display to display the sensor data temperature and humidity taken via ESP8266 Nodemcu.


You can get this display device from online Roboelements.com.

Sketch Diagram:

The below diagram shows the complete circuit connection for setting up the project. The LCD display will display the temperature and humidity data which is collected using the ESP8266 Nodemcu using the I2C interface. This display information will provide the current temperature and humidity details in-room environment.








Relay is to control the FAN when the digital signal received from the ESP8266 Nodemcu. This trigger is generated based on the temperature data i.e. if the room temperature reached above the threshold value (like 30% above) then FAN will get ON to cool the room temperature.

In Previous blog Part 1: Sending Room Temperature and Humidity Data to Google Cloud we are able to send the DHT data from the Raspberry PI to Google IoT Core.



From the previous blog, we have already created Device Registry "iot-sensor",  pub/sub topic "iot_sensor_dht"  and subscription "pub_sub_iot_sensor_dht" which collects the sensor data from device "" using the MQTT protocol.

Now, same sensor data we need to retrieve from the Google Cloud and send to other LCD display device using the ESP8266 Nodemcu. To-Do so we need to connect our ESP8266 Nodemcu to Google Cloud and fetch the data from Google Cloud using the MQTT protocol.



In this section, we will create another device to store the sensor data for ESP display and then send this device data to ESP8266 using the MQTT over this device. To store the data inside this new device we all create the Cloud function which will read the data from the DHT device and send the related temperature and humidity data to the new ESP device using Cloud Pub/Sub mechanism. This cloud function will ensure that whenever DHT device sends the sensor data to the cloud it will trigger the cloud function to store the result data in the new ESP device 

To connect the ESP device to Google Cloud we need to generate the certificate which can be used to secure the communication between IoT core device to the ESP8266 Nodemcu program.

Objective:

  • Generate the Encryption certificates using the OpenSSL command line.
  • Create a Device topic for ESP8266 "iot_sensor_esp".
  • Create a Cloud Function to store data to a new Device topic. 
  • Logging Cloud function events.

Generate Encryption certificates:

To generate the certificate we will use the Google Cloud Console platform. use the OpenSSL utility to generate the ECC certificate using the below commands.

openssl ecparam -genkey -name prime256v1 -noout -out ec_private.pem
openssl ec -in ec_private.pem -pubout -out ec_public.pem

Now download the ec_private.pem and ec_public.pem into your local directory which can be used for configuring the Google IoT device and ESP8266 Nokdemcu.

Keep both certificates at a secure place to avoid the disclose your account settings.


Create a new device topic :


  • Go to google cloud IoT page in the Google cloud console (Link).
  • Then go to the "IoT Core" tab available in the left navigation bar.
  • Click on the created Device registry "iot_sensor"  and then click on Devices tab.
  • Click on "+ Create Device" under Devices Tab.



  • Create a device with Device Id "sensor_esp_display"  as display device for sensor data. 
  • The upload generated a Public key certificate in the Authentication Field using the upload option. Then click on the "create" button to finish the device creation. Keep the rest of the field as it is. 

Now you will be able to see the new "sensor_esp_display" device under the device tab. So we have successfully created the new display device for LCD.

Create a Cloud Function to store data to a new Device topic:

Cloud Functions is Google Cloud’s event-driven serverless compute platform. Run your code locally or in the cloud without having to provision servers. We will use this funciotn to read the device "sensor_dht" and send the temperature and humidity information to device "sensor_esp_display".

To add the cloud function follow the below steps:

  • Go to Google cloud console (Link).
  • Then go to the "Cloud Function" tab available in the left navigation bar.


  • Then click on the "Create Function" button in the Menu field.




  •  Then provide Name "function-esp-display" to cloud function and remaining field details as shown below figure.  


  • Then we will add source code from python Main.py (source code link) content from the git repo to the Source code portion. Thin function run by google cloud when device "sensor_dht" data is updated. 
  • Also, update the REQUIREMENTS.TXT with python dependency. 


  • Add the below environment variable and click on the deploy button.


After successful deployment, we will able to send the DHT sensor data to the ESP display device.


Now let's begin with reading the sensor data from google platform using the ESP8266 Nodemcu which then can control FAN and display devices using Arduino programming.


Installation of Arduino software:


To configure and setup ESP8266 we need to download and install the Arduino software. Please download and install the Arduino software from the link Arduino Software.


Click on the Highlighted portion to download the window installer. Open the downloaded installer and install the Arduino software with system admin settings. Once you successfully install Arduino you will be able to open Arduino Application from the start menu.



Once you installed the IDE successfully open the Arduino program from the start menu. Connect your ESP8255 Nodemcu microcontroller board to PC using USB-Serail Port. The matching COM port will be shown in Ardunio SOftware when you connect the ESP8266 Nodemcu with the PC using USB-Serial Cable.

Now you need to install the ESP8266 Board Package. 
  • Go to File -> Preferences from the Arduino Software
  • Add link http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field in the Arduino preferences. 
With this, we are adding this new ESP8266 microcontroller board to Arduino IDE.


  • Open Board manager to install the ESP8266 package. [Tools --> Board (esp8266 Module) -> Board Manager]. Make sure your ESP8266 is connected to PC using USB cable.
  • Install ESP8266 Nodemcu library using the "esp8266" key in the search bar.



  • After the install process, you should see that the esp8266 package is marked INSTALLED. 
Now we need to install the Google IoT Core library which will help in connecting the ESP8266 device to the Google Cloud IoT Platform. To install the library we need to open the library manager
  • Go to   Libary Manager. with path [Tools --> Manager Libraries -> Libary  Manager].
  • Install the "Google Cloud IoT Core JWT" Library using the below search key "Google iot cloud" in the search bar.

  • Close the Boards Manager window once the install process has completed.
Now libraries are installed we are ready to get the run source program to connect the Google IoT core and display sensor information to LCD display.

Coding:

  • Download the source code from my git repository Reading-Sensor-Data-from-Google-IoT-Cloud
  • Open Arduino file Google_IOT_Sensor_ESP.ino using Arduino Software.
  • Edit the file credentials.h with wifi credentials to connect your ESP8266 device to internet service using local wifi connect
  • Also, edit the Google Cloud project parameters which are created with Google IoT Console.

// WiFi parameters
#define WLAN_SSID       "ssid" // use wifi ssid
#define WLAN_PASS       "password" // use wifi password
#define PROJECT_ID  //"<Your Project ID>"
#define REGION  //"<Your Project's Region>"
#define REGISTRY  //""<Your Registry name>"
#define DEVICE  //"<Your device name>"
  • Now update the Certificate private key in which we have created with OpenSSL from Google Cloud shell Console.

  • Copy above private key content to the Arduino file "ciotc_config.hpp".
  • Once after updating the Project and Key details we build the code using Arduino IDE build command.
  • After Build successful Upload the code to connected ESP8266 NodeMCU using USB-serial cable. Arduino IDE will automatically upload the code to ESP8266. 
  • Then ESP8266 will reset itself and start running with uploaded code.
  • you can monitor the console log enabled in using connecting to serial monitor using Arduino IDE. 

Now Arduino code is running we can see that LCD display will show the temperature and Humidity data fetch from the DHT sensor connected to Raspberry Pi. Make sure the Raspberry Pi is running the python program which publishes the Sensor data to Cloud. (Please see the Previous Blog to make sure your DHT sensor is sending the data to Cloud.)

Logging Cloud function events: 

You can log the Cloud sensor data using the below command on Google Cloud shell console. Please run below command to check the data on device "sensor_esp_display".


(prefab-builder-267610)$ gcloud functions logs read function-esp-display

Let's see the Sensor Data from DHT connected from Raspberry Pi to LCD display connected ESP8266 Nodemcu using Google IoT.

Now you have successfully done all this so if you have successfully connected the sketch diagram correctly then you will be able to LCD will start showing the Sensor data temperature and Humidity.





I hope you like the this blog. Please do like, comment, and share this post to everyone who are interested in the IoT project. 






Smart Home Gardening solution with IOT

Food is an essential part of day-to-day life. So with agriculture, we can cultivate the plant and made food for a living. We have a very ...