In this project I will show how to use the NXP Rapid IoT device to create a Smart Thermostat with data logging capabilities.
The Smart Thermostat will measure the temperature, relative humidity, air pressure and ambient light. The sensor reading will be broadcast over Bluetooth Low Energy. A Raspberry will be used for data logging and visualization, and as well for controlling the heating system.
Such a Smart Thermostat can be placed in each room of a house:
With this approach the desired temperature and humidity can be set separately for each room.
The desired temperature and humidity would be either set directly from the Thermostat or from a mobile app.
Getting StartedThe Getting Started Video series provided by NXP are a good starting point to get familiar with the Rapid IoT device and development environment.
I also took a look on the Rapid IoT Studio examples. They are great way to figure out how to use different components.
Rapid IoT Studio ProjectTo create the Thermostat, first we need to create a Rapid IoT Studio project with the following components:
- 1 x Interval - 1s
- 1 x TSL2572 Ambient Light sensor
- 1 x ENS210 Temperature and Humidity sensor
- 1 x MPL3115 Pressure sensor
- 4 x BLE attributes - for ambient light, temperature, humidity and air pressure
- 4 x Display Screens - for ambient light, temperature, humidity and air pressure
- 4 x Functions - to format the numeric values displayed on the screen
- 1 x SX9500 Touch sensor
- 1 x EmbededPageController - for navigation between the pages - connected to the left and right touch buttons
- 1 x EmbeddedNxpRpkBacklight - for controlling the back light
- 2 x Functions - BrightnessUp, BrightnessDown to control the back-light - connected to the top and bottom touch buttons
The 3 sensors (ambient light, temperature & humidity and air pressure) are queried at each second.
The values read are published on 4 Bluetooth Low Energy Characteristics.
The sensor values are also displayed on the screen on different pages. The touch sensor is used to navigate between the pages.
The brightness of the screen is controlled with the top and bottom touch buttons. The current brightness value is stored in a backLightLevel
variable, while to two functions (BrightnessUp
, BrightnessDown
) connected to the Up/Down Pressed triggers of the touch controller are used to increase or decrease the brightness:
backlight_level_t backLightLevel = BLIGHT_LEVEL_HIGH;
ATMO_Status_t BrightnessUp_trigger(ATMO_Value_t *in, ATMO_Value_t *out) {
switch (backLightLevel) {
case BLIGHT_LEVEL_HIGH:
case BLIGHT_LEVEL_MEDIUM:
backLightLevel = BLIGHT_LEVEL_HIGH;
break;
case BLIGHT_LEVEL_LOW:
backLightLevel = BLIGHT_LEVEL_MEDIUM;
break;
case BLIGHT_LEVEL_OFF:
backLightLevel = BLIGHT_LEVEL_LOW;
break;
}
Backlight_SetLevel(backLightLevel);
return ATMO_Status_Success;
}
ATMO_Status_t BrightnessDown_trigger(ATMO_Value_t *in, ATMO_Value_t *out) {
switch (backLightLevel) {
case BLIGHT_LEVEL_HIGH:
backLightLevel = BLIGHT_LEVEL_MEDIUM;
break;
case BLIGHT_LEVEL_MEDIUM:
backLightLevel = BLIGHT_LEVEL_LOW;
break;
case BLIGHT_LEVEL_LOW:
case BLIGHT_LEVEL_OFF:
backLightLevel = BLIGHT_LEVEL_OFF;
break;
}
Backlight_SetLevel(backLightLevel);
return ATMO_Status_Success;
}
(See the full project file in the attachments)
Rapid IoT Mobile AppWe can also create a mobile project. I haven’t had luck with this. I tried to create a simple app to show the sensor values, but it just fails at provisioning the BLE device / app:
Prerequisites: Raspberry Pi 3 B / 3 B+, Raspbian GNU/Linux 9 (stretch) with the latest updates installed.
We will use the built in Bluetooth Low Energy of the Raspberry Pi 3, to read the sensor values published over BLE.
First we can use the bluetoothctl
to see if we can detect and pair with the NXP Rapid IoT device:
pi@raspberrypi:~ $ bluetoothctl
[NEW] Controller B8:27:EB:62:0D:EA raspberrypi [default]
[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:62:0D:EA Discovering: yes
[NEW] Device 00:60:37:0A:AF:5F PAF5F
[CHG] Device 00:60:37:0A:AF:5F RSSI: -67 \- this is MAC address of the Rapid IoT device
[bluetooth]# pair 00:60:37:0A:AF:5F
Attempting to pair with 00:60:37:0A:AF:5F
[CHG] Device 00:60:37:0A:AF:5F Connected: yes
[NEW] Primary Service
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service0008
00001801-0000-1000-8000-00805f9b34fb
Generic Attribute Profile
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service0008/char0009
00002a05-0000-1000-8000-00805f9b34fb
Service Changed
[NEW] Descriptor
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service0008/char0009/desc000b
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[NEW] Primary Service
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service000c
aa386520-826c-c0cd-accf-40096d5876de
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service000c/char000d
aa386521-826c-c0cd-accf-40096d5876de
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service000c/char000f
aa386522-826c-c0cd-accf-40096d5876de
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service000c/char0011
aa386523-826c-c0cd-accf-40096d5876de
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service000c/char0013
aa386524-826c-c0cd-accf-40096d5876de
Vendor specific
[NEW] Descriptor
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service000c/char0013/desc0015
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[NEW] Primary Service
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service0016
01ff5550-ba5e-f4ee-5ca1-eb1e5e4b1ce0
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service0016/char0017
01ff5551-ba5e-f4ee-5ca1-eb1e5e4b1ce0
Vendor specific
[NEW] Descriptor
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service0016/char0017/desc0019
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[NEW] Primary Service
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001a
bfe433cf-6b5e-4368-abab-b0a59666a402
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001a/char001b
bfe433cf-6b5f-4368-abab-b0a59666a402
Vendor specific
[NEW] Descriptor
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001a/char001b/desc001d
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[NEW] Primary Service
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e
647b1c04-ce8f-4e1f-aea6-09e28d875c51
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char001f
647b1c04-ce8f-4e1f-aea6-09e28d875c52
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char0021
647b1c04-ce8f-4e1f-aea6-09e28d875c53
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char0023
647b1c04-ce8f-4e1f-aea6-09e28d875c54
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char0025
647b1c04-ce8f-4e1f-aea6-09e28d875c55
Vendor specific
[CHG] Device 00:60:37:0A:AF:5F UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Device 00:60:37:0A:AF:5F UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device 00:60:37:0A:AF:5F UUIDs: 01ff5550-ba5e-f4ee-5ca1-eb1e5e4b1ce0
[CHG] Device 00:60:37:0A:AF:5F UUIDs: 647b1c04-ce8f-4e1f-aea6-09e28d875c51
[CHG] Device 00:60:37:0A:AF:5F UUIDs: aa386520-826c-c0cd-accf-40096d5876de
[CHG] Device 00:60:37:0A:AF:5F UUIDs: bfe433cf-6b5e-4368-abab-b0a59666a402
[CHG] Device 00:60:37:0A:AF:5F ServicesResolved: yes
[CHG] Device 00:60:37:0A:AF:5F Name: Atmosphere Project
[CHG] Device 00:60:37:0A:AF:5F Alias: Atmosphere ProjectFailed to pair: org.bluez.Error.AuthenticationCanceled
[PAF5F]# pair 00:60:37:0A:AF:5F
Attempting to pair with 00:60:37:0A:AF:5F
[CHG] Device 00:60:37:0A:AF:5F Paired: yes
Pairing successful
The pairing was successful and a list of BLE services, descriptors and characteristics was shown. The last for 4 characteristics are the BLE characteristics defined in the Rapid IoT Studio, for ambient light, temperature, humidity and pressure:
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char001f
647b1c04-ce8f-4e1f-aea6-09e28d875c52
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char0021
647b1c04-ce8f-4e1f-aea6-09e28d875c53
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char0023
647b1c04-ce8f-4e1f-aea6-09e28d875c54
Vendor specific
[NEW] Characteristic
/org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char0025
647b1c04-ce8f-4e1f-aea6-09e28d875c55
Vendor specific
For example, 647b1c04-ce8f-4e1f-aea6-09e28d875c52
is the ambient light characteristic:
We can also read the attribute's value:
[PAF5F]# select-attribute /org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char001f
[Atmosphere Project:/service001e/char001f]# read
Attempting to read /org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char001f
[CHG] Attribute /org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char001f Value: 0xeb
[CHG] Attribute /org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char001f Value: 0x00
[CHG] Attribute /org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char001f Value: 0x00
[CHG] Attribute /org/bluez/hci0/dev_00_60_37_0A_AF_5F/service001e/char001f Value: 0x00
eb 00 00 00
The value read is 0xeb = 235 (lux)
This matches the values displayed on the screen.
Prerequisites: Docker, docker-compose:
$ curl -sSL https://get.docker.com | sh
$ sudo service docker start
$ sudo pip install docker-compose
An easy way to start InfluxDB and Grafana is use Docker containers. I found some docker compose images build for Raspberry Pi on GitHub in gonzalo123's iot.grafana repository.
To install and start the Docker images run the following commands:
$ git clone https://github.com/gonzalo123/iot.grafana.git && cd iot.grafana
$ docker-compose up
After started, the InfluxDB and Grafana will be available on the following locations:
- InfluxDB -
http://rapsberry-pi-ip:8086/
- Grafana -
http://rapsberry-pi-ip:3000/
We will also need to create a thermostat
database in InfluxDB:
$ curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE thermostat"
Next we will need to populate the InfluxDB database with the BLE data. We will do this with a Python script. For the BLE and the InfluxDB functionality we need to install the bluepy
and influxdb
libraries.
$ sudo pip3 install bluepy influxdb
The Python script connects to the BLE device and reads the ambient light, temperature, humidity and air pressure attributes each seconds. Then, the values are inserted as measurements into the InfluxDB. A measurement looks like:
{
'measurement': 'temperature', 'tags': {
'ble_address': '00:60:37:0A:AF:5F'
},
'fields': {
'value': 25.100000381469727
},
'time': '2019-02-17T14:07:07.577919'
}
To start the script, the following command can be used
$ nohup python3 bleToInfluxDB.py <BLE address> &
(nohup
is used to keep the script running when we log out / disconnect from the Raspberry Pi)
Having this, we can add the InfluxDB database a new data source in Grafana:
And we can create dashboards like this:
One week plot:
(See the insert script and Grafana dashboard files)
Controlling a Heating SystemMost of the heating system support thermostats.
Thermostats are connected to the heating system and acts as switches.
Using a relay module the heating system can be also controlled from Raspberry Pi or Arduino:
A really simple heating controller would do the following:
- if temperature <= TARGET_TEMP - 0.5 °C ==> TURN ON the Heating System
- if temperature >= TARGET_TEMP + 0.5 °C ==> TURN OFF the Heating System
- else keep the current state
while 1:
tempVal = struct.unpack('<f', ch_temp.read())[0]
if tempVal >= TARGET_TEMP + 0.5:
heatingOff();
elif tempVal <= TARGET_TEMP - 0.5:
heatingOn();
time.sleep(30)
(See the full control script in the attachments)
More complex system can be also built. These would also water pumps, air conditioners and dehumidifiers and would allow controlling the temperature and humidity separately in each room.
Future Work- make the Mobile App work
- set target Temperature from the Thermostat
- Air Quality logging
- better battery management (currently is about 3.5h hours with back-light turned off)
Cheers!
Comments