Tag Archives: API

PRACTICALLY USING THE LOAD-SHEDDING API

#89 An example app showing a practical use case with the API

Using the Eskom load-shedding API is very straight forward however I have noticed that a lot of people get stuck on the last call where an html document is returned instead of Json data. This can throw a spanner in the works because a developer can’t just get the results as a key value pair… the HTML must be parsed and then put into Json before it can be used effectively.

This parsing process is actually quite easy and jut requires a loop with a bit of regex magic to return the values line by line.

Once the values are returned we can then place them into our own Json object and then do something useful with them like creating a loop which reads the times and compares them to the time on my PC then when the json objects time is 10 minutes away the loop then initiates a shutdown of my fan so that it doesn’t drain my ups battery unnecessarily.

Below I have written a small proof of concept application in C# to help inspire others with their projects.

I make use of the LoadShedding.NET.dll library made by IsaTippens using my endpoint documentation and documentation from PolarizedIons.

However in the end I only focused on 2 API endpoints to simplify the example app.

This makes my example much simpler.

You can take a look at my example code on GitHub

TUYA SMART API

#55 TUYA API EXAMPLE USE

Tuya Smart is a China-based IoT provider. Tuya Smart provides all kinds of smart home gadgets and gizmos. A cloud support plat form exists as well as an app for Android and an app for IOS.

Here I have an example of the cloud API which can be queried from any device that has internet. The variables are passed as headers in each request.

PHP EXAMPLE:

cURL PHP query example

JavaScript jQuery EXAMPLE:

jQuery example query using headers.

ESKOM’s LOAD SHEDDING API

#52 LOADSHEDDING API

Eskom has a load shedding (Rolling blackouts) API we can utilize. I still need to test the accuracy of the results as well as the functionality of the API.

The good news is that unlike Eskom’s electricity supply this API is always on and actually works (with HTTPS as well)

It looks like Eskom’s software team can do a half decent job. I realize that it’s a bit unfair to give them so much grief. The real issue here is corrupt officials in high places stealing from the people as much as they can before their clocks expire.

There is no excuse… the reality is that we are stuck between a rock and a hard place. I can only hope that the future generations are able to see it for what it is and focus on building South Africa instead of destroying and stealing what others are so desperately trying to improve…

One thing is for sure.. at this rate we are truly moving in the direction of retardation (backwards motion) there’s no middle ground or some type of constant speed things are literally degrading. It’s honestly terrifying to witness this as a youngster.

At least we are not a war torn country like many in the middle east so perhaps I can give credence to the South African police and intelligence agency’s no matter how corrupt they have become they have not stooped to the level of warmongers just yet… (I suppose elite gangsters and mobsters would be a better suited description at this time)

Below is an example of the API I have rewritten in markdown format. A simple four step process to get some usable data.

  1. Get Status
  2. Get Municipalities
  3. Get Surburb Data
  4. Get ScheduleM

    Checkout my practical example application on GitHub
    You can also find more info on my GitHub documentation and my tutorial article of the practical example.

PHP JSON API EXAMPLE

#10 PHP JSON API EXAMPLE

Example calling the scripts in Google Chrome

JSON: JavaScript Object Notation

PHP: Hypertext Preprocessor

MySQL:My Structured Query Language

A simple basic JSON API built with PHP and MySQL. Inside the api folder there are 4 PHP scripts. These scripts can be called using two parameters:

The API Key: e.g = API_KEY=926bc852-1d16-414e-8112-5595bc16ffeb

The Username: e.g = USERNAME=admin@gmail.com

Once the API key is verified the query will return a JSON string with the specified data returned. For example calling checkApiKey.php will return the JSON {"result": 0,"id": "16"} where result = 0 means success and any other result will be a failure code.

A full example calling checkApiKey.php bellow:

http://localhost/PHP_API/api/checkApiKey.php?API_KEY=926bc852-1d16-414e-8112-5595bc16ffeb&USERNAME=admin@gmail.com

Be sure to import test.sql into your MySQL database. test.sql contains two basic tables namely api_keys and users containing the data we want to query with this API.

Please note this is for demonstration purposes. Quite a few security checks have been left out for simplicity’s sake. I’ll make a sophisticated PHP API at a later stage.

API keys database in PHPMyAdmin
Users database in PHPMyAdmin