Last time tried the energy-saving design of the liquid crystal display. This time we will add a thermometer and a hygrometer to monitor the temperature and humidity of the room.
The thermometer has been built in before. [Python] Obtain the room temperature from the temperature sensor with Arduino and display it on the screen (Part 1) [Python] Obtain the room temperature from the temperature sensor with Arduino and display it on the screen (Part 2)
However, an independent hygrometer was difficult. There were few materials and it was a little difficult for me, so This time I will try to lower the difficulty level a little.
There was a low-difficulty and popular temperature / humidity sensor, so I will try using it. Since the thermometer is a set, the previous thermometer will be stored and newly designed.
--Temperature / Humidity Sensor Module DHT11 --One 10kΩ resistor
This is the only material. Add it to the Arduino circuit you used last time. Complex designs are already built into the module, so it's easy to work with.
The page that I referred to this time Temperature and Humidity Measurement Using DHT11 --Arduino --Introduction to IoT from the Basics https://iot.keicode.com/arduino/temperature-dht11.php
DHT11 (light blue parts) in the above figure is upside down. Please note that it will be [GND] [NC] [DATA] [VDD] from the left.
The output is the same digital 8pin as the reference page. The orange and yellow wires are 5V and GND is black. The circuit diagram was simple, but the real circuit was a little crowded.
After this, I plan to add a barometer, but what will happen?
#include <DHT.h>
// Initializing DHT11 sensor
const int PIN_DHT = 8;
DHT dht(PIN_DHT, DHT11);
void setup() {
//start serial connection
Serial.begin(9600);
//configure pin A3 as an input and enable the internal resistor
pinMode(A3, INPUT);
// start DHT11 sensor
dht.begin();
}
void loop() {
// Whether to display in Fahrenheit
bool isFahrenheit = false;
// Get humidity and temperature
float percentHumidity = dht.readHumidity();
float temperature = dht.readTemperature(isFahrenheit);
// Do nothing if not available
if (isnan(percentHumidity) || isnan(temperature)) {
return;
}
// Get heat index
float heatIndex = dht.computeHeatIndex(
temperature,
percentHumidity,
isFahrenheit
);
//read the photocell value into a variable
int photocell = analogRead(A3);
//print out the sensor value in csv format
String s = "";
s += String(temperature, 1) + ",";
s += String(percentHumidity, 1) + ",";
s += String(heatIndex, 1) + ",";
s += String(photocell);
Serial.println(s);
//delay 2000 ms
delay(2000);
}
Most of the added code is for working with the DHT11 library.
I want to get the temperature in degrees Celsius, so I set bool isFahrenheit = false;
.
Also, since the processing was complicated, I made a loop at 2 second intervals. Just in case.
It reads from a digital 8pin and outputs it in CSV format including the value of the previous illuminance sensor.
DHT libraries can be added from the Arduino IDE's Library Manager. From the menu, select "Tools-> Manage Library".
DHT sensor library by Adafruit In the screenshot, I installed Ver.1.3.8.
Adafruit Unified Sensor by Adafruit It is a common library. At this point, Ver.1.1.2 is installed.
From this time, the sensor information will be written to the database.
Since multiple serial connections cannot be made at the same time, (There may be some way, but as a cheaper way) The program that writes to the database keeps running, and other programs share information by referencing the database.
$ mysql -u root -praspberry
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.1.38-MariaDB-0+deb9u1 Raspbian 9.0
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE sensor;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> CREATE USER 'sensorpi'@'localhost' IDENTIFIED BY 'raspberry';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON sensor.* TO 'sensorpi'@'localhost';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> USE mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> SELECT user, password, plugin FROM user;
+------------+-------------------------------------------+--------+
| user | password | plugin |
+------------+-------------------------------------------+--------+
| root | ***************************************** | |
| phpmyadmin | ***************************************** | |
| fxpi | ***************************************** | |
| sensorpi | ***************************************** | |
+------------+-------------------------------------------+--------+
4 rows in set (0.01 sec)
MariaDB [mysql]> QUIT;
Bye
pi@raspberrypi:~ $ mysql -u sensorpi -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 34
Server version: 10.1.38-MariaDB-0+deb9u1 Raspbian 9.0
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> USE sensor;
Database changed
MariaDB [sensor]> CREATE TABLE tbl_serval (time DATETIME,
-> max_temperature FLOAT, avg_temperature FLOAT, min_temperature FLOAT,
-> max_humidity FLOAT, avg_humidity FLOAT, min_humidity FLOAT,
-> max_heatIndex FLOAT, avg_heatIndex FLOAT, min_heatIndex FLOAT,
-> max_pressure INT, avg_pressure INT, min_pressure INT,
-> max_photocell INT, avg_photocell INT, min_photocell INT);
Query OK, 0 rows affected (0.05 sec)
item | Set value |
---|---|
Database | sensor |
user | sensorpi |
table | tbl_serval |
The structure of tbl_serval looks like this. Nulls are allowed for all columns, but we will review them soon.
serval is an abbreviation for SERial VALue.
Column name | Mold | NULL | Description |
---|---|---|---|
time | DATETIME | Tolerance | Recording time |
max_temperature | FLOAT | Tolerance | Temperature (maximum) |
avg_temperature | FLOAT | Tolerance | Temperature (average) |
min_temperature | FLOAT | Tolerance | Temperature (minimum) |
max_humidity | FLOAT | Tolerance | Humidity (maximum) |
avg_humidity | FLOAT | Tolerance | Humidity (average) |
min_humidity | FLOAT | Tolerance | Humidity (minimum) |
max_heatIndex | FLOAT | Tolerance | Feeling temperature (maximum) |
avg_heatIndex | FLOAT | Tolerance | Feeling temperature (average) |
min_heatIndex | FLOAT | Tolerance | Feeling temperature (minimum) |
max_pressure | INT | Tolerance | Atmospheric pressure (maximum) |
avg_pressure | INT | Tolerance | Atmospheric pressure (average) |
min_pressure | INT | Tolerance | Atmospheric pressure (minimum) |
max_photocell | INT | Tolerance | Illuminance (maximum) |
avg_photocell | INT | Tolerance | Illuminance (average) |
min_photocell | INT | Tolerance | Illuminance (minimum) |
I've fixed it here and there, so just an overview. For changes, if you are interested, GitHub (mySensor) & GitHub (Desktop Clock) Take a look at DesktopClock / commit / 437884c6f55cee7693addb0726ccf4ae81c04269).
--Receives sensor information (CSV format) every 2 seconds and divides --Calculate the maximum, average, and minimum values in 1 minute --Write to database --Changed the table clock program (DesktopClock) side from serial connection to DB connection --Get the latest value from the DB every minute and display it on the screen
It will be completed soon.I had a mechanism to turn off the backlight when the illuminance was 50 or less. After this change, even if the room was darkened, it did not disappear immediately, and after a while it turned off, and the reaction slowed down. Restarting the Python program will fix it, but after a short while it will recur.
As a result of investigating Iloilo, it seems that the cause was the receive buffer.
main.py
#Flush the receive buffer
self.ser.flushInput()
If you don't do this, you will not be able to synchronize with the serial connection. It seems that the range of delay increases with the passage of time.
I don't think it's a good idea, but it's probably because the delay has disappeared.