POSTing and JSON parsing

Discussion about any of the included sketches (i.e. WebServer), or user-generated applications.

POSTing and JSON parsing

Postby franklovecchio » Fri Aug 13, 2010 10:59 am

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, which 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

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 (which I imagine will come back with headers included, and I have no idea where to look for parsing information). Help!
franklovecchio
 
Posts: 9
Joined: Wed Aug 04, 2010 5:14 pm

Re: POSTing and JSON parsing

Postby GregEigsti » Sat Aug 14, 2010 5:06 pm

Posting your complete code is always helpful in case someone wants to take a look.

Greg
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website

Re: POSTing and JSON parsing

Postby franklovecchio » Tue Aug 17, 2010 10:37 am

Good point. Well, let's start with parsing. I have an idea of how to do this (using code from the Blink guy), but can I do something with scanf?

I need to parse a session_id (which stays the same length with varying characters) from a returned array. Can I ignore the headers?

Code:
Code: Select all
#include <WiServer.h>
#define WIRELESS_MODE_INFRA   1
#define WIRELESS_MODE_ADHOC   2
// setting this to 1 will show the actual page contents on the serial console
#define API_KEY        "938384834848343"     //your api key here

unsigned char local_ip[] = {192,168,10,xx};                           //ip address of wishield
unsigned char gateway_ip[] = {192,168,10,1};                           //router or gateway ip address
unsigned char subnet_mask[] = {255,255,255,0};                           //subnet mask for the local network
const prog_char ssid[] PROGMEM = {"xxxxxx"};
unsigned char security_type = 3;                                   //0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

//wpa/wpa2 passphrase
const prog_char security_passphrase[] PROGMEM = {"xxxxxx"};           //max 64 characters

//wep 128-bit keys
//sample hex keys
prog_uchar wep_keys[] PROGMEM = {
  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,   // Key 0
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // Key 1
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // Key 2
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00   // Key 3
};

//setup the wireless mode
//infrastructure - connect to ap
//adhoc - connect to another wifi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;

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

//function that parses session id from server
void parseData(char* data, int len) {
  //not exactly sure what this will look like
  char sessionID[40];  //1 over actual length
  char codestart = -1;

  //deal with 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) {
      char c = *(data++);
      Serial.print(c); //*(data++));
      // look for JSON session id i.e. (not e.g) 72169560-a660-11df-afb1-616263646566
     // if( codestart >= 0 && codestart < 41 ) {
      //    sessionID[codestart++] = c;
      //}
      //can i do this with "session_id"
      //if( c=='d' ) {
      //    codestart=0;
      // }
  }
  //we have a session id, so let's parse it
  if( codestart != -1 ) {
      sessionID[40] = 0;  // null-terminate the string
      Serial.print("session id: ");
      Serial.println(sessionID);
  }
}

//ip address of cloud 
uint8 ip[] = {xxx,xx,xxx,xxx};

char hostname[] = "xx.xxx.net";
uint8 ipaddr[]  = {xxx,xxx,xx,113};
char uri[]      = "/api/Login/"; 
char key[] = API_KEY;
const int port  = 80;

//function to combine and allocate
char *combined(char result[], size_t result_sz, const char key[], const char uri[]) {
//test to see if there is sufficient space */
  if (strlen(uri) + strlen(key) + 1 > result_sz) {
    return NULL;
  }
  strcpy(result, uri);
  strcat(result, key);
 
  return result;
}

//a get request
char lon[1024];
char *newURI = combined (lon, sizeof lon, key, uri);
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);
  //getSession.setReturnFunc(parseData);
}

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

franklovecchio
 
Posts: 9
Joined: Wed Aug 04, 2010 5:14 pm

Re: POSTing and JSON parsing

Postby franklovecchio » Tue Aug 17, 2010 10:38 am

I should note that this is what's returned in serial:
Code: Select all
.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"}}
franklovecchio
 
Posts: 9
Joined: Wed Aug 04, 2010 5:14 pm


Return to Sketches and Applications

Who is online

Users browsing this forum: No registered users and 2 guests

cron