I usually start my posts by warning readers that I'm an amateur with no background in coding or electronics. I write these blogs because while I'm figuring out how to do things, I'm working at ground level and my descriptions are generally simple enough for others like myself to follow. There are few complicated tech terms - just idiot-proof directions. That said, this post assumes a working knowledge of Raspberry Pi and Node Red.
This particular project was one of the most challenging I've encountered. I slogged for days trying to figure out why is wasn't working. Hopefully this post will save others some trouble.
Home Automation with Node Red
My entire home automation is controlled by Node Red running on a RasPi. I tried Home Assistant a long time ago, but I found it much more complicated than NR. Dragging wires around is much easier for me than deciphering yaml scrips.
My Node Red incorporates MQTT.
RF, IR and WiFi
Node Red communicates with devices all over the house. Until now, everything was either RF, IR or WiFi. The WiFi devices I built myself with ESP8266s, and Raspberry Pis. A connected Broadlink hub does most of the IR and RF work. Searching whatimade.today will turn up dozens of examples.
Motion Sensor (PIR)
One of the devices I've always wanted for my arsenal is a motion (PIR) sensor. I've built several of them, but always faced the same problems. First, I could never make them stable - the WiFi signal somehow interfered and gave me repeated false positives. I tried all sorts of shielding, but nothing worked - I just couldn't trust the output.
The second thing was power. Both the ESP and the PIR sensor need constant voltage - and no small amount of current. Even 18650s couldn't be relied on for long.
So, all my PIR attempts were eventually abandoned.
I began my Home Automation odyssey around 2016, a couple of years before commercial gadgets flooded the market. I started by flashing ESP8266s and connecting them to relays. It was about two years later that Sonoff entered the field - then all the rest piled on.
IKEA joined the fray (at least in my part of the world) around 2019. By then, my house was automated with WiFi, RF and IR. IKEA used Zigbee, which wasn't on my radar.
Good Quality, Low Cost
During a recent saunter around IKEA I came across their smart bulbs and switches and realized they're well priced and pretty good quality. Maybe there was place for them in my home automation after all? The Motion Sensor intrigued me most - it operates on a couple of CR2032s and claims to work for months.
I put one into my basket along with a dimmable smart bulb. My intention was to get it all working with Node Red without an IKEA hub. (At the time I wasn't even aware that I could pair the bulb to the sensor directly, without the need for anything in between.)
Connecting to Node Red
Zigbee needs its own controller and I'm pleased to say they can be acquired from Aliexpress for a few dollars. The CC2531 can be purchased without firmware, but this requires you to flash it yourself. As I was unfamiliar with the whole process, I bought one already flashed with "SONOFF Zigbee for Zigbee2mqtt."
When I bought it, I had no idea how it would work. Indeed, with it being a "Sonoff" I didn't know if it would work at all with an IKEA device. (To be honest, I still don't know. I tried to find out if there's a difference between regular CC2532 zigbee2mqtt firmware and that of Sonoff, but I never found the answer. If anyone knows, maybe you can leave a comment).
My CC2531 device arrived and then I set about understanding how to make it work.
Setting up the CC2531
The first thing you need to do is get your RasPi communicating with the dongle. It's a USB device but Raspberry Pi OS doesn't know what to do with it without the necessary firmware.
Most of the instructions you'll need can be found at zigbee2mqtt.io. It's a terrific place for information, but for those lacking experience, you'll need to read it carefully.
Here's an example. If you hit the "Getting Started" page, it takes you to this page:
I immediately started typing commands... until, half way down the page I noticed the sentence, "It's assumed, that you've a recent version of Docker and Docker-Compose is installed."
Of course I didn't, so all my typing and running commands had already filled up my pi with redundant crap that I didn't know how to get rid of!
After a bit more clicking around the site, I found the Linux instructions page.
I followed the instructions to the letter and the dongle was quickly installed.
Next step is to install zigbee2mqtt in Node Red. This is also pretty simple. Go to Manage Palette and install this:
Once that's done, you need to get the zigbee2mqtt bridge node to recognize your zigbee devices. For me, this meant putting my IKEA bulb and Motion Sensor into pairing mode and waiting for my dongle to find them. At zigbee2mqtt.io there's a detailed page with instructions for all the devices that are known to pair with it.
Once your devices are in pairing mode tell your bridge to search.
For me, that too, went without a hitch. Within five minutes my bulb and sensor were neatly registered.
So far, so good, I hear you say. What's so complicated about that? Everything went smoothly until now.
Yes, it did. But it's from this point on that I got tied in knots for six solid days.
My Bulb worked fine; with simple commands from an Input Node, I could switch it on and set its brightness without a problem.
But the Motion Sensor was another story altogether. Although it was recognized by the Bridge, it detected nothing. Zilch. I jumped up and down in front of it for hours, waving my arms, and it paid no attention. I brought Significant Other to join me, but our combined bulk did nothing to attract its attention. At first I thought it was faulty, but along the way I paired it directly with the bulb - and it worked perfectly.
To be honest, I wasn't sure how to get zigbee2mqtt to talk to mqtt. Yes, it's written in technical gobbledegook everywhere, but no matter what I did, it didn't work, so I assumed it was my ignorance. I spent days searching forums (fora?), quizzing Google, and trying to understand what I didn't understand.
No videos on youtube covered my very specific problem with the Motion Sensor and I couldn't find a blog post anywhere that dealt with the issue. (Which is one of the reasons I'm writing this.)
I even posted on a Node Red Facebook group. Of the two answers I got, one recommended I migrate to Home Assistant, and the other that "you don't need a hub with IKEA devices" - which kind of missed the point.
It literally took me days and days, looking everywhere and trying everything and I was getting really, really frustrated. This page confirmed that I was doing everything right, but for the life of me, no matter what I did, I could not get the Motion Sensor to activate. It simply ignored all movement. It was supposed to be, simple, but nothing worked.
Each day I'd arrive with another idea. Each day, I tried something else. I even set everything up on another experimental RasPi, away from my daily Node Red.
Day Six and Eureka!
The attentive reader will have noticed, that at the top of this post, I wrote "I had no idea how it would work. Indeed, with it being a "Sonoff" I didn't know if it would work at all with an IKEA device."
It had been niggling at me for days. Was it possible that the Dongle Firmware is the problem? Even if the Sonoff firmware IS compatible with IKEA, (which I still don't know) I did buy the thing for pennies from Aliexpress so I couldn't really expect top quality.
I had nothing to lose. If the thing only worked with the bulb, it wasn't much use for all my other automation ideas.
But, how could I update the Dongle? I don't have the necessary CC Debugger and I didn't want to wait weeks for it to arrive from Aliexpress - let alone fork out for something I'd only use once.
Then I discovered this page. that showed me how I could do it with a RasPi.
The biggest problem was the microscopic connector on the CC2532. I have quite a collection of JST connectors, but nothing as tiny as this guy.
Fortunately, I'm quite a whiz at micro-soldering and this was an appropriate challenge.
Following the wiring diagram very closely, I soldered one end of a JST cable onto the designated pins of the dongle connector.
It's not a very neat job, but I only need it to run once.
Into the other side of the cable, I plugged Dupont wires. This made it easy to plug the appropriate wires into the RasPi pins. (Note, when I did the flashing, the dongle was NOT plugged into the Pi USB. Instead it gets its power from the 3.3v pin of the RasPi.)
It's imperative to get the wiring right, so in my case, I had to pay very close attention to the fact the Dupont wires were a different colour from the JST wires. With a continuity meter I also checked that the wires were connected ONLY to the correct pins on the dongle. (The pins are so small, it's easy to bridge between them). I also had to make sure I was looking at my RasPi pins the right way up.
The only problem I had was that WiringPi was not in my RasPi and I had to get it from somewhere. If you run the script as it appears on the page, it won't work because WiringPi has been abandoned by its creator. However, following instructions on this page will get you what you need.
I'm no longer invisible
When I plugged my re-flashed dongle back into my Pi, nothing worked. Even the bulb wouldn't light. Then I realized that I'd wiped everything clean, so I did the pairing business again and reconfigured the Node Red Bridge. Immediately, without a moment's delay, my motion sensor spotted me! Within two minutes, I had it ringing a bell, then turning on my irrigation. At last it's working and I'm a very satisfied blogger.