Last active
July 31, 2016 14:39
-
-
Save willhooi/5a0eaebf0be179d02333eceac93273b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESPert.h> | |
ESPert espert; | |
const char *host = "api.fixer.io"; | |
const char *mqhost = "www.espert.io"; | |
const char *ssid = "xxxxx"; //change to your own SSID | |
const char *password = "xxxxxx"; //change to your own password | |
String smartphone_key = "xxxxxx"; //change to your own smartphone key. Check out www.espert.io | |
const int httpPort = 80; | |
const char *path = "/latest?base=SGD"; | |
const int sleepTimeS = 43200; //sleep for 12 hours | |
void setup() { | |
espert.init(); | |
espert.oled.init(); | |
delay(2000); | |
espert.oled.clear(); | |
espert.oled.println(espert.info.getId()); | |
espert.oled.println(); | |
int mode = espert.wifi.init(); | |
WiFi.begin (ssid, password); | |
if (mode == ESPERT_WIFI_MODE_CONNECT) { | |
espert.println(">>> WiFi mode: connected."); | |
espert.oled.println("WiFi: connected."); | |
espert.oled.print("IP..: "); | |
espert.oled.println(espert.wifi.getLocalIP()); | |
} else if (mode == ESPERT_WIFI_MODE_DISCONNECT) { | |
espert.println(">>> WiFi mode: disconnected."); | |
espert.oled.println("WiFi: not connected."); | |
} else if (mode == ESPERT_WIFI_MODE_SMARTCONFIG) { | |
espert.println(">>> WiFi mode: smart config."); | |
} else if (mode == ESPERT_WIFI_MODE_SETTINGAP) { | |
espert.println(">>> WiFi mode: access point."); | |
} | |
delay(2000); | |
} | |
void loop() { | |
String exchange = espert.wifi.getHTTP(host, path); //get exchange rates from api.fixer.io | |
if (espert.json.init(exchange)) { | |
if (espert.json.containsKey("base")) { | |
espert.oled.clear(); | |
espert.oled.println("CURRENCY EXCHANGE"); | |
espert.oled.print("Base currency:"); | |
espert.oled.println(espert.json.get("base")); | |
espert.oled.print("Date:"); | |
espert.oled.println(espert.json.get("date")); | |
} | |
} | |
String jsonExchange; | |
int jsonIndex; | |
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); | |
delay(10000); | |
String message = "Currency Exchange Rate: SGD/USD: " + (String)(priceUS) + " "+ "SGD/INR: " + (String)(priceINR) + " " + "SGD/MYR: " + (String)(priceMYR) + " " +"SGD/THB:" + (String)(priceTHB); | |
message.replace(String(" "), String("%20")); | |
String path = "/MySmartphone/send?key=" + smartphone_key + "&message=" + message; | |
espert.println(">>" + espert.wifi.getHTTP(mqhost, path.c_str()) + "<<"); | |
delay(30000); | |
espert.oled.clear(); | |
ESP.deepSleep(sleepTimeS * 1000000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment