Fast & Furious Exchange Rate Tracker With ESP8266 ESPresso Lite V2

by espert in Circuits > Tools

1859 Views, 7 Favorites, 0 Comments

Fast & Furious Exchange Rate Tracker With ESP8266 ESPresso Lite V2

13681850_10154174191370339_1483215455_o.jpg

Inspired by a similar weekend project, I decided to test my programming chops to build a simple/quick & dirty/fast & furious project that uses the ESP8266 ESPresso Lite V2 board to grab external JSON API (in this case I turn to Fixer.io) for a currency exchange website and display it on the OLED screen. In view that I tend to spent some time travelling to US, Malaysia, Indonesia and Thailand, it will be good to use these currencies as comparison against the SGD.

Hardware Setup

13663369_10154174215675339_1650763978_o.jpg

As shown, the hardware setup is fairly easy: just plug the OLED display directly on top the built-in header on the ESPresso Lite V2 board (without the need to use any messy & often confusing jumper wires).

(More information about the hardware is available here.)

Fire Up the Arduino IDE

Screen Shot 2016-08-01 at 7.33.37 AM.png
Screen Shot 2016-08-01 at 7.36.31 AM.png
Screen Shot 2016-08-01 at 7.36.42 AM.png

In this example, the real magic is in the codes. To make it easier for me to test the Arduino sketch, I included my Wi-Fi login credentials (SSID and password) directly so that it will be connected automatically instead of having to configure the Wi-Fi connection each time I upload the sketch to the board. And since the exchange rate does not fluctuate much throughout the day, I decided to make the device sleep most of the time and only wakes up to report the exchange rate at 12-hour intervals.

Dealing With JSON API Data

Screen Shot 2016-08-01 at 7.36.54 AM.png

Using some useful functions like:

espert.wifi.getHTTP(host,path) 
espert.json.init(<string>)</string>
<string></string><string>espert.json.containsKey(<keyword>) </keyword></string>

which are already built-in the ESPert library, I can grab and parse the JSON data directly. However it get tricky when dealing with nested array of data shown above.

Decoding Nested JSON Array

13681850_10154174191370339_1483215455_o.jpg

Using an example borrowed from marcoschwartz, I managed to solve the problem by breaking down the JSON data into individual substrings can count their relative positions to the currency keyword to obtain the exchange rates.

String jsonExchange; int jsonIndex;<br>for (int i = 0; i < exchange.length(); i++) {    
	if (exchange[i] == '{') {      
	jsonIndex = i;      
	break;    }  }
jsonExchange = exchange.substring(jsonIndex);  
jsonExchange.trim();


int rateIndexUS = jsonExchange.indexOf("USD");  
int rateIndexINR = jsonExchange.indexOf("INR");  
int rateIndexMYR = jsonExchange.indexOf("MYR");  
int rateIndexTHB = jsonExchange.indexOf("THB");  

  
String priceStringUS = jsonExchange.substring(rateIndexUS + 5, rateIndexUS +11);  
String priceStringMYR = jsonExchange.substring(rateIndexMYR + 5, rateIndexMYR +10);  
String priceStringINR = jsonExchange.substring(rateIndexINR + 5, rateIndexINR +10);  
String priceStringTHB= jsonExchange.substring(rateIndexTHB + 5, rateIndexTHB +10);  

  
priceStringUS.trim();  
priceStringMYR.trim();  
priceStringINR.trim();  
priceStringTHB.trim();    


float priceUS = priceStringUS.toFloat();  
float priceINR = priceStringINR.toFloat();  
float priceMYR = priceStringMYR.toFloat();  
float priceTHB = priceStringTHB.toFloat();


espert.oled.print("USD:");  
espert.oled.println(priceUS);   
espert.oled.print("INR:");  
espert.oled.println(priceINR);  
espert.oled.print("MYR:");  
espert.oled.println(priceMYR);  
espert.oled.print("THB:");  
espert.oled.println(priceTHB);

Push Notification

Screen Shot 2016-08-01 at 7.46.25 AM.png
13901657_10154174729620339_887010864_o.jpg

Another feature which can easily added is the smartphone notification. As the OLED display the rates, it will also send a similar message to your smartphone, as shown below.

You can added the following code that will trigger the string of message to be pushed to your smartphone.

You will need to also declare the following variable earlier in the sketch:

String smartphone_key = "xxxxxx"; //change to your own smartphone key. Check out <a href="http://www.espert.io">  www.espert.io  </a> 

You can refer to this link for more information regarding creation of the cloud account and obtaining the smartphone key. Of course if you have a much better and efficient way to reduce repetitive lines of codes, do let me know. Thank you and have a good weekend.

(The full Arduino sketch codes is available on Github over here.)

Note: This Instructable is also published in another site.