Fast & Furious Exchange Rate Tracker With ESP8266 ESPresso Lite V2



Introduction: Fast & Furious Exchange Rate Tracker With ESP8266 ESPresso Lite V2

About: Maker evangelist. Start-up entrepreneur. Former civil servant & public school teacher.

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 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.

Step 1: Hardware Setup

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.)

Step 2: Fire Up the Arduino IDE

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.

Step 3: Dealing With JSON API Data

Using some useful functions like:

<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.

Step 4: Decoding Nested JSON Array

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);  

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);  


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


Step 5: Push Notification

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="">  </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.

Be the First to Share


    • Mason Jar Speed Challenge

      Mason Jar Speed Challenge
    • Bikes Challenge

      Bikes Challenge
    • Remix Contest

      Remix Contest