+ show startup and Wi-Fi status on the OLED
+ fetch data immediately after connecting + empty display and a shorter retry interval for failed requests ~ poll normally every 10 seconds and use the configured local endpoint ~ keep previous readings visible and mark them as outdated on failure ~ set the font before drawing status text and tidy serial messages - generic failure screen and placeholder network settings
This commit is contained in:
@@ -13,12 +13,13 @@
|
|||||||
#define OLED_SCL 14
|
#define OLED_SCL 14
|
||||||
#define OLED_SDA 12
|
#define OLED_SDA 12
|
||||||
|
|
||||||
#define UPDATE_INTERVAL_MS 1000
|
#define UPDATE_INTERVAL_MS 10000
|
||||||
|
#define SHORT_UPDATE_INTERVAL_MS (UPDATE_INTERVAL_MS / 10)
|
||||||
|
|
||||||
#define SERVER_URL "http://awair-server/air-data/latest"
|
#define SERVER_URL "http://[IP_ADDRESS]/air-data/latest"
|
||||||
|
|
||||||
#define SSID "-"
|
#define SSID "[SSID]"
|
||||||
#define PASSWD "-"
|
#define PASSWD "[PASS]"
|
||||||
|
|
||||||
WiFiClient client;
|
WiFiClient client;
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
@@ -80,13 +81,21 @@ class Panel{
|
|||||||
panel.begin();
|
panel.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void show_failed(void){
|
void show_outdated(void){
|
||||||
panel.clearBuffer();
|
|
||||||
panel.setFont(u8g2_font_ncenB08_tr);
|
panel.setFont(u8g2_font_ncenB08_tr);
|
||||||
panel.drawStr(0, 10, "Something failed");
|
panel.drawStr(71, 9, "(outdated)");
|
||||||
panel.sendBuffer();
|
panel.sendBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void print_header(const char* msg){
|
||||||
|
panel.clearBuffer();
|
||||||
|
panel.setFont(u8g2_font_ncenB08_tr);
|
||||||
|
panel.drawStr(0, 9, msg);
|
||||||
|
panel.sendBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_empty_data(){ update({}); }
|
||||||
|
|
||||||
void update(const AwairData& data){
|
void update(const AwairData& data){
|
||||||
char buffer[16];
|
char buffer[16];
|
||||||
|
|
||||||
@@ -175,15 +184,31 @@ AwairData data;
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
panel.begin();
|
panel.begin();
|
||||||
|
panel.print_header("Starting...");
|
||||||
|
|
||||||
http.setTimeout(3000);
|
http.setTimeout(3000);
|
||||||
|
|
||||||
WiFi.begin(SSID, PASSWD);
|
Serial.println("\nConnecting to WiFi...");
|
||||||
while(WiFi.status() != WL_CONNECTED) delay(100);
|
panel.print_header("Connecting to WiFi...");
|
||||||
|
|
||||||
Serial.println("Connected!");
|
WiFi.begin(SSID, PASSWD);
|
||||||
|
while(WiFi.status() != WL_CONNECTED){
|
||||||
|
delay(100);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("\nConnected!");
|
||||||
|
panel.print_header("Connected to WiFi!");
|
||||||
|
|
||||||
|
if(request_data(&data) != 0){ panel.print_empty_data(); }
|
||||||
|
else{
|
||||||
|
Serial.println("Got new data -> Updating...");
|
||||||
|
panel.update(data);
|
||||||
|
data.reset_updated();
|
||||||
|
}
|
||||||
|
delay(SHORT_UPDATE_INTERVAL_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@@ -195,18 +220,23 @@ void loop() {
|
|||||||
start = current;
|
start = current;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
size_t update_interval = UPDATE_INTERVAL_MS;
|
||||||
|
|
||||||
if(WiFi.status() != WL_CONNECTED){ delay(100); return; }
|
if(WiFi.status() != WL_CONNECTED){ delay(100); return; }
|
||||||
|
|
||||||
Serial.println("Requesting data...");
|
Serial.println("Requesting data...");
|
||||||
if(request_data(&data) != 0){ panel.show_failed(); }
|
if(request_data(&data) != 0){
|
||||||
|
panel.show_outdated();
|
||||||
if(data.has_updated()){
|
update_interval = SHORT_UPDATE_INTERVAL_MS;
|
||||||
|
}
|
||||||
|
else if(data.has_updated()){
|
||||||
Serial.println("Got new data -> Updating...");
|
Serial.println("Got new data -> Updating...");
|
||||||
panel.update(data);
|
panel.update(data);
|
||||||
data.reset_updated();
|
data.reset_updated();
|
||||||
}
|
}
|
||||||
else Serial.println("Got data but nothing new!");
|
else Serial.println("No new data!");
|
||||||
|
|
||||||
Serial.println("---------- ---------- ----------\n");
|
Serial.println("\n---------- ---------- ----------\n");
|
||||||
delay(UPDATE_INTERVAL_MS); // simple delay is enough <- no state machine
|
|
||||||
|
delay(update_interval); // simple delay is enough <- no state machine
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user