Using TCP Client on WIZwiki-W7500, display weather conditions on led and temperature with servo-motor
Dependencies: WIZnetInterface mbed-src
main.cpp
00001 #include <stdio.h> 00002 #include <string.h> 00003 #include "mbed.h" 00004 #include "EthernetInterface.h" 00005 00006 #define CITY "Seoul" 00007 #define COUNTRY "kr" 00008 #define API_KEY "YOUR_API" //Fix This 00009 #define WEB_SERVER "api.openweathermap.org" 00010 00011 Serial pc(USBTX, USBRX); 00012 EthernetInterface eth; 00013 TCPSocketConnection sock; 00014 00015 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0xFF, 0xFF, 0xFF}; //Fix This 00016 00017 // Function Prototypes 00018 void parsingGetData(void); 00019 void initEthernet(uint8_t* mac_addr); 00020 void requestHTTP(void); 00021 00022 int main() 00023 { 00024 pc.baud(115200); 00025 00026 initEthernet(mac_addr); 00027 00028 while(1) 00029 { 00030 requestHTTP(); 00031 00032 parsingGetData(); 00033 00034 sock.close(); 00035 eth.disconnect(); 00036 00037 wait(60.0); 00038 }; 00039 00040 } 00041 00042 /** 00043 * @brief parsing Get Data from server 00044 * @detail Function get data from server at once & parsing it for take needed data 00045 * @param void 00046 * @return void 00047 * @throws return Occurs when the param value exceeds the specified range. 00048 */ 00049 void parsingGetData(void) 00050 { 00051 char buffer[1024]; 00052 00053 /* get info */ 00054 int ret; 00055 while (true) { 00056 ret = sock.receive_all(buffer, sizeof(buffer)-1); 00057 if (ret <= 0) break; 00058 buffer[ret] = '\0'; 00059 pc.printf("Received %d chars from server: %s\n", ret, buffer); 00060 } 00061 00062 /* parsing weather, city, tempurature */ 00063 char *weather; 00064 char *city; 00065 char *temp; 00066 uint8_t i; 00067 00068 pc.printf("\r\n\r\n======== WeatherForecast ========\r\n"); 00069 weather = strstr(buffer, "main"); 00070 pc.printf("\t State : "); 00071 for(i = 7; i < 20; i++) 00072 { 00073 if(*(weather+i) == '\"') break; 00074 pc.printf("%c", *(weather+i)); 00075 } 00076 00077 city = strstr(buffer, "name"); 00078 pc.printf("\r\n\t City : "); 00079 for(i = 7; i < 20; i++) 00080 { 00081 if(*(city+i) == '\"') break; 00082 pc.printf("%c", *(city+i)); 00083 } 00084 00085 temp = strstr(buffer, "temp"); 00086 pc.printf("\r\n\t temp(kelvin) : "); 00087 for(i = 6; i < 12; i++) 00088 { 00089 if((*(temp+i) == '\"')||(*(temp+i) == ',')) break; 00090 pc.printf("%c", *(temp+i)); 00091 } 00092 pc.printf("\r\n\r\n"); 00093 } 00094 00095 /** 00096 * @brief Request to server using HTTP. 00097 * @detail Sends param values to the server via HTTP. 00098 * @param void 00099 * @return void 00100 * @throws 00101 */ 00102 void requestHTTP(void) 00103 { 00104 char req_buf[256]; 00105 00106 /* TCP socket connect */ 00107 sock.connect(WEB_SERVER, 80); 00108 00109 /* Request to WEB Server using HTTP */ 00110 sprintf(req_buf,"GET /data/2.5/weather?q=%s,%s&appid=%s HTTP/1.1\nHost: %s\nConnection: close\n\n", 00111 CITY,COUNTRY, API_KEY, WEB_SERVER); 00112 sock.send_all(req_buf, strlen(req_buf)); 00113 } 00114 00115 /** 00116 * @brief Initialize W5500 (Ethernet chip) 00117 * @detail Function to initialize W5500 chip to use ethernet 00118 * @param void 00119 * @return void 00120 * @throws 00121 */ 00122 void initEthernet(uint8_t* mac_addr) { 00123 int phy_link; 00124 00125 eth.init(mac_addr); //Use DHCP 00126 00127 eth.connect(); 00128 00129 /* phy link */ 00130 do{ 00131 phy_link = eth.ethernet_link(); 00132 printf("."); 00133 wait(2); 00134 }while(!phy_link); 00135 printf("\r\n"); 00136 00137 printf("IP Address is %s\r\n", eth.getIPAddress()); 00138 }
Generated on Fri Jul 29 2022 20:54:22 by 1.7.2