bool EspDrv::wifiConnect(char* ssid, const char *passphrase) { LOGDEBUG(F("> wifiConnect")); // TODO // Escape character syntax is needed if "SSID" or "password" contains // any special characters (',', '"' and '/') // connect to access point int ret = sendCmd(F("AT+CWJAP_CUR=\"%s\",\"%s\""), 20000, ssid, passphrase); if (ret==TAG_OK) { LOGINFO1(F("Connected to"), ssid); return true; } LOGWARN1(F("Failed to connected to"), ssid); // clean additional messages logged after the FAIL tag delay(1000); espEmptyBuf(false); return false; }
void EspDrv::reset() { LOGDEBUG(F("> reset")); sendCmd(F("AT+RST")); delay(3000); espEmptyBuf(false); // empty dirty characters from the buffer // disable echo of commands sendCmd(F("ATE0")); // set station mode sendCmd(F("AT+CWMODE=1")); // set multiple connections mode sendCmd(F("AT+CIPMUX=1")); // Show remote IP and port with "+IPD" sendCmd(F("AT+CIPDINFO=1")); // Disable autoconnect // Automatic connection can create problems during initialization phase at next boot sendCmd(F("AT+CWAUTOCONN=0")); // enable DHCP sendCmd(F("AT+CWDHCP=1,1")); }
/* * Sends the AT command and stops if any of the TAGS is found. * Extract the string enclosed in the passed tags and returns it in the outStr buffer. * Returns true if the string is extracted, false if tags are not found of timed out. */ bool EspDrv::sendCmdGet(const __FlashStringHelper* cmd, const char* startTag, const char* endTag, char* outStr, int outStrLen) { int idx; bool ret = false; outStr[0] = 0; espEmptyBuf(); LOGDEBUG(F("----------------------------------------------")); LOGDEBUG1(F(">>"), cmd); // send AT command to ESP espSerial->println(cmd); // read result until the startTag is found idx = readUntil(1000, startTag); if(idx==NUMESPTAGS) { // clean the buffer to get a clean string ringBuf.init(); // start tag found, search the endTag idx = readUntil(500, endTag); if(idx==NUMESPTAGS) { // end tag found // copy result to output buffer avoiding overflow ringBuf.getStrN(outStr, strlen(endTag), outStrLen-1); // read the remaining part of the response readUntil(2000); ret = true; } else { LOGWARN(F("End tag not found")); } } else if(idx>=0 and idx<NUMESPTAGS) { // the command has returned but no start tag is found LOGDEBUG1(F("No start tag found:"), idx); } else { // the command has returned but no tag is found LOGWARN(F("No tag found")); } LOGDEBUG1(F("---------------------------------------------- >"), outStr); LOGDEBUG(); return ret; }
uint8_t EspDrv::getScanNetworks() { uint8_t ssidListNum = 0; int idx; bool ret = false; espEmptyBuf(); LOGDEBUG(F("----------------------------------------------")); LOGDEBUG(F(">> AT+CWLAP")); espSerial->println(F("AT+CWLAP")); char buf[100]; idx = readUntil(10000, "+CWLAP:("); while (idx == NUMESPTAGS) { _networkEncr[ssidListNum] = espSerial->parseInt(); //LOGDEBUG1("enc:", _networkEncr[ssidListNum]); // discard , and " characters readUntil(1000, "\""); idx = readUntil(1000, "\"", false); if(idx==NUMESPTAGS) { ringBuf.getStr(_networkSsid[ssidListNum], 1); // 1 = strlen ("\"") } //LOGDEBUG1("ssid:", _networkSsid[ssidListNum]); // discard , character readUntil(1000, ","); _networkRssi[ssidListNum] = espSerial->parseInt(); //LOGDEBUG1("rssi:", _networkRssi[ssidListNum]); idx = readUntil(1000, "+CWLAP:("); if(ssidListNum==WL_NETWORKS_LIST_MAXNUM-1) break; ssidListNum++; } if (idx==-1) return -1; LOGDEBUG1(F("---------------------------------------------- >"), ssidListNum); LOGDEBUG(); return ssidListNum; }
int8_t EspDrv::disconnect() { LOGDEBUG(F("> disconnect")); if(sendCmd(F("AT+CWQAP"))==TAG_OK) return WL_DISCONNECTED; // wait and clear any additional message delay(2000); espEmptyBuf(false); return WL_DISCONNECTED; }
/* * Sends the AT command and returns the id of the TAG. * Return -1 if no tag is found. */ int EspDrv::sendCmd(const __FlashStringHelper* cmd, int timeout) { espEmptyBuf(); LOGDEBUG(F("----------------------------------------------")); LOGDEBUG1(F(">>"), cmd); espSerial->println(cmd); int idx = readUntil(timeout); LOGDEBUG1(F("---------------------------------------------- >"), idx); LOGDEBUG(); return idx; }
void EspDrv::reset() { LOGDEBUG(F("> reset")); sendCmd(F("AT+RST")); delay(3000); espEmptyBuf(false); // empty dirty characters from the buffer // disable echo of commands sendCmd(F("ATE0")); // set station mode sendCmd(F("AT+CWMODE=1")); // set multiple connections mode sendCmd(F("AT+CIPMUX=1")); // enable DHCP sendCmd(F("AT+CWDHCP=1,1")); }
/* * Sends the AT command and returns the id of the TAG. * The additional arguments are formatted into the command using sprintf. * Return -1 if no tag is found. */ int EspDrv::sendCmd(const __FlashStringHelper* cmd, int timeout, ...) { char cmdBuf[CMD_BUFFER_SIZE]; va_list args; va_start (args, timeout); vsnprintf_P (cmdBuf, CMD_BUFFER_SIZE, (char*)cmd, args); va_end (args); espEmptyBuf(); LOGDEBUG(F("----------------------------------------------")); LOGDEBUG1(F(">>"), cmdBuf); espSerial->println(cmdBuf); int idx = readUntil(timeout); LOGDEBUG1(F("---------------------------------------------- >"), idx); LOGDEBUG(); return idx; }