bool EspDrv::wifiStartAP(char* ssid, const char* pwd, uint8_t channel, uint8_t enc) { LOGDEBUG(F("> wifiStartAP")); // set AP mode, use CUR mode to avoid starting the AP at reboot if (sendCmd(F("AT+CWMODE_CUR=2"))!=TAG_OK) { LOGWARN1(F("Failed to set AP mode"), ssid); return false; } // TODO // Escape character syntax is needed if "SSID" or "password" contains // any special characters (',', '"' and '/') // start access point int ret = sendCmd(F("AT+CWSAP_CUR=\"%s\",\"%s\",%d,%d"), 10000, ssid, pwd, channel, enc); if (ret!=TAG_OK) { LOGWARN1(F("Failed to start AP"), ssid); return false; } // enable DHCP for AP mode sendCmd(F("AT+CWDHCP_CUR=0,1")); LOGINFO1(F("Access point started"), ssid); return true; }
void EspDrv::wifiDriverInit(Stream *espSerial) { LOGDEBUG(F("> wifiDriverInit")); EspDrv::espSerial = espSerial; if (sendCmd(F("AT")) != TAG_OK) { LOGERROR(F("Cannot initialize ESP module")); delay(8000); return; } reset(); // check firmware version getFwVersion(); // prints a warning message if the firmware is not 1.X if (fwVersion[0] != '1' or fwVersion[1] != '.') { LOGWARN1(F("Warning: Unsupported firmware"), fwVersion); delay(4000); } else { LOGINFO1(F("Initilization successful -"), fwVersion); } }
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; }
int WiFiEspUDP::read(unsigned char* buffer, size_t len) { int bytes = available(); if(bytes==-1) return -1; if (bytes!=len) LOGWARN1(bytes, len); uint16_t size = 0; if (!EspDrv::getDataBuf(_sock, buffer, &size)) return -1; return size; }
static int firestep_readchar(int c) { switch (c) { case EOF: inbuf[inbuflen] = 0; inbuflen = 0; LOGERROR1("firestep_readchar %s[EOF]", inbuf); return 0; case '\n': inbuf[inbuflen] = 0; if (inbuflen) { // discard blank lines if (strncmp("{\"sr\"",inbuf, 5) == 0) { LOGDEBUG2("firestep_readchar %s (%dB)", inbuf, inbuflen); } else { LOGINFO2("firestep_readchar %s (%dB)", inbuf, inbuflen); } } else { inbufEmptyLine++; if (inbufEmptyLine % 1000 == 0) { LOGWARN1("firestep_readchar skipped %ld blank lines", inbufEmptyLine); } } inbuflen = 0; break; case '\r': // skip break; case 'a': case 'A': case 'b': case 'B': case 'c': case 'C': case 'd': case 'D': case 'e': case 'E': case 'f': case 'F': case 'g': case 'G': case 'h': case 'H': case 'i': case 'I': case 'j': case 'J': case 'k': case 'K': case 'l': case 'L': case 'm': case 'M': case 'n': case 'N': case 'o': case 'O': case 'p': case 'P': case 'q': case 'Q': case 'r': case 'R': case 's': case 'S': case 't': case 'T': case 'u': case 'U': case 'v': case 'V': case 'w': case 'W': case 'x': case 'X': case 'y': case 'Y': case 'z': case 'Z': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.': case '-': case '_': case '/': case '{': case '}': case '(': case ')': case '[': case ']': case '<': case '>': case '"': case '\'': case ':': case ',': case ' ': case '\t': if (c == '{') { if (jsonDepth++ <= 0) { jsonLen = 0; } ADD_JSON(c); } else if (c == '}') { ADD_JSON(c); if (--jsonDepth < 0) { LOGWARN1("Invalid JSON %s", jsonBuf); return 0; } } else { ADD_JSON(c); } if (inbuflen >= INBUFMAX) { inbuf[INBUFMAX] = 0; LOGERROR1("firestep_readchar overflow %s", inbuf); break; } else { inbuf[inbuflen] = c; inbuflen++; LOGTRACE2("firestep_readchar %x %c", (int) c, (int) c); } break; default: // discard unexpected character (probably wrong baud rate) LOGTRACE2("firestep_readchar %x ?", (int) c, (int) c); break; } return 1; }