Get a reply from http post on a String

Adafruit cellular platform - SMS and IoT over celluar

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Elias97
 
Posts: 6
Joined: Tue Oct 08, 2019 12:17 pm

Get a reply from http post on a String

Post by Elias97 »

Hello, I've made a project using a SIM800 module using the FONA library and everything works just fine.
I have a question though. On my server I made a .php file that accepts some json values from the arduino, and echoes back (reply) with some other values from a MySQL table.
At this moment it just prints the reply on Serial monitor. How can I enter the reply in a String?

Thank's in advance!

User avatar
rskup
 
Posts: 230
Joined: Sat Aug 01, 2020 9:04 pm

Re: Get a reply from http post on a String

Post by rskup »

Not sure where you're reading the FONA's serial from, but for me I'm using an Adafruit Feather (#2771) and my FONA is hooked to pins 0 and 1, which is Serial1 on the Feather.

So here's an example of when I want to check whether I'm getting an "OK" back from the FONA:

Code: Select all

while (Serial1.available()){           
  FONAstring = Serial1.readString(); 
  Serial.println(FONAstring);
  FONAstring.replace("\r","");
  FONAstring.replace("\n","");
  if (FONAstring.endsWith("OK")) {
    simcom = 1;
    return true;
  }
  return false;
}
Hope that helps.

User avatar
Elias97
 
Posts: 6
Joined: Tue Oct 08, 2019 12:17 pm

Re: Get a reply from http post on a String

Post by Elias97 »

Hi rskup and thank's for commenting. Unfortunatelly your suggestion doesn't fit my needs.
Just a bit more information about my project:
The sim800 communicates with an arduino mega. All the procedures are automaded so no user serial input here.

I have this function:

Code: Select all


const char url[] = "http://example_site.com/comms.php";
char output[500];
serializeJson(doc, output);  //this is from arduino json 6 library. It generates a char with some json values about 480bytes long.


if (!fona.postData("POST", url, output)) // Can also add authorization token parameter!
       Serial.println(F("Failed to complete HTTP POST..."));
         

 
That works fine and gives me these results:

Code: Select all


---> AT+HTTPINIT
	<--- OK
	---> AT+HTTPPARA="CID",1
	<--- OK
	---> AT+HTTPPARA="URL","http://example_site.com/comms.php"
	<--- OK
	---> AT+HTTPPARA="CONTENT","application/json"
	<--- OK
	---> AT+HTTPDATA=409,10000
	<--- DOWNLOAD
	---> {"dbusername":"xxx","dbpassword":"xxx","dbname":"xxx","Latitude":xxx,"Longitude":xxx,"signalQuality":xxx,"currentSpeed":xxx,"batteryVoltage":xxx,"metersLeft":xxx,"SN":"xxx","Humidity":xxx,"Pressure":xxx,"windSpeed":xxx,"Pump":xxx,"timeLeft":"xxx","Errors":xxx,"setSpeed":xxx,"smsComms":xxx,"smsNotify":xxx,"pumpControl":xxx,"serverUpdate":xxx,"photoUpdate":xxx,"valueUpdate":xxx}
	<--- OK
	---> AT+HTTPACTION=1
	<--- OK
HTTP status: 200
Data length: 15
	---> AT+HTTPREAD
	<--- +HTTPREAD: 15
	<--- 12.6,1,13,0,5,2
	---> AT+HTTPTERM
	<--- OK

As you can see, I get back this: 12.6,1,13,0,5,2. I then have a function with a delimiter that puts each value between commas in a integer/float.
I need to do something like this in pseudo:

String get;
get = 12.6,1,13,0,5,1 or get = AT+HTTPREAD or something

Is there a FONA function that does that automatically?

User avatar
rskup
 
Posts: 230
Joined: Sat Aug 01, 2020 9:04 pm

Re: Get a reply from http post on a String

Post by rskup »

Ug, I'm looking at JsonSerializer.hpp over at github to try to understand. I can sorta see what you're asking scrolling through the code there, but modifying .hpp stuff is beyond me. Your project certainly sounds interesting, it had me googling php and json examples earlier today as years ago I use to work with MySQL. Hope someone else can help you out!

User avatar
Elias97
 
Posts: 6
Joined: Tue Oct 08, 2019 12:17 pm

Re: Get a reply from http post on a String

Post by Elias97 »

Json serializer and deserializer is super easy. It took me some time to understand it though.
Thank's again and I hope I'll get some help here!

User avatar
Elias97
 
Posts: 6
Joined: Tue Oct 08, 2019 12:17 pm

Re: Get a reply from http post on a String

Post by Elias97 »

For everyone interested I found the solution for this problem!

Just do this:

Code: Select all

        String content = "";
        uint16_t statuscode;
        int16_t length;



   
        Serial.println(F("Start Updating Server"));
        Serial.println(F(""));
        Serial.println(F(""));
       if (!fona.HTTP_POST_start(url, F("application/json"), (uint8_t*)output, strlen(output), &statuscode, &length)) {
          Serial.println("Failed!");
        }
        while (length > 0) {
          while (fona.available()){
            char c = fona.read();
            //Serial.write(c);
            content.concat(c);
            length--;
            if (! length) break;
          }
        }
        Serial.println(F("\n****"));
        fona.HTTP_POST_end();

        Serial.print("The content is:");
        Serial.println(content);
        
    
      //after parsing the data, delete everything from String content in order to be clean for the next use
   
        String content = "";

     



Locked
Please be positive and constructive with your questions and comments.

Return to “FONA”