modified by ohneta
Dependencies: DnsQuery ESP8266Interface NetworkSocketAPI mbed
Fork of HelloESP8266Interface by
main.cpp
00001 /* NetworkSocketAPI Example Program 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "mbed.h" 00018 #include "ESP8266Interface.h" 00019 00020 #define RESPOSE_BUFFER_SIZE 2048 00021 00022 00023 Serial pc(USBTX, USBRX); 00024 DigitalOut led1(LED1); 00025 DigitalOut led2(LED2); 00026 00027 uint32_t _tickCount = 0L; 00028 void flash() 00029 { 00030 led1 = !led1; 00031 _tickCount++; 00032 } 00033 00034 ESP8266Interface wifi(p9, p10); 00035 00036 /** 00037 * 00038 */ 00039 bool parseURL(const char *url, char *scheme, char *host, uint16_t *port, char *path) 00040 { 00041 *port = 0; 00042 00043 char *schemePtr = (char *)url; 00044 char *hostPtr = (char *)strstr(url, "://"); 00045 if (hostPtr == NULL) { 00046 pc.printf("Could not find host"); 00047 return false; 00048 } 00049 00050 memcpy(scheme, schemePtr, hostPtr - schemePtr); 00051 scheme[hostPtr - schemePtr] = '\0'; 00052 00053 if (strcmp(scheme, "http") == 0) { 00054 *port = 80; 00055 } else if(strcmp(scheme, "https") == 0) { 00056 *port = 433; 00057 } 00058 00059 hostPtr += 3; 00060 size_t hostLen = 0; 00061 00062 char *portPtr = strchr(hostPtr, ':'); 00063 if (portPtr != NULL) { 00064 hostLen = portPtr - hostPtr; 00065 portPtr++; 00066 if (sscanf(portPtr, "%hu", port) != 1) { 00067 pc.printf("Could not find port"); 00068 return false; 00069 } 00070 } 00071 00072 char* pathPtr = strchr(hostPtr, '/'); 00073 if( hostLen == 0 ) { 00074 hostLen = pathPtr - hostPtr; 00075 } 00076 00077 memcpy(host, hostPtr, hostLen); 00078 host[hostLen] = '\0'; 00079 00080 size_t pathLen; 00081 char *fragmentPtr = strchr(hostPtr, '#'); 00082 if(fragmentPtr != NULL) { 00083 pathLen = fragmentPtr - pathPtr; 00084 } else { 00085 pathLen = strlen(pathPtr); 00086 } 00087 00088 memcpy(path, pathPtr, pathLen); 00089 path[pathLen] = '\0'; 00090 00091 return true; 00092 } 00093 00094 #if 0 00095 bool getHttpDebug(SocketInterface *socket, const char *urlStr, char *rsponse) 00096 { 00097 char scheme[8]; 00098 char host[32]; 00099 uint16_t port; 00100 char path[256]; 00101 00102 bool res = parseURL(urlStr, scheme, host, &port, path); 00103 pc.printf("%lu:parseURL=%d: %s --> %s, %s, %d, %s \n", _tickCount, res, urlStr, scheme, host, port, path); 00104 00105 00106 char host_ip[16]; 00107 { 00108 wifi.getHostByName(host, host_ip); 00109 pc.printf("%lu:host IP address : %s\n", _tickCount, host_ip); 00110 } 00111 00112 uint32_t ret; 00113 00114 //_tickCount = 0; 00115 socket->setAddressPort(host_ip, port); 00116 ret = socket->open(); 00117 led2 = 1; 00118 pc.printf("%d:opend : %d: %s:%d\n", _tickCount, ret, host_ip, port); 00119 00120 char str[512] = {0}; 00121 sprintf(str, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host); 00122 pc.printf("%lu: REQUEST:\n%s\n------\n", _tickCount, str); 00123 00124 // Send Data 00125 pc.printf("%lu:Sending GET data...\n", _tickCount); 00126 socket->send(str, strlen(str)); 00127 00128 pc.printf("%lu:Waiting on reply ... \n", _tickCount); 00129 int revLen = socket->recv(rsponse, 512, 10000); 00130 if (revLen == 0) { 00131 socket->close(); 00132 } 00133 rsponse[revLen] = 0; 00134 pc.printf("%lu:Received String : (%d) : \n%s\n", _tickCount, revLen, rsponse); 00135 00136 00137 led2 = 0; 00138 pc.printf("%lu:socket closed\n", _tickCount); 00139 00140 return true; 00141 } 00142 #endif 00143 00144 bool getHttp(SocketInterface *socket, const char *urlStr, char *rsponse) 00145 { 00146 char scheme[8]; 00147 char host[32]; 00148 uint16_t port; 00149 char path[256]; 00150 bool res = parseURL(urlStr, scheme, host, &port, path); 00151 00152 char host_ip[16]; 00153 wifi.getHostByName(host, host_ip); 00154 00155 socket->setAddressPort(host_ip, port); 00156 uint32_t ret = socket->open(); 00157 00158 char str[512] = {0}; 00159 sprintf(str, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host); 00160 00161 socket->send(str, strlen(str)); 00162 int revLen = socket->recv(rsponse, 512, 10000); 00163 rsponse[revLen] = 0; 00164 00165 socket->close(); 00166 00167 00168 // retBufを httpプロトコルで分解しようか? 00169 { 00170 // 最低限でもresult codeくらい調べたい 00171 } 00172 00173 return true; 00174 } 00175 00176 00177 int main() 00178 { 00179 pc.baud(115200); 00180 00181 00182 Ticker t; 00183 t.attach(flash, 0.1f); 00184 pc.printf("NetworkSocketAPI TEST by ohneta.\r\n"); 00185 00186 int32_t ret = wifi.init(); 00187 pc.printf("wifi.init() : %d\n", ret); 00188 00189 ret = wifi.connect("elecom2g-15BEAC", "2903851513877"); // SSID-name, SSID-passPhase 00190 pc.printf("wifi.connect() : %d\n", ret); 00191 00192 00193 char* myip; 00194 myip = wifi.getIPAddress(); 00195 pc.printf("IP Address is: %s\n", (myip) ? myip : "No IP"); 00196 00197 00198 // I want to IP address of default gateway(wifi wifi router). because that is preferred DNS server. 00199 //wifi.setPreferredDns("192.168.2.1"); // Preferred DNS IP-address 00200 { 00201 // "192.168.2.132" --> "192.168.2.1" (maybe) 00202 uint32_t ip1, ip2, ip3, ip4; 00203 sscanf(myip, "%3lu.%3lu.%3lu.%3lu", &ip1, &ip2, &ip3, &ip4); 00204 00205 char defaultgateway[20] = {0}; 00206 sprintf(defaultgateway, "%lu.%lu.%lu.1", ip1, ip2, ip3); 00207 pc.printf("%lu: Default gateway: %s\n", _tickCount, defaultgateway); 00208 wifi.setPreferredDns(defaultgateway); // defaultgateway is Preferred DNS IP-address 00209 //wifi.setPreferredDns("192.168.2.1"); // defaultgateway is Preferred DNS IP-address 00210 } 00211 00212 00213 00214 SocketInterface *socket = wifi.allocateSocket(SOCK_TCP); 00215 00216 char responseBuffer[RESPOSE_BUFFER_SIZE] = {0}; 00217 getHttp(socket, "http://youthost.com", responseBuffer); 00218 00219 wifi.disconnect(); 00220 pc.printf(":::Finished\n"); 00221 00222 while(1) { 00223 int dummy = 1; 00224 } 00225 }
Generated on Thu Jul 14 2022 07:03:28 by 1.7.2