modified by ohneta

Dependencies:   DnsQuery ESP8266Interface NetworkSocketAPI mbed

Fork of HelloESP8266Interface by NetworkSocketAPI

Committer:
ohneta
Date:
Wed Nov 11 05:57:33 2015 +0000
Revision:
25:8536695a236d
Parent:
24:2b91c0e2067b
Child:
27:4dc43240e498
check finished;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:1984a177ff56 1 /* NetworkSocketAPI Example Program
sam_grove 0:1984a177ff56 2 * Copyright (c) 2015 ARM Limited
sam_grove 0:1984a177ff56 3 *
sam_grove 0:1984a177ff56 4 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 0:1984a177ff56 5 * you may not use this file except in compliance with the License.
sam_grove 0:1984a177ff56 6 * You may obtain a copy of the License at
sam_grove 0:1984a177ff56 7 *
sam_grove 0:1984a177ff56 8 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 0:1984a177ff56 9 *
sam_grove 0:1984a177ff56 10 * Unless required by applicable law or agreed to in writing, software
sam_grove 0:1984a177ff56 11 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 0:1984a177ff56 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 0:1984a177ff56 13 * See the License for the specific language governing permissions and
sam_grove 0:1984a177ff56 14 * limitations under the License.
sam_grove 0:1984a177ff56 15 */
sam_grove 0:1984a177ff56 16
sam_grove 0:1984a177ff56 17 #include "mbed.h"
sarahmarshy 16:bb0a5b830dc5 18 #include "ESP8266Interface.h"
sam_grove 0:1984a177ff56 19
ohneta 25:8536695a236d 20 #define RESPOSE_BUFFER_SIZE 2048
ohneta 24:2b91c0e2067b 21
ohneta 24:2b91c0e2067b 22
ohneta 24:2b91c0e2067b 23 Serial pc(USBTX, USBRX);
ohneta 24:2b91c0e2067b 24 DigitalOut led1(LED1);
ohneta 24:2b91c0e2067b 25 DigitalOut led2(LED2);
ohneta 24:2b91c0e2067b 26
ohneta 24:2b91c0e2067b 27 uint32_t _tickCount = 0L;
ohneta 24:2b91c0e2067b 28 void flash()
ohneta 24:2b91c0e2067b 29 {
ohneta 24:2b91c0e2067b 30 led1 = !led1;
ohneta 24:2b91c0e2067b 31 _tickCount++;
ohneta 24:2b91c0e2067b 32 }
ohneta 24:2b91c0e2067b 33
ohneta 24:2b91c0e2067b 34 ESP8266Interface wifi(p9, p10);
ohneta 24:2b91c0e2067b 35
ohneta 24:2b91c0e2067b 36 /**
ohneta 24:2b91c0e2067b 37 *
ohneta 24:2b91c0e2067b 38 */
ohneta 24:2b91c0e2067b 39 bool parseURL(const char *url, char *scheme, char *host, uint16_t *port, char *path)
ohneta 24:2b91c0e2067b 40 {
ohneta 24:2b91c0e2067b 41 *port = 0;
ohneta 24:2b91c0e2067b 42
ohneta 24:2b91c0e2067b 43 char *schemePtr = (char *)url;
ohneta 24:2b91c0e2067b 44 char *hostPtr = (char *)strstr(url, "://");
ohneta 24:2b91c0e2067b 45 if (hostPtr == NULL) {
ohneta 24:2b91c0e2067b 46 pc.printf("Could not find host");
ohneta 24:2b91c0e2067b 47 return false;
ohneta 24:2b91c0e2067b 48 }
ohneta 24:2b91c0e2067b 49
ohneta 24:2b91c0e2067b 50 memcpy(scheme, schemePtr, hostPtr - schemePtr);
ohneta 24:2b91c0e2067b 51 scheme[hostPtr - schemePtr] = '\0';
ohneta 24:2b91c0e2067b 52
ohneta 24:2b91c0e2067b 53 if (strcmp(scheme, "http") == 0) {
ohneta 24:2b91c0e2067b 54 *port = 80;
ohneta 24:2b91c0e2067b 55 } else if(strcmp(scheme, "https") == 0) {
ohneta 24:2b91c0e2067b 56 *port = 433;
ohneta 24:2b91c0e2067b 57 }
ohneta 24:2b91c0e2067b 58
ohneta 24:2b91c0e2067b 59 hostPtr += 3;
ohneta 24:2b91c0e2067b 60 size_t hostLen = 0;
ohneta 24:2b91c0e2067b 61
ohneta 24:2b91c0e2067b 62 char *portPtr = strchr(hostPtr, ':');
ohneta 24:2b91c0e2067b 63 if (portPtr != NULL) {
ohneta 24:2b91c0e2067b 64 hostLen = portPtr - hostPtr;
ohneta 24:2b91c0e2067b 65 portPtr++;
ohneta 24:2b91c0e2067b 66 if (sscanf(portPtr, "%hu", port) != 1) {
ohneta 24:2b91c0e2067b 67 pc.printf("Could not find port");
ohneta 24:2b91c0e2067b 68 return false;
ohneta 24:2b91c0e2067b 69 }
ohneta 24:2b91c0e2067b 70 }
ohneta 24:2b91c0e2067b 71
ohneta 24:2b91c0e2067b 72 char* pathPtr = strchr(hostPtr, '/');
ohneta 24:2b91c0e2067b 73 if( hostLen == 0 ) {
ohneta 24:2b91c0e2067b 74 hostLen = pathPtr - hostPtr;
ohneta 24:2b91c0e2067b 75 }
ohneta 24:2b91c0e2067b 76
ohneta 24:2b91c0e2067b 77 memcpy(host, hostPtr, hostLen);
ohneta 24:2b91c0e2067b 78 host[hostLen] = '\0';
ohneta 24:2b91c0e2067b 79
ohneta 24:2b91c0e2067b 80 size_t pathLen;
ohneta 24:2b91c0e2067b 81 char *fragmentPtr = strchr(hostPtr, '#');
ohneta 24:2b91c0e2067b 82 if(fragmentPtr != NULL) {
ohneta 24:2b91c0e2067b 83 pathLen = fragmentPtr - pathPtr;
ohneta 24:2b91c0e2067b 84 } else {
ohneta 24:2b91c0e2067b 85 pathLen = strlen(pathPtr);
ohneta 24:2b91c0e2067b 86 }
ohneta 24:2b91c0e2067b 87
ohneta 24:2b91c0e2067b 88 memcpy(path, pathPtr, pathLen);
ohneta 24:2b91c0e2067b 89 path[pathLen] = '\0';
ohneta 24:2b91c0e2067b 90
ohneta 24:2b91c0e2067b 91 return true;
ohneta 24:2b91c0e2067b 92 }
ohneta 24:2b91c0e2067b 93
ohneta 24:2b91c0e2067b 94
ohneta 24:2b91c0e2067b 95
ohneta 25:8536695a236d 96 bool getHttpDebug(SocketInterface *socket, const char *urlStr, char *rsponse)
ohneta 24:2b91c0e2067b 97 {
ohneta 24:2b91c0e2067b 98 char scheme[8];
ohneta 24:2b91c0e2067b 99 char host[32];
ohneta 24:2b91c0e2067b 100 uint16_t port;
ohneta 24:2b91c0e2067b 101 char path[256];
sam_grove 0:1984a177ff56 102
ohneta 24:2b91c0e2067b 103 bool res = parseURL(urlStr, scheme, host, &port, path);
ohneta 24:2b91c0e2067b 104 pc.printf("%lu:parseURL=%d: %s --> %s, %s, %d, %s \n", _tickCount, res, urlStr, scheme, host, port, path);
ohneta 25:8536695a236d 105
ohneta 25:8536695a236d 106
ohneta 24:2b91c0e2067b 107 char host_ip[16];
ohneta 25:8536695a236d 108 {
ohneta 25:8536695a236d 109 wifi.getHostByName(host, host_ip);
ohneta 24:2b91c0e2067b 110 pc.printf("%lu:host IP address : %s\n", _tickCount, host_ip);
ohneta 25:8536695a236d 111 }
ohneta 25:8536695a236d 112
ohneta 24:2b91c0e2067b 113 uint32_t ret;
ohneta 24:2b91c0e2067b 114
ohneta 25:8536695a236d 115 //_tickCount = 0;
ohneta 24:2b91c0e2067b 116 socket->setAddressPort(host_ip, port);
ohneta 24:2b91c0e2067b 117 ret = socket->open();
ohneta 24:2b91c0e2067b 118 led2 = 1;
ohneta 24:2b91c0e2067b 119 pc.printf("%d:opend : %d: %s:%d\n", _tickCount, ret, host_ip, port);
ohneta 24:2b91c0e2067b 120
ohneta 24:2b91c0e2067b 121 char str[512] = {0};
ohneta 24:2b91c0e2067b 122 sprintf(str, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host);
ohneta 24:2b91c0e2067b 123 pc.printf("%lu: REQUEST:\n%s\n------\n", _tickCount, str);
ohneta 24:2b91c0e2067b 124
ohneta 24:2b91c0e2067b 125 // Send Data
ohneta 24:2b91c0e2067b 126 pc.printf("%lu:Sending GET data...\n", _tickCount);
ohneta 24:2b91c0e2067b 127 socket->send(str, strlen(str));
ohneta 24:2b91c0e2067b 128
ohneta 24:2b91c0e2067b 129 pc.printf("%lu:Waiting on reply ... \n", _tickCount);
ohneta 25:8536695a236d 130 int revLen = socket->recv(rsponse, 512, 10000);
ohneta 25:8536695a236d 131 if (revLen == 0) {
ohneta 25:8536695a236d 132 socket->close();
ohneta 25:8536695a236d 133 }
ohneta 25:8536695a236d 134 rsponse[revLen] = 0;
ohneta 25:8536695a236d 135 pc.printf("%lu:Received String : (%d) : \n%s\n", _tickCount, revLen, rsponse);
ohneta 24:2b91c0e2067b 136
ohneta 25:8536695a236d 137
ohneta 24:2b91c0e2067b 138 led2 = 0;
ohneta 24:2b91c0e2067b 139 pc.printf("%lu:socket closed\n", _tickCount);
ohneta 24:2b91c0e2067b 140
ohneta 24:2b91c0e2067b 141 return true;
ohneta 24:2b91c0e2067b 142 }
ohneta 24:2b91c0e2067b 143
ohneta 24:2b91c0e2067b 144
ohneta 24:2b91c0e2067b 145 bool getHttp(SocketInterface *socket, const char *urlStr, char *rsponse)
ohneta 24:2b91c0e2067b 146 {
ohneta 25:8536695a236d 147 char scheme[8];
ohneta 24:2b91c0e2067b 148 char host[32];
ohneta 24:2b91c0e2067b 149 uint16_t port;
ohneta 24:2b91c0e2067b 150 char path[256];
ohneta 25:8536695a236d 151 bool res = parseURL(urlStr, scheme, host, &port, path);
ohneta 24:2b91c0e2067b 152
ohneta 24:2b91c0e2067b 153 char host_ip[16];
ohneta 24:2b91c0e2067b 154 wifi.getHostByName(host, host_ip);
ohneta 25:8536695a236d 155
ohneta 24:2b91c0e2067b 156 socket->setAddressPort(host_ip, port);
ohneta 25:8536695a236d 157 uint32_t ret = socket->open();
ohneta 24:2b91c0e2067b 158
ohneta 24:2b91c0e2067b 159 char str[512] = {0};
ohneta 25:8536695a236d 160 sprintf(str, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host);
ohneta 24:2b91c0e2067b 161
ohneta 24:2b91c0e2067b 162 socket->send(str, strlen(str));
ohneta 25:8536695a236d 163 int revLen = socket->recv(rsponse, 512, 10000);
ohneta 25:8536695a236d 164 rsponse[revLen] = 0;
ohneta 24:2b91c0e2067b 165
ohneta 24:2b91c0e2067b 166 socket->close();
ohneta 25:8536695a236d 167
ohneta 24:2b91c0e2067b 168
ohneta 24:2b91c0e2067b 169 // retBufを httpプロトコルで分解しようか?
ohneta 24:2b91c0e2067b 170 {
ohneta 24:2b91c0e2067b 171 // 最低限でもresult codeくらい調べたい
ohneta 24:2b91c0e2067b 172
ohneta 24:2b91c0e2067b 173 // 必要なヘッダだけ取り出す
ohneta 24:2b91c0e2067b 174 // Date-xx: xxxxxx
ohneta 24:2b91c0e2067b 175 }
ohneta 24:2b91c0e2067b 176
ohneta 24:2b91c0e2067b 177 return true;
ohneta 24:2b91c0e2067b 178 }
ohneta 24:2b91c0e2067b 179
ohneta 24:2b91c0e2067b 180
sam_grove 0:1984a177ff56 181 int main()
sam_grove 20:4cb9ef3b0cc9 182 {
ohneta 24:2b91c0e2067b 183 pc.baud(115200);
ohneta 24:2b91c0e2067b 184
ohneta 24:2b91c0e2067b 185
sam_grove 20:4cb9ef3b0cc9 186 Ticker t;
ohneta 24:2b91c0e2067b 187 t.attach(flash, 0.1f);
ohneta 24:2b91c0e2067b 188 pc.printf("NetworkSocketAPI TEST by ohneta.\r\n");
sam_grove 4:cb8a17dd6746 189
ohneta 24:2b91c0e2067b 190 int32_t ret = wifi.init();
ohneta 24:2b91c0e2067b 191 pc.printf("wifi.init() : %d\n", ret);
ohneta 24:2b91c0e2067b 192
ohneta 24:2b91c0e2067b 193 ret = wifi.connect("elecom2g-15BEAC", "2903851513877"); // SSID-name, SSID-passPhase
ohneta 24:2b91c0e2067b 194 pc.printf("wifi.connect() : %d\n", ret);
ohneta 24:2b91c0e2067b 195
ohneta 25:8536695a236d 196
ohneta 25:8536695a236d 197 char* myip;
ohneta 25:8536695a236d 198 myip = wifi.getIPAddress();
ohneta 25:8536695a236d 199 pc.printf("IP Address is: %s\n", (myip) ? myip : "No IP");
ohneta 25:8536695a236d 200
ohneta 24:2b91c0e2067b 201
ohneta 25:8536695a236d 202 // I want to IP address of default gateway(wifi wifi router). because that is preferred DNS server.
ohneta 25:8536695a236d 203 //wifi.setPreferredDns("192.168.2.1"); // Preferred DNS IP-address
ohneta 25:8536695a236d 204 {
ohneta 25:8536695a236d 205 // "192.168.2.132" --> "192.168.2.1" (maybe)
ohneta 25:8536695a236d 206 uint32_t ip1, ip2, ip3, ip4;
ohneta 25:8536695a236d 207 sscanf(myip, "%3lu.%3lu.%3lu.%3lu", &ip1, &ip2, &ip3, &ip4);
ohneta 25:8536695a236d 208
ohneta 25:8536695a236d 209 char defaultgateway[20] = {0};
ohneta 25:8536695a236d 210 sprintf(defaultgateway, "%lu.%lu.%lu.1", ip1, ip2, ip3);
ohneta 25:8536695a236d 211 pc.printf("%lu: Default gateway: %s\n", _tickCount, defaultgateway);
ohneta 25:8536695a236d 212 wifi.setPreferredDns(defaultgateway); // defaultgateway is Preferred DNS IP-address
ohneta 25:8536695a236d 213 //wifi.setPreferredDns("192.168.2.1"); // defaultgateway is Preferred DNS IP-address
ohneta 25:8536695a236d 214 }
ohneta 25:8536695a236d 215
ohneta 24:2b91c0e2067b 216
ohneta 24:2b91c0e2067b 217
ohneta 24:2b91c0e2067b 218 SocketInterface *socket = wifi.allocateSocket(SOCK_TCP);
sarahmarshy 22:1d355289fc18 219
ohneta 24:2b91c0e2067b 220 char responseBuffer[RESPOSE_BUFFER_SIZE] = {0};
ohneta 25:8536695a236d 221 getHttpDebug(socket, "http://iina.ohneta.net/iinalab_office/api/inout_status.php?person=0&inout=0", responseBuffer);
ohneta 25:8536695a236d 222 //getHttp(socket, "http://iina.ohneta.net/iinalab_office/api/inout_status.php?person=0&inout=0", responseBuffer);
ohneta 24:2b91c0e2067b 223
ohneta 24:2b91c0e2067b 224 wifi.disconnect();
ohneta 25:8536695a236d 225 pc.printf(":::Finished\n");
ohneta 25:8536695a236d 226
ohneta 24:2b91c0e2067b 227 while(1) {
ohneta 25:8536695a236d 228 int dummy = 1;
ohneta 24:2b91c0e2067b 229 }
sam_grove 0:1984a177ff56 230 }