Connected
Gold price box
A desk box that shows the live world gold price. Its light is green when the price has risen since the last reading and red when it has fallen.
- Medium
- about 55 min
- ESP32-S3
- 6 parts
- 11 wires
- 108 lines of code
- Uses Wi-Fi
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
It shows the world spot price in US dollars per ounce, from a free public service. That is not the price a shop in your street charges for a tael of gold, which includes its own margin.
What you learn
Fetching a number from a web API on a schedule, and comparing it with the last one.
How it works
input
Gold price from the internet
decides
ESP32-S3 · Wi-Fi
output
TFT 1.54 in
RGB LED
Powered with 5 V through the board's USB-C port.
It joins your home Wi-Fi. You type the network name and password into the sketch before uploading; they stay on the board.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 6 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 11
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
gold-ticker.ino
// Gold price box
// Shows the live world gold price. The light is green when the price has
// gone up since the last reading and red when it has gone down.
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
// World spot price in US dollars per troy ounce, from gold-api.com (free, no key).
const char* PRICE_URL = "https://api.gold-api.com/price/XAU";
const unsigned long REFRESH_MS = 5UL * 60UL * 1000UL;
const int PIN_TFT_SCL = 12;
const int PIN_TFT_SDA = 11;
const int PIN_TFT_RES = 14;
const int PIN_TFT_DC = 13;
const int PIN_TFT_CS = 10;
const int PIN_RGB_R = 15;
const int PIN_RGB_G = 16;
const int PIN_RGB_B = 18;
Adafruit_ST7789 tft(PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RES);
float lastPrice = 0;
unsigned long lastFetch = 0;
bool haveData = false;
void light(int r, int g, int b) {
analogWrite(PIN_RGB_R, r);
analogWrite(PIN_RGB_G, g);
analogWrite(PIN_RGB_B, b);
}
void draw(float price, float change) {
uint16_t colour = change > 0 ? ST77XX_GREEN : change < 0 ? ST77XX_RED : ST77XX_WHITE;
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_YELLOW);
tft.setTextSize(3);
tft.setCursor(20, 24);
tft.print("GOLD XAU");
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(4);
tft.setCursor(14, 90);
tft.print(price, 1);
tft.setTextSize(2);
tft.setCursor(20, 136);
tft.print("USD per ounce");
tft.setTextColor(colour);
tft.setTextSize(3);
tft.setCursor(20, 184);
tft.print(change >= 0 ? "+" : "");
tft.print(change, 2);
}
bool fetchPrice() {
WiFiClientSecure client;
client.setInsecure();
HTTPClient http;
if (!http.begin(client, PRICE_URL)) return false;
bool ok = false;
if (http.GET() == 200) {
JsonDocument doc;
if (!deserializeJson(doc, http.getString())) {
float price = doc["price"];
float change = lastPrice > 0 ? price - lastPrice : 0;
draw(price, change);
if (change > 0) light(0, 140, 0);
else if (change < 0) light(160, 0, 0);
else light(40, 40, 40);
lastPrice = price;
ok = true;
}
}
http.end();
return ok;
}
void setup() {
pinMode(PIN_RGB_R, OUTPUT);
pinMode(PIN_RGB_G, OUTPUT);
pinMode(PIN_RGB_B, OUTPUT);
SPI.begin(PIN_TFT_SCL, -1, PIN_TFT_SDA, PIN_TFT_CS);
tft.init(240, 240);
tft.setRotation(2);
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(2);
tft.setCursor(20, 110);
tft.print("Wi-Fi...");
light(0, 0, 60);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) delay(250);
}
void loop() {
if (!haveData || millis() - lastFetch > REFRESH_MS) {
if (fetchPrice()) haveData = true;
lastFetch = millis();
if (!haveData) delay(10000);
}
delay(200);
}
Libraries
- ArduinoJson: install it from Sketch > Include Library > Manage Libraries.
- Adafruit GFX Library: install it from Sketch > Include Library > Manage Libraries.
- Adafruit ST7735 and ST7789 Library: install it from Sketch > Include Library > Manage Libraries.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Before uploading, type your Wi-Fi name and password into WIFI_SSID and WIFI_PASS at the top of the sketch.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.