Project in FIBO
Fork of ESP8266 by
Diff: ESP8266.cpp
- Revision:
- 4:8dfe0574a040
- Parent:
- 3:4f24e7e803a1
--- a/ESP8266.cpp Sat Jun 11 14:05:06 2016 +0000 +++ b/ESP8266.cpp Tue Dec 12 11:39:55 2017 +0000 @@ -1,10 +1,11 @@ #include "ESP8266.h" #define HTTPCMD "GET " -#define protocol " HTTP/1.0\n\n" +#define protocol " HTTP/1.1\n\n" // Constructor -ESP8266::ESP8266(PinName tx, PinName rx, int br) : comm(tx, rx) { +ESP8266::ESP8266(PinName tx, PinName rx, int br) : comm(tx, rx) +{ comm.baud(br); } @@ -12,7 +13,8 @@ ESP8266::~ESP8266() { } // Add <CR> + <LF> at the end of the string -void ESP8266::AddEOL(char * s) { +void ESP8266::AddEOL(char * s) +{ char k; k = strlen(s); // Finds position of NULL character s[k] = 0x0D; // switch NULL for <CR> @@ -21,7 +23,8 @@ } // Add one ASCII character at the end of the string -void ESP8266::AddChar(char * s, char c) { +void ESP8266::AddChar(char * s, char c) +{ char k; k = strlen(s); s[k] = c; @@ -29,10 +32,11 @@ } // Converts integer number to null-terminated string -void ESP8266::itoa(int n, char * s) { +void ESP8266::itoa(int n, char * s) +{ char k = 0; char r[11]; - + if(n == 0) { s[0] = '0'; s[1] = 0; @@ -52,24 +56,27 @@ } // Sends command to ESP8266. Receives the command string -void ESP8266::SendCMD(char * s) { +void ESP8266::SendCMD(char * s) +{ AddEOL(s); comm.printf("%s", s); } // Resets the ESP8266 -void ESP8266::Reset(void) { +void ESP8266::Reset(void) +{ char rs[10]; strcpy(rs, "AT+RST"); SendCMD(rs); } // Receive reply until no character is received after a given timeout in miliseconds -bool ESP8266::RcvReply(char * r, int to) { +bool ESP8266::RcvReply(char * r, int to) +{ Timer t; bool ended = 0; char c; - + strcpy(r, ""); t.start(); while(!ended) { @@ -79,7 +86,7 @@ t.start(); } if(t.read_ms() > to) { - ended = 1; + ended = 1; } } AddChar(r, 0x00); @@ -87,7 +94,8 @@ } // Gets the AP list. Parameter: the string to receive the list -void ESP8266::GetList(char * l) { +void ESP8266::GetList(char * l) +{ char rs[15]; strcpy(rs, "AT+CWLAP"); SendCMD(rs); @@ -95,7 +103,8 @@ } // Joins a Wifi AP. Parameters: SSID and Password (strings) -void ESP8266::Join(char * id, char * pwd) { +void ESP8266::Join(char * id, char * pwd) +{ char cmd[255]; strcpy(cmd, "AT+CWJAP="); AddChar(cmd, 0x22); @@ -109,45 +118,79 @@ } // Gets ESP IP. Parameter: string to contain IP -void ESP8266::GetIP(char * ip) { +void ESP8266::GetIP(char * ip) +{ char cmd[15]; strcpy(cmd, "AT+CIFSR"); SendCMD(cmd); RcvReply(ip, 2000); } +void ESP8266::GetCon(char * ipp) +{ + char cmd[15]; + strcpy(cmd, "AT+CWJAP?"); + SendCMD(cmd); + RcvReply(ipp, 2000); +} + //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both -void ESP8266::SetMode(char mode) { +void ESP8266::SetMode(char mode) +{ char cmd[15]; strcpy(cmd, "AT+CWMODE="); mode = mode + 0x30; // Converts number into corresponding ASCII character AddChar(cmd, mode); // Completes command string SendCMD(cmd); } +void ESP8266::Check(char * i) +{ + char cmd[15]; + if(comm.readable()) { + comm.gets(cmd,100);} + strcpy(i,cmd); + +} // Quits the AP -void ESP8266::Quit(void) { +void ESP8266::Quit(void) +{ char cmd[15]; strcpy(cmd, "AT+CWQAP"); SendCMD(cmd); } // Sets single connection -void ESP8266::SetSingle(void) { +void ESP8266::SetSingle(void) +{ char cmd[15]; strcpy(cmd, "AT+CIPMUX=0"); SendCMD(cmd); } // Sets multiple connection -void ESP8266::SetMultiple(void) { +void ESP8266::SetMultiple(void) +{ char rs[15]; strcpy(rs, "AT+CIPMUX=1"); SendCMD(rs); } +void ESP8266::Close(void) +{ + char rs[15]; + strcpy(rs, "AT+CIPCLOSE"); + SendCMD(rs); +} +void ESP8266::Disconnect(void) +{ + char rs[15]; + strcpy(rs, "AT+CWQAP"); + SendCMD(rs); +} // Gets connection status. Parameter: string to contain status -void ESP8266::GetConnStatus(char * st) { +void ESP8266::GetConnStatus(char * st) +{ char cmd[15]; strcpy(cmd, "AT+CIPSTATUS"); SendCMD(cmd); @@ -155,7 +198,8 @@ } // Starts server mode. Parameter: port to be used -void ESP8266::StartServerMode(int port) { +void ESP8266::StartServerMode(int port) +{ char rs[25]; char t[4]; strcpy(rs, "AT+CIPSERVER=1,"); @@ -165,22 +209,27 @@ } // Close server mode. -void ESP8266::CloseServerMode(void) { +void ESP8266::CloseServerMode(void) +{ char rs[20]; strcpy(rs, "AT+CIPSERVER=0"); SendCMD(rs); } -void ESP8266::setTransparent(void){ +void ESP8266::setTransparent(void) +{ char rs[20]; strcpy(rs, "AT+CIPMODE=0"); SendCMD(rs); } -void ESP8266::startTCPConn(char *IP, int port){ +void ESP8266::startTCPConn(char *IP, int port, char * data ) +{ char rs[100]; - sprintf(rs, "AT+CIPSTART=\"TCP\",\"%s\",%d", IP, port); + sprintf(rs, "AT+CIPSTART=4,\"TCP\",\"%s\",%d", IP, port); SendCMD(rs); + RcvReply(data, 5000); + } void ESP8266::sendURL(char *URL, char *command){ @@ -189,14 +238,29 @@ strcpy(http_cmd, HTTPCMD); strcat(http_cmd, URL); - strcat(http_cmd, protocol); strcpy(url, http_cmd); - sprintf(snd,"AT+CIPSENDEX=%d",strlen(url)); + sprintf(snd,"AT+CIPSEND=4,%d",strlen(url)); strcpy(command, url); SendCMD(snd); - wait(3); SendCMD(url); } + + +void ESP8266::sendURL(char *URL, char *command, char * data) +{ + char url[300], snd[300], http_cmd[300]; + + strcpy(http_cmd, HTTPCMD); + strcat(http_cmd, URL); + strcpy(url, http_cmd); - \ No newline at end of file + + sprintf(snd,"AT+CIPSEND=4,%d",strlen(url)); + strcpy(command, url); + SendCMD(snd); + SendCMD(url); + RcvReply(data, 15000); +} + +