Skip to content

Commit

Permalink
update Arduino examples
Browse files Browse the repository at this point in the history
  • Loading branch information
CongducPham committed Nov 2, 2018
1 parent bf2da48 commit e897d9b
Show file tree
Hide file tree
Showing 101 changed files with 4,511 additions and 2,246 deletions.
184 changes: 184 additions & 0 deletions Arduino/Arduino_GPS_Parser_GGA/Arduino_GPS_Parser_GGA.ino
@@ -0,0 +1,184 @@
//////////////////////////////////////////////////////////////////////
// Simple GPS Sensor : parsing GGA NMEA sentence (GPGGA or GNGGA)
// On the Ublox 8 series GPGGA is replaced by GNGGA by default
//
// $GNGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
//
// Copyright (C) 2018 Mamour Diop & Congduc Pham, University of Pau, France
/////////////////////////////////////////////////////////////////////

//#define HARDWARE_SERIAL_GPS
#define SOFTWARE_SERIAL_GPS

#ifdef HARDWARE_SERIAL_GPS

///////////////////////////////
// USING HARDWARE SERIAL
//
// On Arduino with only 1 serial port (TX/RX), it is necessary to perform the following actions
// as the serial port is use for both the GPS and the programming. To see GPS message:

// be sure that serial monitor is not open
// connect only GPS_GND<->GND and GPS_TX<>RX
// after uploading the sketch, connect GPS_VCC<>3.3V
// then open Serial Monitor (set baud rate to 9600)
// do not connect GPS_TX
//
// you should see NMEA messages from the GPS module
// to understand NMEA message: http://aprs.gids.nl/nmea/

// if you are using an Arduino board with several serial port (such as the MEGA)
// then define here the one that is connected to the GPS module
//#define gps_serial Serial3
#define gps_serial Serial

#endif

#ifdef SOFTWARE_SERIAL_GPS

#include <SoftwareSerial.h>
// connect GPS_TX <> 4 and GPS_RX <> 3
SoftwareSerial gps_serial(4,3); //rx,tx for Arduino side

#endif

char GPSBuffer[150];
byte GPSIndex=0;
double gps_latitude = 0.0;
double gps_longitude = 0.0;
char lat_direction, lgt_direction;

void parseNMEA(){
char inByte;
bool detect_new_line=false;

while (!detect_new_line)
{
if (gps_serial.available()) {
inByte = gps_serial.read();

//Serial.print(inByte); // Output exactly what we read from the GPS to debug

if ((inByte =='$') || (GPSIndex >= 150)){
GPSIndex = 0;
}

if (inByte != '\r' && inByte != '\n'){
GPSBuffer[GPSIndex++] = inByte;
}

if (inByte == '\n'){
GPSBuffer[GPSIndex++] = '\0';
detect_new_line=true;
}
}
}
Serial.print("---->");
Serial.println(GPSBuffer);
//Serial.flush();
}

bool isGGA(){
return (GPSBuffer[1] == 'G' && (GPSBuffer[2] == 'P' || GPSBuffer[2] == 'N') && GPSBuffer[3] == 'G' && GPSBuffer[4] == 'G' && GPSBuffer[5] == 'A');
}

// GPGGA and GNGGA for the Ublox 8 series. Ex:
// no fix -> $GPGGA,,,,,,0,00,99.99,,,,,,*63
// fix -> $GPGGA,171958.00,4903.61140,N,00203.95016,E,1,07,1.26,55.1,M,46.0,M,,*68

void decodeGGA(){
int i, j;
char latitude[30], longitude[30];
byte latIndex=0, lgtIndex=0;

for (i=7, j=0; (i<GPSIndex) && (j<9); i++) { // We start at 7 so we ignore the '$GNGGA,'

if (GPSBuffer[i] == ','){
j++; // Segment index
}
else{
if (j == 1){ //Latitude
latitude[latIndex++]= GPSBuffer[i];
}

else if (j == 2){ //Latitude Direction: N=North, S=South
lat_direction = GPSBuffer[i];
}

else if (j == 3){ //Longitude
longitude[lgtIndex++]= GPSBuffer[i];
}

else if (j == 4){ //Longitude Direction: E=East, W=West
lgt_direction = GPSBuffer[i];
}
}
}
//Serial.print("Lat Direction: "); Serial.println(lat_direction);
//Serial.print("Lgt Direction: "); Serial.println(lgt_direction);
gps_latitude = degree_location(atof(latitude), lat_direction);
gps_longitude = degree_location(atof(longitude), lgt_direction);
}

bool isValidLocation(){
return (gps_latitude != 0.0 && gps_longitude != 0.0 && (lat_direction == 'N' || lat_direction=='S') && (lgt_direction=='E'|| lgt_direction=='W'));
}

void display_location(){
Serial.println("==============");
Serial.print("Location: ");
Serial.print(gps_latitude, 5);
Serial.print(", ");
Serial.println(gps_longitude, 5);
Serial.println("==============");
}

//////////////////////////////////////////////////////////////////////////////////
//Convert NMEA latitude & longitude to decimal (Degrees)
// Notice that for latitude of 0302.78469,
// 03 ==> degrees. counted as is
// 02.78469 ==> minutes. divide by 60 before adding to degrees above
// Hence, the decimal equivalent is: 03 + (02.78469/60) = 3.046412
// For longitude of 10601.6986,
// ==> 106+1.6986/60 = 106.02831 degrees
// Location is latitude or longitude
// In Southern hemisphere latitude should be negative
// In Western Hemisphere, longitude degrees should be negative
//////////////////////////////////////////////////////////////////////////////////

double degree_location(double loc, char loc_direction){
double degLoc = 0.0;
double degWhole= loc/100; //gives the whole degree part of latitude
double degDec = (degWhole - ((int)degWhole)) * 100 / 60; //gives fractional part of location
degLoc = (int)degWhole + degDec; //gives complete correct decimal form of location degrees

if (loc_direction =='S' || loc_direction =='W') {
degLoc = (-1)*degLoc;
}
return degLoc;
}

void setup(){
Serial.begin(9600);
//if only 1 serial port then gps_serial is equivalent to Serial
#ifdef SOFTWARE_SERIAL_GPS
gps_serial.begin(9600);
#endif
Serial.println("GPS basic rogram");
}

void loop(){

bool get_fix = false;

while (!get_fix){
parseNMEA();
if (isGGA()){
decodeGGA();
if (isValidLocation()){
display_location();
get_fix = true;
}
}
}
}
4 changes: 2 additions & 2 deletions Arduino/Arduino_LoRa_Demo_Sensor/Arduino_LoRa_Demo_Sensor.ino
Expand Up @@ -154,7 +154,7 @@ void setup()
delay(500);

// initialization of the demo sensor
sensor_demo_Init();
sensor_Init();
}

char *ftoa(char *a, double f, int precision)
Expand Down Expand Up @@ -182,7 +182,7 @@ void loop(void)

if (millis() > nextTransmissionTime) {

sensor_value = sensor_demo_getValue();
sensor_value = sensor_getValue();

Serial.print(F("(Sensor value is "));
Serial.println(sensor_value);
Expand Down
8 changes: 4 additions & 4 deletions Arduino/Arduino_LoRa_Demo_Sensor/my_demo_sensor_code.cpp
Expand Up @@ -11,10 +11,10 @@ char nomenclature_str[4]="TC";
// ADD HERE SOME INITIALIZATION CODE
// HERE WE JUST DECLARE VALUE_PIN_READ AS INPUT PIN

void sensor_demo_Init() {
void sensor_Init() {

// for the temperature sensor
pinMode(VALUE_PIN_READ, INPUT);
pinMode(PIN_READ, INPUT);
pinMode(PIN_POWER, OUTPUT);
}
///////////////////////////////////////////////////////////////////
Expand All @@ -23,15 +23,15 @@ void sensor_demo_Init() {
// CHANGE HERE THE WAY YOU READ A VALUE FROM YOUR SPECIFIC SENSOR
// HERE IT IS AN EXAMPLE WITH THE LM35DZ SIMPLE ANALOG TEMPERATURE SENSOR

double sensor_demo_getValue() {
double sensor_getValue() {

//power the sensor
digitalWrite(PIN_POWER, HIGH);
// wait a bit
delay(500);

//read the raw sensor value
int value = analogRead(VALUE_PIN_READ);
int value = analogRead(PIN_READ);

//power down the sensor
digitalWrite(PIN_POWER, LOW);
Expand Down
6 changes: 3 additions & 3 deletions Arduino/Arduino_LoRa_Demo_Sensor/my_demo_sensor_code.h
Expand Up @@ -8,14 +8,14 @@
#endif

extern char nomenclature_str[4];
void sensor_demo_Init();
double sensor_demo_getValue();
void sensor_Init();
double sensor_getValue();

///////////////////////////////////////////////////////////////////
// CHANGE HERE THE PIN TO READ AND POWER SENSOR
// IF YOU USE A COMPLETELY DIFFERENT SENSOR, YOU MAY NOT NEED THIS LINE

#define VALUE_PIN_READ A0
#define PIN_READ A0
#define PIN_POWER 9
///////////////////////////////////////////////////////////////////

Expand Down

0 comments on commit e897d9b

Please sign in to comment.