This instructable is part of my DIY Home Automation series, check out main article āPlanning a DIY Home Automation Systemā. If you donāt know yet what Homie is, have a look atĀ homie-esp8266Ā +Ā homieĀ fromĀ Marvin Roger.
There are many many sensors. Iām covering the very basic ones in order to give reader the requirements to get started building āsomethingā. That might not be rocket science but that should actually work.
If you donāt have the parts, watch out for my upcoming instructable āSourcing Electronic Parts From Asiaā.
Let me add a few buzz words : IoT, ESP8266, Homie, DHT22, DS18B20, home automation.
Topic should be pretty clear now š
Also, this instructable is now also available from my personal page :Ā http://stuffblog.dullier.eu/
Step 1: Getting Started
Conventions
This instructable usesĀ D1 MiniĀ clones. These are WiFi enabled Arduino compatible controllers usingĀ ESP8266 chip. They ship in very small form factor (~34*25mm) and are dirt cheap (~3-4$ for clones).
Iāll illustrate each build using a D1 Mini, a breadboard and some sensor(s). I include a Bill Of Materials (BOM) for each but will skip obvious things likeĀ jumper wiresĀ and breadboard (miniĀ orĀ full). Iāll focus on āactive partsā.
For wires/cables in diagrams (FritzingĀ +Ā AdaFruitFritzing library), I used:
- Red/Orange for power, usually 3.3V. Sometimes it will be 5V, be careful.
- Black for ground.
- Yellow for digital data signals : Bits are traveling and can be read as-is by chips.
- Blue/Purple for analog data signals : No bits here, just plain voltage that must be measured and computed to understand whatās going on.
Homie for ESP8266Ā ships aĀ dozen examples, thatās where I started to build this instructable.
Breadboard
The D1 is quite breadboard friendly but will save only one row of pins up and down.
Each example will have the D1 on the right side and the components on the left side. The upper and lower power rails will be used to carry either 3.3V or 5V.
Note
Homie examples are built as ā.inoā sketches forĀ Arduino IDE. My own code is however built as ā.ccpā forĀ PlatformIO.
This will make very little difference as sketches are simple enough to be copy/pasted whatever your tool of choice is.
Step 2: Temperature & Humidity : DHT22 / DHT11
Building the device
The DHT22 uses :
- One digital pin to communicate with the controller, connect it to D3
- Two wires for power (3.3V or 5V + GND)
- The digital pin must be kept high (connected to power), for this we use a resistor between power rail and data pin
Code
The PlatformIO project can be downloaded from :Ā https://github.com/amayii0/Homie-DHT22
The original Homie example is here (but does not use a sensor) :Ā https://github.com/marvinroger/homie-esp8266/blob/ā¦
For DHT22, use DHT sensor library (ID=19)
BOM
- Controller : Wemos D1 Mini
- Resistor : 10KĪ©
- Sensor : (one of these)
- DHT22 : Iāve used theĀ 4 pins kindĀ which requires an extra resistor. There areĀ 3 pins modulesĀ shipping as SMD which includes the resistor.
- DHT11Ā : This is cheaper but less accurate, check your requirements
Step 3: Waterproof Temperature : DS18B20
Building the device
The DS18B20 uses :
- One digital pin to communicate with the controller, connect it to D3
- Two wires for power (3.3V or 5V + GND)
- The digital pin must be kept high (connected to power), for this we use a resistor between power rail and data pin
The DS18B20 is aĀ 1-wireĀ sensor. It uses a bus and as such multiple sensors can use a single data pin.
It is also possible to NOT use 3.3V/5V to power the sensor, this is called parasitic power mode. SeeĀ datasheetĀ for details.
Code
The PlatformIO project can be downloaded from :Ā https://github.com/amayii0/Homie-DS18B20
Like for DHT22, the original Homie example is here (but does not use a sensor) :Ā https://github.com/marvinroger/homie-esp8266/blobā¦
For 1-Wire bus, use package OneWire (ID=1)
For DS18B20, use DallasTemperature (ID=54)
BOM
- Controller : Wemos D1 Mini
- Resistor : 4.7KĪ©
- Sensor : DS18B20, pictured is aĀ waterproof one
- 3 pins screw terminalĀ to ease connection of cable to the breadboard
Step 4: Light : Photoresistor / Photocell (digital : On/off)
Building the device
(Sorry, donāt have a Fritzing component for the digital photocell)
The photocell digital module uses :
- One digital pin to communicate with the controller, connect it to D3
- Two wires for power (3.3V + GND)
Its possible to use an analog photocell but this is not documented here, seeĀ AdafruitĀ excellent article āUsing a Photocellā.
Note: In this example thereās a potentiometer on the sensor board. It is used to set the limit between ālightā and ādarkā ambient light. When reading 1 light is off, therefor reading 0 means light if on.
Code
The PlatformIO project can be downloaded from :Ā https://github.com/amayii0/Homie-Photocell
BOM
Controller : Wemos D1 Mini
Sensor :Ā Photosensitive / Light Detection Module
Step 5: Light : Photoresistor / Photocell (analog)
Building the device
The photocell analog sensor acts as a resistor. It will connect between an analog input and 3.3V.
A resistor is put between GND and data pin to create aĀ voltage divider. The purpose is to create a known range of values:
- If there is no light, photocell will basically block VCC, thus connecting GND to your data pin : Pin will read nearly 0.
- It there is a lot of bright light, photocell will let VCC flow to the data pin : Pin will read nearly full voltage and as such near to max (1023).
Note : Analog pins values are read in a 0-1023 range usingĀ analogRead. This is not practical to deal with 1 byte values, for this theĀ Arduino map functionĀ will help reduce from 0-1023 to (for instance) 0-255.
For calibration of min/max values for your sensor, use a sketch likeĀ this one from Arduino.
Code
The PlatformIO project can be downloaded from :Ā https://github.com/amayii0/Homie-PhotocellAnalog
BOM
- Controller : Wemos D1 Mini
- Sensor :Ā Light Dependent Resistor (LDR) / Photoresistor
- Resistor : 1K or 10K, need to calibrate based on your cell
References
- PiDome serverĀ source codeĀ for lighting condition of a location
- Adafruitās āUsing a Photocellā
- āPhotoresistorsā here at instructables
- Some damn crazy āPhotocell Tutorialā if you want some math and graphs
Step 6: Optical Detector : QRD1114
Building the device
Code
BOM
References
- Physical Computing : QRD1114Ā includes sample code to read sensor and use interrupt for rotary encoder + precise PCB design
- QRD1114 Optical Detector Hookup GuideĀ at Sparkfun
Step 7: Final Words
This instructable is a very short one to explain basic monitoring.
To go further weāll need to connect relays, IR emitterā¦ This will hopefully be covered later on as free time allows me to. The major difference is that we wonāt just āreadā (is there light?) but also āwriteā (turn light on!).
Source: Building Homie Devices for IoT or Home Automation