Kazuo Tsubaki
/
ESP-WROOM-02_test
Very simple example using ESP-WROOM-02 (ESP-8266) from LPC1114
Embed:
(wiki syntax)
Show/hide line numbers
ESP8266.cpp
00001 #include "ESP8266.h" 00002 00003 #define LOG_DEBUG(s) if(logger != NULL) { logger->printf("%s\r\n", s); }; 00004 00005 ESP8266::ESP8266(SoftSerial *s) { 00006 stream = s; 00007 } 00008 00009 ESP8266::~ESP8266() { 00010 } 00011 00012 void ESP8266::setLogger(Serial *l) { 00013 logger = l; 00014 } 00015 00016 bool ESP8266::_ok() { 00017 return (strstr(replybuffer, "OK\r\n") != NULL); 00018 } 00019 00020 bool ESP8266::nop() { 00021 sendRequest("AT\r\n"); 00022 timeout=20; 00023 getReply(); 00024 return _ok(); 00025 } 00026 00027 bool ESP8266::reset() { 00028 LOG_DEBUG("Reset & get Firmware"); 00029 sendRequest("AT+RST\r\n"); 00030 timeout=200; 00031 getReply(); 00032 LOG_DEBUG(replybuffer); 00033 return _ok(); 00034 } 00035 00036 void ESP8266::version() { 00037 LOG_DEBUG("Get Version"); 00038 sendRequest("AT+GMR\r\n"); 00039 timeout=200; 00040 getReply(); 00041 LOG_DEBUG(replybuffer); 00042 } 00043 00044 // 1=Station, 2=AP, 3=BOTH, default=Station 00045 bool ESP8266::mode(int mode) { 00046 char cmd[255]; 00047 sprintf(cmd, "AT+CWMODE=%d\r\n", mode); 00048 sendRequest(cmd); 00049 timeout=60; 00050 getReply(); 00051 LOG_DEBUG(replybuffer); 00052 return _ok(); 00053 } 00054 00055 // 0=Single, 1=Multi 00056 bool ESP8266::connectionMode(int connMode) { 00057 char cmd[255]; 00058 sprintf(cmd, "AT+CIPMUX=%d\r\n", connMode); 00059 sendRequest(cmd); 00060 timeout=80; 00061 getReply(); 00062 LOG_DEBUG(replybuffer); 00063 return _ok(); 00064 } 00065 00066 void ESP8266::connectionStatus() { 00067 LOG_DEBUG("Get Connection Status"); 00068 sendRequest("AT+CIPSTA?\r\n"); 00069 timeout=100; 00070 getReply(); 00071 LOG_DEBUG(replybuffer); 00072 } 00073 00074 bool ESP8266::config() { 00075 timeout=1000; getcount=600; 00076 wait(1); 00077 00078 if(!nop()) 00079 return false; 00080 wait(1); 00081 00082 version(); 00083 wait(3); 00084 00085 if(!mode(1)) 00086 return false; 00087 wait(2); 00088 00089 if(!connectionMode(1)) 00090 return false; 00091 wait(2); 00092 00093 return true; 00094 } 00095 00096 bool ESP8266::connect(char *ssid, char *password) { 00097 char cmd[256]; 00098 LOG_DEBUG("Connecting"); 00099 strcpy(cmd, "AT+CWJAP=\""); 00100 strcat(cmd, ssid); 00101 strcat(cmd, "\",\""); 00102 strcat(cmd, password); 00103 strcat(cmd, "\"\r\n"); 00104 LOG_DEBUG(cmd); 00105 sendRequest(cmd); 00106 timeout=10000; 00107 getReply(); 00108 00109 wait(5); 00110 00111 return _ok(); 00112 } 00113 00114 void ESP8266::sendRequest(char *req) { 00115 strcpy(cmdbuffer, req); 00116 sendCmd(); 00117 } 00118 00119 char *ESP8266::readResponse() { 00120 getReply(); 00121 return replybuffer; 00122 } 00123 00124 00125 void ESP8266::sendCmd() { 00126 stream->printf("%s", cmdbuffer); 00127 } 00128 00129 void ESP8266::getReply() { 00130 memset(replybuffer, '\0', sizeof(replybuffer)); 00131 replycount = 0; 00132 t.reset(); t.start(); 00133 while(t.read_ms() < timeout && replycount < getcount) { 00134 if(stream->readable()) { 00135 replybuffer[replycount] = stream->getc(); 00136 replycount++; 00137 } 00138 } 00139 } 00140
Generated on Thu Jul 14 2022 01:02:14 by 1.7.2