itead / ITEADLIB_Arduino_WeeESP8266

An easy-to-use Arduino ESP8266 library besed on AT firmware.
MIT License
526 stars 285 forks source link

Skip HTTP Header from GET request #72

Open LuizHAP opened 8 years ago

LuizHAP commented 8 years ago

Hi Guys,

I need help with my project. Here is my code:

`#include

//DADOS DO WIFI

define WIFI_SSID "SSID"

define WIFI_PSK "123456"

define HOST_NAME "HOST"

define HOST_PORT (80)

ESP8266 wifi(Serial1);

//OUTPUT const int LED_RESPOSTA = 13;

//ENDEREÇO NODE.JS const char http_site[] = "HOST"; const int http_port = 80;

//TRATANDO O BUFFER char recebido; char tratado; char c;

void setup() { //Inicializando serial do Arduino e do ESP82266 para conexão ao WebPage Serial.begin(9600); Serial1.begin(9600); //BaudRate padrão do ESP8266 Serial.print("Iniciando Setup\r\n");

//LED para Debug pinMode(LED_RESPOSTA, OUTPUT);

//CONECTAR WIFI CONFORME CONFIGURAÇÕES conectarWifi(); }

void loop() { ConexaoServer(); }

void conectarWifi() { Serial.println(wifi.getVersion().c_str()); if (wifi.setOprToStationSoftAP()) { Serial.print("Configuracao StationSoft OK!\r\n"); } else { Serial.print("Configuracao StationSoft ERRO\r\n"); } if (wifi.joinAP(WIFI_SSID, WIFI_PSK)) { Serial.print("Conectado ao WIFI com sucesso!\r\n"); Serial.print("IP:"); Serial.println( wifi.getLocalIP().c_str());
} else { Serial.print("Não conectou ao WIFI, refazer as configuracoes\r\n"); }

if (wifi.disableMUX()) {
    Serial.print("Unica conexao OK!\r\n");
} else {
    Serial.print("Unica conexao ERRO\r\n");
}
Serial.print("SETUP Finalizado!\r\n");

}

void ConexaoServer(){ uint8_t buffer[1024] = {0};

if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
    Serial.print("Conexao com o Host OK!\r\n");
} else {
    Serial.print("Conexao com o Host com ERRO!\r\n");
}

char *ComandoGET = "GET /teste HTTP/1.1\r\nHost: SmartHome\r\nConnection: close\r\n\r\n";
wifi.send((const uint8_t*)ComandoGET, strlen(ComandoGET));

uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
if (len > 0) {
    Serial.print("Received:[");
    for(uint32_t i = 0; i < len; i++) {
        Serial.print((char)buffer[i]);
    }
    Serial.print("]\r\n\r\n");
}

}`

And i receive from Node.js

`Received:[HTTP/1.1 200 OK X-Powered-By: Express Content-Type: text/html; charset=utf-8 Content-Length: 4 ETag: W/"4-wArb1VtkpsN6In8g50pGNw" Date: Wed, 31 Aug 2016 01:26:41 GMT Connection: close

Luiz]`

But i want to receive just "Received:[Luiz]". How i can do this?

Thanx!

LuizHAP commented 8 years ago

I change my code like below:

wifi.send((const uint8_t*)ComandoGET, strlen(ComandoGET));

    uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
    char *resposta = buffer;
    for (i = 0; i < len; i++) 
    {
        if (strncmp(resposta++, "\r\n\r\n", 4) == 0) break;
    }
    resposta += 3;

Its really works for me, but when i apply a decision with the variable "resposta", just get stucked. Code:

String tratado = resposta;
    if (tratado="OFF")
    {
      Serial.print("OFF");
    }
    else
    {
      Serial.print("ON");
    }
    tratado = "";

My response for GET Request its only a OFF now, even sending a string "ON" to a "/teste". Its a Buffer stuck?