|
|
为什么直接更改U8G库的实例可以显示,但是应用到GPS里面就没法点亮OLED
以下是代码,求大神查看下。
===============================================================================
#include <dht11.h>
#include "U8glib.h"
#include <SD.h>
File myFile;
dht11 DHT11;
#define GPRMC_TERM "$GPRMC," //定义要解析的指令,因为这条指令包含定位和时间信息
#define DHT11PIN 2
U8GLIB_SSD1309_128X64 u8g(8, 7, 6, 5); // SPI Com: SCK = 8, MOSI = 7, CS = 6, DC = 5
char nmeaSentence[68];
String latitude; //纬度
String longitude; //经度
String lndSpeed; //速度
String gpsTime; //UTC时间,本初子午线经度0度的时间,和北京时间差8小时
String beiJingTime; //北京时间
int humidity,temperature;
void setup()
{
u8g.setFont(u8g_font_unifont);
Serial.begin(9600);
Serial.println(DHT11LIB_VERSION);
Serial1.begin(9600); //定义波特率9600
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
void loop(){
T();
_GPS();
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
}
void SD_write(){
myFile = SD.open("GPS.txt", FILE_WRITE);
myFile.println(longitude+","+latitude+","+beiJingTime);
myFile.close();
}
void draw(){
String tem = String(temperature);
u8g.drawStr( 0, 0, latitude.c_str());
u8g.drawStr( 1, 0, longitude.c_str());
u8g.drawStr( 2, 0, tem.c_str());
u8g.drawStr( 3, 0, beiJingTime.c_str());
}
void _GPS(){
for (unsigned long start = millis(); millis() - start < 1000;) //一秒钟内不停扫描GPS信息
{
while (Serial1.available()) //串口获取到数据开始解析
{
char c = Serial1.read(); //读取一个字节获取的数据
switch(c) //判断该字节的值
{
case '$': //若是$,则说明是一帧数据的开始
Serial1.readBytesUntil('*', nmeaSentence, 67); //读取接下来的数据,存放在nmeaSentence字符数组中,最大存放67个字节
//Serial.println(nmeaSentence);
latitude = parseGprmcLat(nmeaSentence); //获取纬度值
longitude = parseGprmcLon(nmeaSentence);//获取经度值
lndSpeed = parseGprmcSpeed(nmeaSentence);//获取速度值
gpsTime = parseGprmcTime(nmeaSentence);//获取GPS时间
beiJingTime = getBeiJingTime(gpsTime); //获取北京时间
}
}
}
}
void T(){
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);
}
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
} //摄氏温度度转化为华氏温度
double Kelvin(double celsius)
{
return celsius + 273.15;
} //摄氏温度转化为开氏温度
// 露点(点在此温度时,空气饱和并产生露珠)
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}
// 快速计算露点,速度是5倍dewPoint()
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}
String getBeiJingTime(String s)
{
int hour = s.substring(0,2).toInt();
int minute = s.substring(2,4).toInt();
int second = s.substring(4,6).toInt();
hour += 8;
if(hour > 24)
hour -= 24;
s = String(hour) + String(minute) + String(second);
return s;
}
//Parse GPRMC NMEA sentence data from String
//String must be GPRMC or no data will be parsed
//Return Latitude
String parseGprmcLat(String s)
{
int pLoc = 0; //paramater location pointer
int lEndLoc = 0; //lat parameter end location
int dEndLoc = 0; //direction parameter end location
String lat;
/*make sure that we are parsing the GPRMC string.
Found that setting s.substring(0,5) == "GPRMC" caused a FALSE.
There seemed to be a 0x0D and 0x00 character at the end. */
if(s.substring(0,4) == "GPRM")
{
//Serial.println(s);
for(int i = 0; i < 5; i++)
{
if(i < 3)
{
pLoc = s.indexOf(',', pLoc+1);
/*Serial.print("i < 3, pLoc: ");
Serial.print(pLoc);
Serial.print(", ");
Serial.println(i);*/
}
if(i == 3)
{
lEndLoc = s.indexOf(',', pLoc+1);
lat = s.substring(pLoc+1, lEndLoc);
/*Serial.print("i = 3, pLoc: ");
Serial.println(pLoc);
Serial.print("lEndLoc: ");
Serial.println(lEndLoc);*/
}
else
{
dEndLoc = s.indexOf(',', lEndLoc+1);
lat = lat + " " + s.substring(lEndLoc+1, dEndLoc);
/*Serial.print("i = 4, lEndLoc: ");
Serial.println(lEndLoc);
Serial.print("dEndLoc: ");
Serial.println(dEndLoc);*/
}
}
return lat;
}
//}
//}
}
//Parse GPRMC NMEA sentence data from String
//String must be GPRMC or no data will be parsed
//Return Longitude
String parseGprmcLon(String s)
{
int pLoc = 0; //paramater location pointer
int lEndLoc = 0; //lat parameter end location
int dEndLoc = 0; //direction parameter end location
String lon;
/*make sure that we are parsing the GPRMC string.
Found that setting s.substring(0,5) == "GPRMC" caused a FALSE.
There seemed to be a 0x0D and 0x00 character at the end. */
if(s.substring(0,4) == "GPRM")
{
//Serial.println(s);
for(int i = 0; i < 7; i++)
{
if(i < 5)
{
pLoc = s.indexOf(',', pLoc+1);
/*Serial.print("i < 3, pLoc: ");
Serial.print(pLoc);
Serial.print(", ");
Serial.println(i);*/
}
if(i == 5)
{
lEndLoc = s.indexOf(',', pLoc+1);
lon = s.substring(pLoc+1, lEndLoc);
/*Serial.print("i = 3, pLoc: ");
Serial.println(pLoc);
Serial.print("lEndLoc: ");
Serial.println(lEndLoc);*/
}
else
{
dEndLoc = s.indexOf(',', lEndLoc+1);
lon = lon + " " + s.substring(lEndLoc+1, dEndLoc);
/*Serial.print("i = 4, lEndLoc: ");
Serial.println(lEndLoc);
Serial.print("dEndLoc: ");
Serial.println(dEndLoc);*/
}
}
return lon;
}
}
//Parse GPRMC NMEA sentence data from String
//String must be GPRMC or no data will be parsed
//Return Longitude
String parseGprmcSpeed(String s)
{
int pLoc = 0; //paramater location pointer
int lEndLoc = 0; //lat parameter end location
int dEndLoc = 0; //direction parameter end location
String lndSpeed;
/*make sure that we are parsing the GPRMC string.
Found that setting s.substring(0,5) == "GPRMC" caused a FALSE.
There seemed to be a 0x0D and 0x00 character at the end. */
if(s.substring(0,4) == "GPRM")
{
//Serial.println(s);
for(int i = 0; i < 8; i++)
{
if(i < 7)
{
pLoc = s.indexOf(',', pLoc+1);
/*Serial.print("i < 8, pLoc: ");
Serial.print(pLoc);
Serial.print(", ");
Serial.println(i);*/
}
else
{
lEndLoc = s.indexOf(',', pLoc+1);
lndSpeed = s.substring(pLoc+1, lEndLoc);
/*Serial.print("i = 8, pLoc: ");
Serial.println(pLoc);
Serial.print("lEndLoc: ");
Serial.println(lEndLoc);*/
}
}
return lndSpeed;
}
}
//Parse GPRMC NMEA sentence data from String
//String must be GPRMC or no data will be parsed
//Return Longitude
String parseGprmcTime(String s)
{
int pLoc = 0; //paramater location pointer
int lEndLoc = 0; //lat parameter end location
int dEndLoc = 0; //direction parameter end location
String gpsTime;
/*make sure that we are parsing the GPRMC string.
Found that setting s.substring(0,5) == "GPRMC" caused a FALSE.
There seemed to be a 0x0D and 0x00 character at the end. */
if(s.substring(0,4) == "GPRM")
{
//Serial.println(s);
for(int i = 0; i < 2; i++)
{
if(i < 1)
{
pLoc = s.indexOf(',', pLoc+1);
/*Serial.print("i < 8, pLoc: ");
Serial.print(pLoc);
Serial.print(", ");
Serial.println(i);*/
}
else
{
lEndLoc = s.indexOf(',', pLoc+1);
gpsTime = s.substring(pLoc+1, lEndLoc);
/*Serial.print("i = 8, pLoc: ");
Serial.println(pLoc);
Serial.print("lEndLoc: ");
Serial.println(lEndLoc);*/
}
}
return gpsTime;
}
}
// Turn char[] array into String object
String charToString(char *c)
{
String val = "";
for(int i = 0; i <= sizeof(c); i++)
{
val = val + c;
}
return val;
}
char* inttostr(char *str, int i)
{
sprintf(str, "%d", i);
return str;
} |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|