![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Geolocation through Ethernet
Dependencies: mbed HTTPClient MMA8452Q mbed-rtos 4DGL-uLCD-SE NTPClient SDFileSystem1 EthernetInterface
main.cpp
- Committer:
- stephenb
- Date:
- 2020-12-16
- Revision:
- 0:070ff55a028f
- Child:
- 1:b0c480b628cd
File content as of revision 0:070ff55a028f:
#include "mbed.h" #include "NTPClient.h" #include "uLCD_4DGL.h" #include "EthernetInterface.h" #include "HTTPClient.h" //Geolocation output to LCD //Code based on: https://os.mbed.com/users/tlisowski3/notebook/geolocation-and-ntp-clock-on-ulcd-144-g2/ //Set up Networking and LCD EthernetInterface eth; uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; NTPClient ntpClient; HTTPClient httpClient; void parse(char buffer[], int *j, char *string); //FUNCTION TO PARSE HTTP GET DATA 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; } //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 } }