Main control

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply
Valued Contributor
Valued Contributor
Posts: 940
Joined: Wed May 31, 2017 11:57 am

Main control

Post by mnf »

Been a while since I've come up with any new tips (major problems with my eyes have meant little programming for the last few months :( )

So today:

Need to control a mains device using a micro controller. Minimal fuss and maximum safety! I wanted to use either a TP-Link plug or a sonoff device but neither seems to have a local command set or an easy to use web API.


Use IFTTT (IF This Then That) - goto Log in (I used my google account - but do as you will and use Facebook or Create account)
I found the IFTTT website a bit confusing at first - but once you get the hang of it..

1) Click on MyApplets then Create New Applet.

2_ Click on the blue +THIS
Search for WebHooks and select - then click on 'Receive a web request'. Set an event name to suit your purpose and then click Create Trigger.
3) Then click on (blue) +that

4) Search for the device you want to control - for example eWeLink (for sonoff devices) or TP-Link. You will be prompted to log into your account and then can choose from a list of your devices. Choose appropriate device and action.

5) Click create action and then finish.

6) You now need the web address to access to fire your event. (This is where I struggled for a while!) Click on WebHooks (top Left My Applets -> Web Hooks ->) then Settings (top right)
ifttt3.jpg (34.76 KiB) Viewed 3544 times
7) Select the URL and then right click and Goto....

Now you'll have a 'Your Key is:' and a Post / Get web request address which is what you need
ifttt4.jpg (31.62 KiB) Viewed 3544 times
8 ) Copy this! You need to replace {event} with the event name you used way back in step 2 (I used plug_on and plug_off for my two events)
9) Use and enjoy. I used a raspberry pi and 'curl' in a system command in a C block. I created a macro SwitchPlug(bool on):
plug.JPG (20.14 KiB) Viewed 3544 times
Where PLUG_ON and PLUG_OFF are local constants set to

Code: Select all

Now in my flowcode I can do SwitchPlug(true) - to turn on and SwitchPlug(false) to turn off..

(I could have saved a smidge of memory by building the strings at run-time, but on the Pi memory was not an issue)

Note that this should work with any device that can access the web (so I also use control from an Android phone) - for example an ESP device or an Arduino with Ethernet shield.
Some devices accept extra arguments (colour and brightness for example - will add details if there is interest?)


Post Reply