Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Barnabybear
#29699
zioax wrote:I'm tring to make a GET request to a php file running in a localhost machine, but without success.
This is the code:
Code: Select all/*
 *  This sketch sends data via HTTP GET requests to data.sparkfun.com service.
 *
 *  You need to get streamId and privateKey at data.sparkfun.com and paste them
 *  below. Or just customize this script to talk to other HTTP servers.
 *
 */

#include <ESP8266WiFi.h>

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

const char* host = "192.168.1.10";

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());
}



void loop() {
  delay(5000);
 
  Serial.print("connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  client.connect(host, httpPort);
 
  // We now create a URI for the request
  String url = "/wa/examples/wa1.php";


 

 
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +

               "Host: " + host + "\r\n" +
               "Accept: */*\r\n" +
               "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n" +
               "\r\n"
               
               );
  delay(100);
 
  // Read all the lines of the reply from server and print them to Serial

}

The code work well from the browser, so it isn't a server side issue


Hi, below is a setion of the code I use for a "POST" that works - I notice it only uses "\n" in quite a few lines that you use "\r\n".

Code: Select all void loop() {
    if (client.connect(server,80)) {  // connects to thingspeak & sends the value of (pir)
        String postStr = apiKey;
               postStr +="&field1=";
               postStr += String(pir);
               postStr += "\r\n\r\n";
         client.print("POST /update HTTP/1.1\n");
         client.print("Host: api.thingspeak.com\n");
         client.print("Connection: close\n");
         client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
         client.print("Content-Type: application/x-www-form-urlencoded\n");
         client.print("Content-Length: ");
         client.print(postStr.length());
         client.print("\n\n");
         client.print(postStr);


Hope that helps abit.
User avatar
By zioax
#29705 I need to do a GET request, POST isn't allowed. Anyway tried with many header configuration but without success, nothing reply in the Serial.