Go Down

Topic: How to parse dht22 data into string for GET HTTP Request (Read 92 times) previous topic - next topic

Marciokoko

I have this code:

Code: [Select]

  char dataString[20];
  char tempString[20];
  char humString[20];

  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);
}


How should I parse it into a string for http request?  I tried this but it does something funny:

Code: [Select]

void httppost () {

  String nodeData1="/emoncms/input/post.json?apikey=";
  nodeData1.concat(EMON_APIKEY); //JOIN PARAMETER LITERAL STRING => String Object
  Serial.println(nodeData1);
  
  String nodeData15=nodeData1 + "&node=fortnite&json={\"t\":";

  String nodeData2=nodeData15 + tempString; //this joins string with char
  String nodeData3=nodeData2 + ",\"h\":";   //this adds string to a string
  Serial.println(nodeData3);
  Serial.println(humString);
  String nodeData4=nodeData3 + humString;//humString;
  Serial.println(nodeData4);

  String nodeData5=nodeData4 + "}";
  Serial.println(nodeData5);
    
  String finalData=nodeData5; //COMBINE BOTH PREVIOUS STRING OBJECTS
////  String finalData="/emoncms/input/post.json?apikey=bde919c2b47c5f12d0b1b4478f78107f&node=fortnite&json={\"t\":55.66,\"h\":77.88}";
  Serial.println(finalData);
  
  delay(1000);
  String getRequest="GET " + finalData + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: keep-alive\r\n\r\n";
  //String requestLength = String(getRequest.length());
  Serial.println("getRequest");
  Serial.println(getRequest);
}


This is the Serial Monitor, but the last "}" isnt tacked on...why

Code: [Select]

TCP connection ready
/emoncms/input/post.json?apikey=mykey
/emoncms/input/post.json?apikey=mykey&node=fortnite&json={"t":29.40,"h":
30.60
/emoncms/input/post.json?apikey=mykey&node=fortnite&json={"t":29.40,"h":30.60


getRequest
 HTTP/1.1
Host: santiapps.com
Connection: keep-alive


I also tried  converting each individual char[] to a String by using:

Code: [Select]
  String realTempString = String(tempString);
  String realHumString = String(humString);


but in the end when it comes time to print the getRequest I still get blank!  The finalData String always prints perfectly, but after I use it inline with the getRequest:

Code: [Select]

  String getRequest="GET " + finalData + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: keep-alive\r\n\r\n";


it gets blanked out and by the time I do:

Code: [Select]

  Serial.println(getRequest);


and then try to use it to send it as AT Command, its empty!

PaulS

Quote
How should I parse it into a string for http request?
Parsing is the act of getting data FROM a string.

Code: [Select]
  String finalData=nodeData5; //COMBINE BOTH PREVIOUS STRING OBJECTS
That is NOT what the code does.

What Arduino are you compiling the code for? For anything other than an ESP or Due, forget about using Strings. You are most likely running out of memory. The String class does a lot of dynamic allocation, and doesn't have a way to tell you that an allocation failed.
The art of getting good answers lies in asking good questions.


PaulS

Its a Nano.
Then forget about the String class. You can do everything you are trying to do with a char array (that is big enough) and sprintf().
The art of getting good answers lies in asking good questions.

Go Up