Tuesday, April 4, 2017

Group 2_GPS_4/4/17

So today will be our first test run with the soldered hardware. The code has not been modified since last class and will still be working the same.


Indoors it obviously still does not work, so we had to take everything outside.


Once we took it outside, we checked to see if the GPS module was working, which it was and then we checked to see if it was reading and writing to the EEPROM, which it was.



So the soldered hardware is sleeker, more permanent and working well. As you can see in the video however, when it prints from EEPROM, there is some awkward spacing and a random character, so we went back through the code, line by line, in order to clean it up a little bit and ideally make the print out, more cohesive.

#include <Adafruit_GPS.h>

#include <EEPROM.h>

int addr = 0;
const int inPin = 6;     // the number of the toggle pin to enable logging
const int ledPin =  7;      // the number of the LED pin (displays if logging is on)
int buttonState = 0; // variable for reading the toggle status

// what's the name of the hardware serial port?
#define GPSSerial Serial1
// Connect to the GPS on the hardware port
Adafruit_GPS GPS(&GPSSerial);
// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO false

uint32_t timer = millis();


void setup() {
  // put your setup code here, to run once:
  // initialize the pushbutton pin as an input:
  pinMode(inPin, INPUT);
  pinMode(ledPin, OUTPUT);
  //while (!Serial);  // uncomment to have the sketch wait until Serial is ready
  
  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  // also spit it out
  Serial.begin(115200);
     
  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);
  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time
  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz
     
  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_ANTENNA);

  delay(1000);
  
  // Ask for firmware version
  GPSSerial.println(PMTK_Q_RELEASE);

  // clear the eeprom of stale data on inital start
  clearEeprom();
  
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonState = digitalRead(inPin);
  // button state high when logging is on
  if (buttonState == HIGH) {
    // turn on LED to display logging is on
    digitalWrite(ledPin, HIGH);
    // read data from the GPS in the 'main loop'
    char c = GPS.read();
  
    // this if is a place to debug if set true at top of file
    if (GPSECHO)

    if (c) Serial.print(c);
    // if a sentence is received, we can check the checksum, parse it...
    if (GPS.newNMEAreceived()) {
      // a tricky thing here is if we print the NMEA sentence, or data
      // we end up not listening and catching other sentences!
      // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
      Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
      if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
        return; // we can fail to parse a sentence in which case we should just wait for another
      }
      // if millis() or timer wraps around, we'll just reset it
      if (timer > millis()) timer = millis();
     
      // approximately every 2 seconds or so, print out (and write to eeprom) the current location
      if (millis() - timer > 2000) {
        timer = millis(); // reset the timer

        // make sure the GPS has a fix on location before continuing
        if (GPS.fix) {
          Serial.print("Location: ");
          Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
          Serial.print(", ");
          Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);

          // Save location to eeprom in the format
          // lat,lon;
          // ie. 4300.0302N,7868.9946W;
          // writen as ints (ASCII value)
          //23 spaces taken per addr saved
          //1024 space total
          // about 44 addresses able to be logged

          writeToEeprom((String)GPS.latitude,9);
          delay(1);

          writeToEeprom((String)GPS.lat,1);
          EEPROM.write(addr, (int)",");
          addr = addr + 1;
          delay(1);

          writeToEeprom((String)GPS.longitude,9);
          delay(1);

          writeToEeprom((String)GPS.lon,1);
          delay(1);
        
          // write endline char in our case ';'
          EEPROM.write(addr, (int)";");
          addr = addr + 1;
          delay(1);
      
        }
      }
    }
    
    else {
      digitalWrite(ledPin, LOW);
    }
}

// function to clear memory
int clearEeprom(){
  for(int i = 0; i< 1024; i++){
    EEPROM.write(i,"");
  }
  return 0;
}

// writes first count number of chars byte by byte
int writeToEeprom(String data, int count){
  for(int i = 0; i < count; i++){
        EEPROM.write(addr, (int)data[i]);

        addr = addr + 1;
        if (addr >= EEPROM.length()) {
          addr = 0; // resets addr counter
          break;
        }
        
      }
      return 0;
  
}

The last step, will be to take this to battery powered, rather than having to have it plugged into the computer, which would make it fully mobile.


To do this, we just need a battery pack with 3 AAA into the JST port on the Flora board. Once we had the battery pack, all we had to do was plug the Flora into the computer, to load the code, and then unplug it, plug in the battery pack, and we were good to go.


We were able to take the now fully mobile Flora outside, turn it on and flip the switch, wait for the red light on the GPS module to stop blinking, signifying it has gained satellites.


Then once we took it back inside, we were able to plug it into the computer and print out the location data. We were having a little bit of trouble with this, it working sometimes but not all the time, but we were not quite able to figure it out completely.




No comments:

Post a Comment

Group 1 - Blog 3

The code for the OLED display project has been finalized, and it is as follows: /*********************************************************...