loading

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.

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:

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.

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

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

About This Instructable

395views

3favorites

License:

Bio: Maker evangelist. Start-up entrepreneur. Former civil servant & public school teacher.
More by espert:Fast & furious exchange rate tracker with ESP8266 ESPresso Lite V2 Easy NeoPixel Programming with Espert library Build your very first Wi-Fi webserver 
Add instructable to: