Go Down

Topic: Topic: Problem sending a HTTP POST (Read 76 times) previous topic - next topic

JaqueCaamal

I am trying to send data to an HTTP page but I get a watchdog error.

Code: [Select]

#include <ESP8266WiFi.h>

const char* ssid = "---"; // Enter SSID here
const char* password = "---"; // Enter Password here
char server[] = "requestb.in";
String PostData = "Hello World";

WiFiClient client;

void setup() {
  Serial.begin(9600);
  delay(1000);

  Serial.println("Connecting to ");
  Serial.println(ssid);

  //connect to your local wi-fi network
  WiFi.begin(ssid, password);

  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
   if (client.connect(server, 80)) {
    // Make a HTTP request:
    Serial.println("connected");
  client.println("POST /1ip84q91 HTTP/1.1");
  client.println("Host:  requestb.in");
  client.println("User-Agent: Arduino/1.0");
  client.println("Connection: close");
  client.println("Content-Type: application/x-www-form-urlencoded");
  client.print("Content-Length: ");
  client.println(PostData.length());
  client.println();
  client.println(PostData);
  }
 
 
 
 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
 
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}




It connects to the network but doesn't do the POST, what am I doing wrong? Thank you.

I'm using
Arduino IDE 1.6.4
NodeMCU with ESP8266


This is a test to be able to integrate it into a code that I am working on.

PaulS

Code: [Select]
    // do nothing forevermore:
    while (true);

Well, that will certainly trigger a watchdog exception.

Move your post code into setup(). Leave loop() empty. Do NOT try to stop loop() from looping.
The art of getting good answers lies in asking good questions.

pylon

Quote
It connects to the network but doesn't do the POST, what am I doing wrong?
How do you know that? Have you seen the server log? I'd guess that it produces an error on the server because you say that the content is of type 'application/x-www-form-urlencoded' but what you send isn't in that format.

Does that WiFi network has internet access?

JaqueCaamal

How do you know that? Have you seen the server log? I'd guess that it produces an error on the server because you say that the content is of type 'application/x-www-form-urlencoded' but what you send isn't in that format.

Does that WiFi network has internet access?
Yes, I'm sorry. That's what I was trying to say. Yes it has internet access.

pylon

Did you read the rest of my answer?

Go Up