72 lines
2.3 KiB
Arduino
72 lines
2.3 KiB
Arduino
#include <Arduino.h>
|
|
#include <Wire.h>
|
|
#include "config.h"
|
|
#include "display_ui.h"
|
|
#include "sensors.h"
|
|
#include "rtc_time.h"
|
|
#include "forecast.h"
|
|
#include "history.h"
|
|
|
|
static void i2cScan() {
|
|
Serial.println(F("I2C scan:"));
|
|
byte found = 0;
|
|
for (byte addr = 1; addr < 127; addr++) {
|
|
Wire.beginTransmission(addr);
|
|
if (Wire.endTransmission() == 0) {
|
|
Serial.printf(" device at 0x%02X\n", addr);
|
|
found++;
|
|
}
|
|
}
|
|
Serial.printf(" %d device(s) found\n", found);
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
delay(200);
|
|
Serial.println(F("\n=== Weather Predictor booting ==="));
|
|
Wire.begin(I2C_SDA, I2C_SCL); // MUST come before any I2C driver begin()
|
|
i2cScan();
|
|
|
|
displayBegin();
|
|
displaySplash("Weather", "Predictor v0.1");
|
|
|
|
if (!sensorsBegin()) Serial.println(F("BMP180 not found!"));
|
|
if (!rtcBegin()) Serial.println(F("DS3231 not found!"));
|
|
if (rtcLostPower()) {
|
|
Serial.println(F("RTC lost power -> setting to build time"));
|
|
rtcSet(RtcTime{2026, 7, 17, 12, 0, 0}); // temporary; NTP will correct later
|
|
}
|
|
|
|
// --- forecast self-test (remove after Task 8) ---
|
|
struct { float p; Trend tr; } cases[] = {
|
|
{1030, TREND_STEADY}, {1030, TREND_RISING}, {1030, TREND_FALLING},
|
|
{1000, TREND_STEADY}, {1000, TREND_FALLING}, {970, TREND_FALLING},
|
|
};
|
|
for (auto& c : cases) {
|
|
Forecast f = computeForecast(c.p, c.tr, 7);
|
|
Serial.printf("p=%.0f trend=%d -> %c [%s] (%s)\n",
|
|
c.p, c.tr, f.letter, f.text, categoryShort(f.category));
|
|
}
|
|
|
|
// --- history self-test (remove after Task 8) ---
|
|
historyBegin();
|
|
uint32_t base = 1000000000UL;
|
|
for (int i = 0; i < 40; i++) // 40 samples @ 5 min = 3h20m, pressure falling
|
|
historyAdd(base + (uint32_t)i * 300, 1015.0f - i * 0.2f, 20.0f);
|
|
float d;
|
|
if (historyTrendDelta(d))
|
|
Serial.printf("history count=%d 3h delta=%.2f hPa trend=%d\n",
|
|
historyCount(), d, classifyTrend(d));
|
|
else
|
|
Serial.println(F("history: not enough data for trend"));
|
|
}
|
|
|
|
void loop() {
|
|
RtcTime t = rtcNow();
|
|
float abs_ = readAbsPressureHpa();
|
|
Serial.printf("%04u-%02u-%02u %02u:%02u:%02u abs=%.1f msl=%.1f t=%.1f\n",
|
|
t.year, t.month, t.day, t.hour, t.minute, t.second,
|
|
abs_, toSeaLevelHpa(abs_, DEFAULT_ALTITUDE_M), readTemperatureC());
|
|
delay(2000);
|
|
}
|