Raspberry Pieces and Arduino marriage: Everyone can DIY smart home monitoring system
"Open source hardware field has two big magic, Raspberry Piva and Arduino, if you have these two things, congratulations, then you can build a home monitoring system very simple. This article will guide you every Step, make sure you won't encounter problems.
introduce
First of all, there must be an engineer who will have questions, why should I use the Raspberry Piva and Arduino? The Raspberry Pie has a GPIO for simple Boolean mission (open or off) and read some low-end temperature sensors. Is this enough? no! For more complex systems, we need to use a microcontroller to complete heavy work, such as Arduino, which has an ADC (analog-to-digital converter), PWM (pulse width modulation) channel with multiple channels, and very accurate timing.
For example, if you want to measure the power consumption of your housing, you need a current transformer and a basic circuit that will output the voltage you can use using the ADC measurement. If you want to output something between 1 and 0, such as an LED like a breathing lamp, you can use the PWM output. Finally, if you need a very accurate device (such as a PID system, multiplexed LED array or control step motor), a microcontroller is required. In the examples of this article, we will use Arduino UNO as a microcontroller.
BOM and hardware original block diagram
For this item, you will need the following:
Arduino Uno (if you want, you can use different Arduino products)
Raspberry Party (same, any product of the Raspberry Part)
Raspberry Power (for the latest Raspberry Part 3B +, it is best to match an additional power supply)
SD card for Raspbian system installed (Raspbian Lite)
Sensors and other modules of Arduino (depending on what function you want to implement)
The basics of Arduino IDE, Raspbian, C / C ++, HTML, and JavaScript (in fact, just know C / C ++, it is enough.)
The hardware principle block diagram is not complicated, depending on the function you want to implement. In this example, I used temperature / humidity sensors and LEDs.
Arduino code
For simplicity, only how to read temperature and humidity from the DHT11 sensor and how to scintilize the LED.
The data is transmitted by the serial port with the baud rate of 9600. In the main function, we read the serial port and check the received content. If we receive a string "THL", it means that we need to send data from the sensor via the serial port. In order to make this function properly, we use the while loop and send data repeatedly until we receive "OK" to know the data has arrived. If we receive a string "LED", we just flash the LED. Here you can add any functions and strings you want - just remember to send data using "THL".
What you need to know is here we send data in JavaScript Object Notation (JSON). JavaScript Object Notation is an open source format for transmitting data objects. We use this because we can use JavaScript to easily use this data in the main.html file. To use it, we need a library named ArduinoJson, you can download here. Function "Send_Data ()" As the name suggests: it sends data through the serial port in JSON format. To add data, you only need to add a line in this function, as shown below:
Root ["" "" "" "" "] = function_that_return_data ();
"Function_THAT_RETURN_DATA ()" as follows:
INT function_that_return_data () {
Int data;
// Insert Code That Reads Data from a Sensor and Attributes The Value to Variable 'Data'
Return (DATA);
}
This is the full content of the Arduino section. Below you can see the exact code written in this special case with DHT11 sensors and LEDs.
#include "ArduinoJson.h" "
#include "" dht.h ""
DHT DHT; // Give Name to your DHT SENSOR
#define dht11_pin a0 // Pin for DHT11 DATA
#define led_pin a1 // Pin for LED
Void setup ()
{
Serial.begin (9600);
}
void loop ()
{
String Str;
Str = serial.readstring (); // read serial
Str.tolowercase (); // Convert to Lowercase
IF (str == "" THL ")
DO
{
Str = serial.readstring (); // read the Serial Again
Send_data (); // Call Send Data Function
} while (str! = "" ok "); // Continue to send data until we receive an" "ok" "
IF (str == "" led ") {
DigitalWrite (LED_PIN, HIGH); // Turn The Led On (High Is The Voltage Level)
DELAY (1000); // Wait for a second
DigitalWrite (LED_PIN, LOW); // Turn The LED OFF BY MAKING THE VOLTAGE LOW
}
}
void send_data ()
{
StaticjsonBuffer <200> JsonBuffer;
JsonObject & root = jsonbuffer.createObject ();
Root ["Temp"] = get_temperature ();
Root ["" "" ""] = get_humidity ();
ROOT.PRINTTO (SERIAL);
Serial.println ();
}
INT GET_TEMPERATURE () // Function That Return The Temperature As an integer
{
Int temperature;
DHT.READ11 (DHT11_PIN);
Temperature = DHT.TEMPERATURE;
Return (Temperature);
}
INT GET_HUMIDITY () // Function That Return The Temperature As an integer
{
Int humidity;
DHT.READ11 (DHT11_PIN);
Humidity = DHT.HUMIDITY;
Return (Humidity);
}
Set web server on the Raspberry
We will use Raspberry as a web server using NGINX, the following is the process of completing this step and other components required (such as PHP).
Here first explains some basic knowledge. First, you need to install the RaspBian system on the Raspberry, you can connect the keyboard and the display to the Raspberry Pie and power up, or skilled friends can also operate by SSH. Regardless of which method is used, please remember that all commands need to run as root, otherwise you will receive a permissions error, which can be done by writing "sudo" in front. Let's go! ! !
First, update the repository and packages. If you are not familiar with Linux, then I tell you that its role is to update the location of the installation package (application), then update them.
Sudo Apt-Get Update
Sudo Apt-Get Upgrade
Install Nginx, PHP, and Git. When inquiry, enter "Y" and press Enter.
Sudo Apt-Get Install Nginx PHP5-FPM Git
Then you need to change the default nginx directory and use it with PHP.
CD / ETC / Nginx
Sudo nano sites-enabled / default
Here is the code after the modification is complete.
Server {
Listen 80; ## listen for ipv4; this line is default and import
Listen [::]: 80 Default_server ipv6only = on; ## listen for ipv6
Root / var / wwww;
Index index.html index.php;
# Make Site Accessible from http: // localhost /
Server_name_;
Location / {
Index index.html index.php;
# First Attempt to Serve Request As File, THEN As
# Directory, The Fall Back to The CMS.
TRY_FILES $ URI $ URI / INDEX.HTML;
}
Location / doC / {
Alias / usr / share / doc /;
AutoIndex ON;
Allow 127.0.0.1;
Allow :: 1;
Deny all;
}
Location ~ \ .php $ {
Fastcgi_split_path_info ^ (. + \. PHP) (/.+) $;
Fastcgi_pass unix: /var/run/php5-fpm.sock;
Fastcgi_index index.php;
INCLUDE FASTCGI.CONF;
}
}
Modify the restart of Nginx.
Sudo Service Nginx Restart
To test is everything is normal, you can create a PHP test page.
CD / VAR / WWW
Sudo nano test.php
And put it in a Test.php file:
PHP
PHPINFO ();
?>
It is now positioned to raspberry-pi-ip-address/ test.php (for example: 192.168.0.2/test.php). If you see a page containing PHP information, as shown below, then it works normally and continues the next step.
If it doesn't work, read this section again and check if you are doing wrong.
The final step is to enter this code to allow the user to access the serial port in WWW-data (users used by Nginx).
Sudo Usermod -a -g Dialout WWW-DATA
After that, restart it again.
Website file
It is actually almost completed here. Now just download the file to the Raspberry Party and edit as needed.
You can download the file manually and put them in / var / www, or you can perform the following command and automatically download them from Github:
CD / VAR / WWW
Sudo git clone https://github.com/alexonaci/paranoid/tree/aac
With these files, let's take a look at what is it.
/ IMG folder: Background image with thumbnail and index page
Arduinocode.ino: contains files with Arduino Sketch
PHPSERIAL.PHP: PHP library, allow us to communicate with UNOs using PHP
Style.css: CSS on the page
Main.html: the most important page containing JavaScript, buttons, and visual data
RELAY.PHP: Start file with Arduino
Here will explain how to add a project. To add a new button, simply add to the down: