Skip to content

Instantly share code, notes, and snippets.

@elpsk
Last active May 22, 2017 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elpsk/11f131d02d7ea9387cae4c9082dc832f to your computer and use it in GitHub Desktop.
Save elpsk/11f131d02d7ea9387cae4c9082dc832f to your computer and use it in GitHub Desktop.
arduino snippet to make a gps/ble speedometer controlled by ios app
/*
MTB Speedometer 1.0
- GPS module
- BLE 4 module
- 7 Segment display
- DHT sensor for temperature and humidity
(c) 2017 Alberto Pasca
*/
#include <string.h>
#include <ctype.h>
// Adafruit 7 segment
#include <Adafruit_GFX.h>
#include "Adafruit_Sensor.h"
#include "Adafruit_LEDBackpack.h"
// GPS
#include "TinyGPS++.h"
// Bluetooth
#include <SoftwareSerial.h>
// Humidity & Temperature
#include "DHT.h"
// -----------------------------------------------------------------------------
int byteGPS =- 1;
int gpsPinRX = 0;
int gpsPinTX = 1;
int btPinRX = 10;
int btPinTX = 11;
// TinyGPS
TinyGPSPlus gps;
// 7 Segment
Adafruit_7segment matrix = Adafruit_7segment();
// CLK => A5
// DAT => A4
// Bluetooth
SoftwareSerial BTSerial(btPinRX, btPinTX); // Receive (RD), Transmit (TxD)
// DHT sensor
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
char btReceivedData = 'A';
// -----------------------------------------------------------------------------
struct GPSData {
double lat;
double lon;
double alt;
double kmh;
int date;
int time;
};
struct DHTData {
double temp;
double hum;
};
GPSData *gpsData = new GPSData();
DHTData *dhtData = new DHTData();
// -----------------------------------------------------------------------------
void setup() {
pinMode(gpsPinRX, INPUT);
pinMode(gpsPinTX, OUTPUT);
Serial.begin(4800);
// init display
matrix.begin(0x70);
// init serial bt
BTSerial.begin(9600);
// init dht sensor
dht.begin();
}
void loop() {
prepareGPSData( gpsData );
float temp = dht.readTemperature();
float hum = dht.readHumidity();
if ( temp > 0 ) {
dhtData->temp = temp;
}
if ( hum > 0 ) {
dhtData->hum = dht.readHumidity();
}
if ( btReceivedData == 'A' ) {
writeOnDisplay( gpsData->kmh );
} else if ( btReceivedData == 'B' ) {
writeOnDisplay( gpsData->lat );
} else if ( btReceivedData == 'C' ) {
writeOnDisplay( dhtData->temp );
} else if ( btReceivedData == 'D' ) {
writeOnDisplay( dhtData->hum );
}
BTSerial.write( prepareBTData(gpsData, dhtData) );
char currentData = readBluetoothCommand();
if ( currentData == 'A' || currentData == 'B' || currentData == 'C' || currentData == 'D' ) {
btReceivedData = currentData;
}
delay(100);
}
// -----------------------------------------------------------------------------
char* prepareBTData( GPSData *gps, DHTData *dht ) {
String btOutputStr = String(gps->lat);
btOutputStr += "§";
btOutputStr += String(gps->lon);
btOutputStr += "§";
btOutputStr += String(gps->alt);
btOutputStr += "§";
btOutputStr += String(gps->kmh);
btOutputStr += "§";
btOutputStr += String(gps->date);
btOutputStr += "§";
btOutputStr += String(gps->time);
btOutputStr += "§";
btOutputStr += String(dht->temp);
btOutputStr += "§";
btOutputStr += String(dht->hum);
btOutputStr += "#";
char btOutput[btOutputStr.length() * 2];
btOutputStr.toCharArray(btOutput, btOutputStr.length() * 2);
Serial.println( btOutput );
return btOutput;
}
char readBluetoothCommand() {
return BTSerial.read();
}
void writeOnDisplay( double text ) {
matrix.println( text );
matrix.writeDisplay();
}
void prepareGPSData( GPSData *data ) {
// gps
byteGPS = Serial.read(); // Read a byte of the serial port
if ( byteGPS != -1 ) {
// TinyGPS
gps.encode( Serial.read() );
data->lat = gps.location.lat();
data->lon = gps.location.lng();
data->alt = gps.altitude.meters();
data->kmh = gps.speed.kmph();
data->time = gps.time.value();
data->date = gps.date.value();
}
delay(100);
}
// -----------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment