With this Weather API we can collect data for a city by the following methods: city name, city ID, geographic coordinates and even ZIP code. This API has an advantage of being free and easy to use. But there’s a limit; you cannot make more than 60/minute or 50k/day, and if you violate them, your account will be blocked.
In this tutorial, we’ve chosen to call API by city ID. Every city has an ID found on http://bulk.openweathermap.org/sample/city.list.json.gz. The extension .json is used for exchanging data between a browser and a server. The communication can be realized only if the data is in text format.
It has been used an WebClient Class – which helps to download files easily. In our case, the constructor WebClient is used to initialize a new instance:
WebClient client = new WebClient();
We need to encode the data on UTF8, which means it uses 8-bit blocks to represent a character with: client.Encoding = System.Text.Encoding.UTF8
If the internet connection cannot be established, the program will automatically display the message “Internet Connection failed – “throw new WeatherDataServiceException” when an unexpected problem with internet connection occurs. Using throw we create an exception.
In C#, we can use try and catch for the situations when a part of the code cannot succeed.
This works with text-to-speech API – the user asks about the weather and the response from the system is ” The weather in Boston is..”.
When you are working with text-to-speech API, you need to have a file that can be processed very quickly – that’s why we need to work with a server. For BING API, an authorization token is needed and it can be found on https://api.cognitive.microsoft.com/sts/v1.0/issueToken.
This API has a validity of 10 minutes. This worked great as a timer for counting to 9 and renewing the connection every 9 minutes.
As you can see in the picture below, you can see that the final result for the text-to-speech is “Testing the app for Device Plus”.
It was an interesting project overall. The project taught me how important and crucial it is to have a good synchronization between hardware and software to achieve the most optimal performance. Using a lot of libraries from Visual Studio made the project a little bit complicated, but in the end, the result was satisfying.
I think a project like this can be developed and improved in many ways to address each user’s needs.