Compare commits
5
Commits
482a788f10
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f8294d4aed | ||
|
|
4b59b8936b | ||
|
|
1e923249f6 | ||
|
|
11f57a244d | ||
|
|
fc72f9b0bd |
@@ -0,0 +1,9 @@
|
|||||||
|
# Arduino
|
||||||
|
|
||||||
|
A collection of Arduino / ESP microcontroller projects.
|
||||||
|
|
||||||
|
## Projects
|
||||||
|
|
||||||
|
- **[WeatherPredictor](WeatherPredictor/)** — autonomous barometric weather
|
||||||
|
station on a Wemos D1 mini (ESP8266): local Zambretti forecast, TFT display
|
||||||
|
and a self-hosted web interface. See its [README](WeatherPredictor/README.md).
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
# Weather Predictor
|
||||||
|
|
||||||
|
An autonomous **barometric weather station** built on a Wemos D1 mini (ESP8266).
|
||||||
|
It reads atmospheric pressure and temperature, keeps accurate battery-backed
|
||||||
|
time, and predicts near-term local weather entirely on-device using the
|
||||||
|
**Zambretti algorithm** — no internet forecast service required. Readings and
|
||||||
|
the forecast are shown on a small TFT and on a self-hosted web interface styled
|
||||||
|
as a barometer instrument.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **Local Zambretti forecast** from the 3-hour sea-level pressure trend (rising / steady / falling), adjusted for season.
|
||||||
|
- **On-device TFT screen** (160×80): time, date, pressure with trend arrow, temperature, a scaled weather icon, and the station's Wi-Fi/IP.
|
||||||
|
- **Web interface** served from the device (no external CDNs, works offline):
|
||||||
|
- live barometer dial with a needle and a "set hand" ghost at the pressure 3 h ago;
|
||||||
|
- barograph of pressure & temperature with labelled axes;
|
||||||
|
- **forecast change log**;
|
||||||
|
- settings with **city presets** and a UTC-offset time-zone picker.
|
||||||
|
- **Persistence** in LittleFS: settings, pressure history, and the forecast log all survive reboots.
|
||||||
|
- **Wi-Fi provisioning** via WiFiManager (captive portal — no credentials in code) and **NTP** time sync to the DS3231.
|
||||||
|
|
||||||
|
## Hardware
|
||||||
|
|
||||||
|
| Component | Part | Bus / notes |
|
||||||
|
|-----------|------|-------------|
|
||||||
|
| MCU | Wemos D1 mini (ESP8266, 4 MB flash) | — |
|
||||||
|
| Pressure & temperature | BMP180 (GY-68) | I²C, addr `0x77` |
|
||||||
|
| Real-time clock | DS3231 (HW-022, battery-backed) | I²C, addr `0x68` |
|
||||||
|
| Display | 0.96" 80×160 TFT, ST7735S | 4-wire SPI, run landscape 160×80 |
|
||||||
|
|
||||||
|
### Wiring
|
||||||
|
|
||||||
|
The display uses hardware SPI; the two I²C modules share one bus.
|
||||||
|
|
||||||
|
| Signal | Wemos pin | GPIO |
|
||||||
|
|--------|-----------|------|
|
||||||
|
| TFT CS | D8 | 15 |
|
||||||
|
| TFT DC | D3 | 0 |
|
||||||
|
| TFT RST | D4 | 2 |
|
||||||
|
| TFT SDA (MOSI) | D7 | 13 |
|
||||||
|
| TFT SCL (SCK) | D5 | 14 |
|
||||||
|
| TFT BLK (backlight) | D2 | 4 |
|
||||||
|
| I²C SDA (BMP180 + DS3231) | D6 | 12 |
|
||||||
|
| I²C SCL (BMP180 + DS3231) | D1 | 5 |
|
||||||
|
|
||||||
|
All modules run on 3.3 V / GND. I²C is started with `Wire.begin(D6 /*SDA*/, D1 /*SCL*/)`.
|
||||||
|
|
||||||
|
## Build & flash (Arduino IDE)
|
||||||
|
|
||||||
|
1. Install the **ESP8266** board package; select board **"LOLIN(WEMOS) D1 R2 & mini"**.
|
||||||
|
2. Set **Tools → Flash Size** to a layout with a filesystem, e.g. **4MB (FS:2MB)**.
|
||||||
|
3. Install libraries via Library Manager:
|
||||||
|
*Adafruit GFX*, *Adafruit ST7735 and ST7789*, *Adafruit BMP085*, *RTClib*,
|
||||||
|
*ArduinoJson* (v7.x), *WiFiManager* (by tzapu).
|
||||||
|
`ESP8266WiFi`, `ESP8266WebServer`, `LittleFS`, `Wire`, `SPI`, `time` ship with the core.
|
||||||
|
4. Open `WeatherPredictor/WeatherPredictor.ino` and upload.
|
||||||
|
|
||||||
|
## First run
|
||||||
|
|
||||||
|
1. On first boot the device opens a Wi-Fi access point **`WeatherPredictor-Setup`**.
|
||||||
|
Join it from a phone and pick your network in the captive portal.
|
||||||
|
2. After connecting, the device syncs time over NTP and writes it to the DS3231.
|
||||||
|
3. The screen and Serial (115200) print the station's IP — open `http://<ip>/`.
|
||||||
|
4. The forecast shows **"Collecting data…"** until ~3 h of pressure history exists;
|
||||||
|
that is expected — the Zambretti method needs a trend before its first call.
|
||||||
|
|
||||||
|
Set your **altitude** in the web settings (or pick a city preset): sea-level
|
||||||
|
pressure — and therefore the forecast — depends on it (~0.12 hPa per metre).
|
||||||
|
|
||||||
|
## REST API
|
||||||
|
|
||||||
|
| Endpoint | Method | Returns |
|
||||||
|
|----------|--------|---------|
|
||||||
|
| `/` | GET | Web UI |
|
||||||
|
| `/api/current` | GET | time, pressure (abs + sea level), temperature, trend, forecast |
|
||||||
|
| `/api/history` | GET | recent pressure/temperature samples |
|
||||||
|
| `/api/forecasts` | GET | forecast change log (newest first) |
|
||||||
|
| `/api/settings` | GET / POST | altitude, time-zone offset, coordinates |
|
||||||
|
|
||||||
|
## Project structure
|
||||||
|
|
||||||
|
```
|
||||||
|
WeatherPredictor/
|
||||||
|
WeatherPredictor.ino scheduler; wires the modules together
|
||||||
|
config.h pins, addresses, intervals, defaults
|
||||||
|
state.h shared current-readings struct
|
||||||
|
sensors.* BMP180 read + sea-level conversion
|
||||||
|
rtc_time.* DS3231 read/set + NTP sync
|
||||||
|
display_ui.* ST7735 rendering + weather icons
|
||||||
|
forecast.* Zambretti forecast + trend classification
|
||||||
|
history.* pressure ring buffer + LittleFS persistence
|
||||||
|
flog.* forecast change log
|
||||||
|
app_settings.* settings struct + LittleFS JSON
|
||||||
|
net.* WiFiManager connection
|
||||||
|
web_server.* HTTP server + REST API
|
||||||
|
web_page.h web UI (HTML/CSS/JS) in PROGMEM
|
||||||
|
docs/superpowers/ design spec and implementation plan
|
||||||
|
```
|
||||||
|
|
||||||
|
## Notes & gotchas
|
||||||
|
|
||||||
|
- **BGR panel:** this 0.96" ST7735S has red/blue swapped, so colours are built
|
||||||
|
R/B-exchanged in `display_ui.cpp`. If your panel looks wrong, adjust there
|
||||||
|
(and the `invertDisplay` / `setRotation` in `displayBegin()`).
|
||||||
|
- **Restrictive networks:** on some LANs the router does not resolve external
|
||||||
|
hostnames, so NTP uses hard-coded Cloudflare/Google anycast IPs instead of
|
||||||
|
names. Such networks may also block SSH.
|
||||||
|
- **Flash wear:** history is flushed every 15 min and the forecast log only on
|
||||||
|
change, so LittleFS wear leveling keeps flash life at years/decades.
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
Zambretti forecast constants and lookup tables adapted from
|
||||||
|
[G6EJD's ESP Zambretti forecaster](https://github.com/G6EJD/ESP32_Weather_Forecaster_TN061).
|
||||||
@@ -88,6 +88,15 @@ void loop() {
|
|||||||
tSample = now;
|
tSample = now;
|
||||||
sampleNow();
|
sampleNow();
|
||||||
displayRender(g_state);
|
displayRender(g_state);
|
||||||
|
Serial.printf("heap=%u maxblock=%u\n", ESP.getFreeHeap(), ESP.getMaxFreeBlockSize());
|
||||||
|
// Safety net: if the largest contiguous block gets too small, the TCP stack
|
||||||
|
// can no longer send responses. Persist and reboot to defragment.
|
||||||
|
if (ESP.getMaxFreeBlockSize() < 6000) {
|
||||||
|
Serial.println(F("Low memory -> saving history and restarting"));
|
||||||
|
historySave();
|
||||||
|
delay(50);
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (now - tHistory >= HISTORY_INTERVAL_MS) {
|
if (now - tHistory >= HISTORY_INTERVAL_MS) {
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 74 KiB |
@@ -103,7 +103,11 @@ static const char INDEX_HTML[] PROGMEM = R"HTML(
|
|||||||
.cap{color:var(--muted); font-size:.74rem; letter-spacing:.06em}
|
.cap{color:var(--muted); font-size:.74rem; letter-spacing:.06em}
|
||||||
|
|
||||||
/* Forecast log */
|
/* Forecast log */
|
||||||
.log{list-style:none; margin:2px 0 0; padding:0}
|
.log{list-style:none; margin:2px 0 0; padding:0 8px 0 0; max-height:216px; overflow-y:auto;
|
||||||
|
scrollbar-width:thin; scrollbar-color:var(--line) transparent}
|
||||||
|
.log::-webkit-scrollbar{width:8px}
|
||||||
|
.log::-webkit-scrollbar-thumb{background:var(--line); border-radius:8px}
|
||||||
|
.log::-webkit-scrollbar-track{background:transparent}
|
||||||
.log li{display:flex; align-items:center; gap:13px; padding:12px 0; border-top:1px solid var(--line)}
|
.log li{display:flex; align-items:center; gap:13px; padding:12px 0; border-top:1px solid var(--line)}
|
||||||
.log li:first-child{border-top:0}
|
.log li:first-child{border-top:0}
|
||||||
.log .dot{width:9px; height:9px; border-radius:50%; flex:0 0 auto}
|
.log .dot{width:9px; height:9px; border-radius:50%; flex:0 0 auto}
|
||||||
@@ -27,41 +27,44 @@ static void handleCurrent() {
|
|||||||
doc["haveTrend"] = g_state.haveTrend;
|
doc["haveTrend"] = g_state.haveTrend;
|
||||||
doc["forecast"] = g_state.haveTrend ? g_state.forecast.text : "Collecting data...";
|
doc["forecast"] = g_state.haveTrend ? g_state.forecast.text : "Collecting data...";
|
||||||
doc["category"] = g_state.haveTrend ? categoryShort(g_state.forecast.category) : "...";
|
doc["category"] = g_state.haveTrend ? categoryShort(g_state.forecast.category) : "...";
|
||||||
|
doc["heap"] = ESP.getFreeHeap(); // for monitoring memory health
|
||||||
String out;
|
String out;
|
||||||
serializeJson(doc, out);
|
serializeJson(doc, out);
|
||||||
server.send(200, "application/json", out);
|
server.send(200, "application/json", out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Streamed (chunked) so we never build a large String/JsonDocument on the heap.
|
||||||
static void handleHistory() {
|
static void handleHistory() {
|
||||||
JsonDocument doc;
|
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
|
||||||
JsonArray arr = doc.to<JsonArray>();
|
server.send(200, "application/json", "");
|
||||||
|
server.sendContent("[");
|
||||||
|
char buf[80];
|
||||||
int n = historyCount();
|
int n = historyCount();
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
Sample s = historyGet(i);
|
Sample s = historyGet(i);
|
||||||
JsonObject o = arr.add<JsonObject>();
|
snprintf(buf, sizeof(buf), "%s{\"t\":%lu,\"msl\":%.1f,\"temp\":%.1f}",
|
||||||
o["t"] = s.epoch;
|
i ? "," : "", (unsigned long)s.epoch, s.mslHpa, s.tempC);
|
||||||
o["msl"] = s.mslHpa;
|
server.sendContent(buf);
|
||||||
o["temp"] = s.tempC;
|
|
||||||
}
|
}
|
||||||
String out;
|
server.sendContent("]");
|
||||||
serializeJson(doc, out);
|
|
||||||
server.send(200, "application/json", out);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Streamed (chunked). Forecast phrases contain no quotes/backslashes, so they
|
||||||
|
// are safe to embed directly in JSON.
|
||||||
static void handleForecasts() {
|
static void handleForecasts() {
|
||||||
JsonDocument doc;
|
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
|
||||||
JsonArray arr = doc.to<JsonArray>();
|
server.send(200, "application/json", "");
|
||||||
|
server.sendContent("[");
|
||||||
|
char buf[128];
|
||||||
int n = flogCount();
|
int n = flogCount();
|
||||||
for (int i = n - 1; i >= 0; i--) { // newest first
|
for (int i = n - 1, first = 1; i >= 0; i--, first = 0) { // newest first
|
||||||
FcastEntry e = flogGet(i);
|
FcastEntry e = flogGet(i);
|
||||||
JsonObject o = arr.add<JsonObject>();
|
snprintf(buf, sizeof(buf), "%s{\"t\":%lu,\"cat\":\"%s\",\"text\":\"%s\"}",
|
||||||
o["t"] = e.epoch;
|
first ? "" : ",", (unsigned long)e.epoch,
|
||||||
o["cat"] = categoryShort((WxCategory)e.cat);
|
categoryShort((WxCategory)e.cat), forecastTextForLetter(e.letter));
|
||||||
o["text"] = forecastTextForLetter(e.letter);
|
server.sendContent(buf);
|
||||||
}
|
}
|
||||||
String out;
|
server.sendContent("]");
|
||||||
serializeJson(doc, out);
|
|
||||||
server.send(200, "application/json", out);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handleGetSettings() {
|
static void handleGetSettings() {
|
||||||
Reference in New Issue
Block a user