#include <Adafruit_SSD1306.h> // OLED display
#include <Fonts/FreeMono9pt7b.h>
#include <Fonts/FreeMono12pt7b.h>
#include <Fonts/FreeMono24pt7b.h>
#include "DHT.h" // Humid & Temp sensor
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
float knots;
String year(1);
String month(1);
String day(1);
byte hours;
byte minutes;
byte seconds;
String latdeg; //Latitude degrees
String latmins; //Minutes
String longdeg; //Longitude degrees
String longmins;
double Kmsecscumu = 0; //Cumulative Km/hr readings (accumulated once per GPS transmitt (once per second))
String inputString = ""; //Where characters from GPS are stored
int switchdisp = 0;
#define DHTPIN 9 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11 type of temperature & humidity sensor
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
void setup() {
Serial.begin(9600);
pinMode(4, INPUT_PULLUP); //Display data button select connected here
dht.begin(); // Start the DHT sensor
// Generate the high voltage from the 3.3v line internally
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with voltage setting & the I2C addr of the OLED display
display.setTextColor(WHITE, BLACK);
}
void loop() {
while (Serial.available()) { //Read and store characters as they arrive from the GPS
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') { //End of sentenance charater
//Serial.println(inputString);
if ((inputString.substring(0, 6) == "$GNRMC")) { //The sentence with the required data
//Serial.print(inputString);
knots = inputString.substring(46, 51).toInt(); //Speed begins 46 characters from start of string and extends to 51-1 (5 chars total)
hours = inputString.substring(7, 9).toInt(); //Time stamp begins 7 characters from start of string and extends to 13-1 (6 chars total)
minutes = inputString.substring(9, 11).toInt(); //Minutes
seconds = inputString.substring(11, 13).toInt(); //Seconds
day = inputString.substring(53, 55); //Day of month
month = inputString.substring(55, 57); //Month
year = inputString.substring(57, 59); //Year
latdeg = inputString.substring(19, 21);
latmins = inputString.substring(21, 27);
longdeg = inputString.substring(32, 35);
longmins = inputString.substring(35, 41);
Kmsecscumu = Kmsecscumu + (knots * 1.852); //Cumulative Kms-seconds
// Button read code placed here so the pin is checked, & switchdisp incremented,only once per second giving time to release the button
// before switchdisp is incremented additional time(s). Makes use of one second delay in GPS updates w/o desynchonizing message timing.
if (digitalRead(4) == LOW) { //Switch to new display value? = 0 for speed, = 1 for time, = 2 for lat & long, = 3 for temperature & humidity, = 4 for odometer
switchdisp++;
if (switchdisp > 4) { //Roll back to 0
switchdisp = 0;
}
}
display.clearDisplay();
if (switchdisp == 0) { //Display speed?
if (knots * 1.852 <= 9) {
display.setCursor(40, 60);
}
else {
display.setCursor(20, 60);
}
display.setFont(&FreeMono24pt7b);
display.setTextSize(2);
display.print(int(knots * 1.852)); //Display in Kmph
}
if (switchdisp == 1) { //Display time?
display.setFont(&FreeMono12pt7b);
display.setTextSize(1);
display.setCursor(10, 18);
/*if (hours <= 7) { //MST day will be 1 less than UT day if UT hours <= 7
day =
}
if (day == 0) { //Takes care of 1st of month
day++;
}*/
display.print(year + ("/") + month + ("/") + day);
display.setTextSize(2);
if (hours >= 8) { //Convert time from UT 24 hr clock to MST 24 hr clock
hours = hours - 7;
}
if (hours <= 7) {
hours = hours + 17;
}
if (hours > 12) { //Convert time from MST 24 hr clock to MST 12 hr clock
hours = hours - 12;
}
if (hours == 0) {
hours = 1;
}
if (hours <= 9) {
display.setCursor(0, 60);
display.print("0");
display.setCursor(28, 60);
}
else {
display.setCursor(0, 60);
}
display.print(hours);
display.setCursor(50, 55); //Display blinking ":"
if (seconds % 2 > 0) {
display.print(" ");
}
else {
display.print(":");
}
if (minutes <= 9) { //Display a leading "0"
display.setCursor(70, 60);
display.print("0");
display.setCursor(95, 60);
}
else {
display.setCursor(75, 60);
}
display.print(minutes);
}
if (switchdisp == 2) { //Lat & Long
display.setFont(&FreeMono9pt7b);
display.setTextSize(1);
display.setCursor(46, 10);
display.print("LAT");
display.setCursor(15, 25);
display.print(latdeg);
display.setCursor(60, 25);
display.print(latmins);
display.setCursor(40, 43);
display.print("LONG");
display.setCursor(4, 60);
display.print(longdeg);
display.setCursor(60, 60);
display.print(longmins);
}
if (switchdisp == 3) { //DHT readings
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = 0; //dht.readHumidity();
// Read temperature as Celsius (the default)
float t = 0; //dht.readTemperature();
h = h * 0.95; // humidity calibrate
t = t * 0.96; // & deg C
display.setFont(&FreeMono9pt7b);
display.setTextSize(1);
display.setCursor(4, 20);
display.print("Temp");
display.setCursor(74, 20);
display.print("Humid");
display.setFont(&FreeMono24pt7b);
display.setTextSize(1);
display.setCursor (0, 60);
display.print(int(t)); //Back to integer for display
display.setCursor(72, 60);
display.print(int(h));
}
if (switchdisp == 4) { //Odometer
display.setFont(&FreeMono9pt7b);
display.setTextSize(1);
display.setCursor(20, 20);
display.print("Odometer");
display.setTextSize(2);
display.setCursor(20, 60);
display.print((float(Kmsecscumu) / 3600), 1); //Cumulative Km/hr/seconds converted to Kms)
}
display.display(); //Show it all on display
}
inputString = "";
}
}
}