ESP8266 Temperature Measument and Alert System (DS18B20)
by im-pro in Circuits > Sensors
636 Views, 3 Favorites, 0 Comments
ESP8266 Temperature Measument and Alert System (DS18B20)
Motivation
The refrigerated warehouse of my father-in-law broke down and he had some expensive material stored there. He noticed the problem too late and he lost a lot of money.
To prevent this from happening again I wanted build a system that send an Email if something goes wrong and it should be reliable that you just can forget about it.
How does it work?
First of all we need a sensor that can measure the temperature. I decided to use an ESP8266 (ESP-01) and an digital sensor (DS18B20). The ESP8226 just connects to the local network and sends periodical temperature updates. The software is written in a way that it will recover form a lot of problem itself and therefore should work reliable.
The second part is a website with a cronjob running. Its will periodically check if the measured temperature is outside the specified limits and if so send an Email. Additionally if the sensor does not report a new temperature value for a specified time it will also send an Email.
If you run the website on an manged server there is a very low probability of a long downtime and therefore we have a good and robust system!
Whats do you need:
- Web server with PHP 7 and MYSQL 5.5 other version should also work but are not tested
- ESP8266 (ESP-01) or other ESPs + programmer
- DS18B20 or similar
- LM317 voltage regulator for the 3.3V
- USB socket or any other connector for power supply
- Some LEDs, Taster, Resistors, Capacitors, Wire ....
Web Server?
Webhoster!
You need to rent a web server. This hoster should support PHP 7, MYSQL 5.5 or higher and Cronjobs.
I can recommend all-inkl.com I use them for years and did not have any problems and roughly 5€/month is a good price.
There are also free ones or you can host your own website but we want to have an reliable service and therefor I would not recommend that.
Setup Web Server
Step 2.1: Download files
Download the files from github. Use the latest release and download "Source code (zip)".
Step 2.2: Copy files to the server
Upload the files in the folder "WEB" to your web server. This is normally done by FTP. Some web hosters also have tools like webftp. There you can use a website to upload the files. I personally use WinSCP.
The files must be in the root directory of you website.
Step 2.3: Create a database
Create a MYSQL database and make a note of the servername, username, password and database name.
Step 2.4: Settings.php
You need to create a Settings.php file in the root directory. Use the template file "Settings.templet.php", rename it to "Settings.php" and fill in the database login information:
//MYSQL settings define('server_sql',"SERVER-NAME"); define('dbname_sql',"DATABASE-NAME"); define('benutzer_sql',"USER-NAME"); define('passwort_sql',"PASSWORD");
You can also provide an email-address to get error reports if something goes wrong.
define('error_event_email','YOUR-EMAIL-ADDRESS');
For security you have to set 2 passwords:
//Sensor KEY define('sensor_key','CHOOSE-YOUR-KEY'); //Cornjob KEY define('cornjob_key','CHOOSE-YOUR-KEY');
One is for the sensor an the other one is for the cronjob. Use only letters, numbers, "-", "_" to be on the save side use at least 20 characters.
You can edit the file on your computer and just upload it again.
Step 2.5: Cronjob
For the continuous checking of the sensor data we need a cronjob. The most web hosters support this.
For all-inkl this can be found under "Tools"/"Cronjobs".
This service will open a link periodically. For this project I would recommend an interval of 10 minutes. The link will be structured as following:
YOUR_DOMAIN/php/?event=cornjob&key=YOUR-CRONJOB-PASSWORD
The called script will always have the following text in the end "ALL DONE". If this text is missing something went wrong. So you can use that for a filter to send an email. This is not really necessary but can for example report a database problem.
Step 2.6: First use
When you open the website for the first time you should get the following message:
Database was initialized! Username:admin Password:admin
If not you can check the javascript console of your browser for more information about the problem.
Step 2.7: Change password
Your administrator account password must be changed!
For that click on the face icon and type in "admin" as username and "admin" as password and click on "Login". Then click on the gear icon to open the settings and there you can choose "Change password".
Don't forget your new password. You need it for backups or to add new users.
Sensor
Step 3.2: Programming the ESP8266
There are a lot of tutorials how to do that.
I used the Arduino IDE with the ESP8266 community version 3.0.1.
The settings in the image worked for me for the ESP-01 module.
You also need to install the following LIBS:
- OneWire
- DallasTemperature
The Arduino sketch code can be downloaded here.
Step 3.1: Just wire everything together:
The LM317 is used to change the 5V supply form the USB connector to 3.3V for the ESP8266. You can use anything else as you have lying around.
One taster is for reset and the other is used to bring the ESP8266 into programming mode and also to enter the configuration mode.
In configuration mode the sensor is hosting a web interface to set the settings. Just connect to the WiFi access point and the website will open automatically.
I used 2 LEDs. The green one just indicates that power is present and the red on is used to indicate an error.
If an error happens the led will blink as many times as the error number is and then the ESP8266 will reset.
- Error Code 1: SPIFFS initialization fails => Maybe you used the wrong programming settings.
- Error Code 2: Configuration error => Switch to configuration mode to reset the settings.
- Error Code 3: Cannot connect to WiFi => Switch to configuration mode to reset the settings.
- Error Code 4: No Sensor found =>Check the temperature sensor connection.
- Error Code 5: Website not found => Switch to configuration mode to reset the settings.
- Error Code 6: Website PHP error => Check if the website is working.
- Error Code 7: Wrong sensor password => Switch to configuration mode to reset the settings.
- Error Code 8: MAC adress not valid => Check if the website is working.
- Error Code 9: Temperature not a number => Check if the website is working.
Step 3.3: But everything in an enclosure
You can 3D print a case like I did or just use what ever you can find fits.
Setup Sensor
Step 4.1: Configuration Mode
Press the mode button on the sensor for 5 seconds. The error LED should now blink rapidly.
Step 4.2: Connect to the WiFi Access Point
Search for the WiFi with the name "Sensor XX:XX:XX:XX:XX:XX" and connect to it on your phone or computer.
Also note down the MAC address "XX:XX:XX:XX:XX:XX" this is needed if you have multiple sensor and need to distinguish them.
The ESP is configured as an captive portal and the web page should open automatically. If not just open your browser and visit:
http://192.168.111.1
Step 4.3: Enter and save the configuration
You need to enter the WiFi SSID and the password.
Also the domain where your website is hosted (without http://) and the sensor key you choose in Step 2.4.
After you press "Submit and restart" the ESP will save the settings and restart.
Now your sensor should work.
If the red led starts blinking look again on Step 3.1 for more information.
Add User and Link Senor
Step 5.1: Login
For this you have to login as "admin" with the password you changed in step 2.7.
If you use the sensor for yourself and you don't want to have another user skip Step 5.2.
Step 5.2: Add user
Click on the gear icon and select "Add user".
Here you can enter a user name and a password for the new user.
Then click on "Create".
Step 5.2: Link User to Sensor
Click on the gear icon and select "Link User to Sensor".
First you have to select the user you want to link the Sensor to.
Then select the Sensor here the mac address is shown to distinguish different sensors.
Then click on "Link".
Setup Alert
Step 6.1: Login
For this you have to login as the user the senor is linked to.
Step 6.2: Select Sensor
Click on the gear icon and select "Sensor settings".
Select the senor you want to setup. Here again you can distinguish them by there mac address.
Step 6.3: Settings
Here you can set the following settings:
- Name: Here you can enter a Name for the Sensor.
- E-Mail: The Email address the alerts are send to.
- Min Temp.: The lower bound for the measured temperature. If it is undercut an alert will be send.
- Max Temp.: The upper bound for the measured temperature. If it is exceeded an alert will be send.
- Sensor Timeout: If there is no new measurement for the specified time an alert will be send.
- Email Timeout: If an alert was send the next one will only be send if the specified time has passed.
Then click on "Save" to save the settings.
Have Fun
Now everything is Setup!
Here are some additional Functions:
- Click on the gear icon and select "export" to export an .csv file with all the measurements
- As admin user click on the gear icon and select "Create Backup" to backup everything.
- The blue area around the line shows the minimum and maximum measured temperature.
- On the chart you can click and move the time around.
- On the chart you can zoom in and out and the data will be updated automatically.
If you miss something just comment and I will try to implement it.
You can also open an issue on github!