Introduction: Build a Apple HomeKit Temperature Sensor Device Using a ESP8266 and a BME280
In today's instructable, we will be making low cost temperature, humidity and moisture sensor based on either the AOSONG AM2302/DHT22 or BME280 temperature/humidity sensor, YL-69 moisture sensor and the ESP8266/Nodemcu platform. And for displaying the data, we will be using homebridge to integrate to Apple's HomeKit.
This supports multiple devices and device discovery using mDNS, with minimal configuration required in Homebridge.
Parts List
- NodeMCU / New Wireless module NodeMcu Lua WIFI Internet of Things development board based ESP8266 with pcb Antenna and usb port
- These are very cheap on Ali Express, only problem is that shipping takes 4-6 weeks
- Mobile Phone Charger
- Mini USB Cable
- AOSONG AM2302/DHT22 temperature/humidity sensor
Or as an alternative sensor
- Bosch BME280 Temperature, Humidity and Barometric Sensor
- YL-69 Moisture Sensor
- 2N3904 Transistor
- 1K Resistor
- Transistor and Resistor only needed of YL-69 Moisture Sensor
- 5 Pin Female to Female cable set ( 1.5' ) ( DHT )
- 4 Pin Female to Female cable set (1.5') ( BME 280 )
- Heat shrink tubing small
- Container to install NodeMCU
- I used a small plastic food container from dollarama
- 5 small nuts and bolts for mounting NodeMCU
Tools
- Soldering Iron
- Solder
- Wire Cutters
Step 1: Hardware Build - DHT22
Connecting the DHT22
1. Cut the 5 Pin Female to Female cable in half, creating a cable about 9 inches long.
2. On the connecter, pins 2 and 3 are unused and can be removed.
3. Bare about a 1/4" of each wire on the end opposite the connector.
4. With your soldering iron, tin each wire end and the terminals on the DHT22.
5. Cut about 3/4" of heat shrink tubing and push down the wires.
6. Solder the wires to the DHT22 as follows
Connecter Pin --> DHT22 Pin
1 ---> 2 ( Second from left )
4 ---> 1 ( First on the left )
5 ---> 4 ( First on the right )
7. Slide the heat shrink tubing over the DHT22 Pins and shrink the tubing with the soldering iron.
Step 2: Hardware Build - BME280
Connecting the BME280
1. Cut the 4 Pin Female to Female cable in half, creating a cable about 9 inches long.
2. Bare about a 1/4" of each wire on the end opposite the connector.
3. With your soldering iron, tin each wire end.
4. Solder the wires to the BME280 in this order, VCC, GND, SCL, SDA. These need to line up to pins in the connector.
Step 3: Hardware Build - YL-69
Step 4: Build Case
Step 5: Build NodeMCU Firmware
1. Using http://nodemcu-build.com, create a custom firmware containing at least these modules:
adc, ads1115, bit, bme280, dht, file, gpio, i2c, mdns, net, node, tmr, uart, websocket, wifi
2. Please use esptool to install the float firmware onto your nodemcu. There are alot of guides for this, so I won't repeat it here.
Step 6: Connect Sensors
DHT22
1. Position cable connector so that pin 1 connects to D2 on nodemcu, pin 4 with 3v3 and pin 5 with gnu.
BME280
1. Connect the BME280 to the nodeMCO, lining up the pins as follows:
3V3 -> VCC
GND -> GND
D5 -> SCL
D6 -> SDA
Step 7: Install Nodemcu Software
1. Download lua software package from NodeMCU Lua Code
2. Follow the installation instructions in the README located here
https://github.com/NorthernMan54/homebridge-mcuiot/tree/master/lua
Step 8: Testing
1. You can test from the command line with curl or wget, make sure you use the ip address from the Esplorer screen and not mine ;-)
curl 192.168.1.165<br> { "Hostname": "NODE-8689D", "Model": "BME", "Version": "1.2", "Data": {"Temperature": 22.15, "Humidity": 50.453, "Moisture": 8, "Status": 0, "Barometer": 1003.185, "Dew": 11.38 }}
2. In Esplorer you should see the following
GET / HTTP/1.1<br>Host: 192.168.1.165 User-Agent: curl/7.43.0 Accept: */*
Status: 0 Temp: 22.15 Humi: 50.453 Moisture: 8 Baro: 1003.185 Dew: 11.38
3. Using Esplorer install init.lua. The nodemcu portion of the build is now complete.
4. To test mDNS, I use this command on OS X
dns-sd -B _dht22._tcp
And for the 2 devices on the network, I receive the following output:
Browsing for _dht22._tcp DATE: ---Mon 19 Sep 2016--- 21:11:26.737 ...STARTING... Timestamp A/R Flags if Domain Service Type Instance Name 21:11:26.739 Add 3 4 local. _dht22._tcp. NODE-18A6B3 21:11:26.739 Add 2 4 local. _dht22._tcp. NODE-871ED8
Step 9: Homebridge-mcuiot Install
1. Install homebridge using:
npm install -g homebridge
I won't go into a lot of details around the initial install of homebridge and how to configure it to autostart etc. Their are a lot of other guides for this.
2. Install homebridge-mcuiot using:
npm install -g homebridge-mcuiot
3. Update your configuration file, see sample-config.json in this directory.
ie
"bridge": {<br> "name": "Bart", "username": "CC:22:3D:E3:CD:39", "port": 51826, "pin": "031-45-154" },
"description": "HomeBridge",
"platforms": [ { "platform": "mcuiot", "name": "mcuiot" } ],
"accessories": [ ]
}
4. Start homebridge, output should look like this
[10/20/2016, 10:15:20 PM] Loaded plugin: homebridge-mcuiot<br> [10/20/2016, 10:15:20 PM] Registering platform 'homebridge-mcuiot.mcuiot'
[10/20/2016, 10:15:20 PM] ---
[10/20/2016, 10:15:20 PM] Loaded config.json with 0 accessories and 0 platforms.
[10/20/2016, 10:15:20 PM] ---
[10/20/2016, 10:15:20 PM] Loading 0 platforms...
[10/20/2016, 10:15:20 PM] Loading 0 accessories...
Load homebridge-mcuiot.mcuiot
Scan this code with your HomeKit App on your iOS device to pair with Homebridge:
┌────────────┐
│ 031-45-154 │
└────────────┘
[10/20/2016, 10:15:20 PM] [homebridge-mcuiot.mcuiot] Starting mDNS listener
[10/20/2016, 10:15:20 PM] Homebridge is running on port 51826.
[10/20/2016, 10:15:20 PM] [homebridge-mcuiot.mcuiot] Found url <a href="http://NODE-871ED8.local.:80/" rel="nofollow"> http://NODE-871ED8.local.:80/>
[10/20/2016, 10:15:20 PM] [homebridge-mcuiot.mcuiot] Found url <a href="http://NODE-869815.local.:80/" rel="nofollow"> http://NODE-869815.local.:80/>
[10/20/2016, 10:15:20 PM] [homebridge-mcuiot.mcuiot] Found url <a href="http://NODE-8689D.local.:80/" rel="nofollow"> http://NODE-8689D.local.:80/>
[10/20/2016, 10:15:21 PM] [homebridge-mcuiot.mcuiot] addMcuAccessory 195 NODE-8689D BME
[10/20/2016, 10:15:21 PM] [homebridge-mcuiot.mcuiot] addMcuAccessory 195 NODE-871ED8 DHT
[10/20/2016, 10:15:21 PM] [homebridge-mcuiot.mcuiot] addMcuAccessory 195 NODE-869815 DHT
In my environment I have 3 devices running.
Step 10: Homebridge
On your iPhone/iPad start your favourite homekit client and pair you client to homebridge. You should see all the mcuiot devices.
Adding devices
Devices are auto discovered using mDNS, and will add new devices when they appear on mDNS. In the event that devices are not discovered, restarting homebridge will trigger a reconciliation between the plugin and mDNS, and add missing devices. Missing devices are not removed during startup, see below for how to remove non-existent devices.
Removing devices
Devices are removed using the 'Identify Accessory' function. When you use the function from your app, it checks to see if the device is truly not responding then removes the device.