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: Gateway_Cotosys Gateway_Cotosys_os5
Revision 0:e58f27687450, committed 2014-12-16
- Comitter:
- quevedo
- Date:
- Tue Dec 16 01:08:47 2014 +0000
- Child:
- 1:399414d48048
- Commit message:
- First version, only basic commands
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 |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266.cpp Tue Dec 16 01:08:47 2014 +0000
@@ -0,0 +1,90 @@
+#include "ESP8266.h"
+
+// Constructor
+ESP8266::ESP8266(PinName tx, PinName rx) : comm(tx, rx) {
+ comm.baud(115200);
+}
+
+// Destructor
+ESP8266::~ESP8266() { }
+
+// Add <CR> + <LF> at the end of the string
+void ESP8266::AddEOL(char * s) {
+ char k;
+ k = strlen(s); // Finds position of NULL character
+ s[k] = 0x0D; // switch NULL for <CR>
+ s[k + 1] = 0x0A; // Add <LF>
+ s[k + 2] = 0; // Add NULL at the end
+}
+
+// Add one ASCII character at the end of the string
+void ESP8266::AddChar(char * s, char c) {
+ char k;
+ k = strlen(s);
+ s[k] = c;
+ s[k + 1] = 0;
+}
+
+// Sends command to ESP8266. Receives the command string
+void ESP8266::SendCMD(char * s) {
+ AddEOL(s);
+ comm.printf("%s", s);
+}
+
+// Resets the ESP8266
+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
+void ESP8266::RcvReply(char * r, int to) {
+ Timer t;
+ bool ended = 0;
+ char c;
+
+ strcpy(r, "");
+ t.start();
+ while(!ended) {
+ if(comm.readable()) {
+ c = comm.getc();
+ AddChar(r, c);
+ t.start();
+ }
+ if(t.read_ms() > to) {
+ ended = 1;
+ }
+ }
+ AddChar(r, 0x00);
+}
+
+// Gets the AP list. Parameter: the string to receive the list
+void ESP8266::GetList(char * l) {
+ char rs[15];
+ strcpy(rs, "AT+CWLAP");
+ SendCMD(rs);
+ RcvReply(l, 5000); // Needs big timeout because it takes long to start replying
+}
+
+// Joins a Wifi AP. Parameters: SSID and Password (strings)
+void ESP8266::Join(char * id, char * pwd) {
+ char cmd[255];
+ strcpy(cmd, "AT+CWJAP=");
+ AddChar(cmd, 0x22);
+ strcat(cmd, id);
+ AddChar(cmd, 0x22);
+ AddChar(cmd, 0x2C);
+ AddChar(cmd, 0x22);
+ strcat(cmd, pwd);
+ AddChar(cmd, 0x22);
+ SendCMD(cmd);
+}
+
+// Gets ESP IP. Parameter: string to contain IP
+void ESP8266::GetIP(char * ip) {
+ char cmd[15];
+ strcpy(cmd, "AT+CIFSR");
+ SendCMD(cmd);
+ RcvReply(ip, 2000);
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266.h Tue Dec 16 01:08:47 2014 +0000
@@ -0,0 +1,58 @@
+#ifndef ESP8266_H
+#define ESP8266_H
+
+#include <string>
+#include "mbed.h"
+
+class ESP8266
+{
+public:
+/**
+ * ESP8266 constructor
+ *
+ * @param tx TX pin
+ * @param rx RX pin
+ */
+ ESP8266(PinName tx, PinName rx);
+
+ /**
+ * ESP8266 destructor
+ */
+ ~ESP8266();
+
+void SendCMD(char * s);
+void Reset(void);
+void RcvReply(char * r, int to);
+void GetList(char * l);
+void Join(char * id, char * pwd);
+void GetIP(char * ip);
+
+private:
+Serial comm;
+void AddEOL(char * s);
+void AddChar(char * s, char c);
+
+};
+
+#endif
+/*
+ COMMAND TABLE
+ Basic:
+ AT: Just to generate "OK" reply
+ Wifi:
+ AT+RST: restart the module
+ AT+CWMODE: define wifi mode; AT+CWMODE=<mode> 1= Sta, 2= AP, 3=both; Inquiry: AT+CWMODE? or AT+CWMODE=?
+ 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?
+ 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+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
+*/
\ No newline at end of file