Currency rate - currconv.com - ESP32 - Webclient

martinius96
Posts: 33
Joined: Thu Dec 13, 2018 1:39 am

Currency rate - currconv.com - ESP32 - Webclient

Postby martinius96 » Mon Nov 18, 2019 3:11 pm

The currency exchange rate is a very interesting piece of information that can be used in a project with Arduino or a development board from the Espressif platform. There are a number of freely available and documented APIs that can be used to get the current exchange rate of two currencies. For this tutorial, I used the service http://free.currconv.com/, which offers a simple exchange rate of two currencies in JSON format. The user uses his API key and the mentioned currencies in the query to access the exchange rates of those currencies. To get the key just ask via the form on the page, it requires only email. For this tutorial, we'll show the exchange rate between CZK and EUR. I we will count the value of one euro in Czech crowns.

The free program currconv service is limited to 60 queries per hour. If it is exceeded, the user is blocked within the next hour. JSON is a data format with a value that belongs to a variable. The variable is enclosed in quotation marks, and the entire data package is contained in {} brackets.

Service output for EUR_CZK rate: {"EUR_CZK":25.574488}

In our case, the numeric value is 25.574488 and the variable whose value belongs to EUR_CZK. JSON will find application especially in automation systems, big data, databases. Platform Arduino, respectively. ESP32 also has an ArduinoJson library that can be installed to break whole fields with JSON data through it, but in this example we will only use the substring function of the String object to cut off any unnecessary parts, i. {"EUR_CZK":}.

The microcontroller loads into the service output variable as a string that can then be clipped. This means that substring is applied to characters 11-20, which returns only a numeric value in the output, all characters less than 11 and more than 20 are trimmed. To make the rate more usable, we cast it to a float variable with two decimal places (rounding is also taken into account). In the picture you can see the output of Serial Monitor for ESP32 and its example for reading EUR - CZK currency.
Image
Currconv.com can be accessed under HTTP protocol (use for Arduino and Ethernet shield / module, does not support HTTPS) and HTTPS connectivity for ESP8266, ESP32. The ESP32 development board also needs to import the Root CA certificate into the program, in this case CloudFlare Inc ECC CA-2 (used in the example). For ESP8266 it is possible to use fingerprint certificate in SHA1 format (see ES8266 Arduino core 2.5.0+ WiFiClientSecure specification).
Serial monitor output with HTTP header and response:
Image
Currconv.com has a wide variety of currencies that can be combined with each other to get a course.
A complete list of possible names (including Bitcoin) is available at: https://free.currconv.com/api/v7/curren ... do-not-use. The obtained course can be further used, for example for displaying on the display (TFT, LCD), sending it to the database or otherwise using it.
Other projects can be found on my site: https://arduino.php5.sk/?lang=en
If there is time in the future, I can add for other platforms (Arduino + Ethernet W5100 / W5500, ESP8266).


Remember to modify API key, query for state currency in program
Program for ESP32 (Core 0.9X - 1.0.4 compatible):

Code: Select all

