Build an Android App to Control Tp-Link Smart Plugs and Sockets With DroidScript
by wiseman-working in Circuits > Mobile
1289 Views, 1 Favorites, 0 Comments
Build an Android App to Control Tp-Link Smart Plugs and Sockets With DroidScript
We'll build an Android app using a simple coding tool called "DroidScript" to remotely control a TP-Link smart plug. This tutorial shows you how to remotely control individual plugs on the socket to turn a lamp on and off.
Supplies
To complete this tutorial you will need the following:
- DroidScript app - You can download this on the Google PlayStore by searching for 'DroidScript' - https://play.google.com/store/apps/details?id=com.smartphoneremote.androidscriptfree
- A TP-Link smart plug like these:
- https://www.amazon.com/Kasa-Smart-Protector-Required-KP303/dp/B083JKSSR5/
- https://www.amazon.com/TP-Link-KP100KIT-Wi-Fi-Smart-2-Pack/dp/B079C5ZGHL/
- A web browser - This is optional as you can just use the app on your mobile device if preferred
Setup
To complete this tutorial you need to install DroidScript from the PlayStore:
- Using your android phone or tablet, open the PlayStore and search for DroidScript (all one word)
- You should see an application with the title 'DroidScript - JavaScript IDE' - Install this app
You will need to sign up for DroidScript premium for at least one month to get access to the Node.js features, but it's only cheap and you can easily unsubscribe with a couple of clicks using the Google Play app.
For more information on upgrading to DroidScript premium please take a look on the website at www.droidscript.org
Using Your Web Browser
This step is optional, but personally I find it easier to programme in a web browser rather than directly on my mobile device. If you would like to use your web browser to complete this tutorial simply start up the DroidScript app and press the wi-fi symbol at the top of the screen (as shown in image above).
It should turn green and an ip address will appear. Simply type this address into your web browser to load up the IDE.
Coding
Please follow this video tutorial and feel free to extend the functionality to build something unique and exciting!
I have attached the source code in the next step so you can copy + paste the code into your own project to make things easier for you.
Supporting Source Code
//Configure app to use NodeJS as the main scripting engine //giving you the full power of Node directly in your app! cfg.Node //Configure for Material UI and light theme. cfg.MUI, cfg.Light //Make sure the required node modules are installed to ide. //(This downloads modules from https://www.npmjs.com). ide.AddModule( "tplink-smarthome-api" ) //Called when application is started. function OnStart() { //Set MUI primary color. app.InitializeUIKit( MUI.colors.teal.teal, "Light" ) //Create a MUI card layout. lay = MUI.CreateLayout("Linear", "VCenter,FillXY") const { Client } = require('tplink-smarthome-api'); const client = new Client(); // Look for devices, log to console, and turn them on client.startDiscovery().on('device-new', (device) => { device.getSysInfo().then(console.log); let swi = app.AddSwitch( lay, device.alias, .9, .1 ) swi.SetChecked( device.inUse ) swi.SetOnTouch( swi_OnTouch ) swi.plug = device //device.setPowerState(true); }); //Add main layout to app. app.AddLayout(lay) } function swi_OnTouch( toggle ) { this.plug.setPowerState( toggle ) }