车载导航天气挂件
设备
- LCD 1602A显示屏
- 合宙Air 551G导航模块
- DFRobot Beetle ESP32-C3开发板
- 杜邦线若干
连接方式
跟之前的连接一样。
-
ESP32-C3通过自制一分二的线分别给LCD屏幕和Air 551G供电。
-
ESP32-C3的Pin 8(SDA)和9(SCL)与LCD屏幕连接。
-
ESP32-C3的Pin 20(RX)和21(TX)与Air 551G连接。
功能设计
利用ESP32-C3控制GNSS模块,获取当前UTC时间和经纬度等时间和坐标信息,同时连接WIFI获取所在位置的天气信息。
在LCD屏幕上对时间、坐标以及天气信息等进行显示。
实现效果
基本上完成了设计功能。
后期升级主要是用更好的屏幕,现在用的LCD1602A显示太差,不知道为什么显示很不清晰。
现在时间和坐标信息与天气信息是切换显示的,如果屏幕更大一点,可以加入可视化显示和同屏显示。
最重要的是,如果把几个模块设计到一个模块中,套个好看的外壳,摆在车里面当挂件,是不是也很不错?
代码实现
#include <LiquidCrystal_PCF8574.h>
#include <Wire.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
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;
}
|