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
main.cpp@1:b0c480b628cd, 2020-12-17 (annotated)
- Committer:
- stephenb
- Date:
- Thu Dec 17 18:34:29 2020 +0000
- Revision:
- 1:b0c480b628cd
- Parent:
- 0:070ff55a028f
- Child:
- 3:65349fe42061
Final, Part
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:b0c480b628cd | 6 | #include "SDFileSystem.h" |
stephenb | 1:b0c480b628cd | 7 | //Geolocation output to LCD and SD card reader |
stephenb | 0:070ff55a028f | 8 | //Code based on: https://os.mbed.com/users/tlisowski3/notebook/geolocation-and-ntp-clock-on-ulcd-144-g2/ |
stephenb | 0:070ff55a028f | 9 | |
stephenb | 1:b0c480b628cd | 10 | //Set up Networking, LCD and SD Card reader |
stephenb | 1:b0c480b628cd | 11 | uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
stephenb | 1:b0c480b628cd | 12 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card, SPI pins |
stephenb | 0:070ff55a028f | 13 | EthernetInterface eth; |
stephenb | 0:070ff55a028f | 14 | NTPClient ntpClient; |
stephenb | 0:070ff55a028f | 15 | HTTPClient httpClient; |
stephenb | 0:070ff55a028f | 16 | |
stephenb | 0:070ff55a028f | 17 | void parse(char buffer[], int *j, char *string); //FUNCTION TO PARSE HTTP GET DATA |
stephenb | 1:b0c480b628cd | 18 | void sdlog(int type, float time, float input0, char input1[], char input2[], char input3[]); //Function to write to the SD card |
stephenb | 0:070ff55a028f | 19 | char httpGetData[200]; //BUFFER TO HOLD DATA FROM HTTP GET REQUEST |
stephenb | 0:070ff55a028f | 20 | |
stephenb | 0:070ff55a028f | 21 | int main() { |
stephenb | 0:070ff55a028f | 22 | //Display Settings |
stephenb | 0:070ff55a028f | 23 | uLCD.baudrate(115200); //Baudrate of display |
stephenb | 0:070ff55a028f | 24 | uLCD.cls(); //Clear uLCD screen |
stephenb | 0:070ff55a028f | 25 | uLCD.background_color(BLACK); //SET BACKGROUND COLOR TO WHITE |
stephenb | 0:070ff55a028f | 26 | uLCD.color(WHITE); |
stephenb | 0:070ff55a028f | 27 | uLCD.locate(0,0); //Start printing on col0, row0 |
stephenb | 0:070ff55a028f | 28 | uLCD.printf("Getting Location \nInformation..."); |
stephenb | 0:070ff55a028f | 29 | |
stephenb | 0:070ff55a028f | 30 | eth.init(); //USE DHCP to get local IP address |
stephenb | 0:070ff55a028f | 31 | eth.connect(); //Connect to the network |
stephenb | 0:070ff55a028f | 32 | |
stephenb | 0:070ff55a028f | 33 | char success[10]={0}; //success first |
stephenb | 0:070ff55a028f | 34 | char countryFull[20]={0}; //Full Country Name |
stephenb | 0:070ff55a028f | 35 | char countryAbrv[5]={0}; //Abbreviated Country Name or country Code |
stephenb | 0:070ff55a028f | 36 | char stateAbrv[5]={0}; //Abbreviated State or region code |
stephenb | 0:070ff55a028f | 37 | char stateFull[15]={0}; //Full State Name |
stephenb | 0:070ff55a028f | 38 | char city[15]={0}; //City Name |
stephenb | 0:070ff55a028f | 39 | char zip[6]={0}; //ZIP CODE |
stephenb | 0:070ff55a028f | 40 | char latitude[10]={0}; //latitude |
stephenb | 0:070ff55a028f | 41 | char longitude[10]={0}; //longitude |
stephenb | 0:070ff55a028f | 42 | char timeZone[30]={0}; //timeZone |
stephenb | 0:070ff55a028f | 43 | int j=0; |
stephenb | 0:070ff55a028f | 44 | HTTPResult r = httpClient.get("http://ip-api.com/csv",httpGetData,128); //GET GEOLOCATION DATA (CSV) |
stephenb | 0:070ff55a028f | 45 | |
stephenb | 0:070ff55a028f | 46 | if (r==HTTP_OK) { //IF THE DATA WAS RECIEVED |
stephenb | 0:070ff55a028f | 47 | j=0; |
stephenb | 0:070ff55a028f | 48 | //parse and display each of the API's location information strings on the LCD |
stephenb | 0:070ff55a028f | 49 | parse(httpGetData, &j, success); |
stephenb | 0:070ff55a028f | 50 | parse(httpGetData,&j,countryFull); |
stephenb | 0:070ff55a028f | 51 | parse(httpGetData,&j,countryAbrv); |
stephenb | 0:070ff55a028f | 52 | parse(httpGetData,&j,stateAbrv); |
stephenb | 0:070ff55a028f | 53 | parse(httpGetData,&j,stateFull); |
stephenb | 0:070ff55a028f | 54 | parse(httpGetData,&j,city); |
stephenb | 0:070ff55a028f | 55 | parse(httpGetData,&j,zip); |
stephenb | 0:070ff55a028f | 56 | parse(httpGetData,&j,latitude); |
stephenb | 0:070ff55a028f | 57 | parse(httpGetData,&j,longitude); |
stephenb | 0:070ff55a028f | 58 | parse(httpGetData,&j,timeZone); |
stephenb | 0:070ff55a028f | 59 | } |
stephenb | 0:070ff55a028f | 60 | else { //HTTP GET REQUEST ERRORED |
stephenb | 0:070ff55a028f | 61 | uLCD.cls(); |
stephenb | 0:070ff55a028f | 62 | uLCD.printf("HTTP Error %d", r); |
stephenb | 0:070ff55a028f | 63 | return -1; |
stephenb | 0:070ff55a028f | 64 | } |
stephenb | 1:b0c480b628cd | 65 | sdlog(0, 1, 2, latitude, longitude, city);// Save info to SD card |
stephenb | 0:070ff55a028f | 66 | //Print Location Information |
stephenb | 0:070ff55a028f | 67 | uLCD.cls(); |
stephenb | 0:070ff55a028f | 68 | uLCD.locate(0,0); |
stephenb | 0:070ff55a028f | 69 | uLCD.printf("%s, %s\n",city,countryFull); //Print City and Country |
stephenb | 0:070ff55a028f | 70 | uLCD.printf("LAT:%s\nLONG:%s\n",latitude,longitude); //LATITUDE AND LONGITUDE |
stephenb | 0:070ff55a028f | 71 | uLCD.printf("Timezone:\n%s",timeZone); //PRINT TIMEZONE |
stephenb | 0:070ff55a028f | 72 | eth.disconnect(); //DISCONNECT FROM THE NETWORK |
stephenb | 1:b0c480b628cd | 73 | |
stephenb | 0:070ff55a028f | 74 | } |
stephenb | 0:070ff55a028f | 75 | |
stephenb | 0:070ff55a028f | 76 | //SET FOR CSV FORMAT: NEEDS TO BE EDITED IF DIFFERENT FORMAT |
stephenb | 0:070ff55a028f | 77 | void parse(char buffer[], int *j, char *string) { |
stephenb | 0:070ff55a028f | 78 | //extracts next location string data item from buffer |
stephenb | 0:070ff55a028f | 79 | int i=0; |
stephenb | 0:070ff55a028f | 80 | for (i=0; i<=strlen(buffer); i++) { //TOTAL SIZE OF RETURNED DATA |
stephenb | 0:070ff55a028f | 81 | if ((buffer[*j+i] == ',')||(buffer[*j+i] == '\0' )) { //IF comma or end of string |
stephenb | 0:070ff55a028f | 82 | //comma is the string field delimiter |
stephenb | 0:070ff55a028f | 83 | string[i]=0; //SETS END OF SRTRING TO 0 |
stephenb | 0:070ff55a028f | 84 | *j=*j+i+1; //UPDATES to 1 after comma seperated value |
stephenb | 0:070ff55a028f | 85 | break; |
stephenb | 0:070ff55a028f | 86 | } else string[i]=buffer[*j+i]; //Keep adding to the string |
stephenb | 0:070ff55a028f | 87 | } |
stephenb | 0:070ff55a028f | 88 | } |
stephenb | 1:b0c480b628cd | 89 | |
stephenb | 1:b0c480b628cd | 90 | void sdlog(int type, float time, float input0, char input1[], char input2[], char input3[]){ |
stephenb | 1:b0c480b628cd | 91 | FILE *file; |
stephenb | 1:b0c480b628cd | 92 | switch (type){ |
stephenb | 1:b0c480b628cd | 93 | case 0: |
stephenb | 1:b0c480b628cd | 94 | file = fopen("/sd/location.txt", "w"); |
stephenb | 1:b0c480b628cd | 95 | fprintf(file, "TIME: %f, Latitude: %s Longitude: %s, City: %s\n\r", time, input1, input2, input3); |
stephenb | 1:b0c480b628cd | 96 | fclose(file); |
stephenb | 1:b0c480b628cd | 97 | break; |
stephenb | 1:b0c480b628cd | 98 | case 1: |
stephenb | 1:b0c480b628cd | 99 | file = fopen("/sd/temperature.txt", "w"); |
stephenb | 1:b0c480b628cd | 100 | fprintf(file, "TIME: %f, %f degC\n\r", time, input0); |
stephenb | 1:b0c480b628cd | 101 | fclose(file); |
stephenb | 1:b0c480b628cd | 102 | break; |
stephenb | 1:b0c480b628cd | 103 | case 2: |
stephenb | 1:b0c480b628cd | 104 | file = fopen("/sd/acceleration.txt", "w"); |
stephenb | 1:b0c480b628cd | 105 | fprintf(file, "TIME: %f, WARNING: LOAD EXCEDDED MAXIMUM ALLOWED ANGLE\n\r", time); |
stephenb | 1:b0c480b628cd | 106 | fclose(file); |
stephenb | 1:b0c480b628cd | 107 | break; |
stephenb | 1:b0c480b628cd | 108 | default: |
stephenb | 1:b0c480b628cd | 109 | break; |
stephenb | 1:b0c480b628cd | 110 | } |
stephenb | 1:b0c480b628cd | 111 | } |