William Thenaers / ESP8266

Fork of ESP8266 by Janhavi Kulkarni

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP8266.cpp Source File

ESP8266.cpp

00001 #include "ESP8266.h"
00002 
00003 // Constructor
00004 ESP8266::ESP8266(PinName tx, PinName rx, int br) : comm(tx, rx) {
00005     comm.baud(br);
00006 }
00007 
00008 // Destructor
00009 ESP8266::~ESP8266() { }
00010 
00011 // Add <CR> + <LF> at the end of the string
00012 void ESP8266::AddEOL(char * s) {
00013     char k   = strlen(s); // Finds position of NULL character
00014     s[k]     = '\r';    // switch NULL for <CR>
00015     s[k + 1] = '\n';    // Add <LF>
00016     s[k + 2] = '\0';    // Add NULL at the end
00017 }
00018 
00019 // Add one ASCII character at the end of the string
00020 void ESP8266::AddChar(char * s, char c) {
00021     char k   = strlen(s);
00022     s[k]     = c;
00023     s[k + 1] = '\0';
00024 }
00025 
00026 // Sends command to ESP8266. Receives the command string
00027 void ESP8266::SendCMD(char* s) {
00028     comm.printf("%s%s", s, CMD_END);
00029 }
00030 
00031 // Resets the ESP8266
00032 void ESP8266::Reset(void) {
00033     SendCMD("AT+RST");
00034 }
00035 
00036 // Receive reply until no character is received after a given timeout in miliseconds
00037 bool ESP8266::RcvReply(char* r, int to) {
00038     Timer t;
00039     strcpy(r, "");
00040     t.start();
00041     
00042     do {
00043         if(comm.readable()) {
00044             AddChar(r, comm.getc());
00045             t.start();
00046         }
00047     } while(t.read_ms() < to);
00048 
00049     AddChar(r, '\0');
00050     return r[0] != '\0';
00051 }
00052 
00053 // Gets the AP list. Parameter: the string to receive the list
00054 bool ESP8266::GetList(char *l) {
00055     SendCMD("AT+CWLAP");
00056     return RcvReply(l, 5000); // Needs big timeout because it takes long to start replying
00057 }
00058 
00059 // Joins a Wifi AP. Parameters: SSID and Password (strings)
00060 void ESP8266::Join(char *id, char *pwd) {
00061     char cmd[255];
00062     sprintf(cmd, "AT+CWJAP=\"%s\",\"%s\"", id, pwd);
00063     SendCMD(cmd);
00064 }
00065 
00066 // Gets ESP IP. Parameter: string to contain IP
00067 bool ESP8266::GetIP(char *ip) {
00068     SendCMD("AT+CIFSR");
00069     return RcvReply(ip, 2000);
00070 }
00071 
00072 //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both
00073 void ESP8266::SetMode(WiFiMode mode) {
00074     char cmd[15];
00075     strcpy(cmd, "AT+CWMODE=");
00076     AddChar(cmd, mode + '0'); // Completes command string
00077     SendCMD(cmd);
00078 }
00079 
00080 // Quits the AP
00081 void ESP8266::Quit(void) {
00082     SendCMD("AT+CWQAP");
00083 }
00084 
00085 // Sets single connection
00086 void ESP8266::SetSingle(void) {
00087     SendCMD("AT+CIPMUX=0");
00088 }
00089 
00090 // Sets multiple connection
00091 void ESP8266::SetMultiple(void) {
00092     SendCMD("AT+CIPMUX=1");
00093 }
00094 
00095 // Gets connection status. Parameter: string to contain status
00096 bool ESP8266::GetConnStatus(char * st) {
00097     SendCMD("AT+CIPSTATUS");
00098     return RcvReply(st, 2000);
00099 }
00100 
00101 // Starts server mode. Parameter: port to be used
00102 void ESP8266::StartServerMode(int port) {
00103     char rs[25];
00104     sprintf(rs, "AT+CIPSERVER=1,%d", port);
00105     SendCMD(rs);
00106 }
00107 
00108 // Close server mode.
00109 void ESP8266::CloseServerMode(void) {
00110     SendCMD("AT+CIPSERVER=0");
00111 }
00112 
00113 void ESP8266::setTransparent(void){
00114     SendCMD("AT+CIPMODE=0");
00115 }
00116 
00117 void ESP8266::startTCPConn(char *IP, int port){
00118     char rs[100];
00119     sprintf(rs, "AT+CIPSTART=0,\"TCP\",\"%s\",%d", IP, port);
00120     SendCMD(rs);
00121 }
00122 
00123 void ESP8266::sendURL(char *URL, char* IP, char *command){
00124     char snd[20], http_cmd[300];
00125 
00126     sprintf(http_cmd, "GET %s HTTP/1.1%sHost: %s%sConnection: close%s%s", URL, CMD_END, IP, CMD_END, CMD_END, CMD_END);
00127     sprintf(snd,"AT+CIPSEND=0,%d",strlen(http_cmd));
00128     strcpy(command, http_cmd);
00129     
00130     SendCMD(snd);
00131     wait(1);
00132     SendCMD(http_cmd);
00133 }
00134     
00135 void ESP8266::deepsleep(size_t ms) {
00136     char snd[20];
00137     sprintf(snd, "AT+GSLP=%d", ms);
00138     SendCMD(snd);
00139 }
00140