1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

HTTP GET fails from ESP8266 to Emoncms

Discussion in 'Programming/Internet' started by marciokoko, Oct 8, 2018.

  1. marciokoko

    marciokoko Guest

    Ive tried the code with emoncms.org as well and the results are identical; one parameter posts fine, but when I include the second parameter in the json I get HTTP 400 Bad Request from nginx and Im looking in the logs but I dont see anything.

    Im trying to post data from a dht22 to my emoncms server. I was able to post a single value with http post but when I try two values it fails. And when I try http get it fails. Here are the codes and results in the Serial Monitor:

    1. Http Post successful with 1 parameter, Temp:

    void start_test () { //For DHT22 Grove Pro float h = dht.readHumidity(); float t = dht.readTemperature(); dtostrf(t, 4, 2, dataString); Serial.println(dataString); } void httppost () { esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection. if( esp.find("OK")) { Serial.println("TCP connection ready"); } String nodeData="node=fortnite&data={\"a\":"; String nodeWithJson=nodeData + dataString + "}"; String apiKeyString="&apikey="; apiKeyString.concat(EMON_APIKEY); String finalData=nodeWithJson+apiKeyString; Serial.println("finalData"); Serial.println(finalData); delay(1000); String postRequest="POST " + uri + " HTTP/1.1\r\n"+"Host: " + server + "\r\n" + "Accept: *" + "/" + "*\r\n"+"Content-Length: " + finalData.length() + "\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n"+"\r\n" + finalData; Serial.println("postRequest"); Serial.println(postRequest); ...more code }

    This is the Post Request: &node=fortnite&data={"a":28.00}&apikey=mykey

    And I get http 200 OK.

    1. This is the Post code that fails with a second parameter, Humidity:

    void start_test () { //For DHT22 Grove Pro float h = dht.readHumidity(); float t = dht.readTemperature(); dtostrf(t, 4, 2, tempString); //convert flat to char
    dtostrf(h, 4, 2, humString); //convert flat to char strcpy(dataString,tempString); strcat(dataString,humString); Serial.println(dataString); } void httppost () { esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection. if( esp.find("OK")) { Serial.println("TCP connection ready"); } String nodeData1="&node=fortnite&data={\"t\":"; String nodeData2=nodeData1 + tempString; String nodeData3=nodeData2 + ",\"h\":"; String nodeData4=nodeData3 + humString; String nodeData5=nodeData4 + "}"; String apiKeyString="&apikey="; apiKeyString.concat(EMON_APIKEY); String finalData=nodeData5+apiKeyString; Serial.println(finalData.length()); Serial.println(finalData); delay(1000); String postRequest="POST " + uri + " HTTP/1.1\r\n"+"Host: " + server + "\r\n" + "Accept: *" + "/" + "*\r\n"+"Content-Length: " + finalData.length() + "\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n"+"\r\n" + finalData; Serial.println("postRequest"); Serial.println(postRequest); ...more code }

    This is the Post Request:

    &node=fortnite&data={"t":28.40,"h":69.20}&apikey=mykey

    2.b I even tried the url directly and it works fine:

    http://santiapps.com/emoncms/input/post?node=fortnite&data={"t":26.60,"h":49.70}&apikey=mykey

    [​IMG]

    Here is the image of it in my browser 3. I tried this GET request also but it returns a 400 http error:

    #include "SoftwareSerial.h"
    #include "DHT.h"
    #define DHTPIN A1 // what pin we're connected to
    #define DHTTYPE DHT22 // DHT 22 (AM2302)
    DHT dht(DHTPIN, DHTTYPE);

    String ssid ="myssid";
    String password="mypwd";
    String server = "santiapps.com"; // www.example.com
    String uri = "/emoncms/input/post";
    #define EMON_APIKEY F("mykey")

    byte dat [5];
    String temp ,hum;
    String data;
    char dataString[20];
    char tempString[20];
    char humString[20];

    SoftwareSerial esp(6, 7);// RX, TX

    void setup() {
    esp.begin(9600);
    Serial.begin(9600);
    Serial.println("setup");
    reset();
    connectWifi();
    }

    void reset() {
    Serial.println("reset");
    esp.println("AT+RST");
    delay(1000);
    if(esp.find("OK") ) Serial.println("Module Reset");
    }

    void connectWifi() {
    Serial.println("connect to wifi");
    String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
    esp.println(cmd);
    delay(400);
    while (esp.available()){
    String inData = esp.readStringUntil('\n');
    Serial.println("Got reponse from ESP8266: " + inData);
    }

    if(esp.find("OK")) {
    Serial.println("Connected!");
    } else {
    connectWifi();
    Serial.println("Cannot connect to wifi"); }
    }

    void start_test () {
    //For DHT22 Grove Pro
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    dtostrf(t, 4, 2, tempString); //convert flat to char
    dtostrf(h, 4, 2, humString); //convert flat to char
    strcpy(dataString,tempString);
    strcat(dataString,humString);
    Serial.println(dataString);
    }

    void loop(){
    Serial.println("loop");
    start_test();
    httppost();
    delay(100000);//100k ms = 100s or ~ 1.8m
    }

    void httppost () {
    esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");
    if( esp.find("OK")) {
    Serial.println("TCP connection ready");
    }
    String nodeData1="/emoncms/input/post.json?node=fortnite&json={\"t\":";
    String nodeData2=nodeData1 + tempString;
    String nodeData3=nodeData2 + ",\"h\":";
    String nodeData4=nodeData3 + humString;
    String nodeData5=nodeData4 + "}";
    String apiKeyString="&apikey=";
    apiKeyString.concat(EMON_APIKEY); //JOIN PARAMETER LITERAL STRING => String Object
    String finalData=nodeData5+apiKeyString; //COMBINE BOTH PREVIOUS STRING OBJECTS
    Serial.println(finalData);
    delay(1000);
    String getRequest="GET " + finalData + " HTTP/1.0\r\n"+"Host: " + server + "\r\n" + "Connection: keep-alive\r\n\r\n";
    String requestLength = String(getRequest.length());
    Serial.println("getRequest");
    Serial.println(getRequest);

    String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
    esp.print(sendCmd);
    esp.println(requestLength);
    delay(1000);
    if(esp.find(">")) {
    Serial.println("Sending..");
    esp.print(getRequest);
    if(esp.find("SEND OK")) {
    Serial.println("Packet sent");
    while (esp.available()) {
    String tmpResp = esp.readString();
    Serial.println(tmpResp);
    }
    // close the connection
    esp.println("AT+CIPCLOSE");
    }
    }
    }


    And here is the get request:

    setup
    reset
    connect to wifi
    Got reponse from ESP8266: AT+CWJAP="myssid","mypwd"

    Got reponse from ESP8266: WIFI DISCONNEC
    connect to wifi
    Got reponse from ESP8266: AT+CWJAP="myssid","mypwd"

    Got reponse from ESP8266: busy p...

    Connected!
    Cannot connect to wifi
    loop
    29.5051.60
    TCP connection ready
    /emoncms/input/post.json?node=fortnite&json={"t":29.50,"h":51.60}&apikey=mykey
    getRequest
    HTTP/1.0
    Host: santiapps.com
    Connection: keep-alive

    Sending..
    Packet sent

    +IPD,311:HTTP/1.1 400 Bad Request
    Server: nginx
    Date: Mon, 08 Oct 2018 02:00:11 GMT
    C


    Im not sure what is wrong with the post or get request that is making those fail. How can I debug this?

    Login To add answer/comment
     

Share This Page