Components and supplies
Arduino GIGA R1 WiFi
Arduino® GIGA Display Shield
Apps and platforms
arduino IDE
Project description
Code
GIGACryptoDisplay
c
Get crypto info via API and display it on a GIGA
1#include <WiFi.h> 2#include <SPI.h> 3#include <ArduinoHttpClient.h> 4#include <ArduinoJson.h> 5#include "Arduino_GigaDisplay_GFX.h" 6 7// Define colors for use with the display 8#define BLACK 0x0000 9#define GREEN 0x07e0 10#define RED 0xb000 11#define WHITE 0xffff 12 13// Replace with your network credentials 14const char* SSID = " "; 15const char* PASS = " "; 16 17int status = WL_IDLE_STATUS; 18 19// Create the display object that will be used for display functions 20GigaDisplay_GFX display; 21 22// Variables to keep track of previous crypto prices 23float lastUSDPrice = 0; 24float lastEURPrice = 0; 25float lastETHPrice = 0; 26 27// Variables to keep track of the cryptos value difference between each update 28float usdDiff = 0; 29float eurDiff = 0; 30float ethDiff = 0; 31 32void setup() { 33 // Initialize serial communication 34 Serial.begin(9600); 35 delay(3000); 36 display.begin(); //init display library 37 display.setRotation(1); // rotates the display to horizontal 38 39 // Connect to Wi-Fi 40 display.fillScreen(BLACK); // Makes the screen blank 41 display.setCursor(10, 10); //x,y 42 display.setTextSize(5); //adjust text size 43 display.print("Connecting to Wi-Fi...."); 44 45 while (status != WL_CONNECTED) { 46 status = WiFi.begin(SSID, PASS); 47 48 // wait 10 seconds for connection: 49 delay(10000); 50 } 51 52 display.fillScreen(BLACK); // Makes the screen blank 53 display.setCursor(10, 10); //x,y 54 display.print("Connected to Wi-Fi!"); 55 56 // This function makes the API call and displays the results 57 displayCryptoPrice(); 58} 59 60void loop() { 61 // Sets the waiting time between the API calls. This delay function will wait for 5 minutes 62 // before it will use the API call to update the currency values. If you want a shorter interval change this number 63 delay(300000); 64 displayCryptoPrice(); 65} 66 67void displayCryptoPrice() { 68 if (WiFi.status() == WL_CONNECTED) { 69 String usdDiffText, eurDiffText, ethDiffText; // Variables that keep track of the value difference 70 int httpPort = 443; // Port for connecting with HTTPS 71 char serverAddress[] = "api.coinbase.com"; // Server to connect to 72 WiFiSSLClient wifi; 73 HttpClient client = HttpClient(wifi, serverAddress, httpPort); 74 client.get("/v2/exchange-rates?currency=BTC"); // API call 75 76 String result = client.responseBody(); // Collects the API response 77 78 if (result > 0) { 79 DynamicJsonDocument doc(32000); 80 deserializeJson(doc, result); 81 82 //Sorts the information from the API call 83 String currency = doc["data"]["currency"]; 84 float usdPrice = doc["data"]["rates"]["USD"]; 85 float eurPrice = doc["data"]["rates"]["EUR"]; 86 float ethPrice = doc["data"]["rates"]["ETH"]; 87 88 // Display the selected crypto currency on the display, which is gotten from the API call. 89 // If you change the call to track another crypto this will update 90 display.fillScreen(BLACK); 91 display.setCursor(10, 10); 92 display.setTextSize(5); 93 display.print(currency + " Prices"); 94 95 /* Shows the BTC price in USD */ 96 // Changes tect color depending on the updated value 97 if(lastUSDPrice < usdPrice){ 98 display.setTextColor(GREEN); 99 usdDiff = usdPrice - lastUSDPrice; 100 usdDiffText = "(+" + String(usdDiff) + ")"; 101 } 102 if(lastUSDPrice > usdPrice){ 103 display.setTextColor(RED); 104 usdDiff = usdPrice - lastUSDPrice; 105 usdDiffText = ("(" + String(usdDiff) + ")"); 106 } 107 if(lastUSDPrice == 0){ 108 display.setTextColor(WHITE); 109 usdDiffText = " "; 110 } 111 // Updated last value for future comparison 112 lastUSDPrice = usdPrice; 113 // Puts the info on the display 114 display.setTextSize(5); 115 display.setCursor(10, 100); 116 display.print("USD: "); 117 display.setCursor(130, 100); 118 display.print(usdPrice, 2); 119 // Puts the value difference on the display 120 display.setTextSize(2); 121 display.setCursor(440, 100); 122 display.print(usdDiffText); 123 124 /* Shows the BTC price in EUR */ 125 // Changes tect color depending on the updated value 126 if(lastEURPrice < eurPrice){ 127 display.setTextColor(GREEN); 128 eurDiff = eurPrice - lastEURPrice; 129 eurDiffText = ("(+" + String(eurDiff) + ")"); 130 } 131 if(lastEURPrice > eurPrice){ 132 display.setTextColor(RED); 133 eurDiff = eurPrice - lastEURPrice; 134 eurDiffText = ("(" + String(eurDiff) + ")"); 135 } 136 if(lastEURPrice == 0){ 137 display.setTextColor(WHITE); 138 eurDiffText = " "; 139 } 140 // Updated last value for future comparison 141 lastEURPrice = eurPrice; 142 // Puts the info on the display 143 display.setTextSize(5); 144 display.setCursor(10, 190); 145 display.print("EUR: "); 146 display.setCursor(130, 190); 147 display.print(eurPrice, 2); 148 // Puts the value difference on the display 149 display.setTextSize(2); 150 display.setCursor(440, 190); 151 display.print(eurDiffText); 152 153 // /* Shows the BTC price in ETH */ 154 // Changes tect color depending on the updated value 155 if(lastETHPrice < ethPrice){ 156 display.setTextColor(GREEN); 157 ethDiff = ethPrice - lastETHPrice; 158 ethDiffText = ("(+" + String(ethDiff) + ")"); 159 } 160 if(lastETHPrice > ethPrice){ 161 display.setTextColor(RED); 162 ethDiff = ethPrice - lastETHPrice; 163 ethDiffText = ("(" + String(ethDiff) + ")"); 164 } 165 if(lastETHPrice == 0){ 166 display.setTextColor(WHITE); 167 ethDiffText = " "; 168 } 169 // Updated last value for future comparison 170 lastETHPrice = ethPrice; 171 // Puts the info on the display 172 display.setTextSize(5); 173 display.setCursor(10, 280); 174 display.print("ETH: "); 175 display.setCursor(130, 280); 176 display.print(ethPrice, 2); 177 // Puts the value difference on the display 178 display.setTextSize(2); 179 display.setCursor(440, 280); 180 display.print(ethDiffText); 181 182 // Adding additional information 183 display.setTextColor(WHITE); 184 display.setTextSize(4); 185 display.setCursor(130, 380); 186 display.print("Updates every 5 minutes"); 187 } else { 188 // Error checking the HTTPS request 189 display.fillScreen(BLACK); 190 display.setCursor(10, 10); //x,y 191 display.print("Error on HTTP request"); 192 } 193 client.stop(); // End the request 194 } 195}
Downloadable files
3D printable enclosure for GIGA DIsplay Shield
Optional 3D printable enclosure for the GIGA DIspl
https://www.printables.com/model/605051-enclosure-for-arduino-giga-r1-wifi-and-giga-displa
Comments
Only logged in users can leave comments
kalebollich
a month ago
I think this is a great article.