This code adds DHT data and removes the serialmon data to thin the code. Also showing the only next text size up from size 1-2 in the OLED.
/******************************************************************************
Steve_GPS_OLED.ino to display GPS and DHT11 data in OLED, adapted from:
TinyGPSPlus_GPS_Shield.ino
TinyGPS++ Library Example for the SparkFun GPS Logger Shield
By Jim Lindblom @ SparkFun Electronics
February 9, 2016
https://stevepedwards.today/github.com/sparkfun/GPS_Shield
This example uses SoftwareSerial to communicate with the GPS module on
pins 8 and 9. It uses the TinyGPS++ library to parse the NMEA strings sent
by the GPS module, and prints interesting GPS information to the serial
monitor.
After uploading the code, open your serial monitor, set it to 9600 baud, and
watch for latitude, longitude, altitude, course, speed, date, time, and the
number of visible satellites.
Resources:
TinyGPS++ Library - https://stevepedwards.today/github.com/mikalhart/TinyGPSPlus/releases
SoftwareSerial Library
Development/hardware environment specifics:
Arduino IDE 1.6.7
GPS Logger Shield v2.0 - Make sure the UART switch is set to SW-UART
Arduino Uno, RedBoard, Pro, etc.
OLED pins from front are 1 to Nano 5v, 2 to Nano pin GND, 3 to Nano pin A4, 4 to Nano pin A5;
Neo7 GPS pins from front are pin 1 to Nano D9, pin2 to D8, pin 3 to GND, pin 4 to Nano 5V;
******************************************************************************/
//#include is used to include outside libraries in your sketch.
// 0/1 UART for programming/Serial monitor-ing, use SoftwareSerial:
#include <SoftwareSerial.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TinyGPS++.h> // Include the TinyGPS++ library
#include "dht.h"
#define dht_apin A0 // Analog Pin sensor is connected to
// #define is a useful C component that allows the programmer to give a name to a
//constant value before the program is compiled.
#define ARDUINO_GPS_RX 9 // GPS TX, Arduino RX pin
#define ARDUINO_GPS_TX 8 // GPS RX, Arduino TX pin
#define gpsPort ssGPS // Alternatively, use Serial1 on the Leonardo
// Define the serial monitor port. On the Uno, and Leonardo this is 'Serial'
// on other boards this may be 'SerialUSB'
#define SerialMonitor Serial
#define GPS_BAUD 9600 // GPS module baud rate. GP3906 defaults to 9600.
// If you're using an Arduino Uno, RedBoard, or any board that uses the
// 0/1 UART for programming/Serial monitor-ing, use SoftwareSerial:
// OLED screen type parameters
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
dht DHT;
TinyGPSPlus tinyGPS; // Create a TinyGPSPlus object
// NOTE: This define has to be placed below the Adafruit library it comes from as page is read top down!
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
SoftwareSerial ssGPS(ARDUINO_GPS_TX, ARDUINO_GPS_RX); // Create a SoftwareSerial
// Set gpsPort to either ssGPS if using SoftwareSerial or Serial1 if using an
// Arduino with a dedicated hardware serial port
// Integers are your primary data-type for number storage.
// (minimum value of -2^15 and a maximum value of (2^15) - 1)
// Arduino takes care of dealing with negative numbers for you
int m = 0; // global constant to reserve mem for this data, feet to meters later
// The void keyword is used only in function declarations.
// It indicates that the function is expected
// to return no information to the function from which it was called.
// The setup() function is called when a sketch starts. Use it to initialize
//variables, pin modes, start using libraries, etc. The setup function will only
//run once,after each powerup or reset of the Arduino board.
void setup() {
Serial.begin(9600);
DHT.read11(dht_apin);
SerialMonitor.begin(9600);
gpsPort.begin(GPS_BAUD);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
// Address 0x3C for 128x32
for(;;); // Don't proceed, loop forever
}
} // end setup
void printGPS_OLED() {
// OLED screen page 1
// Print latitude, longitude, altitude in feet, course, speed, date, time,
// and the number of visible satellites.
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.print("Lat: "); display.println(tinyGPS.location.lat(), 6);
display.print("Long: "); display.println(tinyGPS.location.lng(), 6);
display.print("Alt: "); display.print(tinyGPS.altitude.feet()); display.println(" feet");
int m = (tinyGPS.altitude.feet()) / 3.28084; // conv ft to meters
display.print("Alt: "); display.print(m); display.print(" meters");
display.display();
delay(3000);
// OLED screen page 2
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.print("Date: "); display.print(tinyGPS.date.day());
display.print("/");
display.print(tinyGPS.date.month());
display.print("/");
display.println(tinyGPS.date.year());
display.print("Time: "); display.print(tinyGPS.time.hour());
display.print(":");
if (tinyGPS.time.minute() < 10) display.print('0');
display.print(tinyGPS.time.minute());
display.print(":");
if (tinyGPS.time.second() < 10) display.print('0');
display.println(tinyGPS.time.second());
display.print("Sats: "); display.println(tinyGPS.satellites.value());
display.println();
display.display();
delay(3000);
} // end printGPS_OLED()
// This custom version of delay() ensures that the tinyGPS object
// is being "fed". From the TinyGPS++ examples.
static void smartDelay(unsigned long ms) {
unsigned long start = millis();
do
{
// If data has come in from the GPS module
while (gpsPort.available())
tinyGPS.encode(gpsPort.read()); // Send it to the encode function
// tinyGPS.encode(char) continues to "load" the tinGPS object with new
// data coming in from the GPS module. As full NMEA strings begin to come in
// the tinyGPS library will be able to start parsing them for pertinent info
} while (millis() - start < ms);
} // end smartDelay()
// After creating a setup() function, which initializes and sets the initial values,
// loop section loops forever so contained functions from above update new data values from GPS
// and print to serialmon and OLED
void loop() {
DHT.read11(dht_apin);
display.clearDisplay();
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.print("humidity %");
display.println();
display.print(DHT.humidity);
display.display();
delay(3000);
display.clearDisplay();
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.print("temp C");
display.println();
display.print(DHT.temperature);
display.display();
delay(3000);//Wait 5 seconds before accessing sensor again.
printGPS_OLED(); // function calling the GPS data to display in the OLED
// "Smart delay" looks for GPS data while the Arduino's not doing anything else
smartDelay(1000);
} //end loop