Library for using the Wifi module ESP8266, first version with basic commands, soon new commands will be added
Dependents: PIR_Sensor_wifi myESP8266forLOstFound Motin_Activated_Temperature_Humidity_BluetoothHC-06_ESP8266_IOT 1Project3_Robot1_Final ... more
Library updated in Dec 28, 2014. Now the ESP baudrate is passed as a parameter in the constructor. Also, some new functions were added. Here is an example code:
ESP8266 example code
#include "mbed.h"
#include <string>
#include "ESP8266.h"
// Objects
Serial pc(USBTX, USBRX);
ESP8266 esp(PTE0, PTE1, 115200);
// Global variables
char snd[255], rcv[1000]; // Strings for sending and receiving commands / data / replies
int main() {
pc.baud(115200);
pc.printf("START\r\n");
wait(3);
pc.printf("Reset ESP\r\n");
esp.Reset();
esp.RcvReply(rcv, 400);
pc.printf("%s", rcv);
wait(2);
pc.printf("Sending AT\r\n");
strcpy(snd, "AT");
esp.SendCMD(snd);
esp.RcvReply(rcv, 400);
pc.printf("%s", rcv);
wait(2);
pc.printf("Set mode to AP\r\n");
esp.SetMode(1);
esp.RcvReply(rcv, 1000);
pc.printf("%s", rcv);
pc.printf("Receiving Wifi List\r\n");
esp.GetList(rcv);
pc.printf("%s", rcv);
pc.printf("Connecting to AP\r\n");
esp.Join("MyAP", "MyPasswd"); // Replace MyAP and MyPasswd with your SSID and password
esp.RcvReply(rcv, 1000);
pc.printf("%s", rcv);
wait(8);
pc.printf("Getting IP\r\n");
esp.GetIP(rcv);
pc.printf("%s", rcv);
pc.printf("Setting multiple connections\r\n");
esp.SetMultiple();
esp.RcvReply(rcv, 1000);
pc.printf("%s", rcv);
pc.printf("Getting Connection Status\r\n");
esp.GetConnStatus(rcv);
pc.printf("%s", rcv);
pc.printf("Start server mode on port 80\r\n");
esp.StartServerMode(80);
esp.RcvReply(rcv, 1000);
pc.printf("%s", rcv);
wait(4);
pc.printf("Close server mode\r\n");
esp.CloseServerMode();
esp.RcvReply(rcv, 1000);
pc.printf("THE END");
while(1);
}
Revision 1:399414d48048, committed 2014-12-17
- Comitter:
- quevedo
- Date:
- Wed Dec 17 13:54:34 2014 +0000
- Parent:
- 0:e58f27687450
- Child:
- 2:77388e8f0697
- Commit message:
- Added functions: SetMode (STA, AP, or both), Quit(quits the AP connection), and SetSingle / SetMultiple for single and multiple connections
Changed in this revision
| ESP8266.cpp | Show annotated file Show diff for this revision Revisions of this file |
| ESP8266.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/ESP8266.cpp Tue Dec 16 01:08:47 2014 +0000
+++ b/ESP8266.cpp Wed Dec 17 13:54:34 2014 +0000
@@ -87,4 +87,34 @@
strcpy(cmd, "AT+CIFSR");
SendCMD(cmd);
RcvReply(ip, 2000);
+}
+
+//Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both
+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);
+}
+
+// Quits the AP
+void ESP8266::Quit(void) {
+ char cmd[15];
+ strcpy(cmd, "AT+CWQAP");
+ SendCMD(cmd);
+}
+
+// Sets single connection
+void ESP8266::SetSingle(void) {
+ char cmd[15];
+ strcpy(cmd, "AT+CIPMUX=0");
+ SendCMD(cmd);
+}
+
+// Sets multiple connection
+void ESP8266::SetMultiple(void) {
+ char rs[15];
+ strcpy(rs, "AT+CIPMUX=1");
+ SendCMD(rs);
}
\ No newline at end of file
--- a/ESP8266.h Tue Dec 16 01:08:47 2014 +0000
+++ b/ESP8266.h Wed Dec 17 13:54:34 2014 +0000
@@ -26,6 +26,10 @@
void GetList(char * l);
void Join(char * id, char * pwd);
void GetIP(char * ip);
+void SetMode(char mode);
+void Quit(void);
+void SetSingle(void);
+void SetMultiple(void);
private:
Serial comm;
@@ -45,14 +49,14 @@
AT+CWJAP: join the AP wifi; AT+ CWJAP =<ssid>,< pwd > - ssid = ssid, pwd = wifi password, both between quotes; Inquiry: AT+ CWJAP?
AT+CWLAP: list the AP wifi
AT+CWQAP: quit the AP wifi; Inquiry: AT+CWQAP=?
- AT+CWSAP: set the parameters of AP; AT+CWSAP= <ssid>,<pwd>,<chl>,<ecn> - ssid, pwd, chl = channel, ecn = encryption; Inquiry: AT+CWJAP?
+ * AT+CWSAP: set the parameters of AP; AT+CWSAP= <ssid>,<pwd>,<chl>,<ecn> - ssid, pwd, chl = channel, ecn = encryption; Inquiry: AT+CWJAP?
TCP/IP:
- AT+CIPSTATUS: get the connection status
- AT+CIPSTART: set up TCP or UDP connection 1)single connection (+CIPMUX=0) AT+CIPSTART= <type>,<addr>,<port>; 2) multiple connection (+CIPMUX=1) AT+CIPSTART= <id><type>,<addr>, <port> - id = 0-4, type = TCP/UDP, addr = IP address, port= port; Inquiry: AT+CIPSTART=?
- AT+CIPSEND: send data; 1)single connection(+CIPMUX=0) AT+CIPSEND=<length>; 2) multiple connection (+CIPMUX=1) AT+CIPSEND= <id>,<length>; Inquiry: AT+CIPSEND=?
- AT+CIPCLOSE: close TCP or UDP connection; AT+CIPCLOSE=<id> or AT+CIPCLOSE; Inquiry: AT+CIPCLOSE=?
+ * AT+CIPSTATUS: get the connection status
+ * AT+CIPSTART: set up TCP or UDP connection 1)single connection (+CIPMUX=0) AT+CIPSTART= <type>,<addr>,<port>; 2) multiple connection (+CIPMUX=1) AT+CIPSTART= <id><type>,<addr>, <port> - id = 0-4, type = TCP/UDP, addr = IP address, port= port; Inquiry: AT+CIPSTART=?
+ * AT+CIPSEND: send data; 1)single connection(+CIPMUX=0) AT+CIPSEND=<length>; 2) multiple connection (+CIPMUX=1) AT+CIPSEND= <id>,<length>; Inquiry: AT+CIPSEND=?
+ * AT+CIPCLOSE: close TCP or UDP connection; AT+CIPCLOSE=<id> or AT+CIPCLOSE; Inquiry: AT+CIPCLOSE=?
AT+CIFSR: Get IP address; Inquiry: AT+ CIFSR=?
AT+CIPMUX: set mutiple connection; AT+ CIPMUX=<mode> - 0 for single connection 1 for mutiple connection; Inquiry: AT+CIPMUX?
- AT+CIPSERVER: set as server; AT+ CIPSERVER= <mode>[,<port> ] - mode 0 to close server mode, mode 1 to open; port = port; Inquiry: AT+CIFSR=?
- +IPD: received data
+ * AT+CIPSERVER: set as server; AT+ CIPSERVER= <mode>[,<port> ] - mode 0 to close server mode, mode 1 to open; port = port; Inquiry: AT+CIFSR=?
+ * +IPD: received data
*/
\ No newline at end of file
Antonio Quevedo