Accessing a Web API

After long time without posting anything, here is a project done not so long ago. I think it is a good experiment in web access and if you are looking for a complex light control; here it is.

In this post I will try to explain a C# method to access a simple Web API to get the sunset time and the sunrise time based on latitude and longitude coordinates. But first a little bit of history…

Those old enough and used to electrical things, may promptly recognize the device below.

In this picture is possible to all the components of a street light photorelay.
 (Not much really…). The LDR (light-dependent resistor) on the right, the Thermal Relay on the left and In the center, the adjusting screw.

Working Principle:

If a strong light (The Sun) reaches the LDR it will drop the resistance, increasing the current and  through the heating coil, it heats up the bimetallic strip. Due to the heat generated by the coil, both of the metals are expanded. But expansion of metal B is more than expansion of metal A. Due to this dissimilar expansion the bimetallic strip will bend towards metal A, opening the normally close contacts. When there is no light enough to keep the strip bent, the contact will close turning on the light bulb circuit.

Now let see and IOT counterpart of a photo relay:

Netduino Basic Connections

This project access the site sunrise-sunset.org thru its API, build and sending the local coordinates like this:

http://api.sunrise-sunset.org/json?lat=-22.9520946&lng=-43.2261077

To access the API I used the WebRequestMultithreaded from Netduino Samples project. A very nice and clear C# web request solution. Apart of  a simple error handling added to be used on my project,  this Web Request is like the original, including the debug messages are still there.

The site API will reply a Json object like this:

{
"results": {
"sunrise": "7:59:05 AM",
"sunset": "9:19:14 PM",
"solar_noon": "2:39:09 PM",
"day_length": "13:20:09",
"civil_twilight_begin": "7:34:39 AM",
"civil_twilight_end": "9:43:40 PM",
"nautical_twilight_begin": "7:05:28 AM",
"nautical_twilight_end": "10:12:50 PM",
"astronomical_twilight_begin": "6:35:17 AM",
"astronomical_twilight_end": "10:43:01 PM"
},
"status": "OK"
}

As you can see; there are other data related to sun position on the replayed object and this project can easily be modified to use them. This can be done in HandleResults.ParseJson method, replacing the Json key names.

It’s necessary to use a Json parser to parse the object keys “sunrise” and “sunset”. Wandering on internet I found a nice Json parser  here . Thanks to Mario Vernari for sharing this nice work. Next step is convert the time string to milliseconds and correct the time by apply the offsets GMT  and summer time, if in use.

Next, using sunset and sunriset times, spin off two threads to wait to time out and execute the events, in this case is just turn on or off the IO port D0.

The program is set to repeat this sequence every day at 1:00PM, since the sunrise and sunset time changes a little bit day by day.

The program also saves the results as default values, case the API access fails three times, default values will be used instead.

Below a simplified flow chart of the hole process:

 The system clock must be correct in order to trigger turn on/off events in the right time. This makes this project a perfect match to another project published here in this blog.

The the project was original built in C# for Netduino 2 plus using Visual Studio 2012 IDE as other related posts in this blog. The code can be found on my GitHub repository here.

By the way; the coordinates used as example are from Cristo Redentor, Rio de Janeiro. A Nice place to watch the sunset or sunrise…