Uploading data to Google Docs

Discussions related to the WiServer add-on code for WiShield. WiServer is a friendly, easy-to-use front end for webserver and webclient usages.

Moderator: shard7

Uploading data to Google Docs

Postby RobertMParker » Sun Sep 12, 2010 5:55 pm

I'm new here. This is my first post! My project mostly involves a lot of data collection, and I wanted to store that data online. I looked into using pachube, but I didn't like it. The free version only holds one month of data, and I couldn't find a way to delete testing/erroneous data. I really just wanted to put my data into a Google Docs Spreadsheet, and from there I can export it wherever I like.

I made a simple Google Doc Form that feeds my spreadsheet, and then I make one POST request to that form to add data. It even timestamps the entry. Here is an example of a POST request. The form that this is linked to has two text boxes for collecting data. Replace <ENTER_YOUR_KEY_HERE> with the key to your spreadsheet. And replace <DATA1> and <DATA2> with the data values that you want.

entry.0.single and entry.1.single are the names of the textboxes on the form, I think the names are standardized this way. Anyway you can read the source of the form and get the names.

Code: Select all
POST https://spreadsheets.google.com/formResponse?formkey=<ENTER_YOUR_KEY_HERE>&ifq HTTP/1.1
Host: spreadsheets.google.com
Content-Type:application/x-www-form-urlencoded
Content-Length:75

entry.0.single=<DATA1>&entry.1.single=<DATA2>&pageNumber=0&backupCache=&submit=Submit


I used the code that GregEigsti wrote in this thread for the post request:
viewtopic.php?f=15&t=86&start=10


Currently the code is sending hardcoded values, but it will be trivial to change this to some actual data.

Code: Select all
    /*
    * A simple sketch that uses WiServer to PUT (via POST) to GoogleDocs
    */

    #include <WiServer.h>

    //Wireless configuration defines ----------------------------------------
    #define WIRELESS_MODE_INFRA   1
    #define WIRELESS_MODE_ADHOC   2
    #define DEBUG_PRINT

    //Wireless configuration parameters ----------------------------------------
    unsigned char local_ip[] = {192,168,2,100};   // IP address of WiShield
    unsigned char gateway_ip[] = {192,168,2,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"};    // max 32 bytes
    unsigned char security_type    = 2;               // 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
    prog_uchar wep_keys[] PROGMEM = {
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // 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;
    // End of wireless configuration parameters ----------------------------------------


    //global state data
    boolean loop_cnt = 0;
    // IP Address for spreadsheets.google.com
    uint8 ip[] = {74,125,67,102};
    char hostName[] = "spreadsheets.google.com\nContent-Type:application/x-www-form-urlencoded";
    char url[] = "/formResponse?formkey=INSERT_KEY_HERE";
    // A request that POSTS data to GoogleDocs
    POSTrequest postGoogleDocs(ip, 80, hostName, url, feedData);

    // 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.
    #ifdef DEBUG_PRINT
       while (len-- > 0) {
          Serial.print(*(data++));
       }
    #endif //DEBUG_PRINT
    }

    //body data function, provides data for the POST command in comma separated values (CSV)
    //currently POSTs one value but more can be added by separating with comma (no spaces)
    //Your program will not want to use the hardcoded values below, rather it would read the
    //sensor(s) and build the data string shown below.
    void feedData()
    {
       char buf[] = "entry.0.single=9999&entry.1.single=9999&pageNumber=0&backupCache=&submit=Submit";
       WiServer.print(buf);
    }

    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 WiServer to generate log messages (optional)
    #ifdef DEBUG_PRINT
       Serial.begin(9600);
       //WiServer.enableVerboseMode(true);
    #endif //DEBUG_PRINT

       // Have the processData function called when data is returned by the server
       postGoogleDocs.setReturnFunc(printData);

       loop_cnt = 0;
    }


    void loop()
    {
       if(loop_cnt == 0){
          postGoogleDocs.submit();   
          loop_cnt++;
       }

       // Run WiServer
       WiServer.server_task();

       delay(10);
    }



Anyway I thought this was pretty cool, and I couldn't find anything similar. This is my first Arduino project as well, it is a fun platform. Let me know if you have any advice or criticism.
RobertMParker
 
Posts: 1
Joined: Sun Sep 12, 2010 5:24 pm

Re: Uploading data to Google Docs

Postby GregEigsti » Sun Sep 12, 2010 11:06 pm

Thanks for sharing! Neat stuff!
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: Uploading data to Google Docs

Postby pfin » Wed Nov 03, 2010 8:53 pm

Hi,

Thank you for the code. It works great. I have one question, sorry if it is stupid, I am new to Arduino. I have the code uploading analog data to a Google doc, but it loads data exactly every three minutes. I can't for the life of me find where the timing is set in the program. Where is the delay between uploads set in the code? I see the delay(10) but that isn't three minutes.

Thanks

*** UPDATE:

I left it on overnight and it looks like once my computer when to sleep, the WiShield stopped resetting, and stopped sending new data, but stayed connected. So it seems like the timing was accidental and the resetting of the shield cause it to send data. Any one else have this problem of the shield resetting every three minutes?

Example data: With timing exactly every 3 minutes...
11/3/2010 23:20:08 100 508
11/3/2010 23:23:08 100 512
11/3/2010 23:26:08 100 511
11/3/2010 23:29:09 100 511
pfin
 
Posts: 1
Joined: Wed Nov 03, 2010 8:44 pm


Return to WiServer

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest