arduino - Problem creating a HTTP POST message with the wiserver-library -
i call rest-service black widow 1.0 board (with wi-fi) using wiserver library.
the http post should this:
http://sensor.zonosoft.com/sensorservice.svc/savemeasurement http/1.0 content-type: application/json; charset=utf-8 {"id":0,"version":0,"name":"toilet 1","sensors":[{"id":0,"version":0,"measurements":[{"time":"\/date(1295820124154+0100)\/","value":1}],"name":"dor"},{"id":0,"version":0,"measurements":[{"time":"\/date(1295820124155+0100)\/","value":1}],"name":"tonden"}]}
when test post request fiddler2's request builder, fiddler adds these 2 lines header.
host: sensor.zonosoft.com content-length: 257 , fiddler detects response: http/1.1 200 ok cache-control: private content-length: 10 content-type: application/json; charset=utf-8 server: microsoft-iis/7.5 x-aspnet-version: 4.0.30319 set-cookie: asp.net_sessionid=unstlzfj43ug2xrdyua5x2fk; path=/; httponly x-powered-by: asp.net date: tue, 25 jan 2011 14:33:10 gmt connection: close "success!"
that how it's supposed work.
here arduino sketch use postrequest. have tried lot of things working, best can come with. unfortunately won't generate correct post message. reason be?
#include <wiserver.h> #define wireless_mode_infra 1 #define wireless_mode_adhoc 2 // wireless configuration parameters ---------------------------------------- unsigned char local_ip[] = {192,168,1,2}; // ip address of wishield unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway ip address unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask local network const prog_char ssid[] progmem = {"__yourssid__"}; // max 32 bytes unsigned char security_type = 3; // 0 - open; 1 - wep; 2 - wpa; 3 - wpa2 // wpa/wpa2 passphrase const prog_char security_passphrase[] progmem = {"__yourpassword__"}; // max 64 characters 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 }; unsigned char wireless_mode = wireless_mode_infra; unsigned char ssid_len; unsigned char security_passphrase_len; void printdata(char* data, int len) { serial.println("printdata"); // print data returned server // note data not null-terminated, may broken smaller packets, , // includes http header. while (len-- > 0) { serial.print(*(data++)); } } uint8 ip[] = {195,128,175,40}; postrequest sendinfo( ip, 80, "sensor.zonosoft.com\ncontent-type: application/json; charset=utf-8", "/sensorservice.svc/savemeasurement", createbody); void setup() { wiserver.init(null); serial.begin(9600); wiserver.enableverbosemode(true); sendinfo.setreturnfunc(printdata); } // time (in millis) when data should retrieved long updatetime = 0; void createbody() { char json[] = "{\"id\":0,\"version\":0,\"name\":\"toilet 2\",\"sensors\":[{\"id\":0,\"version\":0,\"measurements\":[{\"time\":\"\\/date(1295820124154+0100)\\/\",\"value\":1}],\"name\":\"dor\"},{\"id\":0,\"version\":0,\"measurements\":[{\"time\":\"\\/date(1295820124155+0100)\\/\",\"value\":1}],\"name\":\"tonden\"}]}"; wiserver.print(json); } void loop() { // check if it's time update if (millis() >= updatetime) { serial.println("start post"); sendinfo.submit(); serial.println("end post"); updatetime += 1000 * 30; serial.println(updatetime); } wiserver.server_task(); delay(10); }
i have set http trace using wireshark, , found wiserver adds own hardcoded content-type: application/x-www-form-urlencoded
. content-type
appeared twice in header.
i have changed hardcoded content type application/json; charset=utf-8
in wishield\strings.c
. , removed content type hostname.
it works!
Comments
Post a Comment