#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
// Pinos da Tela
#define TFT_DC 16
#define TFT_CS 17
#define TFT_RST 22
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// APIs em HTTP simples para não exigir certificados pesados no Pico W
const char* urlFiat = "https://economia.awesomeapi.com.br/json/last/USD-BRL,EUR-BRL";
const char* urlBtc = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=brl";
void setup() {
Serial1.begin(115200);
tft.begin();
tft.setRotation(1); // Modo Paisagem
tft.fillScreen(ILI9341_BLACK);
// Tela de Inicialização
tft.setCursor(20, 40);
tft.setTextColor(ILI9341_CYAN);
tft.setTextSize(2);
tft.println("MONITOR FINANCEIRO IOT");
tft.setCursor(20, 80);
tft.setTextColor(ILI9341_WHITE);
tft.print("Conectando ao Wi-Fi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
tft.print(".");
}
desenharPainelFixo();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
String precoUSD = "Erro API";
String precoEUR = "Erro API";
String precoBTC = "Erro API";
WiFiClient client; // Cliente de rede estável para o simulador
HTTPClient http;
// 1. Busca Dólar e Euro
if (http.begin(client, urlFiat)) {
int httpCode = http.GET();
if (httpCode == 200) {
String payload = http.getString();
JsonDocument doc;
deserializeJson(doc, payload);
precoUSD = "R$ " + String(doc["USDBRL"]["bid"].as<float>(), 2);
precoEUR = "R$ " + String(doc["EURBRL"]["bid"].as<float>(), 2);
}
http.end();
}
// 2. Busca Bitcoin
if (http.begin(client, urlBtc)) {
int httpCode = http.GET();
if (httpCode == 200) {
String payload = http.getString();
JsonDocument doc;
deserializeJson(doc, payload);
precoBTC = "R$ " + String(doc["bitcoin"]["brl"].as<long>());
}
http.end();
}
// Atualiza os valores na tela
atualizarValores(precoUSD, precoEUR, precoBTC);
}
// Atualiza a cada 15 segundos no simulador
delay(15000);
}
void desenharPainelFixo() {
tft.fillScreen(ILI9341_BLACK);
// Moldura externa azul
tft.drawRect(5, 5, tft.width() - 10, tft.height() - 10, ILI9341_BLUE);
// Cabeçalho
tft.fillRect(6, 6, tft.width() - 12, 30, ILI9341_BLUE);
tft.setCursor(45, 13);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("MARKET MONITOR - PICO W");
tft.setTextSize(2);
// Dólar
tft.setTextColor(ILI9341_GREEN);
tft.setCursor(20, 60); tft.print("USD / Dolar:");
// Euro
tft.setTextColor(ILI9341_ORANGE);
tft.setCursor(20, 110); tft.print("EUR / Euro:");
// Bitcoin
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(20, 160); tft.print("BTC / Bitcoin:");
// Rodapé
tft.drawFastHLine(5, 210, tft.width() - 10, ILI9341_DARKGREY);
tft.setCursor(20, 218);
tft.setTextColor(ILI9341_MAROON);
tft.setTextSize(1);
tft.print("Atualizando dados via API a cada 15s...");
}
void atualizarValores(String usd, String eur, String btc) {
tft.setTextSize(2);
tft.fillRect(180, 58, 130, 22, ILI9341_BLACK);
tft.fillRect(180, 108, 130, 22, ILI9341_BLACK);
tft.fillRect(180, 158, 130, 22, ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(180, 60); tft.print(usd);
tft.setCursor(180, 110); tft.print(eur);
tft.setCursor(180, 160); tft.print(btc);
}