priyanka Siddalingappa / Mbed 2 deprecated Garage_Control

Dependencies:   mbed HBridge MQ7 Ton SimpleScheduler Pir_sensor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP8266.cpp Source File

ESP8266.cpp

00001 #include "ESP8266.h"
00002 #define HTTPCMD "GET "
00003 #define protocol " HTTP/1.0\n\n"
00004 
00005 
00006 // Constructor
00007 ESP8266::ESP8266(PinName tx, PinName rx, int br) : comm(tx, rx) {
00008     comm.baud(br);
00009 }
00010 
00011 // Destructor
00012 ESP8266::~ESP8266() { }
00013 
00014 // Add <CR> + <LF> at the end of the string
00015 void ESP8266::AddEOL(char * s) {
00016     char k;
00017     k = strlen(s); // Finds position of NULL character
00018     s[k] = 0x0D; // switch NULL for <CR>
00019     s[k + 1] = 0x0A; // Add <LF>
00020     s[k + 2] = 0; // Add NULL at the end
00021 }
00022 
00023 // Add one ASCII character at the end of the string
00024 void ESP8266::AddChar(char * s, char c) {
00025     char k;
00026     k = strlen(s);
00027     s[k] = c;
00028     s[k + 1] = 0;
00029 }
00030 
00031 // Converts integer number to null-terminated string
00032 void ESP8266::itoa(int n, char * s) {
00033     char k = 0;
00034     char r[11];
00035     
00036     if(n == 0) {
00037         s[0] = '0';
00038         s[1] = 0;
00039     } else {
00040         while(n != 0) {
00041             r[k]= (n % 10) + '0';
00042             n = n / 10;
00043             k++;
00044         }
00045         while(k > 0) {
00046             s[n] = r[k - 1] + '0';
00047             n++;
00048             k--;
00049         }
00050         s[n] = 0;
00051     }
00052 }
00053 
00054 // Sends command to ESP8266. Receives the command string
00055 void ESP8266::SendCMD(char * s) {
00056     AddEOL(s);
00057     comm.printf("%s", s);
00058 }
00059 
00060 // Resets the ESP8266
00061 void ESP8266::Reset(void) {
00062     char rs[10];
00063     strcpy(rs, "AT+RST");
00064     SendCMD(rs);
00065 }
00066 
00067 // Receive reply until no character is received after a given timeout in miliseconds
00068 bool ESP8266::RcvReply(char * r, int to) {
00069     Timer t;
00070     bool ended = 0;
00071     char c;
00072     
00073     strcpy(r, "");
00074     t.start();
00075     while(!ended) {
00076         if(comm.readable()) {
00077             c = comm.getc();
00078             AddChar(r, c);
00079             t.start();
00080         }
00081         if(t.read_ms() > to) {
00082                 ended = 1;
00083         }
00084     }
00085     AddChar(r, 0x00);
00086     return ended;
00087 }
00088 
00089 // Gets the AP list. Parameter: the string to receive the list
00090 void ESP8266::GetList(char * l) {
00091     char rs[15];
00092     strcpy(rs, "AT+CWLAP");
00093     SendCMD(rs);
00094     RcvReply(l, 5000); // Needs big timeout because it takes long to start replying
00095 }
00096 
00097 // Joins a Wifi AP. Parameters: SSID and Password (strings)
00098 void ESP8266::Join(char * id, char * pwd) {
00099     char cmd[255];
00100     strcpy(cmd, "AT+CWJAP=");
00101     AddChar(cmd, 0x22);
00102     strcat(cmd, id);
00103     AddChar(cmd, 0x22);
00104     AddChar(cmd, 0x2C);
00105     AddChar(cmd, 0x22);
00106     strcat(cmd, pwd);
00107     AddChar(cmd, 0x22);
00108     SendCMD(cmd);
00109 }
00110 
00111 // Gets ESP IP. Parameter: string to contain IP
00112 void ESP8266::GetIP(char * ip) {
00113     char cmd[15];
00114     strcpy(cmd, "AT+CIFSR");
00115     SendCMD(cmd);
00116     RcvReply(ip, 2000);
00117 }
00118 
00119 //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both
00120 void ESP8266::SetMode(char mode) {
00121     char cmd[15];
00122     strcpy(cmd, "AT+CWMODE=");
00123     mode = mode + 0x30; // Converts number into corresponding ASCII character
00124     AddChar(cmd, mode); // Completes command string
00125     SendCMD(cmd);
00126 }
00127 
00128 // Quits the AP
00129 void ESP8266::Quit(void) {
00130     char cmd[15];
00131     strcpy(cmd, "AT+CWQAP");
00132     SendCMD(cmd);
00133 }
00134 
00135 // Sets single connection
00136 void ESP8266::SetSingle(void) {
00137     char cmd[15];
00138     strcpy(cmd, "AT+CIPMUX=0");
00139     SendCMD(cmd);
00140 }
00141 
00142 // Sets multiple connection
00143 void ESP8266::SetMultiple(void) {
00144     char rs[15];
00145     strcpy(rs, "AT+CIPMUX=1");
00146     SendCMD(rs);
00147 }
00148 
00149 // Gets connection status. Parameter: string to contain status
00150 void ESP8266::GetConnStatus(char * st) {
00151     char cmd[15];
00152     strcpy(cmd, "AT+CIPSTATUS");
00153     SendCMD(cmd);
00154     RcvReply(st, 2000);
00155 }
00156 
00157 // Starts server mode. Parameter: port to be used
00158 void ESP8266::StartServerMode(int port) {
00159     char rs[25];
00160     char t[4];
00161     strcpy(rs, "AT+CIPSERVER=1,");
00162     itoa(port, t);
00163     strcat(rs, t);
00164     SendCMD(rs);
00165 }
00166 
00167 // Close server mode.
00168 void ESP8266::CloseServerMode(void) {
00169     char rs[20];
00170     strcpy(rs, "AT+CIPSERVER=0");
00171     SendCMD(rs);
00172 }
00173 
00174 void ESP8266::setTransparent(void){
00175     char rs[20];
00176     strcpy(rs, "AT+CIPMODE=0");
00177     SendCMD(rs);
00178 }
00179 
00180 void ESP8266::startTCPConn(char *IP, int port){
00181     char rs[100];
00182     sprintf(rs, "AT+CIPSTART=\"TCP\",\"%s\",%d", IP, port);
00183     SendCMD(rs);
00184 }
00185 
00186 void ESP8266::sendURL(char *URL, char *command){
00187     char url[300], snd[300], http_cmd[300];
00188     
00189     strcpy(http_cmd, HTTPCMD);
00190     
00191     strcat(http_cmd, URL);
00192     strcat(http_cmd, protocol);
00193     
00194     strcpy(url, http_cmd);
00195     sprintf(snd,"AT+CIPSENDEX=%d",strlen(url));
00196     strcpy(command, url);
00197     SendCMD(snd);
00198     wait(3);
00199     SendCMD(url);
00200 }
00201     
00202