Arduino Yun Mini + Adafruit Ultimate GPS


Excel

Photo






Sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arduino YUN mini connected to a Adafruit Ultimate GPS | |
// Copyright (C) 2017 https://www.roboticboat.uk | |
// d7ea1aa3-5c61-4c79-b070-bd7fb060b852 | |
// | |
// This program is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// | |
// This program is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU General Public License for more details. | |
// | |
// You should have received a copy of the GNU General Public License | |
// along with this program. If not, see <https://www.gnu.org/licenses/>. | |
// These Terms shall be governed and construed in accordance with the laws of | |
// England and Wales, without regard to its conflict of law provisions. | |
#include <Bridge.h> | |
#include <YunServer.h> | |
#include <YunClient.h> | |
#include <SoftwareSerial.h> | |
// gpsSerial(receive from GPS,transmit to the GPS module) | |
SoftwareSerial gpsSerial(10,11); | |
// The one global server object listening for incoming connections. | |
#define PORT 23 | |
YunServer Server(PORT); | |
// Define an array of simultaneous clients | |
#define NUM_CLIENTS 1 | |
YunClient clients[NUM_CLIENTS]; | |
// Global variables | |
String inputSerial1 = ""; // a string to hold incoming data | |
String inputStore = ""; | |
boolean IsReadySerial1 = false; // whether the string is complete | |
void setup() | |
{ | |
Bridge.begin(); | |
Server.noListenOnLocalhost(); // Listen for external connections | |
Server.begin(); | |
//Receive from the GPS device (the NMEA sentences) - Green wire | |
pinMode(10, INPUT); | |
//Transmit to the GPS device - Yellow wire | |
pinMode(11, OUTPUT); | |
// Keep the User informed | |
Serial.begin(9600); | |
Serial.println("Initializing GPS"); | |
gpsSerial.begin(9600); | |
delay(100); | |
// The Adafruit GPS flashes every second while search. Once every 15 seconds once a fix is found | |
// | |
// $GPGGA,094728.000,5126.4900,N,00016.0200,E,2,08,1.30,19.4,M,47.0,M,0000,0000*52 | |
// $GPGSA,A,3,05,20,13,24,15,30,28,17,,,,,2.08,1.30,1.63*06 | |
// $GPRMC,094728.000,A,5126.4900,N,00016.0200,E,0.01,259.87,310318,,,D*6B | |
// $GPVTG,259.87,T,,M,0.01,N,0.02,K,D*3A | |
// $GPGGA,094729.000,5126.4900,N,00016.0200,E,2,08,1.30,19.4,M,47.0,M,0000,0000*53 | |
// $GPGSA,A,3,05,20,13,24,15,30,28,17,,,,,2.08,1.30,1.63*06 | |
// $GPRMC,094729.000,A,5126.4900,N,00016.0200,E,0.00,264.45,310318,,,D*6B | |
// $GPVTG,264.45,T,,M,0.00,N,0.01,K,D*38 | |
//gpsSerial.println("$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28"); | |
gpsSerial.println("$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*27"); | |
delay(100); | |
// Reserve 200 bytes for the inputSerial1 string | |
inputSerial1.reserve(200); | |
} | |
void loop() | |
{ | |
while (gpsSerial.available()) | |
{ | |
// Read the new byte: | |
char nextChar = (char)gpsSerial.read(); | |
// Append to the inputSerial1 string | |
inputSerial1 += nextChar; | |
// If a newline, then set flag so that the main loop will process the string | |
if (nextChar == '\n') { | |
inputStore = inputSerial1; | |
inputSerial1 = ""; | |
} | |
} | |
// Process every client | |
for (byte i = 0; i<NUM_CLIENTS; i++) | |
{ | |
if (clients[i].connected()) // Is it connected? | |
process(i); // It's connected, process the client | |
else | |
{ | |
if ((bool)clients[i]) // Not connected. Is the client open? | |
{ | |
// The client is not connected, but is currently open. | |
Serial.print("Lost connection to client "); | |
Serial.println(i); | |
clients[i].stop(); // Close and clean up the old connection | |
} | |
// At this point, the client is not connected, and is closed. | |
clients[i] = Server.accept(); // Try to establish a connection. | |
if (clients[i].connected()) // Was a connection made? | |
{ | |
Serial.println("New connection on client "); | |
//Serial.println(i); | |
clients[i].print(inputStore); | |
clients[i].stop(); | |
} | |
} | |
} | |
} | |
void process(byte index) | |
{ | |
// Read and echo all available data from the client. | |
String str = ""; | |
// Read all available data, append to string | |
while (clients[index].available()) | |
str = str + (char)clients[index].read(); | |
// Trim off non-printable characters. | |
str.trim(); | |
// Got something? If so, print out what was recevied, send client ID back to other side | |
if (str.length() > 0) | |
{ | |
Serial.print("Received from client "); | |
Serial.print(index); | |
Serial.println(": received \"" + str + "\""); | |
clients[index].print("Client "); | |
clients[index].println(index); | |
} | |
} | |