#include
#include
#include
#include
#include
const char* ssid = "**";
const char* password = "**";
String openWeatherMapApiKey = "***";
double time_utc=0;
double lat=34;
double lon=113;
unsigned long lastTime = 0;
unsigned long timerDelay = 10000;
LiquidCrystal_PCF8574 lcd(0x27);
String jsonBuffer;
String lineData="";
int show = -1;
String comdata="";
void setup()
{
int error;
Serial.begin(115200);
Serial.println("LCD...");
Serial1.begin(115200,SERIAL_8N1,20,21);
while (!Serial&&!Serial1)
;
WiFi.begin(ssid, password);
Serial.println("Connecting WIFI...");
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 10 seconds (timerDelay variable), it will take 10 seconds before publishing the first reading.");
Serial.println("Dose: check for LCD");
Wire.begin();
Wire.beginTransmission(0x27);
error = Wire.endTransmission();
Serial.print("Error: ");
Serial.print(error);
if (error == 0) {
Serial.println(": LCD found.");
show = 0;
lcd.begin(16, 2);
} else {
Serial.println(": LCD not found.");
}
lcd.setBacklight(0);
delay(400);
lcd.setBacklight(255);
lcd.home();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WIFI&SCREEN&GNSS");
lcd.setCursor(0, 1);
lcd.print("IS READY!");
lcd.noBlink();
lcd.noCursor();
delay(1000);
}
void loop()
{
while(Serial1.available()>0)
{
lineData=char(Serial1.read());
comdata+=lineData;
delay(2);
}
if (comdata.length()>0)
{
Serial.println(comdata);
int ic=comdata.indexOf('*');
String tmp=comdata.substring(1,ic);
if(tmp.substring(0,5)==("GNGGA"))
{
lcd.home();
lcd.clear();
lcd.setCursor(0, 0);
time_utc=tmp.substring(6,16).toDouble()+80000;
if (time_utc>240000)
time_utc-=240000;
Serial.println("T="+String(time_utc));
lcd.print("T="+String(time_utc));
lat=tmp.substring(17,19).toDouble()+tmp.substring(19,27).toDouble()/60;
lon=tmp.substring(30,33).toDouble()+tmp.substring(33,41).toDouble()/60;
Serial.println("Lat="+String(lat));
Serial.println("Lon="+String(lon));
lcd.setCursor(0, 1);
lcd.print("B="+String(lat).substring(0,6)+" L="+String(lon).substring(0,7));
}
comdata="";
}
if ((millis() - lastTime) > timerDelay) {
if(WiFi.status()== WL_CONNECTED){
String serverPath = "http://api.openweathermap.org/data/2.5/weather?lat=" + String(lat) + "&lon=" + String(lon) + "&appid=" + openWeatherMapApiKey;
jsonBuffer = httpGETRequest(serverPath.c_str());
Serial.println(jsonBuffer);
JSONVar myObject = JSON.parse(jsonBuffer);
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON object = ");
Serial.println(myObject);
Serial.print("Temperature: ");
double c = myObject["main"]["temp"];
c = c-273.15;
Serial.println(c);
lcd.home();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T="+String(c)+" ");
Serial.print("Pressure: ");
Serial.println(myObject["main"]["pressure"]);
Serial.print("Humidity: ");
Serial.println(myObject["main"]["humidity"]);
double pres=myObject["main"]["pressure"];
lcd.print("P="+String(pres));
Serial.print("Wind Speed: ");
Serial.println(myObject["wind"]["speed"]);
lcd.setCursor(0, 1);
double wind=myObject["wind"]["speed"];
lcd.print("Wind="+String(wind)+" ");
double Hu=myObject["main"]["humidity"];
lcd.print("H="+String(Hu));
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
return payload;
}