Here is Ver 1_2 of my little sketch for a Mega2560 It used a GPS receiver, a BMP085 Barometer/thermometer, a DHT22 hygrometer and a 320X240 Itead display.
Comments are Most welcome. It's attached to the previous post. and "Presented Here":
/*
* TimeGPS.pde
* example code illustrating time synced from a GPS
* Robert K.Johnson
*/
#include "DHT.h"
#include <Time.h>
#include <TinyGPS.h>
// GPS Library is the work of Mikal Hart
#include <UTFT.h>
#include <Wire.h>
#include <BMP085.h>
extern uint8_t SmallFont[];
UTFT myGLCD(ITDB32S,38,39,40,41); // Mega2560
BMP085 dps = BMP085(); // Digital Pressure Sensor instance
TinyGPS gps; // GPS Receiver instance
#define DHTPIN 8 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
long Temperature = 0, Pressure = 0, Altitude = 0, Far = 0, Pres = 0; //, Far2 = 0; // Variable's for display and conversion
const int offset = -7; // offset hours from gps time (UTC)
time_t prevDisplay = 0; // when the digital clock was displayed "0"
void setup()
{
Wire.begin(); // Start Wire (IIC)
delay(1000); // Waait for IIC to init.
dht.begin();
myGLCD.InitLCD(); // '0' is default landscape '1' is portrait
myGLCD.setFont(SmallFont);
//Serial1.begin(9600); // 9600 baud for Skylabs SKM53
Serial1.begin(4800); // 4800 baud for U-Blox_6-1
setSyncProvider(gpsTimeSync);
myGLCD.clrScr();
myGLCD.setColor( 180,0, 0);
myGLCD.drawRect(0, 6, 150,90);
// uncomment for different initialization settings
//dps.init(); // QFE (Field Elevation above ground level) is set to 0 meters.
// same as init(MODE_STANDARD, 0, true);
//dps.init(MODE_STANDARD, 101850, false); // 101850Pa = 1018.50hPa, false = using Pa units
// this initialization is useful for normalizing pressure to specific datum.
// OR setting current local hPa information from a weather station/local airport (QNH).
dps.init(MODE_ULTRA_HIGHRES,5366, true); // 53.66 meters, true = using meter units (176' Google Earth)
// this initialization is useful if current altitude is known,
// pressure will be calculated based on TruePressure and known altitude.
// note: use zeroCal only after initialization.
// dps.zeroCal(101800, 0); // set zero point
}
void loop()
{
while (Serial1.available())
{
gps.encode(Serial1.read()); // process gps messages
}
if(timeStatus()!= timeNotSet)
{
if( now() != prevDisplay) //update the display only if the time has changed
{
prevDisplay = now();
digitalClockDisplay();
}
}
}
void digitalClockDisplay()
{ // digital clock display of the time
//myGLCD.setColor(255,255,255);
myGLCD.print("Date :",8 ,12); // Print date label
myGLCD.printNumI(month(),64,12,2,'0'); // Prints 2 digit month with leading 0
myGLCD.print("/",80 ,12); //
myGLCD.printNumI(day(),88,12,2,'0'); // Prints 2 digit day with leading 0
myGLCD.print("/20",104,12);
myGLCD.printNumI(year(),112,12); // Print the Year
myGLCD.print("Time :",8 ,24); // Print Time label
myGLCD.printNumI(hourFormat12(),64,24,2,'0'); // Prints 2 digit 12 hour format with leading 0
myGLCD.printNumI(minute(),84,24,2,'0');
myGLCD.printNumI(second(),104,24,2,'0');
myGLCD.print("RH % :",8 ,36); // Print weather info labels
//myGLCD.print(" : % RH",94 ,36);
myGLCD.print("Baro :",8 ,48); // Print weather info labels
myGLCD.print(":InHg",102 ,48);
myGLCD.print("Temp :",8 ,60);
myGLCD.print("Temp :",8 ,72);
myGLCD.print(":DegF",102 ,60);
myGLCD.print(":DegC",102 ,72);
if (isAM())
{
myGLCD.setColor( 0,180, 0); //set rect line color for AM indicator color
myGLCD.drawRect(0, 6, 150,90);
myGLCD.setColor(255,255,255);
myGLCD.print(":AM",120,24);
}
if(isPM())
{
//fillScr(180,180,180);
myGLCD.setColor( 0, 0,180); //set rect line color for PM indicator color
myGLCD.drawRect(0, 6, 150,90); //draw a line 4 lines away from text ***78
myGLCD.setColor(180,180,180); //Set text color
myGLCD.print(":PM",120,24);
}
{
dps.getPressure(&Pressure); // Get Barimetric data +/- 1% accurate, in Millibars X 10
dps.getAltitude(&Altitude); // get Altimeter Data... Call doesn't return good data
dps.getTemperature(&Temperature); // Get Temperature Data +/- 1% accurate, Deg. C X 10
Temperature = Temperature +2 ; //Calibration factor for fine temp cal
Far = (((Temperature * 18) + 3200) / 10); // Far(enheit) conversion X 10
Pressure = ((2953 * Pressure) / 100000); // Comvert Pressure in Millibars X 10 to In/Hg X 10
Pressure = Pressure - 22 ; // Pressure Calibration adjustment subtracts .2 In/Hg
float PreS = (Pressure / 100.0);
float TemP = (Far/10.0);
myGLCD.printNumF(PreS,2,64 ,48);
myGLCD.printNumF(TemP,2,64 ,60);
// myGLCD.printNumF(t,2,64 ,90);
float h = dht.readHumidity();
float t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h))
{
h = 00.0;
myGLCD.printNumF(h,2,64 ,36);
}
else
{
myGLCD.printNumF(h,2,64 ,36);
myGLCD.printNumF(t,2,64 ,72);
}
}
}
time_t gpsTimeSync() // returns time if avail from gps, else returns 0
{
unsigned long fix_age = 0 ;
// OK TO HERE
gps.get_datetime(NULL,NULL, &fix_age);
// unsigned long time_since_last_fix; // **********COMMENTING THIS WAS DUMB...*************
//if(fix_age < 1000) // ******BUT THIS******
return gpsTimeToArduinoTime(); // return time only if updated recently by gps
//return 0; // ******AND THIS MAKES IT WORK, WHY??******
}
time_t gpsTimeToArduinoTime()
{ // returns time_t from gps date and time with the given offset hours
tmElements_t tm;
int year;
gps.crack_datetime(&year, &tm.Month, &tm.Day, &tm.Hour, &tm.Minute, &tm.Second,NULL,NULL );
tm.Year = year - 1970;
time_t time = makeTime(tm);
return time + (offset * SECS_PER_HOUR);
}
Comments and suggestions are humbly requested. I can cut and paste fairly well. I've learned enough C and C++ from a cold start in march to get this far.
Bob