Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESP8266 WiFiClient simple HTTP GET

I'm working on simple problem of reading a webpage using ESP8266 and ESP8266WiFi library.

I changed only a few lines in example and don't know whats the problem. Thats my code:

include <ESP8266WiFi.h>

const char* ssid     = "WiwoNET";
const char* password = "xxxxxxx";

const char* host = "https://pure-caverns-1350.herokuapp.com";

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
  delay(5000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  
  // We now create a URI for the request
  String url = "/stan";
  
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  delay(10);
  
  // Read all the lines of the reply from server and print them to Serial
  Serial.println("Respond:");
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("closing connection");
}

And what I see in serial monitor is:

Connecting to WiwoNET
.......
WiFi connected
IP address: 
192.168.0.111
connecting to https://pure-caverns-1350.herokuapp.com
Requesting URL: /stan
Informacja zwrotna:
HTTP/1.1 400 Bad Request
Connection: close
Server: Cowboy
Date: Thu, 03 Dec 2015 23:38:59 GMT
Content-Length: 0


closing connection

I was looking at heroku's logs and nothing is showing there. Thank you in advance for any kind of help.

like image 764
wiwo Avatar asked Dec 03 '15 23:12

wiwo


People also ask

Does ESP8266 support HTTP?

With this example, your ESP8266 can make HTTP POST requests using three different types of body requests: URL encoded, JSON object or plain text. These are the most common methods and should integrate with most APIs or web services.


1 Answers

You must have been following the example at https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino

There's one crucial piece you missed, though. The host value must not be prepended with a scheme in URI-style such as http:// or https://. Look at the example again and use

const char* host = "pure-caverns-1350.herokuapp.com";

instead.

You can see very well what's going on under the hood of HTTP if run curl -v http://pure-caverns-1350.herokuapp.com/stan in your console.

like image 181
Marcel Stör Avatar answered Oct 24 '22 03:10

Marcel Stör