Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: PIR_Sensor_wifi ThingSpeak_ESP8266 heart_rate_monitor
Fork of ESP8266 by
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; 00014 k = strlen(s); // Finds position of NULL character 00015 s[k] = 0x0D; // switch NULL for <CR> 00016 s[k + 1] = 0x0A; // Add <LF> 00017 s[k + 2] = 0; // Add NULL at the end 00018 } 00019 00020 // Add one ASCII character at the end of the string 00021 void ESP8266::AddChar(char * s, char c) { 00022 char k; 00023 k = strlen(s); 00024 s[k] = c; 00025 s[k + 1] = 0; 00026 } 00027 00028 // Converts integer number to null-terminated string 00029 void ESP8266::itoa(int n, char * s) { 00030 char k = 0; 00031 char r[11]; 00032 00033 if(n == 0) { 00034 s[0] = '0'; 00035 s[1] = 0; 00036 } else { 00037 while(n != 0) { 00038 r[k]= (n % 10) + '0'; 00039 n = n / 10; 00040 k++; 00041 } 00042 while(k > 0) { 00043 s[n] = r[k - 1] + '0'; 00044 n++; 00045 k--; 00046 } 00047 s[n] = 0; 00048 } 00049 } 00050 00051 // Sends command to ESP8266. Receives the command string 00052 void ESP8266::SendCMD(char * s) { 00053 AddEOL(s); 00054 comm.printf("%s", s); 00055 } 00056 00057 // Resets the ESP8266 00058 void ESP8266::Reset(void) { 00059 char rs[10]; 00060 strcpy(rs, "AT+RST"); 00061 SendCMD(rs); 00062 } 00063 00064 // Receive reply until no character is received after a given timeout in miliseconds 00065 void ESP8266::RcvReply(char * r, int to) { 00066 Timer t; 00067 bool ended = 0; 00068 char c; 00069 00070 strcpy(r, ""); 00071 t.start(); 00072 while(!ended) { 00073 if(comm.readable()) { 00074 c = comm.getc(); 00075 AddChar(r, c); 00076 00077 t.start(); 00078 } 00079 if(t.read_ms() > to) { 00080 ended = 1; 00081 } 00082 } 00083 AddChar(r, 0x00); 00084 } 00085 00086 // Gets the AP list. Parameter: the string to receive the list 00087 void ESP8266::GetList(char * l) { 00088 char rs[15]; 00089 strcpy(rs, "AT+CWLAP"); 00090 SendCMD(rs); 00091 RcvReply(l, 5000); // Needs big timeout because it takes long to start replying 00092 } 00093 00094 // Joins a Wifi AP. Parameters: SSID and Password (strings) 00095 void ESP8266::Join(char * id, char * pwd) { 00096 char cmd[255]; 00097 strcpy(cmd, "AT+CWJAP="); 00098 AddChar(cmd, 0x22); 00099 strcat(cmd, id); 00100 AddChar(cmd, 0x22); 00101 AddChar(cmd, 0x2C); 00102 AddChar(cmd, 0x22); 00103 strcat(cmd, pwd); 00104 AddChar(cmd, 0x22); 00105 SendCMD(cmd); 00106 } 00107 00108 // Gets ESP IP. Parameter: string to contain IP 00109 void ESP8266::GetIP(char * ip) { 00110 char cmd[15]; 00111 strcpy(cmd, "AT+CIFSR"); 00112 SendCMD(cmd); 00113 RcvReply(ip, 2000); 00114 } 00115 00116 //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both 00117 void ESP8266::SetMode(char mode) { 00118 char cmd[15]; 00119 strcpy(cmd, "AT+CWMODE="); 00120 mode = mode + 0x30; // Converts number into corresponding ASCII character 00121 AddChar(cmd, mode); // Completes command string 00122 SendCMD(cmd); 00123 } 00124 00125 // Quits the AP 00126 void ESP8266::Quit(void) { 00127 char cmd[15]; 00128 strcpy(cmd, "AT+CWQAP"); 00129 SendCMD(cmd); 00130 } 00131 00132 // Sets single connection 00133 void ESP8266::SetSingle(void) { 00134 char cmd[15]; 00135 strcpy(cmd, "AT+CIPMUX=0"); 00136 SendCMD(cmd); 00137 } 00138 00139 // Sets multiple connection 00140 void ESP8266::SetMultiple(void) { 00141 char rs[15]; 00142 strcpy(rs, "AT+CIPMUX=1"); 00143 SendCMD(rs); 00144 } 00145 00146 // Gets connection status. Parameter: string to contain status 00147 void ESP8266::GetConnStatus(char * st) { 00148 char cmd[15]; 00149 strcpy(cmd, "AT+CIPSTATUS"); 00150 SendCMD(cmd); 00151 RcvReply(st, 2000); 00152 } 00153 00154 // Starts server mode. Parameter: port to be used 00155 void ESP8266::StartServerMode(int port) { 00156 char rs[25]; 00157 char t[4]; 00158 strcpy(rs, "AT+CIPSERVER=1,"); 00159 itoa(port, t); 00160 strcat(rs, t); 00161 SendCMD(rs); 00162 } 00163 00164 // Close server mode. 00165 void ESP8266::CloseServerMode(void) { 00166 char rs[20]; 00167 strcpy(rs, "AT+CIPSERVER=0"); 00168 SendCMD(rs); 00169 }
Generated on Sat Jul 16 2022 02:26:09 by
1.7.2
