Skip to content

Instantly share code, notes, and snippets.

@JvGinkel
Last active November 16, 2020 16:01
Show Gist options
  • Save JvGinkel/7715977a3bfae073dbac43ce700a4f3e to your computer and use it in GitHub Desktop.
Save JvGinkel/7715977a3bfae073dbac43ce700a4f3e to your computer and use it in GitHub Desktop.
openrc-gps-speedo
// Load Wi-Fi library
#include <WiFi.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>
//#include "SD.h"
//#include <SPI.h>
// Define CS pin for the SD card module
#define SD_CS 5
TinyGPS gps;
SoftwareSerial gpsSerial(14, 27);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
// Replace with your network credentials
const char* ssid = "OpenRC_GPS";
const char* password = NULL;
float top_kmph = 0.00;
float current_kmph;
int current_sats;
float flat, flon;
unsigned long age;
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
void setup() {
gpsSerial.begin(9600);
Serial.begin(115200);
Serial.println(TinyGPS::library_version());
// // Initialize SD card
// SD.begin(SD_CS);
//
// uint8_t cardType = SD.cardType();
// if (cardType == CARD_NONE) {
// Serial.println("No SD card attached");
// return;
// }
// if (!SD.begin(SD_CS)) {
// Serial.println("Card Mount Failed");
// return;
// }
//
// Serial.println("Initializing SD card...");
// if (!SD.begin(SD_CS)) {
// Serial.println("ERROR - SD card initialization failed!");
// return; // init failed
// }
//
// // If the data.txt file doesn't exist
// // Create a file on the SD card and write the data labels
// File file = SD.open("/data.txt");
// if (!file) {
// Serial.println("File doens't exist");
// Serial.println("Creating file...");
// writeFile(SD, "/data.txt", "Reading ID, Date, Hour, Temperature \r\n");
// }
// else {
// Serial.println("File already exists");
// }
// file.close();
// Connect to Wi-Fi network with SSID and password
Serial.print("Setting AP (Access Point)…");
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.begin();
}
static void print_date(TinyGPS &gps)
{
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long age;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
if (age == TinyGPS::GPS_INVALID_AGE)
Serial.print("********** ******** ");
else
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
month, day, year, hour, minute, second);
Serial.print(sz);
}
// print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
}
void loop() {
bool newData = false;
for (unsigned long start = millis(); millis() - start < 500;) {
while (gpsSerial.available()) {
char c = gpsSerial.read();
//Serial.write(c); // uncomment this line if you want to see the GPS data flowing
if (gps.encode(c)) // Did a new valid sentence come in?
newData = true;
}
}
if (newData) {
gps.f_get_position(&flat, &flon, &age);
// Current kmph
current_kmph = gps.f_speed_kmph() == TinyGPS::GPS_INVALID_F_SPEED ? 0 : gps.f_speed_kmph();
current_sats = gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites();
// print_date(gps);
// Serial.print("SPEED=");
// Serial.print(current_kmph);
// Serial.print(" SAT=");
// Serial.println(current_sats);
// Serial.print(" LAT=");
// Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
// Serial.print(" LON=");
// Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
// Serial.print(" PREC=");
// Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
if (current_kmph > top_kmph) {
top_kmph = current_kmph;
}
}
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
if (header.indexOf("GET /reset") >= 0) {
Serial.println("Reset top speed to 0.00");
top_kmph = 0.00;
client.println("HTTP/1.1 302 Found");
client.println("Location: /");
client.println("Connection: close");
client.println();
break;
}
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// // turns the GPIOs on and off
// if (header.indexOf("GET /26/on") >= 0) {
// Serial.println("GPIO 26 on");
// output26State = "on";
// digitalWrite(output26, HIGH);
// } else if (header.indexOf("GET /26/off") >= 0) {
// Serial.println("GPIO 26 off");
// output26State = "off";
// digitalWrite(output26, LOW);
// } else if (header.indexOf("GET /27/on") >= 0) {
// Serial.println("GPIO 27 on");
// output27State = "on";
// digitalWrite(output27, HIGH);
// } else if (header.indexOf("GET /27/off") >= 0) {
// Serial.println("GPIO 27 off");
// output27State = "off";
// digitalWrite(output27, LOW);
// }
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); // <meta http-equiv=\"refresh\" content=\"2\">
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>OpenF1 RC - GPS Speed</h1>");
client.println("<center><table border=1>");
client.print("<tr><td width=\"150\">Top Speed</td><td>");
client.print(top_kmph);
client.println("</td></tr>");
client.print("<tr><td>Current Speed</td><td>");
client.print(current_kmph);
client.println("</td></tr>");
client.print("<tr><td>GPS Sats</td><td>");
client.print(current_sats);
client.println("</td></tr>");
client.print("<tr><td>LONG</td><td>");
client.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
client.println("</td></tr>");
client.print("<tr><td>LAT</td><td>");
client.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
client.println("</td></tr>");
client.println( "</table></center>");
client.println("<p><a href=\"/reset\"><button class=\"button\">Reset</button></a></p>");
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
// Serial.println("Client disconnected.");
// Serial.println("");
}
}
//// Write to the SD card (DON'T MODIFY THIS FUNCTION)
//void writeFile(fs::FS &fs, const char * path, const char * message) {
// Serial.printf("Writing file: %s\n", path);
//
// File file = fs.open(path, FILE_WRITE);
// if (!file) {
// Serial.println("Failed to open file for writing");
// return;
// }
// if (file.print(message)) {
// Serial.println("File written");
// } else {
// Serial.println("Write failed");
// }
// file.close();
//}
//// Append data to the SD card (DON'T MODIFY THIS FUNCTION)
//void appendFile(fs::FS &fs, const char * path, const char * message) {
// Serial.printf("Appending to file: %s\n", path);
//
// File file = fs.open(path, FILE_APPEND);
// if (!file) {
// Serial.println("Failed to open file for appending");
// return;
// }
// if (file.print(message)) {
// Serial.println("Message appended");
// } else {
// Serial.println("Append failed");
// }
// file.close();
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment