Geolocation through Ethernet

Dependencies:   mbed HTTPClient MMA8452Q mbed-rtos 4DGL-uLCD-SE NTPClient SDFileSystem1 EthernetInterface

main.cpp

Committer:
stephenb
Date:
2020-12-17
Revision:
1:b0c480b628cd
Parent:
0:070ff55a028f
Child:
3:65349fe42061

File content as of revision 1:b0c480b628cd:

#include "mbed.h"
#include "NTPClient.h"
#include "uLCD_4DGL.h"
#include "EthernetInterface.h"
#include "HTTPClient.h"
#include "SDFileSystem.h"
//Geolocation output to LCD and SD card reader
//Code based on: https://os.mbed.com/users/tlisowski3/notebook/geolocation-and-ntp-clock-on-ulcd-144-g2/

//Set up Networking, LCD and SD Card reader
uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card, SPI pins
EthernetInterface eth;
NTPClient ntpClient;
HTTPClient httpClient;

void parse(char buffer[], int *j, char *string); //FUNCTION TO PARSE HTTP GET DATA
void sdlog(int type, float time, float input0, char input1[], char input2[], char input3[]); //Function to write to the SD card
char httpGetData[200]; //BUFFER TO HOLD DATA FROM HTTP GET REQUEST
 
int main() {
    //Display Settings
    uLCD.baudrate(115200); //Baudrate of display
    uLCD.cls();    //Clear uLCD screen
    uLCD.background_color(BLACK); //SET BACKGROUND COLOR TO WHITE
    uLCD.color(WHITE);
    uLCD.locate(0,0);  //Start printing on col0, row0
    uLCD.printf("Getting Location \nInformation...");

    eth.init(); //USE DHCP to get local IP address
    eth.connect(); //Connect to the network
        
    char success[10]={0};  //success first
    char countryFull[20]={0}; //Full Country Name
    char countryAbrv[5]={0}; //Abbreviated Country Name or country Code
    char stateAbrv[5]={0}; //Abbreviated State or region code
    char stateFull[15]={0}; //Full State Name
    char city[15]={0}; //City Name
    char zip[6]={0}; //ZIP CODE
    char latitude[10]={0}; //latitude
    char longitude[10]={0}; //longitude
    char timeZone[30]={0}; //timeZone
    int j=0;
    HTTPResult r = httpClient.get("http://ip-api.com/csv",httpGetData,128); //GET GEOLOCATION DATA (CSV)
    
    if (r==HTTP_OK) { //IF THE DATA WAS RECIEVED
        j=0;
        //parse and display each of the API's location information strings on the LCD
        parse(httpGetData, &j, success); 
        parse(httpGetData,&j,countryFull);
        parse(httpGetData,&j,countryAbrv);
        parse(httpGetData,&j,stateAbrv);
        parse(httpGetData,&j,stateFull);
        parse(httpGetData,&j,city);
        parse(httpGetData,&j,zip);
        parse(httpGetData,&j,latitude);
        parse(httpGetData,&j,longitude);
        parse(httpGetData,&j,timeZone);
    } 
    else { //HTTP GET REQUEST ERRORED
        uLCD.cls();
        uLCD.printf("HTTP Error %d", r);
        return -1;
    }
    sdlog(0, 1, 2, latitude, longitude, city);// Save info to SD card
    //Print Location Information
    uLCD.cls();
    uLCD.locate(0,0);
    uLCD.printf("%s, %s\n",city,countryFull); //Print City and Country
    uLCD.printf("LAT:%s\nLONG:%s\n",latitude,longitude); //LATITUDE AND LONGITUDE 
    uLCD.printf("Timezone:\n%s",timeZone); //PRINT TIMEZONE
    eth.disconnect(); //DISCONNECT FROM THE NETWORK 
    
}

//SET FOR CSV FORMAT: NEEDS TO BE EDITED IF DIFFERENT FORMAT
void parse(char buffer[], int *j, char *string) {
//extracts next location string data item from buffer
    int i=0;
    for (i=0; i<=strlen(buffer); i++) {  //TOTAL SIZE OF RETURNED DATA
        if ((buffer[*j+i] == ',')||(buffer[*j+i] == '\0' )) { //IF comma or end of string
            //comma is the string field delimiter
            string[i]=0; //SETS END OF SRTRING TO 0
            *j=*j+i+1; //UPDATES to 1 after comma seperated value
            break;
        } else string[i]=buffer[*j+i]; //Keep adding to the string
    }
}

void sdlog(int type, float time, float input0, char input1[], char input2[], char input3[]){
     FILE *file;
    switch (type){
    case 0:
            file = fopen("/sd/location.txt", "w");
        fprintf(file, "TIME: %f, Latitude: %s Longitude: %s, City: %s\n\r", time, input1, input2, input3);
        fclose(file);
        break;
    case 1:
            file = fopen("/sd/temperature.txt", "w");
        fprintf(file, "TIME: %f, %f degC\n\r", time, input0);
        fclose(file);
        break;
    case 2:
            file = fopen("/sd/acceleration.txt", "w");
        fprintf(file, "TIME: %f, WARNING: LOAD EXCEDDED MAXIMUM ALLOWED ANGLE\n\r", time);
        fclose(file);
        break;
    default:
    break;
    }
}