Home IoT Hub Series · 16
[Part 16] Are the lights off and the door locked? Light and lock status node
Track living room light and front door lock state as simple hub slots.
This page combines living-room light state and door-lock state. These are small status signals, but together they make the hub feel more like a real home dashboard.
Keep each input simple and test one signal at a time. When both work separately, combine them into one node.
![[Part 16] Are the lights off and the door locked? Light and lock status node hero image](/iot-lab/assets/living-light-door-lock-hero.png)
This article is part of the Home IoT Hub system connected to the central NodeMCU server built in Part 2: Building the Brain. The light sensor and door-lock input let the hub track living-room lighting and entrance security together. The collected data is sent over the local
IoT_Hub_Net network and updates slot 11 for living-room light and slot 10 for door-lock state on the central server (/set?no11=light&no10=lock), becoming part of the decision pipeline for the 20-channel smart-home controller.Parts and cost
| Part | Qty | Unit price |
|---|---|---|
| Wemos D1 Mini | 1 pc | $1.50 |
| light sensor (LDR + 10kΩ) | 1 set | $1 |
| door lock status sensor | 1 pc | $1.50 |
| Jumper wires 5 pc | — | — |
| Total | $4 |
Pin wiring
| Sensor / module | Board pin |
|---|---|
| LDR one side | 3.3V |
| LDR other side | GND |
| VCC | 3.3V |
| GND | GND |
| OUT | D1 (GPIO5) |
Full code
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> const char* ssid = "IoT_Hub_Net"; const char* password = "iothub1234"; const char* hubURL = "http://192.168.1.1"; #define LIGHT_PIN A0 #define LOCK_PIN 5 // D1 #define LIGHT_TH 700 // above this value = light ON #define INTERVAL 5000 void setup() { Serial.begin(115200); pinMode(LOCK_PIN, INPUT_PULLUP); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.print("Connecting to IoT_Hub_Net"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nConnected!"); } void loop() { int lightRaw = analogRead(LIGHT_PIN); String light = (lightRaw > LIGHT_TH) ? "ON" : "OFF"; String lock = (digitalRead(LOCK_PIN) == LOW) ? "1" : "0"; // LOW = note= note Serial.print("Light: "); Serial.print(lightRaw); Serial.print(" -> "); Serial.print(light); Serial.print(" | Lock: "); Serial.println(lock); WiFiClient client; HTTPClient http; String url = String(hubURL) + "/set?no11=" + light + "&no10=" + lock; http.begin(client, url); http.GET(); http.end(); Serial.println("HTTP -> " + url); delay(INTERVAL); }
Upload and check
Connect the Wemos D1 Mini to the PC with a USB data cable, select the ESP8266 board and port in the Arduino IDE, upload the sketch, then open Tools → Serial Monitor and set the baud rate to 115200.
Serial Monitor
After uploading, open Tools → Serial Monitor in the Arduino IDE and set the baud rate to 115200. The following is an example Serial Monitor flow for checking normal operation. This is not a real screenshot. It shows light state and door-lock state updating slots 11 and 10 together.
[BOOT] Home IoT Node #16 - Light and door lock [SENSOR] light_raw=842 light=ON [INPUT] optocoupler door_lock=CLOSED [FILTER] street_light_guard=PASS [HTTP] GET /set?no11=1&no10=1 [HUB] 200 OK - slots no11/no10 updated [SAFETY] door-lock line isolated by optocoupler
- If
Connectedorconnectedappears, the Wi-Fi step passed. - If
HTTP 200or200 OKappears, the value reached the brain server. - If the value does not update, check both the slot number in the URL and the sensor wiring.
Build notes
- Turn the light on and off, then lock and unlock the door state signal. Confirm each slot changes independently.
- Power on the hub brain first so the node can join
IoT_Hub_Net. - Use the same slot number written in the source code.
- Open the serial monitor at 115200 baud and confirm
HTTP 200. - If the value does not appear on the hub, recheck wiring, GPIO number, and Wi-Fi connection.
One D1 Mini Guarding Both Your Living-Room Light and Front Door
Wire a CDS photoresistor (is the living-room light on?) and a reed switch (is the front door locked?) to a single board, and one tiny D1 Mini becomes a reliable gatekeeper in your home IoT hub. Here we skip wiring and code and focus on where to mount this pair and what to do with it.
Troubleshooting checkpoints
The node code can look short, but real home installation is affected by placement, power, Wi-Fi quality, and sensor noise. Check these points before letting the node become a decision input for the brain server.
| Symptom | Likely cause | Field fix |
|---|---|---|
| The light shows on after it is off | TV light, street light, or window light reaches the LDR | Narrow the sensor field of view with black heat-shrink tube and aim it only toward the ceiling light. |
| Day/night thresholds drift | LDR resistance changes heavily with location and weather | Log values for a few days and use separate on/off thresholds. |
| Door-lock wiring is risky | Door-lock board voltage does not directly match ESP8266 3.3V logic | Do not connect directly; read only isolated state through an optocoupler or relay contact. |
| The reported lock state differs from reality | Manual operation or auto-lock delay is not reflected immediately | Use a stable final latch signal or status LED instead of the short motor-drive moment. |
Practical Uses
- "You left the light on" alert: If the door locks (reed opens) and the CDS still reads 'bright' after a set delay, push a reminder that the living-room light is still on.
- Unlocked-door warning: At night (CDS dark), if the reed reports the door open/unlocked, send a bedtime security alert.
- Occupancy estimate: Sustained light during the day suggests someone is home; door locked plus lights off for a long stretch implies you're out, triggering heating and standby-power savings.
- Energy-saving report: Log how many hours the light stayed on to build a weekly power-saving summary.
- Security scenario: While the system judges you're out, any door opening fires an intrusion alert, optionally linked to a snapshot camera.
Recommended Placement
- CDS (photoresistor): Aim for reflected light, not directly under the bulb. Facing the bulb makes on/off easy but leaves you vulnerable to outdoor sunlight. Mounted on a ceiling corner or high on a wall reading indoor reflected light, it reads both daylight and lamplight stably. Avoid spots hit by direct window sun.
- Reed switch + magnet: Put the reed on the fixed door frame and the magnet on the moving door leaf (or beside the deadbolt). When fully closed the two should align within 10mm, alignment marks facing each other. Placing the magnet at the deadbolt position lets you sense not just 'closed' but 'locked'.
Field Tips / Cautions
- CDS threshold calibration: One threshold for day and night misjudges often. Log analog values over a full day, identify four bands (bright noon, overcast, lamp-only, full dark), then set separate thresholds for light-detection and for day/night. A 5-10s moving average filters momentary shadows and flicker.
- Reed gap and door warping: Seasonal warping or sagging widens the gap and can falsely read 'always open'. Align with margin; if false triggers persist, step up to a larger magnet or fine-tune the position.
- Analog + digital at once: The D1 Mini has only one ADC (A0), so give A0 to the CDS and a digital input (internal pull-up) to the reed. Handle the reed on state-change (edge detection) and sample the CDS periodically to keep the loop light. Absorb chatter with software debounce.
FAQ
- Q. Won't closing the curtains fool the CDS into thinking it's night? A. That's why the reflected-light spot and a dedicated light-on threshold matter. Using a different value for 'light on' than for 'day/night' keeps light detection accurate even with curtains drawn.
- Q. It shows 'locked' when I only pushed the door shut without locking. A. Move the magnet from the door edge to where the deadbolt engages. That separates 'door closed' from actually locked.
- Q. The two sensors' alerts get confusing. A. Combine states into scenario-level alerts (e.g., 'door locked + light on -> leaving-home light reminder'). Combined conditions cut false alarms far more than single-sensor events.
This article is part of the 20-channel Home IoT system built from the Part 2 central brain server and the distributed edge nodes in Parts 3-19. Use this map to check the slot and sensor flow across the series.
[Part 1] Home IoT Hub overview[Part 2] Central brain server · slots 1-20[Part 3] Stop guessing room comfort · slots 1/2[Part 4] Did I close the door? · slots 9[Part 5] Is the fridge really cooling? · slots 12[Part 6] Should I open the window? · slots 3[Part 7] Stop bathroom mold before it starts · slots 19[Part 8] Is the boiler actually running? · slots 5/17[Part 9] Did I leave the gas stove on? · slots 6[Part 10] Get warned before gas smell becomes obvious · slots 7[Part 11] Catch flame signs while away from home · slots 8[Part 12] Is bad air making you foggy? · slots 13/14[Part 13] Why is the power bill so high? · slots 15[Part 14] Is water leaking, and how much are we using? · slots 16[Part 15] Do not leave windows open in the rain · slots 18[Part 16] Are the lights off and the door locked? · slots 11/10[Part 17] Control the air conditioner from the IoT hub · slots 4[Part 18] Is the dog bowl empty? · slots 20[Part 19] Cool the room only when someone is there · slots 1/4[Part 20] The house finally fits on one screen · slots all