Wednesday, April 18, 2018

OLED GPS display on Arduino with a little C++

 The Arduino language is actually C++ but you never see that unless you look at library source code which tends to be a C++ class. I've written a little code to read the $4.10 GPS and display a bit of info on a small OLED display using the excellent Adafruit library.

To avoid spaghetti code I put the parsing of the NMEA string into a class. This is a quick hack but you don't see many examples like this so here you go.

Just for fun I've boxed this little project up all held together with hot glue and running from a 3.3V LiPo cell. The low price of the GPS along with the low price of an Arduino Nano Pro makes this a very attractive platform for building embedded computing devices.


Update: now with charger

I've added a USB charging board, this one from Jaycar. I have some others on order. This one will not run the Arduino board if the battery is removed, I read that some do and that would be a useful feature.





/*********************************************************************
Display info from a cheap GPS on an Adafruit OLD display
*********************************************************************/

#include 
#include 
#include 
#include 

SoftwareSerial GPS(2,3); // rx pin = 2, tx pin for GPS

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

class NmeaRecord {
  public:
    NmeaRecord();
    void clearFields();
    bool parseLine(char *line); // return true if useful
    
    // GPGGA fields
    enum {
      eLabel,
      eTimeStamp,
      eLat,
      eLatNS,
      eLon,
      eLonEW,
      eFix,
      eSatellites,
      eHorizDilution,
      eAltitude,
      eHeight,
      eEmpty1,
      eEmpty2,
      eChecksum
    };
    // string lengths, +1 for null terminator
    enum {
      kLabelLen = 7,
      kTimeStampLen = 11,
      kLatLen = 9,
      kLatNSLen = 2,
      kLonLen = 10,
      kLonEWLen = 2,
      kFixLen = 2,
      kSatellitesLen = 3,
      kHorizontalDilutionLen = 4,
      kAltitudeLen = 7
    };
    char label[kLabelLen];
    char timeStamp[kTimeStampLen];
    char lat[kLatLen];
    char latNS[kLatNSLen];
    char lon[kLonLen];
    char lonEW[kLonEWLen];
    char fix[kFixLen];
    char satellites[kSatellitesLen];
    char horizontalDilution[kHorizontalDilutionLen];
    char altitude[kAltitudeLen];
  private:
    void storeField(int index, char*field);
};

NmeaRecord nmeaRecord = NmeaRecord();

void setup()   { 
  GPS.begin(9600);               
  //Serial.begin(115200);
  //Serial.println("started");
  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  // init done
  
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("Looking for satellites...");
}


void loop() {
  
  String line = GPS.readStringUntil('$');
  
  // GPGGA,054540.000,3346.7737,S,15113.2178,E,2,12,0.99,94.2,M,21.9
  if(nmeaRecord.parseLine(line.c_str())) {
    display.clearDisplay();
    display.setCursor(0,0);
    display.print("UTC: ");
    display.print(formatTime(nmeaRecord.timeStamp));
    display.print(" sats=");
    display.println(nmeaRecord.satellites);
  
    display.print("Lat: ");
    display.print(formatLat(nmeaRecord.lat));
    display.print(",");
    display.println(nmeaRecord.latNS);
  
    display.print("Lng: ");
    display.print(formatLng(nmeaRecord.lon));
    display.print(",");
    display.println(nmeaRecord.lonEW);
  
    display.print("Alt: ");
    display.print(nmeaRecord.altitude);
    display.print("m   fix=");
    display.print(nmeaRecord.fix);
    display.display();
  }
}


NmeaRecord::NmeaRecord() {
  this->clearFields();
}

// return true if it's a useful line
bool NmeaRecord::parseLine(char *line) {
  char *token;
  token = strtok(line, ",");
  if(strcmp(token, "GPGGA") != 0) {
    return false; // don't parse
  }
  int fieldIndex = 0;
  while(token != NULL) {
    this->storeField(fieldIndex, token);
    fieldIndex++;
    token = strtok(NULL, ",");
  }
  return true;
}

void NmeaRecord::clearFields() {
  strcpy(this->label, "");
  strcpy(this->timeStamp, "");
  strcpy(this->lat, "");
  strcpy(this->latNS, "");
  strcpy(this->lon, "");
  strcpy(this->lonEW, "");
  strcpy(this->fix, "");
  strcpy(this->satellites, "0");
  strcpy(this->horizontalDilution, "");
  strcpy(this->altitude, "");
}

void NmeaRecord::storeField(int index, char*field) {
  Serial.print("Storefield index = ");
    Serial.print(index);
    Serial.print(", field = ");
    Serial.println(field);
    
  switch(index) {
    case eLabel:
      strncpy(label, field, kLabelLen);
      break;
    case eTimeStamp:
      strncpy(timeStamp, field, kTimeStampLen);
      Serial.print("got timeStamp = ");
      Serial.println(this->timeStamp);
      break;
    case eLat:
      strncpy(lat, field, kLatLen);
      break;
    case eLatNS:
      strncpy(latNS, field, kLatNSLen);
      break;
    case eLon:
      strncpy(lon, field, kLonLen);
      break;
    case eLonEW:
      strncpy(lonEW, field, kLonEWLen);
      break;
    case eFix:
      strncpy(fix, field, kFixLen);
      break;
    case eSatellites:
      strncpy(satellites, field, kSatellitesLen);
      break;
    case eHorizDilution:
      strncpy(horizontalDilution, field, kHorizontalDilutionLen);
      break;
    case eAltitude:
      strncpy(altitude, field, kAltitudeLen);
      break;
  }
}

String formatTime(String timeString) {
  String newTime = timeString.substring(0,2);
  newTime += ":";
  newTime += timeString.substring(2,4);
  newTime += ":";
  newTime += timeString.substring(4,6);
  return newTime;
}

String formatLat(String raw) {
  // 4807.038,N   Latitude 48 deg 07.038' N
  String result = raw.substring(0,2);
  result += " deg ";
  result += raw.substring(2,7);
  return result;
}

String formatLng(String raw) {
  //  01131.000,E  Longitude 11 deg 31.000' E
  String result = raw.substring(0,3);
  result += " deg ";
  result += raw.substring(3,8);
  return result;
}

No comments: