First Final Version, Will probably need revision with regards to LCD and maybe the clock

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

Committer:
stephenb
Date:
Wed Dec 16 18:52:01 2020 +0000
Revision:
0:070ff55a028f
Child:
1:b0c480b628cd
Test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stephenb 0:070ff55a028f 1 #include "mbed.h"
stephenb 0:070ff55a028f 2 #include "NTPClient.h"
stephenb 0:070ff55a028f 3 #include "uLCD_4DGL.h"
stephenb 0:070ff55a028f 4 #include "EthernetInterface.h"
stephenb 0:070ff55a028f 5 #include "HTTPClient.h"
stephenb 0:070ff55a028f 6 //Geolocation output to LCD
stephenb 0:070ff55a028f 7 //Code based on: https://os.mbed.com/users/tlisowski3/notebook/geolocation-and-ntp-clock-on-ulcd-144-g2/
stephenb 0:070ff55a028f 8
stephenb 0:070ff55a028f 9 //Set up Networking and LCD
stephenb 0:070ff55a028f 10 EthernetInterface eth;
stephenb 0:070ff55a028f 11 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
stephenb 0:070ff55a028f 12 NTPClient ntpClient;
stephenb 0:070ff55a028f 13 HTTPClient httpClient;
stephenb 0:070ff55a028f 14
stephenb 0:070ff55a028f 15 void parse(char buffer[], int *j, char *string); //FUNCTION TO PARSE HTTP GET DATA
stephenb 0:070ff55a028f 16 char httpGetData[200]; //BUFFER TO HOLD DATA FROM HTTP GET REQUEST
stephenb 0:070ff55a028f 17
stephenb 0:070ff55a028f 18 int main() {
stephenb 0:070ff55a028f 19 //Display Settings
stephenb 0:070ff55a028f 20 uLCD.baudrate(115200); //Baudrate of display
stephenb 0:070ff55a028f 21 uLCD.cls(); //Clear uLCD screen
stephenb 0:070ff55a028f 22 uLCD.background_color(BLACK); //SET BACKGROUND COLOR TO WHITE
stephenb 0:070ff55a028f 23 uLCD.color(WHITE);
stephenb 0:070ff55a028f 24 uLCD.locate(0,0); //Start printing on col0, row0
stephenb 0:070ff55a028f 25 uLCD.printf("Getting Location \nInformation...");
stephenb 0:070ff55a028f 26
stephenb 0:070ff55a028f 27 eth.init(); //USE DHCP to get local IP address
stephenb 0:070ff55a028f 28 eth.connect(); //Connect to the network
stephenb 0:070ff55a028f 29
stephenb 0:070ff55a028f 30 char success[10]={0}; //success first
stephenb 0:070ff55a028f 31 char countryFull[20]={0}; //Full Country Name
stephenb 0:070ff55a028f 32 char countryAbrv[5]={0}; //Abbreviated Country Name or country Code
stephenb 0:070ff55a028f 33 char stateAbrv[5]={0}; //Abbreviated State or region code
stephenb 0:070ff55a028f 34 char stateFull[15]={0}; //Full State Name
stephenb 0:070ff55a028f 35 char city[15]={0}; //City Name
stephenb 0:070ff55a028f 36 char zip[6]={0}; //ZIP CODE
stephenb 0:070ff55a028f 37 char latitude[10]={0}; //latitude
stephenb 0:070ff55a028f 38 char longitude[10]={0}; //longitude
stephenb 0:070ff55a028f 39 char timeZone[30]={0}; //timeZone
stephenb 0:070ff55a028f 40 int j=0;
stephenb 0:070ff55a028f 41 HTTPResult r = httpClient.get("http://ip-api.com/csv",httpGetData,128); //GET GEOLOCATION DATA (CSV)
stephenb 0:070ff55a028f 42
stephenb 0:070ff55a028f 43 if (r==HTTP_OK) { //IF THE DATA WAS RECIEVED
stephenb 0:070ff55a028f 44 j=0;
stephenb 0:070ff55a028f 45 //parse and display each of the API's location information strings on the LCD
stephenb 0:070ff55a028f 46 parse(httpGetData, &j, success);
stephenb 0:070ff55a028f 47 parse(httpGetData,&j,countryFull);
stephenb 0:070ff55a028f 48 parse(httpGetData,&j,countryAbrv);
stephenb 0:070ff55a028f 49 parse(httpGetData,&j,stateAbrv);
stephenb 0:070ff55a028f 50 parse(httpGetData,&j,stateFull);
stephenb 0:070ff55a028f 51 parse(httpGetData,&j,city);
stephenb 0:070ff55a028f 52 parse(httpGetData,&j,zip);
stephenb 0:070ff55a028f 53 parse(httpGetData,&j,latitude);
stephenb 0:070ff55a028f 54 parse(httpGetData,&j,longitude);
stephenb 0:070ff55a028f 55 parse(httpGetData,&j,timeZone);
stephenb 0:070ff55a028f 56 }
stephenb 0:070ff55a028f 57 else { //HTTP GET REQUEST ERRORED
stephenb 0:070ff55a028f 58 uLCD.cls();
stephenb 0:070ff55a028f 59 uLCD.printf("HTTP Error %d", r);
stephenb 0:070ff55a028f 60 return -1;
stephenb 0:070ff55a028f 61 }
stephenb 0:070ff55a028f 62
stephenb 0:070ff55a028f 63 //Print Location Information
stephenb 0:070ff55a028f 64 uLCD.cls();
stephenb 0:070ff55a028f 65 uLCD.locate(0,0);
stephenb 0:070ff55a028f 66 uLCD.printf("%s, %s\n",city,countryFull); //Print City and Country
stephenb 0:070ff55a028f 67 uLCD.printf("LAT:%s\nLONG:%s\n",latitude,longitude); //LATITUDE AND LONGITUDE
stephenb 0:070ff55a028f 68 uLCD.printf("Timezone:\n%s",timeZone); //PRINT TIMEZONE
stephenb 0:070ff55a028f 69 eth.disconnect(); //DISCONNECT FROM THE NETWORK
stephenb 0:070ff55a028f 70 }
stephenb 0:070ff55a028f 71
stephenb 0:070ff55a028f 72 //SET FOR CSV FORMAT: NEEDS TO BE EDITED IF DIFFERENT FORMAT
stephenb 0:070ff55a028f 73 void parse(char buffer[], int *j, char *string) {
stephenb 0:070ff55a028f 74 //extracts next location string data item from buffer
stephenb 0:070ff55a028f 75 int i=0;
stephenb 0:070ff55a028f 76 for (i=0; i<=strlen(buffer); i++) { //TOTAL SIZE OF RETURNED DATA
stephenb 0:070ff55a028f 77 if ((buffer[*j+i] == ',')||(buffer[*j+i] == '\0' )) { //IF comma or end of string
stephenb 0:070ff55a028f 78 //comma is the string field delimiter
stephenb 0:070ff55a028f 79 string[i]=0; //SETS END OF SRTRING TO 0
stephenb 0:070ff55a028f 80 *j=*j+i+1; //UPDATES to 1 after comma seperated value
stephenb 0:070ff55a028f 81 break;
stephenb 0:070ff55a028f 82 } else string[i]=buffer[*j+i]; //Keep adding to the string
stephenb 0:070ff55a028f 83 }
stephenb 0:070ff55a028f 84 }