1

I've 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 I'm looking in the logs but I don't see anything.

I'm 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.

  2. 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`
    
  3. 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`
    

    Here is the image of it in my browser

    Browser URL window

  4. 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
    

I'm not sure what is wrong with the POST or GET request that is making those fail. How can I debug this?

  • do you send the postString? what are the & doing in request string? it should be only & – Juraj Oct 8 '18 at 19:57
  • 3
    Possible duplicate of Http post to Emoncms doesn't work – Juraj Oct 8 '18 at 20:01
  • No that's just what copy/paste does. It's just the &. I'll fix that in the post, thanks – marciokoko Oct 8 '18 at 20:39
  • @juraj is not a duplicate. That is Post, this is Get – marciokoko Oct 8 '18 at 20:47
  • but you ave the same errors and GET still doesn't work – Juraj Oct 9 '18 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.