Exemple #1
0
ESP8266::ESP8266(PinName tx, PinName rx, PinName reset, const char *ssid, const char *phrase, uint32_t baud) :
    wifi(tx, rx), reset_pin(reset), buf_ESP8266(ESP_MBUFFE_MAX)
{
    INFO("Initializing ESP8266 object");
    memset(&state, 0, sizeof(state));


    strcpy(this->ssid, ssid);
    strcpy(this->phrase, phrase);
    inst = this;
    attach_rx(false);

    wifi.baud(baud); // initial baud rate of the ESP8266

    state.associated = false;
    state.cmdMode = false;
}
Wifly::Wifly(   PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec):
    wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(512)
{
    this->wpa = wpa;
    security = sec;

    // change all ' ' in '$' in the ssid and the passphrase
    strcpy(this->ssid, ssid);
    for (int i = 0; i < strlen(ssid); i++) {
        if (this->ssid[i] == ' ')
            this->ssid[i] = '$';
    }
    strcpy(this->phrase, phrase);
    for (int i = 0; i < strlen(phrase); i++) {
        if (this->phrase[i] == ' ')
            this->phrase[i] = '$';
    }

    inst = this;
    attach_rx(false);
    cmd_mode = false;
}
int Wifly::send(const char * str, int len, const char * ACK, char * res, int timeout)
{
    char read;
    size_t found = string::npos;
    string checking;
    Timer tmr;
    int result = 0;

    DBG("will send: %s\r\n",str);

    attach_rx(false);

    //We flush the buffer
    while (wifi.readable())
        wifi.getc();

    if (!ACK || !strcmp(ACK, "NO")) {
        for (int i = 0; i < len; i++)
            result = (putc(str[i]) == str[i]) ? result + 1 : result;
    } else {
        //We flush the buffer
        while (wifi.readable())
            wifi.getc();

        tmr.start();
        for (int i = 0; i < len; i++)
            result = (putc(str[i]) == str[i]) ? result + 1 : result;

        while (1) {
            if (tmr.read_ms() > timeout) {
                //We flush the buffer
                while (wifi.readable())
                    wifi.getc();

                DBG("check: %s\r\n", checking.c_str());

                attach_rx(true);
                return -1;
            } else if (wifi.readable()) {
                read = wifi.getc();
                if ( read != '\r' && read != '\n') {
                    checking += read;
                    found = checking.find(ACK);
                    if (found != string::npos) {
                        wait(0.01);

                        //We flush the buffer
                        while (wifi.readable())
                            wifi.getc();

                        break;
                    }
                }
            }
        }
        DBG("check: %s\r\n", checking.c_str());

        attach_rx(true);
        return result;
    }

    //the user wants the result from the command (ACK == NULL, res != NULL)
    if ( res != NULL) {
        int i = 0;
        Timer timeout;
        timeout.start();
        tmr.reset();
        while (1) {
            if (timeout.read() > 2) {
                if (i == 0) {
                    res = NULL;
                    break;
                }
                res[i] = '\0';
                DBG("user str 1: %s\r\n", res);

                break;
            } else {
                if (tmr.read_ms() > 300) {
                    res[i] = '\0';
                    DBG("user str: %s\r\n", res);

                    break;
                }
                if (wifi.readable()) {
                    tmr.start();
                    read = wifi.getc();

                    // we drop \r and \n
                    if ( read != '\r' && read != '\n') {
                        res[i++] = read;
                    }
                }
            }
        }
        DBG("user str: %s\r\n", res);
    }

    //We flush the buffer
    while (wifi.readable())
        wifi.getc();

    attach_rx(true);
    DBG("result: %d\r\n", result)
    return result;
}
Exemple #4
0
bool ESP8266::sendCommand(const char * cmd, const char * ACK, char * res, int timeout)
{
    char read;
    size_t found = string::npos;
    string checking = "";
    Timer tmr;
    int result = 0;

    DBG("sendCmd:\t %s",cmd);

    attach_rx(true);

    //We flush the buffer
    while (readable())
        getc();

    if (!ACK || !strcmp(ACK, "NO")) {
        for (int i = 0; i < strlen(cmd); i++) {
            result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
            wait(0.005f); // prevents stuck recieve ready (?) need to let echoed character get read first
        }
        putc(13); //CR
        wait(0.005f); // wait for echo
        putc(10); //LF

    } else {
        //We flush the buffer
        while (readable())
            getc();

        tmr.start();
        for (int i = 0; i < strlen(cmd); i++) {
            result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
            wait(.005); // wait for echo
        }
        putc(13); //CR
        wait(0.005f); // wait for echo
        putc(10); //LF

        while (1) {
            if (tmr.read_ms() > timeout) {
                //We flush the buffer
                while (readable())
                    getc();

                DBG("check:\t %s", checking.c_str());

                attach_rx(true);
                return -1;
            } else if (readable()) {
                read = getc();
                //printf("%c",read); //debug echo
                if ( read != '\r' && read != '\n') {
                    checking += read;
                    found = checking.find(ACK);
                    if (found != string::npos) {
                        wait(0.01f);

                        //We flush the buffer
                        while (readable())
                            read = getc();
                        //printf("%c",read); //debug echo
                        break;
                    }
                }
            }
        }
        DBG("check: %s", checking.c_str());

        attach_rx(true);
        return result;
    }

    //the user wants the result from the command (ACK == NULL, res != NULL)
    if (res != NULL) {
        int i = 0;
        Timer timeout;
        timeout.start();
        tmr.reset();
        while (1) {
            if (timeout.read() > 2) {
                if (i == 0) {
                    res = NULL;
                    break;
                }
                res[i] = '\0';
                DBG("user str 1: %s", res);

                break;
            } else {
                if (tmr.read_ms() > 300) {
                    res[i] = '\0';
                    DBG("user str: %s", res);

                    break;
                }
                if (readable()) {
                    tmr.start();
                    read = getc();

                    // we drop \r and \n
                    if ( read != '\r' && read != '\n') {
                        res[i++] = read;
                    }
                }
            }
        }
        DBG("user str: %s", res);
    }

    //We flush the buffer
    while (readable())
        getc();

    attach_rx(true);
    DBG("result: %d", result)
    return result;
}