WiFi Sensors, Relays, etc ...
Introduction
This page describes my experiments with WiFi Sensors based on ESP8266 modules and some other home automation projects.
ESP8266 and sensors
I use ESP-07 and ESP-12E modules and simple universal board. For measuring I use following sensor types:
- Dallas DS18B20 for temperature
- Bosch BMP180 for temperature and barometric pressure
- BH1750 for light
- Si7021 for humidity and temperature
- CSS811 for indoor air quality
ESP8266 has pins Reset and GPIO16 connected and deep sleep functionality is used. Sensors measure values every 5 minutes and sleeps between measuring.
Powering
Current that draws ESP8266 depends on mode (STA vs. AP) and from state. Critical is boot, firmware flash and WiFi operations. See more info about it. With insufficient power supply you may have problems: unexpected reboots, error messages to serial line after failed boot, etc. Also account reserve for powering your sensors.
Typical terminal message after ESP8266 boot failure |
I have tried following powering methods:
- 2x AA battery Deep sleep functionality allows live with two Alkaline AA batteries more than one month, if you use DS18B20 only. In my experience, that modules works with voltage about 2.8 V correctly.
- XC6206P332PR This Fixed LDO Voltage Regulator is part of white PCBs that you can buy on Chinese shops. It's able to serve max. 200 mA, which is insufficient for reliable work of ESP8266. I can't recommend it !
- AMS1117 With max. output current 1 A* it's suitable for ESP8266 powering (* Really less, calculate dissipation from voltage drop * current !). Note: don't use unreliable pin's in production, rather solder In/Out wires to PCB.
- LM3940 is a 1 A low-dropout regulator. Input Voltage range: 4.5 ÷ 5.5 V. Best 3.3 V linear regulator, I've used. Don't forget to solder blocking capacitors as described in datasheet.
- POLOLU-2842 Step down converter; Uin: 3.4 ÷ 36 V; Uout: 3.3 V; 500 mA; I use it with Li-Ion batteries. Very good experience.
XC6206P332PR |
AMS1117 |
LM3940 |
POLOLU-2842 |
Measuring battery
I use two Li-Ion batteries in series for powering sensors (with POLOLU-2842) and ADC input for measuring battery voltage. Note, that ADC in ESP8266 has range cca 0.2 ÷ 1.2 V.
Source code
Here is source code, developed using Arduino IDE. It's common for my all sensors and particular one must be enabled by uncommenting some lines at top of code.
ESP8266 programming
For programming, I use CP2104 USB UART and Level shifter for 5 V ⇄ 3.3 V conversion.
IOT database
I store measured data to database. Database model is very simple and self explained, I hope:
Scripts for initial populate of DB tables are located in project IotDbScripts
. Here is crucial parts:
create_tasbles.sql | populate.sql
IotDatabase
IotDatabase is ORM layer between DB tables and Java. It's use
EclipseLink
as ORM engine. For each database table, there is mapping class - Person
, SensorType
, Sensor
, etc.
Also service layer for obvious tasks is created.
IotService
IotService is Java EE application based on:
- Jersey - JAVA EE JAX-RS API
- Jersey MVC module - JAVA EE MVC implementation
IotService is deployed on Apache Tomcat server, and this service has following purposes:
InsertSensorValueService
This JAX-RS Service is responsible for storing data from sensors, and for sending alerts. Everytime sensor measures value, WiFi connection is established and following HTTP GET request with sensor code and measured value is sent to IotService:
http://server:8080/IotService/rest/insert-value/LV_TEMP_02/20.07
Request contains sensor code and measured value.
When measured value is under/over defined alert, email message is sent, like this:
GetChartDataController
This MVC Controller generates web page with chart. It loads values and create desired averages. Controller stores JSON data for chart to MVC Model. In next phase, processing is redirect to JSP page (MVC View). Google charts API is used for chart rendering.
Other Services
I have developed some other JAX-RS Services for getting data in JSON and other things.
Base station
For displaying measured values without PC or tablet I made simple station with Arduino LEONARDO ETH and 20*4 LCD display. It's housed in old box with power supply. There are five front panel buttons, that I use for choosing values to be displayed. Here is source code, developed using Arduino IDE.
LEONARDO ETH |
Base station |
Base station + Old power supply |
Greenhouse temperatures |
Humidity values |
Testboard values |
Expand board |
Expand board with DS1307 RTC |
Relays driven by exp. board |
Relays
Base station contains two power relays to control 230 V outlets. Required state of relays is stored to database tables and base station check this using JAX-RS service and set desired state. For driving relays there is expand board with PCF8574, the I²C 8-bit expander and ULN2803A.
Basen pump controller
One of two relays I use for switching basen pump ON/OFF to give maximum efficiency of solar panels. The system consists of the following parts:
- ESP8266 station with two water-proof DS18B20 for measuring temperatures of basen water and solar panel. It sending values to IOT service.
- Base station periodically get desired states of relays form IOT service and sets them.
- On IOT service is running
BasenPumpControllerJob
that analyses temperature trend on solar panel and other conditions. It sets relay states to control basen pump.
When pump control works correctly, temperature of solar panel has typical sawtooth shape, water in basen temperature rises gently.
Measuring indoor Air quality
The CCS811 Air Quality Breakout is a digital gas sensor with I²C bus. I tried it with ESP8266 but it not works for me. So I connected it to Fishino UNO and made simple measuring station. There is also Si7021 for measuring temperature and relative humidity. Both sensors operates at 3.3 Volts, so there is Level shifter for 5 V ⇄ 3.3 V conversion on I²C bus.
Fishino UNO with LCD |
AQ station init. screen |
AQ station - values |
Sensors |
Moderate values |
Display page 2 |
Display page 3 |
Display page 4 |
Station displays sensors values on LCD and sends it to IOT service. There is also RGB status LED and 4-buttons keyboard for settings various modes. Here is source code, developed using Arduino IDE.
Conclusion
At this time, I have six ESP modules with various sensors in gardenhouse, greenhouse and outdoor. Server side service is under daily development, but if somebody is interested about source code, let me mail, it's nothing secret.