Pets
Cloud pet
A little cloud that lives on your desk and feels the real weather: it smiles under a clear sky and sulks when it rains outside.
- Medium
- about 55 min
- ESP32-S3
- 6 parts
- 10 wires
- 116 lines of code
- Uses Wi-Fi
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
The pet's mood is one number from a weather service, the same number a forecast uses. Pat it and it tells you the temperature.
What you learn
Turning data from the internet into a character, and mixing a slow job (fetching) with a fast one (reading touch).
How it works
input
Touch sensor
decides
ESP32-S3 · Wi-Fi
output
TFT 1.54 in
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 10
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
cloud-pet.ino
// Cloud pet
// A little cloud whose mood follows the real weather outside. Pat it and it
// tells you the temperature.
#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";
// Your town. These are for Ho Chi Minh City.
const float LATITUDE = 10.82;
const float LONGITUDE = 106.63;
const unsigned long REFRESH_MS = 15UL * 60UL * 1000UL;
const int PIN_TOUCH_IO = 4;
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;
Adafruit_ST7789 tft(PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RES);
int weatherCode = 1;
float temperature = 0;
bool haveData = false;
unsigned long lastFetch = 0;
void drawCloud() {
bool raining = weatherCode >= 51;
bool sunny = weatherCode == 0;
uint16_t sky = sunny ? 0x5D9F : raining ? 0x2945 : 0x7BEF;
uint16_t body = raining ? 0xA534 : ST77XX_WHITE;
tft.fillScreen(sky);
if (sunny) tft.fillCircle(190, 50, 30, ST77XX_YELLOW);
tft.fillCircle(75, 125, 40, body);
tft.fillCircle(125, 100, 55, body);
tft.fillCircle(175, 125, 40, body);
tft.fillRect(75, 125, 100, 40, body);
// eyes
tft.fillCircle(105, 115, 7, ST77XX_BLACK);
tft.fillCircle(145, 115, 7, ST77XX_BLACK);
// mouth: a smile in fine weather, a flat line in rain
if (raining) {
tft.fillRect(110, 138, 30, 4, ST77XX_BLACK);
for (int i = 0; i < 4; i++) tft.fillRoundRect(70 + i * 32, 180, 6, 22, 3, 0x05FF);
} else {
tft.fillCircle(125, 132, 14, ST77XX_BLACK);
tft.fillRect(108, 116, 34, 16, body);
}
}
void drawTemperature() {
tft.fillRoundRect(40, 190, 160, 40, 8, ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(3);
tft.setCursor(70, 199);
tft.print(temperature, 1);
tft.print(" C");
}
bool fetchWeather() {
WiFiClientSecure client;
client.setInsecure();
HTTPClient http;
String url = "https://api.open-meteo.com/v1/forecast?latitude=" + String(LATITUDE, 2) + "&longitude=" + String(LONGITUDE, 2) +
"¤t=temperature_2m,weather_code";
if (!http.begin(client, url)) return false;
bool ok = false;
if (http.GET() == 200) {
JsonDocument doc;
if (!deserializeJson(doc, http.getString())) {
temperature = doc["current"]["temperature_2m"];
weatherCode = doc["current"]["weather_code"];
ok = true;
}
}
http.end();
return ok;
}
void setup() {
pinMode(PIN_TOUCH_IO, INPUT);
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...");
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 (fetchWeather()) {
haveData = true;
drawCloud();
}
lastFetch = millis();
if (!haveData) delay(10000);
}
if (haveData && digitalRead(PIN_TOUCH_IO) == HIGH) {
drawTemperature();
delay(3000);
drawCloud();
}
delay(30);
}
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”.