//Author: Bc. Martin Chlebovec
//Website: https://arduino.php5.sk?lang=en
//PayPal: martinius96@gmail.com
#include <WiFi.h>
#include <WiFiClientSecure.h>
const char * ssid = "WIFI_NAME";
const char * password = "WIFI_PASS";
const char* host = "free.currconv.com";
const int httpsPort = 443; //http port
WiFiClientSecure client;
const char* test_root_ca = \
                           "-----BEGIN CERTIFICATE-----\n" \
                           "MIIDdzCCAl+gAwIBAgIEAgAAuTANBgkqhkiG9w0BAQUFADBaMQswCQYDVQQGEwJJ\n" \
                           "RTESMBAGA1UEChMJQmFsdGltb3JlMRMwEQYDVQQLEwpDeWJlclRydXN0MSIwIAYD\n" \
                           "VQQDExlCYWx0aW1vcmUgQ3liZXJUcnVzdCBSb290MB4XDTAwMDUxMjE4NDYwMFoX\n" \
                           "DTI1MDUxMjIzNTkwMFowWjELMAkGA1UEBhMCSUUxEjAQBgNVBAoTCUJhbHRpbW9y\n" \
                           "ZTETMBEGA1UECxMKQ3liZXJUcnVzdDEiMCAGA1UEAxMZQmFsdGltb3JlIEN5YmVy\n" \
                           "VHJ1c3QgUm9vdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKMEuyKr\n" \
                           "mD1X6CZymrV51Cni4eiVgLGw41uOKymaZN+hXe2wCQVt2yguzmKiYv60iNoS6zjr\n" \
                           "IZ3AQSsBUnuId9Mcj8e6uYi1agnnc+gRQKfRzMpijS3ljwumUNKoUMMo6vWrJYeK\n" \
                           "mpYcqWe4PwzV9/lSEy/CG9VwcPCPwBLKBsua4dnKM3p31vjsufFoREJIE9LAwqSu\n" \
                           "XmD+tqYF/LTdB1kC1FkYmGP1pWPgkAx9XbIGevOF6uvUA65ehD5f/xXtabz5OTZy\n" \
                           "dc93Uk3zyZAsuT3lySNTPx8kmCFcB5kpvcY67Oduhjprl3RjM71oGDHweI12v/ye\n" \
                           "jl0qhqdNkNwnGjkCAwEAAaNFMEMwHQYDVR0OBBYEFOWdWTCCR1jMrPoIVDaGezq1\n" \
                           "BE3wMBIGA1UdEwEB/wQIMAYBAf8CAQMwDgYDVR0PAQH/BAQDAgEGMA0GCSqGSIb3\n" \
                           "DQEBBQUAA4IBAQCFDF2O5G9RaEIFoN27TyclhAO992T9Ldcw46QQF+vaKSm2eT92\n" \
                           "9hkTI7gQCvlYpNRhcL0EYWoSihfVCr3FvDB81ukMJY2GQE/szKN+OMY3EU/t3Wgx\n" \
                           "jkzSswF07r51XgdIGn9w/xZchMB5hbgF/X++ZRGjD8ACtPhSNzkE1akxehi/oCr0\n" \
                           "Epn3o0WC4zxe9Z2etciefC7IpJ5OCBRLbf1wbWsaY71k5h+3zvDyny67G7fyUIhz\n" \
                           "ksLi4xaNmjICq44Y3ekQEe5+NauQrz4wlHrQMz2nZQ/1/I6eYs9HRCwBXbsdtTLS\n" \
                           "R9I4LtD+gdwyah617jzV/OeBHRnDJELqYzmp\n" \
                           "-----END CERTIFICATE-----\n";

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  client.setCACert(test_root_ca);
  Serial.println("WiFi uspesne pripojene");
  Serial.println("IP adresa: ");
  Serial.println(WiFi.localIP());
  Serial.println("Ready");
  client.stop();
  if (client.connect(host, httpsPort)) {
    String url = "/api/v7/convert?q=EUR_CZK&compact=ultra&apiKey=XXXXXXXXX";
    client.print(String("GET ") + url + " HTTP/1.0\r\n" + "Host: " + host + "\r\n" + "User-Agent: ESP32\r\n" + "Connection: close\r\n\r\n");
    while (client.connected()) {
      String line = client.readStringUntil('\n');
      if (line == "\r") {
        break;
      }
    }
    String line = client.readStringUntil('\n');
    String abc = line.substring(11, 20);
    Serial.println("Kurz (orezany text): ");
    Serial.println(abc);
    float kurz = abc.toFloat();
    Serial.println("Kurz (float): ");
    Serial.println(kurz);
  }
}

void loop() {

}

Who is online

Users browsing this forum: No registered users and 35 guests