Some folks actually have a weird attitude towards PHP. I understand that it isn’t a “pure” language. It’s not truly “objective”. It’s inconsistent. Too many functions. Whatever. For me it’s still the Swiss Army Knife of programming languages (after “C” of course).
So I need a Wemo server. Something that behaves like a Belkin “smart” plug that Amazon’s Alexa can identify. Only that it manages a number of devices. In other words – Alexa sees a number of Wemo compatible plugs but in reality it’s my Raspberry Pi that talks “Wemo” towards Alexa and does whatever I want with the commands it receives.
Here’s a short primer:
Wemo-devices are identified by Alexa by answering a multicast udp ping with a setup-url that contains a port number that is unique for that device. Alexa will then send an HTTP query to that port and the Wemo-device answers with it’s “friendly” name and other related stuff. If Alexa wants to send a command, it again sends an HTTP request to the specified url (including the unique port) and the device acts accordingly.
I went out looking for a Wemo server that would easily be hacked so that it would do what I require – converting the Wemo protocol into my masterminded “Ela” protocol that is being used to talk to my (hacked) switches in the house. Though I found a few, they were either in python (nothing wrong with that, I just hope the python people can finally agree on what is THE version number). But I didn’t want to install a complete python environment and I didn’t want to re-program my “Ela” protocol in python. So I looked for either “C” or “PHP” versions. But I tend to grasp my chest and look for my heart pills (just kiddin’) whenever I read something like “phar” or “composer”. People – it’s a small script. I don’t need special libraries or special environments for that.
So what is a hacker-woman supposed to do if she doesn’t find what she wants? Right. She asks her husband for help. And helping he did. He brought a cup of steaming hot coffee (with cream) and said – have fun. After all – he’s busy mastering his .. 8th or 9th computer language. This time it is “swift“. No wonder our parrot is called “Cocoa” ?
And fun I had.
Here’s the principal idea:
- Create sockets for the discovery UDP port and for all pseudo-devices;
- Wait on socket_select() until something happens;
- Make a socket_getsockname() request to find out which port was targeted;
- Write a little code to get the message and to parse it;
- Send back the right XML or Soap answer;
- Convert and/or forward the request to whatever you want;
- Enjoy the coffee;
That’s it. It took half an afternoon and about 355 lines of code, from which about 100 are the pre-formatted xml responses and 50 or so are needed for my revolutionary “Ela” protocol. It’s fantastic. There was never a better protocol. So great. You get my drift 😉
So .. why did I do that?
Easy. It completely circumvents the Alexa “skill” environment. Alexa speaks “Wemo” natively which allows you to put the discovered devices into simple groups with a simple wake word. In order to turn my office light on, I don’t have to invoke the skill name (“Alexa .. tell office to turn the light on“) – a simple “Alexa .. office light on” will do now. That’s really cool for simple devices, but I’ll have to maintain the skill environment for those actions that require a dialog or to play audio or video.
PHP to the rescue. Again.
PS: I am perfectly willing to share the php code. Let me know. I’ll clean it up a little an make it available. It’s been written in PHP7 (though I’d say it will work in PHP5 as well) – no special libraries needed.
About the author:
Michaela Merz is an entrepreneur and first generation hacker. Her career started even before the Internet was available. She invented and developed a number of technologies now considered to be standard in modern web-environments. Among other things, she developed, founded, managed and sold Germany’s third largest Internet Online Service “germany.net” . She is very much active in the Internet business and enjoys “hacking” modern technologies like block chain, IoT and mobile-, voice- and web-based services.