CoolKit (eWeLink) API Script in PHP
(Another donateWare script from MRE Software)

 

     I had a need to read the temperature from my Sonoff TH20 temperature probe in my duck pond and post it on my weather website.  I needed to run it as a simple cron job.  Searching on the web, I found no example code written in PHP to accomplish this task and I had no desire to install Node JS and a big application just to read the temperature.  So I set out to write a PHP script to do the job.  The code on this page is the result of my efforts.  Hopefully it will serve as an example for how to access this API using PHP.

     This script uses version 2 of the CoolKit API and implements the OAuth 2.0 credential and token system.  To use this you will need to register at https://dev.ewelink.cc/#/ and apply for developer status.  Once approved you go to the console and create an app.  You will be issued an app id and app secret.  When creating an app you will also need to specify a redirect url.  That url should point back to the location and name of this script on your website.  Once you have those credentials you edit the variables in the script to use them.  FYI: I named my script th20oa2.php and that is in the $redir variable in the code.  Change it if you want.

     How it works: After editing your credentials, redirection url, and device id you install the script on your website.  Make sure the redirection url on the CoolKit developer console matches the full url to the script.  This is a security measure and the authentication will fail without it.  The first thing you will need to do is log into the script using a web browser.  You do this by accessing the script with the login parameter.  example: https://yourdomain.com/th20oa2.php?login=1 When you do this the script will return a link to click.  Clicking the link will take you to the OAuth 2.0 login page where you use your normal eWeLink cellphone app credentials.  Once you hit the login button, CoolKit will send the authorization code back to the script on your website.  The script will process the code and obtain the access and refresh tokens needed to make API calls.  The tokens are valid for 30 days and will be stored in a file.  The good news is that the tokens can be refreshed without requiring another manual log in.  This script checks the expiration date and refreshes the tokens after 28 days so the script can operate indefinitely without requiring a human log in operation.  Once you are logged in and have the tokens the script can read your devices.  In my case, all I needed to do was read the temperature of one device but other users may want to do things like control devices etc.  Those operations are explained in the API documentation.  The hard part of using this API is logging in, getting, and refreshing the tokens.  I am publishing this to demonstrate how that is done with PHP.  It took me several hours to figure it all out so maybe this will save you some time!

     Developer's tip: The script has to be live on the Internet to get the tokens.  Once the tokens are obtained and saved to a file on your website, you can copy that token file to your local development computer environment and use the tokens for 28 days.  When they refresh they will need to be synced again.  This allows you to work on the script locally.

Check the update date/time on the second line of the script to insure that you have the latest version.

This code is for informational purposes only.
USE AT YOUR OWN RISK

ENJOY!

 


 

 
 
If you find this code of value, donate a little to the cause.  Think of the time you just saved!
USE AT YOUR OWN RISK!

Questions / Comments?  Email me.

MRE Logo