Introduction: UCL - IIOT Greenhouse
This project is an extension of our earlier project with the Greenhouse (https://www.instructables.com/id/EAL-EMBEDDED-GREE...).
In this project we added a database, where we log all our data and then visualize it with node-red for a greater overview.
The content we log in our database is Humidity, Temperature and moisture of the soil, which is shown in different diagrams.
Besides dataloggin we are also able to control which profile is active in the Greenhouse and remotely control it.
Then we are also able to control the pump and fan manually.
Step 1: Installation Guide
First step is to install all the different components.
Inside the brackets (), we have listed where the component is connected to. So for example, Arduino is connected to Raspberry Pi via a USB cable.
- Arduino (Raspberry Pi)
- Raspberry Pi 3 B+
- Soil hygrometer (Arduino)
- DHT11 sensor (Arduino)
- HG-320 Submersible Water Pump (Relay)
- 5V relay (Arduino)
- A computer fan (Relay)
- 230V power supply (Pump)
- Raspbian (OS for Raspberry Pi)
- Arduino IDE
- Python (Raspberry Pi)
- Node-Red (Raspberry Pi)
- MySQL server (freemysqlhosting.net)
First you will have to connect the hardware components, so follow this guide to build the greenhouse: Install Guide.
Then you will have to install Raspbian OS on your Raspberry Pi. After that you will have to install Python, and then install the python libraries.
Next step is to install Node-Red on the Raspberry Pi, and then navigate to the pallette manager and install the modules stated earlier.
Then go to this site Free MySQL Server and create a free MySQL server.
When all of this is done, you are ready to transfer the python script to your Raspberry Pi, import the Node-Red script and upload the code for the Arduino.
Step 2: Showcase of Control
Step 3: List of Parts/software Used in the Project
We've used the following technology to make the Greenhouse
- Raspberry Pi
Step 4: I/0 List
Step 5: Wiring Diagram
Step 6: The Arduino Code
The Arduino code works by printing the data, measured by the sensors, to the serial connection where it is read by the Raspberry Pi and transferred to the database.
The Arduino also have some digital inputpins connected to the Raspberry Pi that the Arduino reads and if one of the three becomes HIGH the profile will change because of an IF statement.
Also we have upgraded the code to use Millis instead of delay that enables the buttoms and rest of the code to be read all the time instead of an interval by the old delay.
Step 7: Raspberry Pi 3 B+
We used a Raspberry Pi 3 B+ for connecting our Arduino with the internet and a MySQL database. This made it possible for us to store data from our sensors and make a visual interface for the end user. For the user interface we used Node-Red with the Dashboard palette.
But before we could show our sensor data on Node-Red, we needed a way to upload the data on a MySQL database, and for that we made a Python script that would run on our Raspberry Pi.
Step 8: Python
The Python script is used for receiving data from the serial-communication coming from the Arduino. The script then sends the data to a MySQL database.
We used two libraries, pyserial and mysqlclient.
So first step would be to download these two libraries:
PySerial is used for collecting data from the Arduino via serial-communication.
device = '/dev/ttyUSB0' arduino = serial.Serial(device, 9600)
The first line are being used for defining our COM-port. On the Raspberry Pi it is /dev/ttyUSB0, that we use for the Arduino. The second line is for opening the serial port to the Arduino. We just define which COM-port and at what speed the connection is running at.
The rest of the code is running in a while loop.
Next we use multiple Try and Except blocks. First the code is trying to run inside the Try block, if that fails then it runs the Except block. But if the Try block runs fine, it doesn’t run the Except block, it just runs the rest of the code.
So inside the Try blocks we have code that will read the serial-communication and then send it off to our MySQL database.
hygrolist = arduino.readlines(1) templist = arduino.readlines(2)<br>humidlist = arduino.readlines(3)
So the above code is for reading lines in the serial-communication. The number in the end of the code is defining the line that has been read in the serial. So these lines are being categorized in different variables.
When data from the Arduino is received, we used the mysqlclient module for sending the data to our MySQL Server.
<p>db = _mysql.connect(host="sql7.freemysqlhosting.net",user="sql7256552",passwd="3ebtbP8FQ2",db="sql7256552")</p>
This line is for connecting to our MySQL database. It specifies the server, username, password and which database it should connect to inside the server. Here you should specify the connection to your MySQL DB.
<p>db.query("INSERT<br>INTO `TempHumid`(`temp`,`humid`,`hygro`) VALUES (%s,%s,%s)" % (temp,humid,hygro))</p>
So here we take our DB connection and make an SQL query. The query says that values has to be inserted inside the table “TempHumid” and then into the columns “temp”, “humid” and “hygro”. The last part “(%s,%s,%s)” is string formatting and is used to give the database the a format that it can read.
And all this action is put into a while loop, so that we keep getting data sent to the MySQL server.
If you want to see all the code, download the python script (TempHumid.py).
Step 9: MySQL
For the MySQL server, we used a free service on www.freemysqlhosting.net. We could have made a server locally on the Raspberry Pi, but we went with the free service to make it fully connected to the cloud/internet.
To access your MySQL, you have to go to phpmyadmin.co and login with the credentials from your freemysqlhosting account.
When you are inside, you have to create a table called "TempHumid", inside this table you have to create 4 columns called, "ID", "temp", "humid" and "hygro". The first column (ID) you have to tick the box A_I (Auto Increment). This is so that the ID column gives every dataset an ID. All the following columns have to be set as an INT (integer), and set the standard value to NULL.
Step 10: Node-Red
In our project we used Node-Red for making a graphical interface. Node-Red is running on the Raspberry Pi and is collecting data from our MySQL database and is showing these data with doughnut shaped gauges and graphical charts, so that the end user can monitor the data. The smart thing about Node-Red is that it is viewable on any device, which means that the site will be resized for the given device that is viewing the content.
To install our Node-Red programming, look at Step 1 and download the document called "Node-Red.docx". Then copy and paste the text into Node-Red via the import function in the upper right corner.
After that change the DB settings for your MySQL DB.
4 years ago
Nice design. Thanks for sharing.