WiShield POSTing and JSON parsing

My original post is here: Asynclabs.com.

I’m am having some basic C trouble, along with some other more advanced stuff.

I can POST to my own API, but I am having trouble adding a concatenated variable (basic C stuff…still learning), as well as with parsing a JSON returned array (200 status returned along with some other info).

I use a modified WebClient Sketch with a WiShield, and it doesn’t compile…I’ve tried every combination my young C mind can think of. For example:

char mqttCode[11]; //then I run some stuff and get an RFID badge in form of a string

//the original, non-concatenated post request looks like below, and works
//const prog_char twitter[] PROGMEM = {"POST /APICode/api/Sensor/ HTTP/1.1\r\nUser-Agent: uIP/1.0\r\nHost: myurl.com\r\nContent-Length: 24\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\nFacebookBadge=1234567899"};

strcpy(one, "POST /APICode/Sensor/ HTTP/1.1\r\nUser-Agent: uIP/1.0\r\nHost: myurl.com\r\nContent-Length: 25\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\nFacebookBadge=");
char posting;
posting = strcat(one, mqttCode);
const prog_char twitter[] PROGMEM = {posting};

void loop()
{
//if something happens, i post again
const prog_char twitter[] PROGMEM = {posting};
WiFi.run();
}

I’m also unsure about what function to use to get returned JSON information from my POST request (which I imagine will come back with headers included, and I have no idea where to look for parsing information). Help!

strcpy(one, "POST /APICode/Sensor/ HTTP/1.1\r\nUser-Agent: uIP/1.0\r\nHost: myurl.com\r\nContent-Length: 25\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\nFacebookBadge=");

Executable code, like this call to strcpy, need to be inside a function.

const prog_char twitter[] PROGMEM = {posting};

PROGMEM is used for things that will not change for the life of the program. As a local variable inside the loop function, this is certainly not the case.

char posting;
posting = strcat(one, mqttCode);

Concatenating that long string of characters with another string, and stuffing them into a single character is going to be quite a trick.

There really is no reason to concatenate anything. When you POST, the POST command is not complete until a carriage return is send. So, just send the first part, which can be just a constant string:

Whatever.print("The long string goes here");
Whatever.print(mqttCode);
Whatever.println();

As for parsing the return data, we'll need to wait to see what kind of data you get in response to the POST, first.

Very helpful. I’m now getting a header back and a JSON array, which I need to parse the session_id from (which i use to POST). Can I ignore the header and use the C JSON library?

Here’s what I get retured to serial:

xx.xxx.net
xx.xxx.net
121
0xx.xxx.net
164xx.xxx.net
HTTP/1.1 200 OK
Date: Mon, 16 Aug 2010 23:33:47 GMT
Server: Apache/2.2.9 (Fedora)
X-Powered-By: PHP/5.2.6
Content-Length: 291
Connection: close
Conte

«
®
­
164xx.xxx.net
text/html; charset=UTF-8

 {"status":"200","message":"OK","session_id":"b77cc3f0-a98e-11df-916a-616263646566","timestamp":"1282001627_71.229.147.191","Ap

«
®
­
155xx.xxx.net
ensor\/19216810158\/#":"get_post","Sensor\/19216810159\/#":"get_post"},"MqttTopic":{"Sensor\/19216810158\/#":"sub_pub","Sensor\/19216810159\/#":"sub_pub"}}

My current code:

//function that prints data from the server
void printData(char* data, int len) {
  //print the data returned by the server
  //note that the data is not null-terminated, may be broken up into smaller packets, and includes the HTTP header. 
  while (len-- > 0) {
    Serial.print(*(data++));
  } 
}

GETrequest getSession(ipaddr, port, hostname, newURI);

void setup() {
  //initialize wiserver (we'll pass null for the page serving function since we don't need to serve web pages) 
  WiServer.init(NULL);

  //enable serial output and ask wierver to generate log messages (optional)
  Serial.begin(57600);
  WiServer.enableVerboseMode(true);

  //have the processData function called when data is returned by the server
  getSession.setReturnFunc(printData);
}

// Time (in millis) when the data should be retrieved 
long updateTime = 0;

void loop(){
  
  // Check if it's time to get an update
  //if (millis() >= updateTime) {
  getSession.submit();    
  // Get another update one min from now
  //  updateTime += 1000 * 20;
  //}

  // Run WiServer
  WiServer.server_task();

  delay(10);
}