Connect Wi-Fi

Dependencies:   mbed

Fork of Server-ESP8266 by Digital dog

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 void ESP8266::AddEOL(char * s) {
00012     char k;
00013     k = strlen(s); // Finds position of NULL character
00014     s[k] = 0x0D; // switch NULL for <CR>
00015     s[k + 1] = 0x0A; // Add <LF>
00016     s[k + 2] = 0; // Add NULL at the end
00017 }
00018 
00019 void ESP8266::AddChar(char * s, char c) {
00020     char k;
00021     k = strlen(s);
00022     s[k] = c;
00023     s[k + 1] = 0;
00024 }
00025 
00026 void ESP8266::itoa(int n, char * s) {
00027     char k = 0;
00028     char r[11];
00029     
00030     if(n == 0) {
00031         s[0] = '0';
00032         s[1] = 0;
00033     } else {
00034         while(n != 0) {
00035             r[k]= (n % 10) + '0';
00036             n = n / 10;
00037             k++;
00038         }
00039         while(k > 0) {
00040             s[n] = r[k - 1] + '0';
00041             n++;
00042             k--;
00043         }
00044         s[n] = 0;
00045     }
00046 }
00047 
00048 void ESP8266::SendCMD(char * s) {
00049     AddEOL(s);
00050     comm.printf("%s", s);
00051 }
00052 
00053 void ESP8266::Reset(void) {
00054     char rs[10];
00055     strcpy(rs, "AT+RST");
00056     SendCMD(rs);
00057 }
00058 
00059 void ESP8266::RcvReply(char * r, int to) {
00060     Timer t;
00061     bool ended = 0;
00062     char c;
00063     
00064     strcpy(r, "");
00065     t.start();
00066     while(!ended) {
00067         if(comm.readable()) {
00068             c = comm.getc();
00069             AddChar(r, c);
00070             t.start();
00071         }
00072         if(t.read_ms() > to) {
00073                 ended = 1;
00074         }
00075     }
00076     AddChar(r, 0x00);
00077 }
00078 
00079 void ESP8266::SetMode(char mode) {
00080     char cmd[15];
00081     strcpy(cmd, "AT+CWMODE=");
00082     mode = mode + 0x30; // Converts number into corresponding ASCII character
00083     AddChar(cmd, mode); // Completes command string
00084     SendCMD(cmd);
00085 }
00086 
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 void ESP8266::SetWiFi(char * name, char * ad) {
00095     char cmd[255];
00096     strcpy(cmd, "AT+CWSAP=");
00097     AddChar(cmd, 0x22);
00098     strcat(cmd, name);
00099     AddChar(cmd, 0x22);
00100     AddChar(cmd, 0x2C);
00101     AddChar(cmd, 0x22);
00102     strcat(cmd, ad);
00103     AddChar(cmd, 0x22);
00104     AddChar(cmd, 0x2C);
00105     AddChar(cmd, 0x35);
00106     AddChar(cmd, 0x2C);
00107     AddChar(cmd, 0x33);
00108     SendCMD(cmd);
00109 }
00110 
00111 void ESP8266::Join(char * id, char * pwd) {
00112     char cmd[255];
00113     strcpy(cmd, "AT+CWJAP=");
00114     AddChar(cmd, 0x22);
00115     strcat(cmd, id);
00116     AddChar(cmd, 0x22);
00117     AddChar(cmd, 0x2C);
00118     AddChar(cmd, 0x22);
00119     strcat(cmd, pwd);
00120     AddChar(cmd, 0x22);
00121     SendCMD(cmd);
00122 }
00123 
00124 void ESP8266::check(void) {
00125     char rs[10];
00126     strcpy(rs, "AT+CWJAP?");
00127     SendCMD(rs);
00128 }
00129 
00130 void ESP8266::GetIP(char * ip) {
00131     char cmd[15];
00132     strcpy(cmd, "AT+CIFSR");
00133     SendCMD(cmd);
00134     RcvReply(ip, 2000);
00135 }
00136 
00137 void ESP8266::SetMultiple(void) {
00138     char rs[15];
00139     strcpy(rs, "AT+CIPMUX=1");
00140     SendCMD(rs);
00141 }
00142 
00143 void ESP8266::SetSingle(void) {
00144     char cmd[15];
00145     strcpy(cmd, "AT+CIPMUX=0");
00146     SendCMD(cmd);
00147 }
00148 
00149 void ESP8266::Recieve(int hh) {
00150     char rs[25];
00151     char t[4];
00152     strcpy(rs, "AT+CIPSEND=0,");
00153     itoa(hh, t);
00154     strcat(rs, t);
00155     SendCMD(rs);
00156 }
00157 
00158 void ESP8266::StartServerMode(void) {
00159     char cmd[255];
00160     strcpy(cmd, "AT+CIPSERVER=");
00161     AddChar(cmd, 0x31);
00162     AddChar(cmd, 0x2C);
00163     AddChar(cmd, 0x38);
00164     AddChar(cmd, 0x30);
00165     SendCMD(cmd);
00166 }
00167 
00168 void ESP8266::SetTransfer(void) {
00169     char rs[15];
00170     strcpy(rs, "AT+CIPMODE=0");
00171     SendCMD(rs);
00172 }
00173 
00174 void ESP8266::GetCurrent(void) {
00175     char rs[10];
00176     strcpy(rs, "AT+CIPSTO?");
00177     SendCMD(rs);
00178 }
00179 
00180 void ESP8266::SetTime(char time) {
00181     char cmd[15];
00182     strcpy(cmd, "AT+CIPSTO=");
00183     time = time + 0x30; // Converts number into corresponding ASCII character
00184     AddChar(cmd, time); // Completes command string
00185     SendCMD(cmd);
00186 }
00187 
00188 void ESP8266::SetPath(char * t, char * o, char * v) {
00189     char cmd[255];
00190     strcpy(cmd, "AT+CIPSTART=1,");
00191     AddChar(cmd, 0x22);
00192     strcat(cmd, t);
00193     AddChar(cmd, 0x22);
00194     AddChar(cmd, 0x2C);
00195     AddChar(cmd, 0x22);
00196     strcat(cmd, o);
00197     AddChar(cmd, 0x22);
00198     AddChar(cmd, 0x2C);
00199     AddChar(cmd, 0x22);
00200     strcat(cmd, v);
00201     AddChar(cmd, 0x22);
00202     SendCMD(cmd);
00203 }
00204 
00205 void ESP8266::CheckIP(void) {
00206     char rs[10];
00207     strcpy(rs, "AT+CWLIF");
00208     SendCMD(rs);
00209 }