Advertisement
learnelectronics

ESP8266 Bitcoin Price Tracker

May 29th, 2017
3,544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. /*
  2.  * ESP8266 Bitcoin Price Tracker
  3.  *
  4.  * learnelectronics
  5.  * 29 MAY 2017
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  *
  9.  *
  10.  *
  11.  * NOTE: If you have trouble with this sketch visit: http://www.coindesk.com/api/
  12.  */
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.                                                                   // Libraries
  22. #include <ESP8266WiFi.h>                                          //
  23. #include <ArduinoJson.h>
  24. #include <Wire.h>
  25. #include "SSD1306.h"
  26.  
  27.                                                                   // Pins
  28. #define SDA D2
  29. #define SCL D1
  30. #define I2C 0x3C
  31.  
  32.                                                                   // Create display
  33. SSD1306 display(I2C, SDA, SCL);
  34.  
  35.                                                                   // WiFi settings
  36. const char* ssid     = "xxxxxxxxxxxx";                            //replace with your SSID
  37. const char* password = "xxxxxxxxxxxx";                            //replace with your PW
  38.  
  39.                                                                   // API server
  40. const char* host = "api.coindesk.com";
  41.  
  42. void setup() {
  43.  
  44.                                                                   // Serial
  45.   Serial.begin(115200);
  46.   delay(10);
  47.  
  48.    // Initialize display
  49.   display.init();
  50.   display.flipScreenVertically();
  51.   display.clear();
  52.   display.display();
  53.  
  54.                                                                   // We start by connecting to a WiFi network
  55.   Serial.println();
  56.   Serial.println();
  57.   Serial.print("Connecting to ");
  58.   Serial.println(ssid);
  59.  
  60.   WiFi.begin(ssid, password);
  61.  
  62.   while (WiFi.status() != WL_CONNECTED) {
  63.     delay(500);
  64.     Serial.print(".");
  65.   }
  66.  
  67.   Serial.println("");
  68.   Serial.println("WiFi connected");  
  69.   Serial.println("IP address: ");
  70.   Serial.println(WiFi.localIP());
  71. }
  72.  
  73. void loop() {
  74.  
  75.                                                                  // Connect to API
  76.   Serial.print("connecting to ");
  77.   Serial.println(host);
  78.  
  79.                                                                  // Use WiFiClient class to create TCP connections
  80.   WiFiClient client;
  81.   const int httpPort = 80;
  82.   if (!client.connect(host, httpPort)) {
  83.     Serial.println("connection failed");
  84.     return;
  85.   }
  86.  
  87.                                                                 // We now create a URI for the request
  88.   String url = "/v1/bpi/currentprice.json";
  89.  
  90.   Serial.print("Requesting URL: ");
  91.   Serial.println(url);
  92.  
  93.                                                                 // This will send the request to the server
  94.   client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  95.                "Host: " + host + "\r\n" +
  96.                "Connection: close\r\n\r\n");
  97.   delay(100);
  98.  
  99.                                                                 // Read all the lines of the reply from server and print them to Serial
  100.   String answer;
  101.   while(client.available()){
  102.     String line = client.readStringUntil('\r');
  103.     answer += line;
  104.   }
  105.  
  106.   client.stop();
  107.   Serial.println();
  108.   Serial.println("closing connection");
  109.  
  110.                                                                 // Process answer
  111.   Serial.println();
  112.   Serial.println("Answer: ");
  113.   Serial.println(answer);
  114.  
  115.                                                                 // Convert to JSON
  116.   String jsonAnswer;
  117.   int jsonIndex;
  118.  
  119.   for (int i = 0; i < answer.length(); i++) {
  120.     if (answer[i] == '{') {
  121.       jsonIndex = i;
  122.       break;
  123.     }
  124.   }
  125.  
  126.                                                                 // Get JSON data
  127.   jsonAnswer = answer.substring(jsonIndex);
  128.   Serial.println();
  129.   Serial.println("JSON answer: ");
  130.   Serial.println(jsonAnswer);
  131.   jsonAnswer.trim();
  132.  
  133.                                                                 // Get rate as float
  134.   int rateIndex = jsonAnswer.indexOf("rate_float");
  135.   String priceString = jsonAnswer.substring(rateIndex + 12, rateIndex + 18);
  136.   priceString.trim();
  137.   float price = priceString.toFloat();
  138.  
  139.                                                                 // Print price
  140.   Serial.println();
  141.   Serial.println("Bitcoin price: ");
  142.   Serial.println(price);
  143.  
  144.                                                                 // Display on OLED
  145.   display.clear();
  146.   display.setFont(ArialMT_Plain_24);
  147.   display.drawString(26, 20, priceString);
  148.   display.display();
  149.  
  150.                                                                 // Wait 5 seconds
  151.   delay(5000);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement