#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>
// Set the LCD address to 0x27 or your specific address and define the size (20x4 LCD)
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int displayInterval = 3000; // 3 seconds delay
unsigned long lastDisplayChange = 0;
int currentPage = 0;
String receivedMETAR = "";
bool metarReceived = false;
// Struct to hold parsed METAR data
struct METARData {
String station;
String datetime;
String wind;
String visibility;
String clouds;
String temperature;
String dewpoint;
String pressure;
};
// Create a METARData object
METARData metarData;
// Function to simulate METAR parsing and populate the METARData object
void parseMETAR(String metarMessage) {
// Parse the METAR message (this is a simplified example)
// In reality, METAR parsing can be more complex based on various formats
metarData.station = metarMessage.substring(0, 4); // First 4 chars are the station code
metarData.datetime = "2024-10-09 12:30"; // Example datetime
metarData.wind = "Wind: 220/15KT"; // Example wind info
metarData.visibility = "Visibility: 10KM"; // Example visibility
metarData.clouds = "Clouds: SCT030 BKN040"; // Example cloud coverage
metarData.temperature = "Temp: 22°C"; // Example temperature
metarData.dewpoint = "Dewpoint: 12°C"; // Example dewpoint
metarData.pressure = "Pressure: 1012 hPa"; // Example pressure
}
// Function to convert METARData object to JSON
String metarToJSON() {
// Create a JSON document
StaticJsonDocument<256> doc;
// Populate JSON with data from the metarData struct
doc["station"] = metarData.station;
doc["datetime"] = metarData.datetime;
doc["wind"] = metarData.wind;
doc["visibility"] = metarData.visibility;
doc["clouds"] = metarData.clouds;
doc["temperature"] = metarData.temperature;
doc["dewpoint"] = metarData.dewpoint;
doc["pressure"] = metarData.pressure;
// Convert JSON object to string
String jsonStr;
serializeJson(doc, jsonStr);
return jsonStr;
}
// Function to display METAR data on the LCD in pages
void displayMETAR() {
lcd.clear();
if (currentPage == 0) {
lcd.setCursor(0, 0); lcd.print(metarData.station);
lcd.setCursor(0, 1); lcd.print(metarData.datetime);
lcd.setCursor(0, 2); lcd.print(metarData.wind);
lcd.setCursor(0, 3); lcd.print(metarData.visibility);
} else if (currentPage == 1) {
lcd.setCursor(0, 0); lcd.print(metarData.clouds);
lcd.setCursor(0, 1); lcd.print(metarData.temperature);
lcd.setCursor(0, 2); lcd.print(metarData.dewpoint);
lcd.setCursor(0, 3); lcd.print(metarData.pressure);
}
else {
metarReceived = false;
lcd.clear();
lcd.setCursor(0, 0); lcd.print("Waiting for METAR");
}
currentPage = (currentPage + 1) % 3; // Toggle between pages 0, 1 and 2
}
void setup() {
// Initialize serial and LCD
Serial.begin(9600);
lcd.init();
lcd.backlight();
// Display startup message
lcd.setCursor(0, 0); lcd.print("Waiting for METAR");
// Wait for a METAR message over Serial (for example)
}
void loop() {
// Check for incoming METAR message
if (Serial.available()) {
char incomingChar = Serial.read();
if (incomingChar == '\n') {
metarReceived = true;
parseMETAR(receivedMETAR);
receivedMETAR = ""; // Reset buffer
} else {
receivedMETAR += incomingChar;
}
}
// If METAR received, process and display data
if (metarReceived) {
if (millis() - lastDisplayChange > displayInterval) {
lastDisplayChange = millis();
displayMETAR(); // Show the data on the LCD
}
// Output METAR data as JSON over Serial
String jsonStr = metarToJSON();
Serial.println(jsonStr);
}
}