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
Revision 19:46fa6a0a9b5c, committed 2017-04-07
- Comitter:
- kei44
- Date:
- Fri Apr 07 08:51:49 2017 +0000
- Parent:
- 18:a02a73acd3c8
- Commit message:
- Modify old code.
Changed in this revision
diff -r a02a73acd3c8 -r 46fa6a0a9b5c Servo.lib --- a/Servo.lib Mon Jun 29 07:41:31 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/simon/code/Servo/#36b69a7ced07
diff -r a02a73acd3c8 -r 46fa6a0a9b5c WIZnetInterface.lib --- a/WIZnetInterface.lib Mon Jun 29 07:41:31 2015 +0000 +++ b/WIZnetInterface.lib Fri Apr 07 08:51:49 2017 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#7d7553e0578e +http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#c91884bd2713
diff -r a02a73acd3c8 -r 46fa6a0a9b5c main.cpp --- a/main.cpp Mon Jun 29 07:41:31 2015 +0000 +++ b/main.cpp Fri Apr 07 08:51:49 2017 +0000 @@ -1,132 +1,138 @@ +#include <stdio.h> +#include <string.h> #include "mbed.h" #include "EthernetInterface.h" -#include "Servo.h" -DigitalOut bled(D6); -DigitalOut gled(D5); -DigitalOut rled(D4); +#define CITY "Seoul" +#define COUNTRY "kr" +#define API_KEY "YOUR_API" //Fix This +#define WEB_SERVER "api.openweathermap.org" -Servo myservo(D15); +Serial pc(USBTX, USBRX); +EthernetInterface eth; +TCPSocketConnection sock; -int main() { - - int phy_link; - printf("Wait a second...\r\n"); - uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; +uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0xFF, 0xFF, 0xFF}; //Fix This + +// Function Prototypes +void parsingGetData(void); +void initEthernet(uint8_t* mac_addr); +void requestHTTP(void); + +int main() +{ + pc.baud(115200); - EthernetInterface eth; - eth.init(mac_addr); //Use DHCP - - while(1){ - - eth.connect(); + initEthernet(mac_addr); - /* 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()); + while(1) + { + requestHTTP(); + + parsingGetData(); - /* TCP socket connect */ - TCPSocketConnection sock; - sock.connect("api.openweathermap.org", 80); - - /* weather */ - char http_cmd[] = "GET /data/2.5/weather?q=Seoul,kr HTTP/1.0\n\n"; - //char http_cmd[] = "GET /data/2.5/weather?q=London,uk HTTP/1.0\n\n"; - //char http_cmd[] = "GET /data/2.5/weather?q=Berlin,de HTTP/1.0\n\n"; - sock.send_all(http_cmd, sizeof(http_cmd)-1); - + sock.close(); + eth.disconnect(); + + wait(60.0); + }; + +} + +/** + * @brief parsing Get Data from server + * @detail Function get data from server at once & parsing it for take needed data + * @param void + * @return void + * @throws return Occurs when the param value exceeds the specified range. + */ +void parsingGetData(void) +{ + char buffer[1024]; + /* get info */ - char buffer[420]; int ret; while (true) { - ret = sock.receive(buffer, sizeof(buffer)-1); - if (ret <= 0) - break; + ret = sock.receive_all(buffer, sizeof(buffer)-1); + if (ret <= 0) break; buffer[ret] = '\0'; - printf("Received %d chars from server: %s\n", ret, buffer); + pc.printf("Received %d chars from server: %s\n", ret, buffer); } - - /* get weather, city, tempurature */ + + /* parsing weather, city, tempurature */ char *weather; char *city; - char *tempure; - - char weather_stu[3]; - char city_name[6]; - char tempure_data[6]; + char *temp; + uint8_t i; - weather = strstr(buffer, "main"); - printf("%.4s\n", weather + 7); - for(int i=0; i<3;i++){ - weather_stu[i] = weather[i+7]; - printf("%c",weather_stu[i]); - } - - city = strstr(buffer, "name"); - printf("%.6s\n", city + 7); - for(int k=0; k<6;k++){ - city_name[k] = city[k+7]; - printf("%c",city_name[k]); - } - - tempure = strstr(buffer, "temp"); - printf("%.3s\n", tempure + 6); - for(int j=0; j<6;j++){ - tempure_data[j] = tempure[j+6]; - printf("%c",tempure_data[j]); - } + 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)); + } - /*tempurature display */ - float data[2]={0}; - data[0] = tempure_data[1]-'7'; - data[1] = tempure_data[2]-'3'; - - myservo = (data[0]*10+data[1])/40; + 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"); +} - printf("%f",(data[0]*10+data[1])/40); - - /* weather display */ - if(strcmp(weather_stu,"Clo")==0) { - rled = 1; - gled = 1; - bled = 1; - }else if(strcmp(weather_stu,"Rai")==0) { - rled = 0; - gled = 0; - bled = 1; - }else if(strcmp(weather_stu,"Cle")==0){ - rled = 1; - gled = 1; - bled = 0; - }else if(strcmp(weather_stu,"Mis")==0){ - rled = 1; - gled = 0; - bled = 1; - }else if(strcmp(weather_stu,"Haz")==0) { - rled = 1; - gled = 1; - bled = 1; - }else if(strcmp(weather_stu,"Fog")==0){ - rled = 1; - gled = 0; - bled = 1; - }else { - rled = 0; - gled = 0; - bled = 0; - } +/** + * @brief Request to server using HTTP. + * @detail Sends param values to the server via HTTP. + * @param void + * @return void + * @throws + */ +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)); +} - sock.close(); - - eth.disconnect(); +/** + * @brief Initialize W5500 (Ethernet chip) + * @detail Function to initialize W5500 chip to use ethernet + * @param void + * @return void + * @throws + */ +void initEthernet(uint8_t* mac_addr) { + int phy_link; + + eth.init(mac_addr); //Use DHCP - wait(60.0); - }; - + 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()); } \ No newline at end of file
diff -r a02a73acd3c8 -r 46fa6a0a9b5c mbed-src.lib --- a/mbed-src.lib Mon Jun 29 07:41:31 2015 +0000 +++ b/mbed-src.lib Fri Apr 07 08:51:49 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed-src/#30f9462b5296 +http://mbed.org/users/mbed_official/code/mbed-src/#a11c0372f0ba