Geolocation demo using Nokia LCD. See http://mbed.org/users/4180_1/notebook/geolocation-nokia-lcd-display/

Dependencies:   NetServices mbed HTTPClient_ToBeRemoved

Committer:
4180_1
Date:
Fri May 25 00:24:31 2012 +0000
Revision:
0:019b7bf35f8c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:019b7bf35f8c 1 #include "mbed.h"
4180_1 0:019b7bf35f8c 2 #include "EthernetNetIf.h"
4180_1 0:019b7bf35f8c 3 #include "HTTPClient.h"
4180_1 0:019b7bf35f8c 4 #include "NokiaLCD.h"
4180_1 0:019b7bf35f8c 5 //Geolocation using IP address - get web page with location data
4180_1 0:019b7bf35f8c 6 // displays location fields on LCD from web page ";....location text...;"
4180_1 0:019b7bf35f8c 7 // see http://mbed.org/users/4180_1/notebook/geolocation-nokia-lcd-display/
4180_1 0:019b7bf35f8c 8
4180_1 0:019b7bf35f8c 9 NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
4180_1 0:019b7bf35f8c 10 EthernetNetIf eth;
4180_1 0:019b7bf35f8c 11 HTTPClient http;
4180_1 0:019b7bf35f8c 12
4180_1 0:019b7bf35f8c 13 void parse(char buffer[], int *j, char *string) {
4180_1 0:019b7bf35f8c 14 //extracts next location string data item from buffer
4180_1 0:019b7bf35f8c 15 int i=0;
4180_1 0:019b7bf35f8c 16 for (i=0; i<=strlen(buffer); i++) {
4180_1 0:019b7bf35f8c 17 if ((buffer[*j+i] == ';')||(buffer[*j+i] == '\0' )) {
4180_1 0:019b7bf35f8c 18 //semicolon is the string field delimiter
4180_1 0:019b7bf35f8c 19 string[i]=0;
4180_1 0:019b7bf35f8c 20 *j=*j+i+1;
4180_1 0:019b7bf35f8c 21 break;
4180_1 0:019b7bf35f8c 22 } else string[i]=buffer[*j+i];
4180_1 0:019b7bf35f8c 23 }
4180_1 0:019b7bf35f8c 24 }
4180_1 0:019b7bf35f8c 25
4180_1 0:019b7bf35f8c 26 int main() {
4180_1 0:019b7bf35f8c 27 char result [4]={0};
4180_1 0:019b7bf35f8c 28 char ip [17]={0};
4180_1 0:019b7bf35f8c 29 char country_abbr[10]={0};
4180_1 0:019b7bf35f8c 30 char country[60]={0};
4180_1 0:019b7bf35f8c 31 char region[40]={0};
4180_1 0:019b7bf35f8c 32 char city[60]={0};
4180_1 0:019b7bf35f8c 33 char zipcode[10]={0};
4180_1 0:019b7bf35f8c 34 char latitude[10]={0};
4180_1 0:019b7bf35f8c 35 char longitude[10]={0};
4180_1 0:019b7bf35f8c 36 char timezone[7]={0};
4180_1 0:019b7bf35f8c 37 char buffer[256]={0};
4180_1 0:019b7bf35f8c 38 float flatitude=0.0;
4180_1 0:019b7bf35f8c 39 float flongitude=0.0;
4180_1 0:019b7bf35f8c 40 float ftimezone=0.0;
4180_1 0:019b7bf35f8c 41 int j=0;
4180_1 0:019b7bf35f8c 42
4180_1 0:019b7bf35f8c 43 //Setup network - get IP address using DHCP
4180_1 0:019b7bf35f8c 44 lcd.cls();
4180_1 0:019b7bf35f8c 45 lcd.locate(0,2);
4180_1 0:019b7bf35f8c 46 lcd.printf("Net setup");
4180_1 0:019b7bf35f8c 47 lcd.locate(0,3);
4180_1 0:019b7bf35f8c 48 EthernetErr ethErr = eth.setup();
4180_1 0:019b7bf35f8c 49 if (ethErr) {
4180_1 0:019b7bf35f8c 50 lcd.printf("Error %d", ethErr);
4180_1 0:019b7bf35f8c 51 return -1;
4180_1 0:019b7bf35f8c 52 }
4180_1 0:019b7bf35f8c 53 lcd.printf(" Net OK");
4180_1 0:019b7bf35f8c 54 wait(.5);
4180_1 0:019b7bf35f8c 55 lcd.cls();
4180_1 0:019b7bf35f8c 56 lcd.locate(0,2);
4180_1 0:019b7bf35f8c 57 lcd.printf("IP Address");
4180_1 0:019b7bf35f8c 58 lcd.locate(0,3);
4180_1 0:019b7bf35f8c 59 lcd.printf("Geolocation API");
4180_1 0:019b7bf35f8c 60 HTTPText txt;
4180_1 0:019b7bf35f8c 61 //iPinfoDB API - get web page with location data
4180_1 0:019b7bf35f8c 62 //Insert your free key from www.ipinfo.com for the API in the URL below
4180_1 0:019b7bf35f8c 63 HTTPResult r = http.get("http://api.ipinfodb.com/v3/ip-city/?key=<PUT_YOUR_API_KEY_HERE>", &txt);
4180_1 0:019b7bf35f8c 64 if (r==HTTP_OK) {
4180_1 0:019b7bf35f8c 65 //got web page text data OK
4180_1 0:019b7bf35f8c 66 strcpy(buffer,txt.gets());
4180_1 0:019b7bf35f8c 67 wait(1);
4180_1 0:019b7bf35f8c 68 while (1) {
4180_1 0:019b7bf35f8c 69 j=0;
4180_1 0:019b7bf35f8c 70 //parse and display each of the API's location information strings on the LCD
4180_1 0:019b7bf35f8c 71 parse(buffer, &j, result);
4180_1 0:019b7bf35f8c 72 lcd.cls();
4180_1 0:019b7bf35f8c 73 lcd.locate(0,2);
4180_1 0:019b7bf35f8c 74 lcd.printf("result: %s", result);
4180_1 0:019b7bf35f8c 75 if (result[0]!='O') { //needs valid key
4180_1 0:019b7bf35f8c 76 wait(1);
4180_1 0:019b7bf35f8c 77 lcd.cls();
4180_1 0:019b7bf35f8c 78 lcd.locate(0,2);
4180_1 0:019b7bf35f8c 79 lcd.printf("Get Free API key");
4180_1 0:019b7bf35f8c 80 lcd.printf("www.iPinfoDB.com");
4180_1 0:019b7bf35f8c 81 return(-1);
4180_1 0:019b7bf35f8c 82 }
4180_1 0:019b7bf35f8c 83 wait(1);
4180_1 0:019b7bf35f8c 84 j++;
4180_1 0:019b7bf35f8c 85 parse(buffer, &j, ip);
4180_1 0:019b7bf35f8c 86 lcd.locate(0,2);
4180_1 0:019b7bf35f8c 87 lcd.printf("IP address:");
4180_1 0:019b7bf35f8c 88 lcd.locate(0,3);
4180_1 0:019b7bf35f8c 89 lcd.printf(" %s", ip);
4180_1 0:019b7bf35f8c 90 parse(buffer, &j, country_abbr);
4180_1 0:019b7bf35f8c 91 lcd.locate(0,4);
4180_1 0:019b7bf35f8c 92 lcd.printf("Country code:");
4180_1 0:019b7bf35f8c 93 lcd.locate(0,5);
4180_1 0:019b7bf35f8c 94 lcd.printf(" %s", country_abbr);
4180_1 0:019b7bf35f8c 95 parse(buffer, &j, country);
4180_1 0:019b7bf35f8c 96 lcd.locate(0,6);
4180_1 0:019b7bf35f8c 97 lcd.printf("Country:");
4180_1 0:019b7bf35f8c 98 lcd.locate(0,7);
4180_1 0:019b7bf35f8c 99 lcd.printf(" %s", country);
4180_1 0:019b7bf35f8c 100 parse(buffer, &j, region);
4180_1 0:019b7bf35f8c 101 lcd.locate(0,8);
4180_1 0:019b7bf35f8c 102 lcd.printf("Region or State:");
4180_1 0:019b7bf35f8c 103 lcd.locate(0,9);
4180_1 0:019b7bf35f8c 104 lcd.printf(" %s", region);
4180_1 0:019b7bf35f8c 105 parse(buffer, &j, city);
4180_1 0:019b7bf35f8c 106 lcd.locate(0,10);
4180_1 0:019b7bf35f8c 107 lcd.printf("City:");
4180_1 0:019b7bf35f8c 108 lcd.locate(0,11);
4180_1 0:019b7bf35f8c 109 lcd.printf(" %s", city);
4180_1 0:019b7bf35f8c 110 parse(buffer, &j, zipcode);
4180_1 0:019b7bf35f8c 111 lcd.locate(0,12);
4180_1 0:019b7bf35f8c 112 lcd.printf("Zipcode:");
4180_1 0:019b7bf35f8c 113 lcd.locate(0,13);
4180_1 0:019b7bf35f8c 114 lcd.printf(" %s", zipcode);
4180_1 0:019b7bf35f8c 115 wait(5);
4180_1 0:019b7bf35f8c 116 parse(buffer, &j, latitude);
4180_1 0:019b7bf35f8c 117 sscanf(latitude,"%f",&flatitude);
4180_1 0:019b7bf35f8c 118 lcd.cls();
4180_1 0:019b7bf35f8c 119 lcd.locate(0,2);
4180_1 0:019b7bf35f8c 120 lcd.printf("Latitude:");
4180_1 0:019b7bf35f8c 121 lcd.locate(0,3);
4180_1 0:019b7bf35f8c 122 lcd.printf(" %f", flatitude);
4180_1 0:019b7bf35f8c 123 parse(buffer, &j, longitude);
4180_1 0:019b7bf35f8c 124 sscanf(longitude,"%f",&flongitude);
4180_1 0:019b7bf35f8c 125 lcd.locate(0,4);
4180_1 0:019b7bf35f8c 126 lcd.printf("Longitude:");
4180_1 0:019b7bf35f8c 127 lcd.locate(0,5);
4180_1 0:019b7bf35f8c 128 lcd.printf(" %f", flongitude);
4180_1 0:019b7bf35f8c 129 parse(buffer, &j, timezone);
4180_1 0:019b7bf35f8c 130 sscanf(timezone,"%f",&ftimezone);
4180_1 0:019b7bf35f8c 131 lcd.locate(0,6);
4180_1 0:019b7bf35f8c 132 lcd.printf("Timezone:");
4180_1 0:019b7bf35f8c 133 lcd.locate(0,7);
4180_1 0:019b7bf35f8c 134 lcd.printf(" %f", ftimezone);
4180_1 0:019b7bf35f8c 135 wait(4);
4180_1 0:019b7bf35f8c 136 }
4180_1 0:019b7bf35f8c 137 } else {
4180_1 0:019b7bf35f8c 138 lcd.cls();
4180_1 0:019b7bf35f8c 139 lcd.locate(0,2);
4180_1 0:019b7bf35f8c 140 lcd.printf("HTTP Error %d", r);
4180_1 0:019b7bf35f8c 141 return -1;
4180_1 0:019b7bf35f8c 142 }
4180_1 0:019b7bf35f8c 143 }