I used the example code from the Arduino IDE and made just a few modifications. Based on the LED blinking, I can tell that the Wi-Fi connection was established correctly. However, my POST request is not being sent. Again, based on the LED blinking, it seems that the ESP is trying to send it, but there’s no error occurring.
I’m using an online service to test the request sending, which generates a URL that I can use for testing (https://webhook.site/).
Does anyone know what might be going wrong?
/**
PostHTTPClient.ino
Created on: 21.11.2016
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define SERVER_IP "https://webhook.site/fedbf703-b2e7-42a7-9d6d-c72a2441bd0d" //EXEMPLE
#ifndef STASSID
#define STASSID "SID"
#define STAPSK "PASSWORD"
#endif
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println();
WiFi.begin(STASSID, STAPSK);
while (WiFi.status() != WL_CONNECTED) { // Wait for a second
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // Turn the LED off by making the voltage HIGH
delay(300);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
}
void piscaRapido(){
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
delay(300); // Wait for a second
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
delay(300);
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
delay(300); // Wait for a second
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
delay(300);
}
void piscaLento(){
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
delay(1000); // Wait for a second
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
delay(1000);
}
void loop() {
// wait for WiFi connection
if ((WiFi.status() == WL_CONNECTED)) {
piscaRapido();
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
// configure traged server and url
http.begin(client, SERVER_IP); // HTTP
http.addHeader("Content-Type", "application/json");
Serial.print("[HTTP] POST...\n");
// start connection and send HTTP header and body
int httpCode = http.POST("{\"hello\":\"world\"}");
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
const String& payload = http.getString();
Serial.println("received payload:\n<<");
Serial.println(payload);
Serial.println(">>");
}
} else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
delay(2000); // Wait for a second
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
}
http.end();
} else{
piscaLento();
}
delay(10000);
}