After reading a few different tutorials on how to post http I'm still failing. Can anyone see anything wrong in my code? (The code is based on a few tutorials, http://bitsofgyan.com/index.php/2016/05/01/http-requests-with-esp8266/ among them.
My code:
#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"
#include <aJSON.h>
void setup() {
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
WiFi.begin("XXXXXX", "YYYYYYY");
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Setup done");
}
void loop() {
if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
HTTPClient http;
http.begin("192.168.1.29:8080/");
aJsonObject *root;
root=aJson.createObject();
aJson.addStringToObject(root,"message", "Hello from ESP8266");
char *payload=aJson.print(root);
int httpCode = http.POST((uint8_t *)payload,strlen(payload));
if(httpCode == HTTP_CODE_OK)
{
Serial.print("HTTP response code ");
Serial.println(httpCode);
String response = http.getString();
}
else
{
Serial.println("Error in HTTP request");
Serial.println(httpCode);
}
http.end(); //Close connection
}
else{
Serial.println("Error in WiFi connection");
}
delay(30000); //Send a request every 30 seconds
}
The server is a simple webserver written in python
from bottle import route, run, request
@route('/', method='POST')
def index():
postdata = request.body.read()
print postdata
run(host='192.168.1.29', port=8080, debug=True)
All I get back is a -1 response, aka something seems wrong on the ESP side.
Also, everything works when i try to post with cURL.
Any ideas?