ESP8266 – Ikea Molgan light Hack – added retries to prevent battery drain.

I was on holiday over the weekend and the unit works like a charm, I was notified of all activities detected by the PIR set above my main door.

With wifi_station_set_auto_connect(TRUE) the constant tries to connect to a missing AP or wrong password will run the batteries flat in no time.

Default retries is now set to 4 in which after that the ESP8266 will go in deep sleep to conserve power.

HTTPS and REST protocol.

Programming the ESP8266 to use REST protocol directly is a more practical solution in most case as not everyone have a MQTT broker to work with.

The problem is most notification service now uses SSL for security reason, and the ESP8266 stack can’t seem to work with HTTPS servers.

Did some test with,  on port 80 and a non secure connection, my code works fine. The ESP connected and return a page. On a secure connection, on port 443 it fails on handshake.

I think the current SSL stack in the ESP8266 sdk cannot handle strong encryption, or the certificates might be too big or heavy for it to handle.

It is beyond my skills to get SSL working, I guess I will have to work on another solution.

ESP8266 – Ikea Molgan light Hack – Software and actual usage.

After hacking the hardware, the next part is programming the firmware.

The basic flow control is simple.

  1. boot
  2. log on wifi
  3. get time from NTP server (
  4. format text with timestamp
  5. log on to MQTT broker
  6. publish formated text to MQTT broker
  7. go into deep sleep.

Full source available on GitHub.

MH6After I have a decently working firmware, it is time to put everything together. I added a capacitor to the power rails to stabilize and eliminate false trigger. I think the sudden power draw by the ESP8266 somehow caused the BISS0001 to reset and kept the light on.


I have to trim off some plastic to fit the everything into a small package.


Looks complete and nice as a package with it’s cover is on.


Everything works almost perfectly but I think it will be more practical to run it off a wall plug, As I can imagine that the battery would last less than 2 weeks in this config, that will limit it’s usefullness.


I set up mqttwarn to push notification to my phone and computer (pushbullet) whenever motion is detected by the PIR.


ESP8266 – Ikea Molgan light Hack – Hardware.

The first ESP8266 project I worked on is a wifi PIR detector.


My original plan was to use an cheap Ebay PIR and 3D printed case for this project but I happen to have a spare Ikea Molgan PIR light lying around, I opened it up and take a peek inside and decided to work this hack with it, overall this is an attractive and cheap unit. (SGD$9.90)


Removing the top dome was easy as it is friction fitted and held together with some tape, upon closer examination, it uses the same parts as most cheap Ebay PIR, just with a bigger dome lens, LEDs and battery holder. Running on 3 x AAA battery , voltage wise it is pretty ideal but I don’t think it will last too long once I hook the ESP8266 to it.

CDs and PIR in the Middle surrounded by 5 ultra bright LEDs

On the topside after removing the plastic dome, you see the PIR and LEDs, at the side is a light sensitive photo-diode that lowers it’s resistance in the dark, arming the PIR.

I removed the photo diode so the unit will work in daylight.



On the back of the PCB, the circuit is very similar to cheap Ebay PIR, with the same control chip, BISS0001 and RC circuits to control trigger pulse and re-arming timing. Voltage regulator is a 3V, 30mA LDO – HT7130, not enough for the ESP8266 as it draw about 250mA with it’s radio on.


Next I removed 3 out of 5 470R current limiting resistors, disabling the LEDs. Out of the remaining 2 LEDs,  1 is for visual feedback and 1 for the opto-coupler. I replaced the 470R at the end of the project with a 1K to dim the light and lower the power consumption. Resistor R10 (pulse length) is changed to 100K from 1M to shorten the pulse, we only need enough time for the ESP8266 to power up get an IP, SNTP time and publish a MQTT message.


An opto-coupler was used in place of an LED to solve the voltage difference between the light and ESP8266. The voltage supplied to the LED is 4.5V and the chip runs on 3v, voltage at the output pin to the base of the switching transistor is only 1.8v.


A simple LDO is used to convert the 4.5v to 3.3v and the rest is hooked up based on this schematic. The pin CH_PD is pulled high when the PIR is triggered, booting the ESP8266. One abnormal behaviour with this light is that it will stay triggered if the ESP8266 radio is on,  the solution is to put it into deep sleep with the radio off after the message is sent.



Software and actual usage. 

ESP8266 – Native SDK.

nodemcu is a wonderful piece of code but it don’t work well on my ESP-01, it will go into a boot loop with just a few lines of code. Initially I though it was my mistake but it does that even with published examples.

Moving on, I took a bit of time to set up esp-open-sdk and started to work on what I think will be a base for my future ESP8266 projectts.

My basic code is based on tuanpm’s native mqtt library and stdout which reclaims the RX pin for use as GPIO3.

blink1The code basically logs on to my wifi AP then to the MQTT broker (mosquitto) on my server. By default, I can control a LED connected to GPIO2 by publishing a 1 or 0 to the topic subscribed by the ESP-01 and read a switch on GPIO0. RX/GPIO3 can be configure for other use.

Code available on my Github repository.