My first device

If you have read my latest intro posts, I guess you have, by now, a clue on the setup and what’s next 🙂

The box with the ESP module and the sensors
The box with the ESP module and the sensors

The main idea, to be able to turn on/off lights via the mobile phone, or even not needing to turn lights on and use a movement sensor led me to this… first device.

It is used for the bathroom and the hall next to it, with sensors for temperature, movement and light. Using those, it automatically turns on bath light when movement is detected and there isn’t enough light in the bathroom. Same for turning off, when no movement is detected, it turns the light off.

As for the “hub”, on the Raspberry Pi, I’ve used two home automation apps: Domoticz and Home Assistant. As this was the first device, I experimented with both assistant apps, having multiple iterations, until I got to the final version.

Home Assistant
Home Assistant – device tab – temperature, humidity, motion and light sensors, switches

I started using Domoticz, making http requests to send sensor data and turn lights on/off, and all “decisions” and the functionality was in the ESP module. At a later point, I switched using Home Assistant (setup on Pi, as, having the ESP module just to read sensors and report data to the hub, where the flows were, using automations.

The wall switch

More testing... before have it in case
More testing… before having it placed in case

As I wanted to be able to still use the integrated wall switch, the device has to be next to the switch (became kind-of useless, the wall switch is not quite used because of the movement, light and automation that turns light on/off). And, with some changes: it doesn’t command the lights anymore, but it’s connected to the ESP module, acting as a “sensor”, letting the ESP know when the switch was pressed, and with the 2 channel relay, the ESP turns the lights on/off.

One issue I’ve faced, in time, with this change: from time to time, the light was automatically turning on and off, in a few milliseconds. Like the switch was pressed, on and off, very fast. It looks that the switch, being designed for high voltage, doesn’t handle low voltage that well. So, I had to fix this by software, by adding a delay in code, when the switch is used. This removed entirely the false triggers, and only affected the switch usage by having 300ms delay between the press and the action happening.

As the switch isn’t connected to mains anymore, it doesn’t have on/off positions, and now acts as a toggle for the light.

Positioning and mains

On the early stages (idea-like early), I wanted to have it all hidden inside the wall switch. But, in the end, it didn’t fit, and I had to find a case for it and place it next to the wall switch. Maybe… in the future, with a 3D printer, I’ll design a custom case for it.

Also, the plan was to use power from the switch, but, on my house setup, on the switches, I don’t have both hot and neutral lines, it’s just one. So, in this specific project, I was “lucky” to have an outlet nearby.


For hardware, I have the power supply – used a 220v micro-usb phone charger – hidden in the power outlet, having the 5v wire, that goes to the device box, powering up the ESP module using the micro USB.

inside the case...
inside the case…

In the box, I have the ESP module and a 2 channel relay – for hall and bathroom lights. In this first project, I planned to solder all the wires to the ESP, insulated with heat shrinkable tube. As this worked, it turned out to be difficult to maintain and change, when needed, sensor pins. So, on next iterations, removed the soldered and I sticked to having Dupont wires.

Temperature, humidity, light and movement sensors
Temperature, humidity, light and movement sensors

As for the sensors, they are inside the bathroom, one meter away from the main ESP, having a different case for the movement, temperature and light sensor. For this I used one main wire, having 6 wires: two for power all the sensors, two for temperature sensor, one for light sensor and one for the movement sensor.


As for software, I started with Arduino and sticked to it 🙂

In the start, we have the include section, the declarations, where I have them grouped by “type”: the pins (with comments, to know where are on the ESP), some variables for reporting intervals, the MQTT channels.

For the setup (in Arduino, the setup() method is called once, when the app starts, used to init the pins, variables)  I’ve separated it in to methods: a general one, included in setup.h, that’s used in all devices, preparing the wifi connection, serial and MQTT; and a device-specific one, where I setup the pins, init the sensors (in this case, only the temperature sensor), subscribe to MQTT channels for switches and one special channel I’ve added, in order to be able to change some of the variables in the code – the temperature/light reporting interval.

Same thing for the loop (as the name says, it just… loops – allowing you to read sensors, send the values, react when needed), divided in 2 parts, main and appLoop. In my application loop, I’ve added methods for each sensor (temperature, light and movement) and for the two switches – so it can send the MQTT message when the switch is pressed, light condition changed or for temperature update.

I’ve also added some “helper” methods, that get used across all devices, like methods for publishing the MQTT messages, preparing the messages to have a specific format, a method used for logging – sending some messages to a MQTT channel, and those, with an automation from Home assistant, get forwarded to a Telegram bot on my phone.

Future… ideas/improvements

  • maybe updating the cases, using some custom 3D printed ones 🙂
  • with the sensors connected and working, uploading new versions of the code turned out to be difficult – try to setup over the air updates;
  •  try using PCBs

Please let me know what you think, any ideas to get this improved, make it better 🙂 using the comments section bellow.

Thank you for standing by me ’till the end… I hope I’ll see you soon – on my next article I’ll introduce you to my heat device 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *