modified by ohneta

Dependencies:   DnsQuery ESP8266Interface NetworkSocketAPI mbed

Fork of HelloESP8266Interface by NetworkSocketAPI

Committer:
ohneta
Date:
Tue Nov 10 14:58:47 2015 +0000
Revision:
24:2b91c0e2067b
Parent:
22:1d355289fc18
Child:
25:8536695a236d
ESP8226?HTTP????????????; HelloESP8226Interface????????????????????????????????

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 24:2b91c0e2067b 20 #define RESPOSE_BUFFER_SIZE 1024
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 24:2b91c0e2067b 96 bool getHttpDebug(SocketInterface *socket, const char *urlStr)
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 24:2b91c0e2067b 105 #if 1
ohneta 24:2b91c0e2067b 106 char host_ip[16];
ohneta 24:2b91c0e2067b 107 wifi.getHostByName(host, host_ip);
ohneta 24:2b91c0e2067b 108 #else
ohneta 24:2b91c0e2067b 109 char *host_ip = "54.249.236.250";
ohneta 24:2b91c0e2067b 110 #endif
ohneta 24:2b91c0e2067b 111 pc.printf("%lu:host IP address : %s\n", _tickCount, host_ip);
ohneta 24:2b91c0e2067b 112 uint32_t ret;
ohneta 24:2b91c0e2067b 113
ohneta 24:2b91c0e2067b 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 static char retBuf[512] = {0};
ohneta 24:2b91c0e2067b 129 pc.printf("%lu:Waiting on reply ... \n", _tickCount);
ohneta 24:2b91c0e2067b 130 int revLen = socket->recv(retBuf, 512, 10000);
ohneta 24:2b91c0e2067b 131 retBuf[revLen] = 0;
ohneta 24:2b91c0e2067b 132 pc.printf("%lu:Received String : (%d) : \n%s\n", _tickCount, revLen, retBuf);
ohneta 24:2b91c0e2067b 133
ohneta 24:2b91c0e2067b 134 socket->close();
ohneta 24:2b91c0e2067b 135 led2 = 0;
ohneta 24:2b91c0e2067b 136 pc.printf("%lu:socket closed\n", _tickCount);
ohneta 24:2b91c0e2067b 137
ohneta 24:2b91c0e2067b 138 return true;
ohneta 24:2b91c0e2067b 139 }
ohneta 24:2b91c0e2067b 140
ohneta 24:2b91c0e2067b 141
ohneta 24:2b91c0e2067b 142 bool getHttp(SocketInterface *socket, const char *urlStr, char *rsponse)
ohneta 24:2b91c0e2067b 143 {
ohneta 24:2b91c0e2067b 144 uint32_t ret;
ohneta 24:2b91c0e2067b 145
ohneta 24:2b91c0e2067b 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 24:2b91c0e2067b 150 int 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 24:2b91c0e2067b 154
ohneta 24:2b91c0e2067b 155
ohneta 24:2b91c0e2067b 156 socket->setAddressPort(host_ip, port);
ohneta 24:2b91c0e2067b 157 ret = socket->open();
ohneta 24:2b91c0e2067b 158
ohneta 24:2b91c0e2067b 159 led2 = 1;
ohneta 24:2b91c0e2067b 160 char str[512] = {0};
ohneta 24:2b91c0e2067b 161 sprintf(str, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host); // HTTP/1.1の最低限なリクエスト
ohneta 24:2b91c0e2067b 162
ohneta 24:2b91c0e2067b 163 // Send Data
ohneta 24:2b91c0e2067b 164 socket->send(str, strlen(str));
ohneta 24:2b91c0e2067b 165
ohneta 24:2b91c0e2067b 166 // Receive Data
ohneta 24:2b91c0e2067b 167 int revLen = socket->recv(rsponse, RESPOSE_BUFFER_SIZE, 10000);
ohneta 24:2b91c0e2067b 168 rsponse[revLen] = 0;
ohneta 24:2b91c0e2067b 169 pc.printf("%lu:Received String : (%d) : \n%s\n", _tickCount, revLen, rsponse);
ohneta 24:2b91c0e2067b 170 socket->close();
ohneta 24:2b91c0e2067b 171 led2 = 0;
ohneta 24:2b91c0e2067b 172
ohneta 24:2b91c0e2067b 173 // retBufを httpプロトコルで分解しようか?
ohneta 24:2b91c0e2067b 174 {
ohneta 24:2b91c0e2067b 175 // 最低限でもresult codeくらい調べたい
ohneta 24:2b91c0e2067b 176
ohneta 24:2b91c0e2067b 177 // 必要なヘッダだけ取り出す
ohneta 24:2b91c0e2067b 178 // Date-xx: xxxxxx
ohneta 24:2b91c0e2067b 179 }
ohneta 24:2b91c0e2067b 180
ohneta 24:2b91c0e2067b 181 return true;
ohneta 24:2b91c0e2067b 182 }
ohneta 24:2b91c0e2067b 183
ohneta 24:2b91c0e2067b 184
ohneta 24:2b91c0e2067b 185
sam_grove 2:7283ce112304 186
sam_grove 0:1984a177ff56 187 int main()
sam_grove 20:4cb9ef3b0cc9 188 {
ohneta 24:2b91c0e2067b 189 pc.baud(115200);
ohneta 24:2b91c0e2067b 190
ohneta 24:2b91c0e2067b 191
sam_grove 20:4cb9ef3b0cc9 192 Ticker t;
ohneta 24:2b91c0e2067b 193 t.attach(flash, 0.1f);
ohneta 24:2b91c0e2067b 194 pc.printf("NetworkSocketAPI TEST by ohneta.\r\n");
sam_grove 4:cb8a17dd6746 195
ohneta 24:2b91c0e2067b 196 int32_t ret = wifi.init();
ohneta 24:2b91c0e2067b 197 pc.printf("wifi.init() : %d\n", ret);
ohneta 24:2b91c0e2067b 198
ohneta 24:2b91c0e2067b 199 ret = wifi.connect("elecom2g-15BEAC", "2903851513877"); // SSID-name, SSID-passPhase
ohneta 24:2b91c0e2067b 200 pc.printf("wifi.connect() : %d\n", ret);
ohneta 24:2b91c0e2067b 201
ohneta 24:2b91c0e2067b 202 wifi.setPreferredDns("192.168.2.1"); // Preferred DNS IP-address
ohneta 24:2b91c0e2067b 203
sarahmarshy 18:a5830a6d3e11 204 char* ip;
sarahmarshy 18:a5830a6d3e11 205 ip = wifi.getIPAddress();
ohneta 24:2b91c0e2067b 206 pc.printf("IP Address is: %s\n", (ip) ? ip : "No IP");
ohneta 24:2b91c0e2067b 207
ohneta 24:2b91c0e2067b 208
ohneta 24:2b91c0e2067b 209 SocketInterface *socket = wifi.allocateSocket(SOCK_TCP);
sarahmarshy 22:1d355289fc18 210
ohneta 24:2b91c0e2067b 211 char responseBuffer[RESPOSE_BUFFER_SIZE] = {0};
ohneta 24:2b91c0e2067b 212 getHttp(socket, "http://iina.ohneta.net/iinalab_office/api/inout_status.php?person=0&inout=0", responseBuffer);
ohneta 24:2b91c0e2067b 213
ohneta 24:2b91c0e2067b 214 wifi.disconnect();
bridadan 14:c47437f5dae8 215
ohneta 24:2b91c0e2067b 216 while(1) {
ohneta 24:2b91c0e2067b 217 }
sam_grove 0:1984a177ff56 218 }