modified by ohneta

Dependencies:   DnsQuery ESP8266Interface NetworkSocketAPI mbed

Fork of HelloESP8266Interface by NetworkSocketAPI

Committer:
ohneta
Date:
Sun Nov 15 16:46:42 2015 +0000
Revision:
27:4dc43240e498
Parent:
25:8536695a236d
hidden for private informations....:)

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 27:4dc43240e498 94 #if 0
ohneta 25:8536695a236d 95 bool getHttpDebug(SocketInterface *socket, const char *urlStr, char *rsponse)
ohneta 24:2b91c0e2067b 96 {
ohneta 24:2b91c0e2067b 97 char scheme[8];
ohneta 24:2b91c0e2067b 98 char host[32];
ohneta 24:2b91c0e2067b 99 uint16_t port;
ohneta 24:2b91c0e2067b 100 char path[256];
sam_grove 0:1984a177ff56 101
ohneta 24:2b91c0e2067b 102 bool res = parseURL(urlStr, scheme, host, &port, path);
ohneta 24:2b91c0e2067b 103 pc.printf("%lu:parseURL=%d: %s --> %s, %s, %d, %s \n", _tickCount, res, urlStr, scheme, host, port, path);
ohneta 25:8536695a236d 104
ohneta 25:8536695a236d 105
ohneta 24:2b91c0e2067b 106 char host_ip[16];
ohneta 25:8536695a236d 107 {
ohneta 25:8536695a236d 108 wifi.getHostByName(host, host_ip);
ohneta 24:2b91c0e2067b 109 pc.printf("%lu:host IP address : %s\n", _tickCount, host_ip);
ohneta 25:8536695a236d 110 }
ohneta 25:8536695a236d 111
ohneta 24:2b91c0e2067b 112 uint32_t ret;
ohneta 24:2b91c0e2067b 113
ohneta 25:8536695a236d 114 //_tickCount = 0;
ohneta 24:2b91c0e2067b 115 socket->setAddressPort(host_ip, port);
ohneta 24:2b91c0e2067b 116 ret = socket->open();
ohneta 24:2b91c0e2067b 117 led2 = 1;
ohneta 24:2b91c0e2067b 118 pc.printf("%d:opend : %d: %s:%d\n", _tickCount, ret, host_ip, port);
ohneta 24:2b91c0e2067b 119
ohneta 24:2b91c0e2067b 120 char str[512] = {0};
ohneta 24:2b91c0e2067b 121 sprintf(str, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host);
ohneta 24:2b91c0e2067b 122 pc.printf("%lu: REQUEST:\n%s\n------\n", _tickCount, str);
ohneta 24:2b91c0e2067b 123
ohneta 24:2b91c0e2067b 124 // Send Data
ohneta 24:2b91c0e2067b 125 pc.printf("%lu:Sending GET data...\n", _tickCount);
ohneta 24:2b91c0e2067b 126 socket->send(str, strlen(str));
ohneta 24:2b91c0e2067b 127
ohneta 24:2b91c0e2067b 128 pc.printf("%lu:Waiting on reply ... \n", _tickCount);
ohneta 25:8536695a236d 129 int revLen = socket->recv(rsponse, 512, 10000);
ohneta 25:8536695a236d 130 if (revLen == 0) {
ohneta 25:8536695a236d 131 socket->close();
ohneta 25:8536695a236d 132 }
ohneta 25:8536695a236d 133 rsponse[revLen] = 0;
ohneta 25:8536695a236d 134 pc.printf("%lu:Received String : (%d) : \n%s\n", _tickCount, revLen, rsponse);
ohneta 24:2b91c0e2067b 135
ohneta 25:8536695a236d 136
ohneta 24:2b91c0e2067b 137 led2 = 0;
ohneta 24:2b91c0e2067b 138 pc.printf("%lu:socket closed\n", _tickCount);
ohneta 24:2b91c0e2067b 139
ohneta 24:2b91c0e2067b 140 return true;
ohneta 24:2b91c0e2067b 141 }
ohneta 27:4dc43240e498 142 #endif
ohneta 24:2b91c0e2067b 143
ohneta 24:2b91c0e2067b 144 bool getHttp(SocketInterface *socket, const char *urlStr, char *rsponse)
ohneta 24:2b91c0e2067b 145 {
ohneta 25:8536695a236d 146 char scheme[8];
ohneta 24:2b91c0e2067b 147 char host[32];
ohneta 24:2b91c0e2067b 148 uint16_t port;
ohneta 24:2b91c0e2067b 149 char path[256];
ohneta 25:8536695a236d 150 bool res = parseURL(urlStr, scheme, host, &port, path);
ohneta 24:2b91c0e2067b 151
ohneta 24:2b91c0e2067b 152 char host_ip[16];
ohneta 24:2b91c0e2067b 153 wifi.getHostByName(host, host_ip);
ohneta 25:8536695a236d 154
ohneta 24:2b91c0e2067b 155 socket->setAddressPort(host_ip, port);
ohneta 25:8536695a236d 156 uint32_t ret = socket->open();
ohneta 24:2b91c0e2067b 157
ohneta 24:2b91c0e2067b 158 char str[512] = {0};
ohneta 25:8536695a236d 159 sprintf(str, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host);
ohneta 24:2b91c0e2067b 160
ohneta 24:2b91c0e2067b 161 socket->send(str, strlen(str));
ohneta 25:8536695a236d 162 int revLen = socket->recv(rsponse, 512, 10000);
ohneta 25:8536695a236d 163 rsponse[revLen] = 0;
ohneta 24:2b91c0e2067b 164
ohneta 24:2b91c0e2067b 165 socket->close();
ohneta 25:8536695a236d 166
ohneta 24:2b91c0e2067b 167
ohneta 24:2b91c0e2067b 168 // retBufを httpプロトコルで分解しようか?
ohneta 24:2b91c0e2067b 169 {
ohneta 24:2b91c0e2067b 170 // 最低限でもresult codeくらい調べたい
ohneta 24:2b91c0e2067b 171 }
ohneta 24:2b91c0e2067b 172
ohneta 24:2b91c0e2067b 173 return true;
ohneta 24:2b91c0e2067b 174 }
ohneta 24:2b91c0e2067b 175
ohneta 24:2b91c0e2067b 176
sam_grove 0:1984a177ff56 177 int main()
sam_grove 20:4cb9ef3b0cc9 178 {
ohneta 24:2b91c0e2067b 179 pc.baud(115200);
ohneta 24:2b91c0e2067b 180
ohneta 24:2b91c0e2067b 181
sam_grove 20:4cb9ef3b0cc9 182 Ticker t;
ohneta 24:2b91c0e2067b 183 t.attach(flash, 0.1f);
ohneta 24:2b91c0e2067b 184 pc.printf("NetworkSocketAPI TEST by ohneta.\r\n");
sam_grove 4:cb8a17dd6746 185
ohneta 24:2b91c0e2067b 186 int32_t ret = wifi.init();
ohneta 24:2b91c0e2067b 187 pc.printf("wifi.init() : %d\n", ret);
ohneta 24:2b91c0e2067b 188
ohneta 24:2b91c0e2067b 189 ret = wifi.connect("elecom2g-15BEAC", "2903851513877"); // SSID-name, SSID-passPhase
ohneta 24:2b91c0e2067b 190 pc.printf("wifi.connect() : %d\n", ret);
ohneta 24:2b91c0e2067b 191
ohneta 25:8536695a236d 192
ohneta 25:8536695a236d 193 char* myip;
ohneta 25:8536695a236d 194 myip = wifi.getIPAddress();
ohneta 25:8536695a236d 195 pc.printf("IP Address is: %s\n", (myip) ? myip : "No IP");
ohneta 25:8536695a236d 196
ohneta 24:2b91c0e2067b 197
ohneta 25:8536695a236d 198 // I want to IP address of default gateway(wifi wifi router). because that is preferred DNS server.
ohneta 25:8536695a236d 199 //wifi.setPreferredDns("192.168.2.1"); // Preferred DNS IP-address
ohneta 25:8536695a236d 200 {
ohneta 25:8536695a236d 201 // "192.168.2.132" --> "192.168.2.1" (maybe)
ohneta 25:8536695a236d 202 uint32_t ip1, ip2, ip3, ip4;
ohneta 25:8536695a236d 203 sscanf(myip, "%3lu.%3lu.%3lu.%3lu", &ip1, &ip2, &ip3, &ip4);
ohneta 25:8536695a236d 204
ohneta 25:8536695a236d 205 char defaultgateway[20] = {0};
ohneta 25:8536695a236d 206 sprintf(defaultgateway, "%lu.%lu.%lu.1", ip1, ip2, ip3);
ohneta 25:8536695a236d 207 pc.printf("%lu: Default gateway: %s\n", _tickCount, defaultgateway);
ohneta 25:8536695a236d 208 wifi.setPreferredDns(defaultgateway); // defaultgateway is Preferred DNS IP-address
ohneta 25:8536695a236d 209 //wifi.setPreferredDns("192.168.2.1"); // defaultgateway is Preferred DNS IP-address
ohneta 25:8536695a236d 210 }
ohneta 25:8536695a236d 211
ohneta 24:2b91c0e2067b 212
ohneta 24:2b91c0e2067b 213
ohneta 24:2b91c0e2067b 214 SocketInterface *socket = wifi.allocateSocket(SOCK_TCP);
sarahmarshy 22:1d355289fc18 215
ohneta 24:2b91c0e2067b 216 char responseBuffer[RESPOSE_BUFFER_SIZE] = {0};
ohneta 27:4dc43240e498 217 getHttp(socket, "http://youthost.com", responseBuffer);
ohneta 24:2b91c0e2067b 218
ohneta 24:2b91c0e2067b 219 wifi.disconnect();
ohneta 25:8536695a236d 220 pc.printf(":::Finished\n");
ohneta 25:8536695a236d 221
ohneta 24:2b91c0e2067b 222 while(1) {
ohneta 25:8536695a236d 223 int dummy = 1;
ohneta 24:2b91c0e2067b 224 }
sam_grove 0:1984a177ff56 225 }