loading

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,bme280,bit,dht,file,gpio,mdns,net,node,tmr,uart,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 zip file from https://github.com/NorthernMan54/nodemcu-dht-yl69...

2. Unzip package

3. Using ESPlorer save each file to ESP, except test.lua and init.la

4. Configure your wifi setup - In config.lua add your wifi SSID and passwords. Please note that the configuration supports multiple wifi networks, one per config line.

module.SSID["network name"] = "password"

5. Model - Either DHT, BME or DHT-YL, used by homebridge-mcuiot to determine which sensor is installed. module.Model = "DHT"

or

module.Model = "DHT-YL"

or

module.Model = "BME"

6. Save config.lua to ESP

7. Save test.lua to ESP. Your nodemcu should now boot, output looks like this

> dofile("test.lua")<br>Booting...
Setting Init Timer
Configuring Wifi ...
> Connecting to 67 Bonacres ...
IP unavailable, Waiting...
====================================
ESP8266 mode is: 1
MAC address is: 5e:cf:7f:86:08:9d
IP is 192.168.1.165
====================================
Registering service dht22 with mDNS
Web Server Started

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" }
     ],
"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/</a>
[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/</a>
[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/</a>
[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.

<p>hey</p><p>What am I doing wrong this time? the only accessory I'm getting in HomeApp is MCU-IOT reset switch for some reason :/</p>
Dom,<br><br>The error you showed from homebridge is a timeout when trying to reach your nodemcu device. It looks like it was seen and discovered, but when it was queried by the plugin the request timed out. Is the nodemcu working okay, was there any errors on its console?
<p>this is what I'm getting in ESPlorer:</p><p>PORT OPEN 115200</p><p>Communication with MCU..Got answer! Communication with MCU established.</p><p>AutoDetect firmware...</p><p>Can't autodetect firmware, because proper answer not received (may be unknown firmware). </p><p>Please, reset module or continue.</p><p>�c��o'�lno���bx��l;l{lp�n��dcg�|�ć�;�b��og�l��l`�ogd�d`gs�ۓn;s�``�g��#�gd���'o��lx�o�{�����#g�|��l�#��ng�$�l �o'l`orǛ�'cd`8�g�r�����d�c'�|$섇#��no�d`�no$`'{���g�ls��'�d����g��s��'|�dd$l`��{�d�l�</p><p>NodeMCU custom build by frightanic.com</p><p>branch: master</p><p>commit: b96e31477ca1e207aa1c0cdc334539b1f7d3a7f0</p><p>SSL: false</p><p>modules: adc,bit,bme280,file,gpio,mdns,net,node,tmr,uart,wifi</p><p> build built on: 2017-03-04 19:40</p><p> powered by Lua 5.1.4 on SDK 2.0.0(656edbf)</p><p>Booting...</p><p>Setting Init Timer</p><p>Configuring Wifi ...</p><p>&gt; Connecting to TPLink-2.4Ghz ...</p><p>IP unavailable, Waiting...</p><p>IP unavailable, Waiting...</p><p>IP unavailable, Waiting...</p><p>====================================</p><p>ESP8266 mode is: 1</p><p>MAC address is: 5e:cf:7f:d5:ea:6f</p><p>IP is 192.168.1.158</p><p>====================================</p><p>Registering service dht22 with mDNS</p><p>Web Server Started</p>
<p>Dom,</p><p>It looks like the nodemcu is working correctly, but the true test is when you poll it for data. Can you try this from the command line on your machine.</p><p>curl 192.168.1.158</p><p>It should respond with something like this</p><p>{ &quot;Hostname&quot;: &quot;NODE-18A6B3&quot;, &quot;Model&quot;: &quot;DHT-YL&quot;, &quot;Version&quot;: &quot;1.1&quot;, &quot;Data&quot;: {&quot;Temperature&quot;: 20.9, &quot;Humidity&quot;: 27, &quot;Moisture&quot;: 1024, &quot;Status&quot;: 0 }}</p><p>Also, what sensor do you have attached? DHT or BME ?</p>
<p>hey</p><p>I'm using only BME, I also have a second ESP8266, also no response from it :/</p><p>I possibly messed something up in lua files :P</p><p>soo</p><p>my config.lua:</p><p>local module = {}</p><p>module.SSID = {}</p><p>module.SSID[&quot;TPLink-2.4Ghz&quot;] = &quot;XXXXXXXX&quot;</p><p>module.SSID[&quot;The_Beach&quot;] = &quot;brownieg&quot;</p><p>-- Options are DHT or DHT-YL, used by homebridge to determine if moisture data is valid.</p><p>module.Model = &quot;BME-GD&quot;</p><p>module.Version = &quot;1.3&quot;</p><p>module.ID = wifi.sta.gethostname()</p><p>module.ledRed = 0 -- gpio16</p><p>module.ledBlue = 4 -- gpio2</p><p>module.sensor = 7 -- gpio14</p><p>module.bme280scl = 5 -- D5</p><p>module.bme280sda = 6 -- D6</p><p>return module</p><p>init.lua:</p><p>config = require(&quot;config&quot;)</p><p>led = require(&quot;led&quot;)</p><p>bme = require(&quot;bme&quot;)</p><p>app = require(&quot;main&quot;)</p><p>setup = require(&quot;setup&quot;)</p><p>led.boot()</p><p>setup.start()</p><p>-- Never gets here</p><p>main.lua:</p><p>local module = {}</p><p>function module.start()</p><p>-- Turn off YL-69</p><p>gpio.mode(5, gpio.OUTPUT)</p><p>gpio.write(5, gpio.LOW)</p><p> -- Start a simple http server</p><p>print(&quot;Web Server Started&quot;)</p><p>srv=net.createServer(net.TCP)</p><p>srv:listen(80,function(conn)</p><p> conn:on(&quot;receive&quot;,function(conn,payload)</p><p> led.flashRed()</p><p> print(payload)</p><p> gpio.write(5, gpio.HIGH)</p><p> tmr.alarm(2,1,tmr.ALARM_SINGLE,function()</p><p> moist_value=adc.read(config.YL69)</p><p> gpio.write(5, gpio.LOW)</p><p> temp = -999</p><p> humi = -999</p><p> baro = -999</p><p> dew = -999</p><p> gdstring = &quot;&quot;</p><p> if string.find(config.Model,&quot;BME&quot;) then</p><p> status, temp, humi, baro, dew = bme.read()</p><p> else</p><p> status, temp, humi, temp_dec, humi_dec = dht.read(config.DHT22)</p><p> end</p><p>-- print(&quot;Heap Available:&quot; .. node.heap())</p><p> if string.find(config.Model,&quot;GD&quot;) then</p><p> green, red = gd.getDoorStatus()</p><p> gdstring = &quot;, \&quot;Green\&quot;: \&quot;&quot;..green..&quot;\&quot;, \&quot;Red\&quot;: \&quot;&quot;..red..&quot;\&quot;&quot;</p><p> end</p><p>-- print(&quot;Heap Available:&quot; .. node.heap())</p><p>-- print(&quot;33&quot;)</p><p> majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info()</p><p>-- print(&quot;35&quot;)</p><p> print(&quot;Status: &quot;..status..&quot;\nTemp: &quot;..temp..&quot;\nHumi: &quot;..humi..&quot;\nMoisture: &quot;..moist_value..</p><p> &quot;\nBaro: &quot;..baro..&quot;\nDew: &quot;..dew..&quot;\n&quot;)</p><p> local response = { &quot;HTTP/1.1 200 OK\n&quot;, &quot;Server: ESP (nodeMCU) &quot;..chipid..&quot;\n&quot;,</p><p> &quot;Content-Type: application/json\n&quot;,</p><p> &quot;Access-Control-Allow-Origin: *\n\n&quot;,</p><p> &quot;{ \&quot;Hostname\&quot;: \&quot;&quot;..config.ID..&quot;\&quot;, \&quot;Model\&quot;: \&quot;&quot;..config.Model..&quot;\&quot;, \&quot;Version\&quot;: \&quot;&quot;..config.Version..</p><p> &quot;\&quot;, \&quot;Firmware\&quot;: \&quot;&quot;..majorVer..&quot;.&quot;..minorVer..&quot;.&quot;..devVer..&quot;\&quot;, \&quot;Data\&quot;: {\&quot;Temperature\&quot;: &quot;..temp..</p><p> &quot;, \&quot;Humidity\&quot;: &quot;..humi..&quot;, \&quot;Moisture\&quot;: &quot;..moist_value..</p><p> &quot;, \&quot;Status\&quot;: &quot;..status..&quot;, \&quot;Barometer\&quot;: &quot;..baro..&quot;, \&quot;Dew\&quot;: &quot;..dew..&quot;&quot;..gdstring..&quot; }}\n&quot; }</p><p> local function sender (conn)</p><p> if #response&gt;0 then conn:send(table.remove(response,1))</p><p> else conn:close()</p><p> end</p><p> end</p><p> conn:on(&quot;sent&quot;, sender)</p><p> sender(conn)</p><p> end)</p><p> end)</p><p> conn:on(&quot;sent&quot;,function(conn) conn:close() end)</p><p>end)</p><p>end</p><p>return module</p>
<p>Dom,</p><p>In config.lua you can remove this line, as it isn't needed</p><p>module.SSID[&quot;The_Beach&quot;] = &quot;brownieg&quot;</p><p>And the most important part, change this line from</p><p>module.Model = &quot;BME-GD&quot;</p><p>to </p><p>module.Model = &quot;BME&quot;</p><p>And that should be it</p>
<p><a href="https://www.instructables.com/member/NorthernM" rel="nofollow">NorthernM</a></p><p>I made those changes in config.lua, ESPlorer prints now this error (see printscreen) when I'm trying to pull curl in Terminal</p><p>thanks for your time and help :)</p>
<p>You need to add to config.lua this line</p><p>module.YL69=0</p>
<p>yup all good now, in homekit I can only see Temperature, the rest only in EVE. For some reason I can only add Temperature to the favourites in Eve, the rest (pressure and humidity) can be seen when I click on the temperature tab in EVE. Siri works fine though, giving all of three values when asked :)</p>
<p>This is normal. I set this up as a single temperature sensor accessory that also supports pressure and humidity as well. In home, if you look at the details you should see humidity as well, pressure is not supported by Home.</p>
<p>thank you very much for all your help !</p><p>true gent !</p>
<p>Dom,</p><p>Not sure if your interested, but I recently updated all my temperate sensor plugins to log the data to Google sheets so you can create graphs etc.</p><p>Sean</p>
<p>That looks perfectly normal, when you restart homebridge and the timeout happens do any errors appear on the nodemcu? I was expecting to see some type of exception on the nodemcu.</p>

About This Instructable

4,707views

20favorites

License:

Bio: Creator of multiple Apple HomeKit devices using homebridge in an attempt to increase the WAF score of my home automation attempts.
More by NorthernM:Take Snapshots of Who Visits Your Door With Homebridge and Google Drive Receive Picture Notifications When People Approach Your House With HomeKit Build a Apple HomeKit Temperature Sensor Device Using a RaspberryPI and a BME280 
Add instructable to: