ESP8266
Dependencies: mbed
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::Join(char * id, char * pwd) { 00095 char cmd[255]; 00096 strcpy(cmd, "AT+CWJAP="); 00097 AddChar(cmd, 0x22); 00098 strcat(cmd, id); 00099 AddChar(cmd, 0x22); 00100 AddChar(cmd, 0x2C); 00101 AddChar(cmd, 0x22); 00102 strcat(cmd, pwd); 00103 AddChar(cmd, 0x22); 00104 SendCMD(cmd); 00105 } 00106 00107 void ESP8266::look(void) { 00108 char rs[10]; 00109 strcpy(rs, "AT+CWSAP?"); 00110 SendCMD(rs); 00111 } 00112 00113 void ESP8266::GetIP(char * ip) { 00114 char cmd[15]; 00115 strcpy(cmd, "AT+CIFSR"); 00116 SendCMD(cmd); 00117 RcvReply(ip, 2000); 00118 } 00119 00120 void ESP8266::SetSingle(void) { 00121 char cmd[15]; 00122 strcpy(cmd, "AT+CIPMUX=0"); 00123 SendCMD(cmd); 00124 } 00125 00126 void ESP8266::SetPath(char * t, char * o, char * v) { 00127 char cmd[255]; 00128 strcpy(cmd, "AT+CIPSTART="); 00129 AddChar(cmd, 0x22); 00130 strcat(cmd, t); 00131 AddChar(cmd, 0x22); 00132 AddChar(cmd, 0x2C); 00133 AddChar(cmd, 0x22); 00134 strcat(cmd, o); 00135 AddChar(cmd, 0x22); 00136 AddChar(cmd, 0x2C); 00137 AddChar(cmd, 0x22); 00138 strcat(cmd, v); 00139 AddChar(cmd, 0x22); 00140 SendCMD(cmd); 00141 }
Generated on Fri Jul 22 2022 10:47:38 by
1.7.2