Using TCP Client on WIZwiki-W7500, display weather conditions on led and temperature with servo-motor
Dependencies: WIZnetInterface mbed-src
Prerequisite
This example is for PIR test using digital I/O.
To implement this function, you need a Platform board, network Interface board.
- WIZwiki-W7500 from WIZnet (Platform board and Ethernet I/F board)
Hardware Configuration
WIZwiki-W7500 Pin map
Just connect Ethernet Cable & USB Cable
Software
Init Ethernet
void initEthernet(uint8_t* mac_addr) { int phy_link; eth.init(mac_addr); //Use DHCP eth.connect(); /* phy link */ do{ phy_link = eth.ethernet_link(); printf("."); wait(2); }while(!phy_link); printf("\r\n"); printf("IP Address is %s\r\n", eth.getIPAddress()); }
Request to server using HTTP
void requestHTTP(void) { char req_buf[256]; /* TCP socket connect */ sock.connect(WEB_SERVER, 80); /* Request to WEB Server using HTTP */ sprintf(req_buf,"GET /data/2.5/weather?q=%s,%s&appid=%s HTTP/1.1\nHost: %s\nConnection: close\n\n", CITY,COUNTRY, API_KEY, WEB_SERVER); sock.send_all(req_buf, strlen(req_buf)); }
Get data from server & Parsing it
void parsingGetData(void) { char buffer[1024]; /* get info */ int ret; while (true) { ret = sock.receive_all(buffer, sizeof(buffer)-1); if (ret <= 0) break; buffer[ret] = '\0'; pc.printf("Received %d chars from server: %s\n", ret, buffer); } /* parsing weather, city, tempurature */ char *weather; char *city; char *temp; uint8_t i; pc.printf("\r\n\r\n======== WeatherForecast ========\r\n"); weather = strstr(buffer, "main"); pc.printf("\t State : "); for(i = 7; i < 20; i++) { if(*(weather+i) == '\"') break; pc.printf("%c", *(weather+i)); } city = strstr(buffer, "name"); pc.printf("\r\n\t City : "); for(i = 7; i < 20; i++) { if(*(city+i) == '\"') break; pc.printf("%c", *(city+i)); } temp = strstr(buffer, "temp"); pc.printf("\r\n\t temp(kelvin) : "); for(i = 6; i < 12; i++) { if((*(temp+i) == '\"')||(*(temp+i) == ',')) break; pc.printf("%c", *(temp+i)); } pc.printf("\r\n\r\n"); }
Caution
Must fix API_KEY & MAC Address
main.cpp@9:4757a976148d, 2012-07-27 (annotated)
- Committer:
- emilmont
- Date:
- Fri Jul 27 13:59:39 2012 +0000
- Revision:
- 9:4757a976148d
- Parent:
- 7:65188f4a8c25
- Child:
- 11:59dcefdda506
Cleanup example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:bb128f0e952f | 1 | #include "mbed.h" |
donatien | 0:bb128f0e952f | 2 | #include "EthernetInterface.h" |
donatien | 0:bb128f0e952f | 3 | |
emilmont | 7:65188f4a8c25 | 4 | int main() { |
donatien | 0:bb128f0e952f | 5 | EthernetInterface eth; |
donatien | 0:bb128f0e952f | 6 | eth.init(); //Use DHCP |
donatien | 0:bb128f0e952f | 7 | eth.connect(); |
emilmont | 2:e087e9b789e9 | 8 | printf("IP Address is %s\n", eth.getIPAddress()); |
donatien | 0:bb128f0e952f | 9 | |
emilmont | 7:65188f4a8c25 | 10 | TCPSocketConnection sock; |
donatien | 0:bb128f0e952f | 11 | sock.connect("mbed.org", 80); |
donatien | 0:bb128f0e952f | 12 | |
emilmont | 9:4757a976148d | 13 | char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"; |
emilmont | 9:4757a976148d | 14 | sock.send_all(http_cmd, sizeof(http_cmd)); |
emilmont | 7:65188f4a8c25 | 15 | |
emilmont | 9:4757a976148d | 16 | char buffer[300]; |
donatien | 0:bb128f0e952f | 17 | int ret; |
emilmont | 7:65188f4a8c25 | 18 | while (true) { |
emilmont | 9:4757a976148d | 19 | ret = sock.receive(buffer, sizeof(buffer)-1); |
emilmont | 7:65188f4a8c25 | 20 | if (ret <= 0) |
emilmont | 7:65188f4a8c25 | 21 | break; |
emilmont | 9:4757a976148d | 22 | buffer[ret] = '\0'; |
emilmont | 9:4757a976148d | 23 | printf("Received %d chars from server:\n%s\n", ret, buffer); |
emilmont | 7:65188f4a8c25 | 24 | } |
donatien | 0:bb128f0e952f | 25 | |
emilmont | 7:65188f4a8c25 | 26 | sock.close(); |
donatien | 0:bb128f0e952f | 27 | |
emilmont | 7:65188f4a8c25 | 28 | eth.disconnect(); |
donatien | 5:01f6c3e112af | 29 | |
emilmont | 9:4757a976148d | 30 | while(1) {} |
donatien | 0:bb128f0e952f | 31 | } |