modified by ohneta
Dependencies: DnsQuery ESP8266Interface NetworkSocketAPI mbed
Fork of HelloESP8266Interface by
Diff: main.cpp
- Revision:
- 25:8536695a236d
- Parent:
- 24:2b91c0e2067b
- Child:
- 27:4dc43240e498
diff -r 2b91c0e2067b -r 8536695a236d main.cpp --- a/main.cpp Tue Nov 10 14:58:47 2015 +0000 +++ b/main.cpp Wed Nov 11 05:57:33 2015 +0000 @@ -17,7 +17,7 @@ #include "mbed.h" #include "ESP8266Interface.h" -#define RESPOSE_BUFFER_SIZE 1024 +#define RESPOSE_BUFFER_SIZE 2048 Serial pc(USBTX, USBRX); @@ -93,7 +93,7 @@ -bool getHttpDebug(SocketInterface *socket, const char *urlStr) +bool getHttpDebug(SocketInterface *socket, const char *urlStr, char *rsponse) { char scheme[8]; char host[32]; @@ -102,16 +102,17 @@ bool res = parseURL(urlStr, scheme, host, &port, path); pc.printf("%lu:parseURL=%d: %s --> %s, %s, %d, %s \n", _tickCount, res, urlStr, scheme, host, port, path); -#if 1 + + char host_ip[16]; - wifi.getHostByName(host, host_ip); -#else - char *host_ip = "54.249.236.250"; -#endif + { + wifi.getHostByName(host, host_ip); pc.printf("%lu:host IP address : %s\n", _tickCount, host_ip); + } + uint32_t ret; -_tickCount = 0; +//_tickCount = 0; socket->setAddressPort(host_ip, port); ret = socket->open(); led2 = 1; @@ -125,13 +126,15 @@ pc.printf("%lu:Sending GET data...\n", _tickCount); socket->send(str, strlen(str)); - static char retBuf[512] = {0}; pc.printf("%lu:Waiting on reply ... \n", _tickCount); - int revLen = socket->recv(retBuf, 512, 10000); - retBuf[revLen] = 0; -pc.printf("%lu:Received String : (%d) : \n%s\n", _tickCount, revLen, retBuf); + int revLen = socket->recv(rsponse, 512, 10000); + if (revLen == 0) { + socket->close(); + } + rsponse[revLen] = 0; +pc.printf("%lu:Received String : (%d) : \n%s\n", _tickCount, revLen, rsponse); - socket->close(); + led2 = 0; pc.printf("%lu:socket closed\n", _tickCount); @@ -141,34 +144,27 @@ bool getHttp(SocketInterface *socket, const char *urlStr, char *rsponse) { - uint32_t ret; - - char scheme[8]; + char scheme[8]; char host[32]; uint16_t port; char path[256]; - int res = parseURL(urlStr, scheme, host, &port, path); + bool res = parseURL(urlStr, scheme, host, &port, path); char host_ip[16]; wifi.getHostByName(host, host_ip); - - + socket->setAddressPort(host_ip, port); - ret = socket->open(); + uint32_t ret = socket->open(); -led2 = 1; char str[512] = {0}; - sprintf(str, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host); // HTTP/1.1の最低限なリクエスト + sprintf(str, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host); - // Send Data socket->send(str, strlen(str)); + int revLen = socket->recv(rsponse, 512, 10000); + rsponse[revLen] = 0; - // Receive Data - int revLen = socket->recv(rsponse, RESPOSE_BUFFER_SIZE, 10000); - rsponse[revLen] = 0; -pc.printf("%lu:Received String : (%d) : \n%s\n", _tickCount, revLen, rsponse); socket->close(); -led2 = 0; + // retBufを httpプロトコルで分解しようか? { @@ -182,8 +178,6 @@ } - - int main() { pc.baud(115200); @@ -199,20 +193,38 @@ ret = wifi.connect("elecom2g-15BEAC", "2903851513877"); // SSID-name, SSID-passPhase pc.printf("wifi.connect() : %d\n", ret); - wifi.setPreferredDns("192.168.2.1"); // Preferred DNS IP-address + + char* myip; + myip = wifi.getIPAddress(); + pc.printf("IP Address is: %s\n", (myip) ? myip : "No IP"); + - char* ip; - ip = wifi.getIPAddress(); - pc.printf("IP Address is: %s\n", (ip) ? ip : "No IP"); + // I want to IP address of default gateway(wifi wifi router). because that is preferred DNS server. + //wifi.setPreferredDns("192.168.2.1"); // Preferred DNS IP-address + { + // "192.168.2.132" --> "192.168.2.1" (maybe) + uint32_t ip1, ip2, ip3, ip4; + sscanf(myip, "%3lu.%3lu.%3lu.%3lu", &ip1, &ip2, &ip3, &ip4); + + char defaultgateway[20] = {0}; + sprintf(defaultgateway, "%lu.%lu.%lu.1", ip1, ip2, ip3); +pc.printf("%lu: Default gateway: %s\n", _tickCount, defaultgateway); + wifi.setPreferredDns(defaultgateway); // defaultgateway is Preferred DNS IP-address + //wifi.setPreferredDns("192.168.2.1"); // defaultgateway is Preferred DNS IP-address + } + SocketInterface *socket = wifi.allocateSocket(SOCK_TCP); char responseBuffer[RESPOSE_BUFFER_SIZE] = {0}; - getHttp(socket, "http://iina.ohneta.net/iinalab_office/api/inout_status.php?person=0&inout=0", responseBuffer); + getHttpDebug(socket, "http://iina.ohneta.net/iinalab_office/api/inout_status.php?person=0&inout=0", responseBuffer); + //getHttp(socket, "http://iina.ohneta.net/iinalab_office/api/inout_status.php?person=0&inout=0", responseBuffer); wifi.disconnect(); - + pc.printf(":::Finished\n"); + while(1) { + int dummy = 1; } }