esp8266 websocket and socket
Revision 0:46166e7d81a8, committed 2015-06-12
- Comitter:
- metabi814
- Date:
- Fri Jun 12 10:06:12 2015 +0000
- Commit message:
- ESP8266 socket and websocket ; -set ip ; -set mac
Changed in this revision
diff -r 000000000000 -r 46166e7d81a8 ESP8266.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266.cpp Fri Jun 12 10:06:12 2015 +0000 @@ -0,0 +1,481 @@ +#include "mbed.h" +#include "ESP8266.h" + +#define MAX_TRY_WRITE 20 +#define MAX_TRY_READ 10 + +//Debug is disabled by default +#if 0 +#define DBG(x, ...) std::printf("[WebSocket : DBG]"x"\r\n", ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[WebSocket : WARN]"x"\r\n", ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[WebSocket : ERR]"x"\r\n", ##__VA_ARGS__); +#else +#define DBG(x, ...) +#define WARN(x, ...) +#define ERR(x, ...) +#endif + +#define INFO(x, ...) printf("[WebSocket : INFO]"x"\r\n", ##__VA_ARGS__); + +esp8266* esp8266::inst; + +esp8266::esp8266(PinName tx,PinName rx,int baudRate, const char *ssid, const char *password,const char *host,const char *port) : Modem(tx,rx,baudRate) +{ + inst = this; + _ssid=ssid; + _password=password; + _host=host; + _port=port; +} + +bool esp8266::join() +{ + char cmd[64]; + int r; + //disable echo + sendCmd("ATE0\r\n"); + + r=sendCmdAndWaitForResp("AT+CWMODE=1\r\n","OK",DEFAULT_TIMEOUT,CMD); + printf("wifi.CWMODE=%d\r\n",r); + + snprintf(cmd,sizeof(cmd),"AT+CWJAP=\"%s\",\"%s\"\r\n",_ssid,_password); + printf("mensaje=%s\r\n",cmd); + r=sendCmdAndWaitForResp(cmd,"OK",15,CMD); + printf("wifi.CWJAP=%d\r\n",r); + + if(r==0) { + getip(); + return true; + } else { + return false; + } + + +} + +int esp8266::connect(int socket) +{ + char cmd[64]; + int r; + + r=sendCmdAndWaitForResp("AT+CIPMUX=1\r\n","OK",DEFAULT_TIMEOUT,CMD); + printf("socket.CIPMUX1=%d\r\n",r); + + + sprintf(cmd,"AT+CIPSTART=%d,\"TCP\",\"%s\",%s\r\n",socket,_host,_port); + printf("mensaje2=%s\r\n",cmd); + r=sendCmdAndWaitForResp(cmd,"CONNECT",15,CMD); + printf("socket.AT+CIPSTART=%d\r\n",r); + + return r; +} + +int esp8266::send(int socket,const char * str,int len) +{ + + char cmd[100]; + cleanBuffer(cmd,100); + wait(0.3); + if(len > 0) { + snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d,%d\r\n",socket,len); + printf("mensaje3=%s\r\n",cmd); + if(0 != sendCmdAndWaitForResp(cmd,">",2,CMD)) { + return -1; + } + sendCmd(str); + + } + return len; +} + +bool esp8266::disconnect(int socket) +{ + char cmd[35]; + sprintf(cmd,"AT+CIPCLOSE=%d\r\n",socket); + sendCmd(cmd); + + return true; +} + + +void esp8266::reset() +{ + sendCmdAndWaitForResp("AT+CWMODE=1\r\n","OK",DEFAULT_TIMEOUT,CMD); + sendCmd("AT+RST\r\n"); +} + +bool esp8266::is_connected() //RETURN 2 GOT IP 3 CONNECTED 4 DISCONNECTED +{ + char resp[20]; + + sendCmd("AT+CIPSTATUS\r\n"); + readBuffer(resp,sizeof(resp),DEFAULT_TIMEOUT/2); + if(NULL != strstr(resp,"STATUS:3")) { + //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CONNECTED" + return true; + } else { + //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CLOSED" + //+CIPSTATUS: 0,,"","","","INITIAL" + return false; + } +} + +void esp8266::getip() +{ + sendCmd("AT+CIFSR\r\n"); + readBuffer(_ip,100,10); +} + +void esp8266::set_sta_ip(char * s_ip) +{ + int r; + char cmd[30]; + sprintf(cmd,"AT+CIPSTA=\"%s\"\r\n",s_ip); + r=sendCmdAndWaitForResp(cmd,"OK",5,CMD); + printf("mensaje4=%s\r\n",cmd); + printf("cambio ip=%d\r\n",r); + getip(); +} + +void esp8266::list_ap() +{ + int r; + r=sendCmdAndWaitForResp("AT+CWLAP\r\n","+",5,CMD); + printf("cambio ip=%d\r\n",r); + readBuffer(_lap,2000,10); +} + +int esp8266::recv(char* buf, int len) +{ + cleanBuffer(buf,len); + readBuffer(buf,len,DEFAULT_TIMEOUT/2); + return len; + //return strlen(buf); +} + +//----------------------------------websocket------------------------------------------------------- + +void esp8266::websocket(char * url) +{ + int ret = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path)); + if(ret) { + ERR("URL parsing failed; please use: \"ws://ip-or-domain[:port]/path\""); + return; + } + + if(port == 0) { //TODO do handle WSS->443 + port = 80; + } + + if(strcmp(scheme, "ws")) { + ERR("Wrong scheme, please use \"ws\" instead"); + } +} + +int esp8266::parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen) //Parse URL +{ + char* schemePtr = (char*) url; + char* hostPtr = (char*) strstr(url, "://"); + if(hostPtr == NULL) { + WARN("Could not find host"); + return -1; //URL is invalid + } + + if( maxSchemeLen < hostPtr - schemePtr + 1 ) { //including NULL-terminating char + WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1); + return -1; + } + memcpy(scheme, schemePtr, hostPtr - schemePtr); + scheme[hostPtr - schemePtr] = '\0'; + + hostPtr+=3; + + size_t hostLen = 0; + + char* portPtr = strchr(hostPtr, ':'); + if( portPtr != NULL ) { + hostLen = portPtr - hostPtr; + portPtr++; + if( sscanf(portPtr, "%hu", port) != 1) { + WARN("Could not find port"); + return -1; + } + } else { + *port=0; + } + char* pathPtr = strchr(hostPtr, '/'); + if( hostLen == 0 ) { + hostLen = pathPtr - hostPtr; + } + + if( maxHostLen < hostLen + 1 ) { //including NULL-terminating char + WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1); + return -1; + } + memcpy(host, hostPtr, hostLen); + host[hostLen] = '\0'; + + size_t pathLen; + char* fragmentPtr = strchr(hostPtr, '#'); + if(fragmentPtr != NULL) { + pathLen = fragmentPtr - pathPtr; + } else { + pathLen = strlen(pathPtr); + } + + if( maxPathLen < pathLen + 1 ) { //including NULL-terminating char + WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1); + return -1; + } + memcpy(path, pathPtr, pathLen); + path[pathLen] = '\0'; + + return 0; +} + +bool esp8266::w_connect(int socket) { + char cmd[200]; +/* + while (connect(socket) < 0) { + ERR("Unable to connect to (%s) on port (%d)", host, port); + wait(0.2); + return false; + } +*/ + // sent http header to upgrade to the ws protocol + + printf("path=%s\r\n",path); + printf("host=%s\r\n",host); + printf("port=%d\r\n",port); + + sprintf(cmd, "GET %s HTTP/1.1\r\n", path); + printf("n-GET=%d\r\n",strlen(cmd)); + printf("%s\r\n",cmd); + write(cmd, strlen(cmd)); + + sprintf(cmd, "Host: %s:%d\r\n", host, port); + printf("n-host=%d\r\n",strlen(cmd)); + printf("%s\r\n",cmd); + write(cmd, strlen(cmd)); + + sprintf(cmd, "Upgrade: WebSocket\r\n"); + printf("n-up=%d\r\n",strlen(cmd)); + printf("%s\r\n",cmd); + write(cmd, strlen(cmd)); + + sprintf(cmd, "Connection: Upgrade\r\n"); + printf("n-con=%d\r\n",strlen(cmd)); + printf("%s\r\n",cmd); + write(cmd, strlen(cmd)); + + sprintf(cmd, "Sec-WebSocket-Key: L159VM0TWUzyDxwJEIEzjw==\r\n"); + printf("n-key=%d\r\n",strlen(cmd)); + printf("%s\r\n",cmd); + write(cmd, strlen(cmd)); + + sprintf(cmd, "Sec-WebSocket-Version: 13\r\n\r\n"); + printf("n-ver=%d\r\n",strlen(cmd)); + printf("%s\r\n",cmd); + int ret = write(cmd, strlen(cmd)); + if (ret != strlen(cmd)) { + disconnect(socket); + ERR("Could not send request"); + return false; + } + + ret = read(cmd, 200, 100); + if (ret < 0) { + disconnect(socket); + ERR("Could not receive answer\r\n"); + return false; + } + + cmd[ret] = '\0'; + DBG("recv: %s\r\n", cmd); + + if ( strstr(cmd, "DdLWT/1JcX+nQFHebYP+rqEx5xI=") == NULL ) { + ERR("Wrong answer from server, got \"%s\" instead\r\n", cmd); + do { + ret = read(cmd, 200, 100); + if (ret < 0) { + ERR("Could not receive answer\r\n"); + return false; + } + cmd[ret] = '\0'; + printf("%s",cmd); + } while (ret > 0); + disconnect(socket); + return false; + } + + INFO("\r\nhost: %s\r\npath: %s\r\nport: %d\r\n\r\n", host, path, port); + return true; +} + +int esp8266::sendLength(uint32_t len, char * msg) { //sendlength(5,msg+1) + + if (len < 126) { + msg[0] = len | (1<<7); + return 1; + } else if (len < 65535) { + msg[0] = 126 | (1<<7); + msg[1] = (len >> 8) & 0xff; + msg[2] = len & 0xff; + return 3; + } else { + msg[0] = 127 | (1<<7); + for (int i = 0; i < 8; i++) { + msg[i+1] = (len >> i*8) & 0xff; + } + return 9; + } +} + +int esp8266::readChar(char * pC, bool block) { + return read(pC, 1, 1); +} + +int esp8266::sendOpcode(uint8_t opcode, char * msg) { + msg[0] = 0x80 | (opcode & 0x0f); + return 1; +} + +int esp8266::sendMask(char * msg) { + for (int i = 0; i < 4; i++) { + msg[i] = 0; + } + return 4; +} + +int esp8266::w_send(char * str) { + char msg[strlen(str) + 15]; //msg[20] + int idx = 0; + idx = sendOpcode(0x01, msg); //msg[0]=0x81 + idx += sendLength(strlen(str), msg + idx);// sendlength(5,msg+1) idx=2 + idx += sendMask(msg + idx); //idx=6 + memcpy(msg+idx, str, strlen(str)); + int res = write(msg, idx + strlen(str)); + return res; +} + +bool esp8266::w_read(char * message) { + int i = 0; + uint32_t len_msg; + char opcode = 0; + char c; + char mask[4] = {0, 0, 0, 0}; + bool is_masked = false; + Timer tmr; + + // read the opcode + tmr.start(); + while (true) { + if (tmr.read() > 3) { + DBG("timeout ws\r\n"); + return false; + } + + if(!is_connected()) + { + WARN("Connection was closed by server"); + return false; + } + + //----------socket.set_blocking(false, 1); + if (recv(&opcode, 1) != 1) { + + //socket.set_blocking(false, 2000); + return false; + } + + //-----------socket.set_blocking(false, 2000); + + if (opcode == 0x81) + break; + } + DBG("opcode: 0x%X\r\n", opcode); + + readChar(&c); + len_msg = c & 0x7f; + is_masked = c & 0x80; + if (len_msg == 126) { + readChar(&c); + len_msg = c << 8; + readChar(&c); + len_msg += c; + } else if (len_msg == 127) { + len_msg = 0; + for (int i = 0; i < 8; i++) { + readChar(&c); + len_msg += (c << (7-i)*8); + } + } + + if (len_msg == 0) { + return false; + } + DBG("length: %d\r\n", len_msg); + + if (is_masked) { + for (i = 0; i < 4; i++) + readChar(&c); + mask[i] = c; + } + + int nb = read(message, len_msg, len_msg); + if (nb != len_msg) + return false; + + for (i = 0; i < len_msg; i++) { + message[i] = message[i] ^ mask[i % 4]; + } + + message[len_msg] = '\0'; + + return true; +} +char* esp8266::getPath() { + return path; +} + +int esp8266::write(char * str, int len) { + int res = 0, idx = 0; + + for (int j = 0; j < MAX_TRY_WRITE; j++) { + /* + if(!is_connected()) + { + WARN("Connection was closed by server"); + break; + } + */ + if ((res = send(4,str + idx,len - idx)) == -1) + continue; + + idx += res; + + if (idx == len) + return len; + } + + return (idx == 0) ? -1 : idx; +} + +int esp8266::read(char * str, int len, int min_len) { + int res = 0, idx = 0; + + for (int j = 0; j < MAX_TRY_WRITE; j++) { + + if ((res = recv(str + idx, len - idx)) == -1) + continue; + + idx += res; + + if (idx == len || (min_len != -1 && idx > min_len)) + return idx; + } + + return (idx == 0) ? -1 : idx; +} + +
diff -r 000000000000 -r 46166e7d81a8 ESP8266.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266.h Fri Jun 12 10:06:12 2015 +0000 @@ -0,0 +1,76 @@ +//#ifndef __esp8266_H__ +//#define __esp8266_H__ + +#include "mbed.h" +#include "modem.h" + +#define MAX_SOCK_NUM 4 //(0~4) +class esp8266: public Modem +{ +public: + /*Constructor*/ + esp8266(PinName tx,PinName rx,int baudRate,const char *ssid =NULL,const char *password=NULL,const char *host=NULL,const char *port=NULL); + + static esp8266* getInstance() { + return inst; + }; + //-------------------------------wifi socket---------------------------- + bool join(); // connection au access point return true false + + int connect(int socket); // return 0 success -1 error + + int send(int socket,const char * str,int len); + + int recv(char* buf, int len); + + bool disconnect(int socket); + + bool is_connected(); + + void reset(); // reset esp8266 + + void set_sta_ip(char * s_ip); + void list_ap(); + void getip(); + char _ip[100]; + char _lap[2000]; + //-------------------------------wifi websocket---------------------------- + void websocket(char * url); + bool w_connect(int socket); + int w_send(char * str); + bool w_read(char * message); + //bool w_is_connected(); + //bool close(); + char* getPath(); + + //------------------------------------------------------------------------- + + +protected: + + static esp8266* inst; + const char* _ssid; + const char* _password; + const char* _host; + const char* _port; + +private : + + //void fillFields(char * url); + int parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen); //Parse URL + int sendOpcode(uint8_t opcode, char * msg); + int sendLength(uint32_t len, char * msg); + int sendMask(char * msg); + int readChar(char * pC, bool block = true); + + char scheme[8]; + uint16_t port; + char host[32]; + char path[64]; + + int read(char * buf, int len, int min_len = -1); + int write(char * buf, int len); + + +}; +//#endif \ No newline at end of file
diff -r 000000000000 -r 46166e7d81a8 modem/modem.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modem/modem.cpp Fri Jun 12 10:06:12 2015 +0000 @@ -0,0 +1,129 @@ +/* + modem.cpp + 2014 Copyright (c) Seeed Technology Inc. All right reserved. + + Author:lawliet zou(lawliet.zou@gmail.com) + 2014-2-24 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "modem.h" + +char Modem::readByte(void) +{ + return serialModem.getc(); +} + +bool Modem::readable() +{ + return serialModem.readable(); +} + +int Modem::readBuffer(char *buffer,int count, unsigned int timeOut) +{ + int i = 0; + timeCnt.start(); + while(1) { + while (serialModem.readable()) { + char c = serialModem.getc(); + buffer[i++] = c; + if(i >= count)break; + } + if(i >= count)break; + if(timeCnt.read() > timeOut) { + timeCnt.stop(); + timeCnt.reset(); + break; + } + } + return 0; +} + +void Modem::cleanBuffer(char *buffer, int count) +{ + for(int i=0; i < count; i++) { + buffer[i] = '\0'; + } +} + +void Modem::sendCmd(const char* cmd) +{ + serialModem.puts(cmd); +} + +void Modem::sendATTest(void) +{ + sendCmdAndWaitForResp("AT\r\n","OK",DEFAULT_TIMEOUT,CMD); +} + +bool Modem::respCmp(const char *resp, unsigned int len, unsigned int timeout) +{ + int sum=0; + timeCnt.start(); + + while(1) { + if(serialModem.readable()) { + char c = serialModem.getc(); + sum = (c==resp[sum]) ? sum+1 : 0; + if(sum == len)break; + } + if(timeCnt.read() > timeout) { + timeCnt.stop(); + timeCnt.reset(); + return false; + } + } + timeCnt.stop(); + timeCnt.reset(); + + return true; +} + +int Modem::waitForResp(const char *resp, unsigned int timeout,DataType type) +{ + int len = strlen(resp); + int sum=0; + timeCnt.start(); + + while(1) { + if(serialModem.readable()) { + char c = serialModem.getc(); + sum = (c==resp[sum]) ? sum+1 : 0; + if(sum == len)break; + } + if(timeCnt.read() > timeout) { + timeCnt.stop(); + timeCnt.reset(); + return -1; + } + } + timeCnt.stop(); + timeCnt.reset(); + + if(type == CMD) { + while(serialModem.readable()) { + char c = serialModem.getc(); + } + } + + return 0; +} + +int Modem::sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type) +{ + sendCmd(data); + return waitForResp(resp,timeout,type); +}
diff -r 000000000000 -r 46166e7d81a8 modem/modem.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modem/modem.h Fri Jun 12 10:06:12 2015 +0000 @@ -0,0 +1,128 @@ +/* + modem.h + 2014 Copyright (c) Seeed Technology Inc. All right reserved. + + Author:lawliet zou(lawliet.zou@gmail.com) + 2014-2-24 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef __MODEM_H__ +#define __MODEM_H__ + +#include "mbed.h" + +#define DEFAULT_TIMEOUT 5 + +enum DataType { + CMD = 0, + DATA = 1, +}; + +/** Modem class. + * Used for Modem communication. attention that Modem module communicate with MCU in serial protocol + */ +class Modem +{ + +public: + /** Create Modem Instance + * @param tx uart transmit pin to communicate with Modem + * @param rx uart receive pin to communicate with Modem + * @param baudRate baud rate of uart communication + */ + Modem(PinName tx, PinName rx, int baudRate) : serialModem(tx, rx) { + serialModem.baud(baudRate); + }; + + Serial serialModem; +protected: + /** Power on Modem + */ + void preInit(void); + + /** check serialModem is readable or not + * @returns + * true on readable + * false on not readable + */ + bool readable(); + + /** read one byte from serialModem + * @returns + * one byte read from serialModem + */ + char readByte(void); + + /** read from Modem module and save to buffer array + * @param buffer buffer array to save what read from Modem module + * @param count the maximal bytes number read from Modem module + * @param timeOut time to wait for reading from Modem module + * @returns + * 0 on success + * -1 on error + */ + int readBuffer(char* buffer,int count, unsigned int timeOut); + + + /** clean Buffer + * @param buffer buffer to clean + * @param count number of bytes to clean + */ + void cleanBuffer(char* buffer, int count); + + /** send AT command to Modem module + * @param cmd command array which will be send to GPRS module + */ + void sendCmd(const char* cmd); + + /**send "AT" to Modem module + */ + void sendATTest(void); + + /** compare the response from GPRS module with a string + * @param resp buffer to be compared + * @param len length that will be compared + * @param timeout waiting seconds till timeout + */ + bool respCmp(const char *resp, unsigned int len, unsigned int timeout); + + /** check Modem module response before time out + * @param *resp correct response which Modem module will return + * @param *timeout waiting seconds till timeout + * @returns + * 0 on success + * -1 on error + */ + int waitForResp(const char *resp, unsigned int timeout,DataType type); + + /** send AT command to GPRS module and wait for correct response + * @param *cmd AT command which will be send to GPRS module + * @param *resp correct response which GPRS module will return + * @param *timeout waiting seconds till timeout + * @returns + * 0 on success + * -1 on error + */ + int sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type); + + Timer timeCnt; + +private: + +}; + +#endif