[SOLVED] modify wifi shield code for Yun, logging data to google spreadsheet

Hi, I am trying to log data using my Yun to google spreadsheet. Here is a piece of code I found online written for wifi-shield, what should I change so to use it for Yun? Thank for helping!

/* Arduino to Google Docs
 created 2011
 
This example code is in the public domain.
 
http://www.open-electronics.org
 
 
http://www.futurashop.it
 
https://spreadsheets.google.com/formResponse?formkey=dDBMdUx3TmQ5Y2xvX2Z0V183UVp2U0E6MQ &ifq&entry.0.single=Boris&entry.2.single=Landoni&submit=Submit
Original from http://goodsite.cocolog-nifty.com/uessay/2010/07/arduinogoogle-d.html
Modified by John Missikos 11/6/11
Modified by Andrea Fainozzi 30/6/11
Modified by Boris Landoni 8/7/11
 
*/
 

char formkey[] = "1RQRb3XwlONtrOwqJs1UBcrH9O8BWca6UulcT22NrF2U"; //Replace with your Key
byte mac[] = { 0x90,0xA2,0xDA,0xF4,0x0D,0x1C};  //Replace with your Ethernet shield MAC
byte ip[] = { 192,168,2,244};  //The Arduino device IP address
byte subnet[] = { 255,255,255,0};
byte gateway[] = { 192,168,0,254};
byte server[] = { 209,85,229,101 }; // Google IP
Client client(server, 80);
 
void setup()
{
  Serial.begin(9600);
  //Ethernet.begin(mac, ip , gateway , subnet);
  //delay(1000);
  Serial.println("connecting...");
}
 
void loop(){
  String data;
  data+="";
  data+="entry.751179938=";
  data+=analogRead(A0);
 // data+="&entry.2.single=";
  //data+=analogRead(A1);
  data+="&submit=Submit";
 
  if (client.connect()) {
    Serial.println("connected");
 
    client.print("POST /formResponse?formkey=");
    client.print(formkey);
    client.println("&ifq HTTP/1.1");
    client.println("Host: spreadsheets.google.com");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.print(data);
    client.println();
 
    Serial.print("POST /formResponse?formkey=");
    Serial.print(formkey);
    Serial.println("&ifq HTTP/1.1");
    Serial.println("Host: spreadsheets.google.com");
    Serial.println("Content-Type: application/x-www-form-urlencoded");
    Serial.println("Connection: close");
    Serial.print("Content-Length: ");
    Serial.println(data.length());
    Serial.println();
    Serial.print(data);
    Serial.println();
 
  }
  delay(1000);
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
 
  delay(10000);
 
}

S_sneeze:
what should I change so to use it for Yun?

Quite a bit needs to change.

Everything dealing with the MAC address, IP, gateway, and subnet goes away. The "Client" object goes away and gets replaced by an HttpClient object. The way that it currently makes the connection (opening the Client, sending the HTTP headers, etc) also goes away and gets replaced by HttpClient style calls.

It will be a LOT easier to start with something already designed for the Yun. These may help:
http://forum.arduino.cc/index.php?topic=198347.0
http://forum.arduino.cc/index.php?topic=189966.0
http://forum.arduino.cc/index.php?topic=207876.0

Searching the forum for "Yun Google Spreadsheet" turned up quite a few results.

ShapeShifter:
Quite a bit needs to change.

Everything dealing with the MAC address, IP, gateway, and subnet goes away. The "Client" object goes away and gets replaced by an HttpClient object. The way that it currently makes the connection (opening the Client, sending the HTTP headers, etc) also goes away and gets replaced by HttpClient style calls.

Thank you for replying! I understand that the MAC address and stuff have to go away but I am not sure how to change the "Client" object to "HttpClient" object... And I did search for Arduino Yun Google spreadsheet but most of them use Temboo and I don't want to spend money on this.

S_sneeze:
And I did search for Arduino Yun Google spreadsheet but most of them use Temboo and I don't want to spend money on this.

ShapeShifter gave to you 3 links to threads where people sends data to Google spreadsheet without Temboo.

Angelo9999:
ShapeShifter gave to you 3 links to threads where people sends data to Google spreadsheet without Temboo.

To be fair, the third one uses Temboo. The second one mentions Temboo, but the posted code doesn't use it. The first post in the first listed thread has a link to a project that uses Temboo, but a few posts down, code is posted that doesn't use Temboo.

S_sneeze: when looking at those search results, don't be too quick to dismiss a thread because Temboo is mentioned in there somewhere, If you keep reading the thread, you may find a non-Temboo solution. I know that in the past six months, I've seen several mentions of ways to get to a Google spreadsheet without using Temboo. The information is out there...

Thanks ShapeShifter. This is the tutorial I found: http://www.open-electronics.org/how-send-data-from-arduino-to-google-docs-spreadsheet/

Does it mean that it will work once I replace the client object with httpclient? And my other problem is that I can't find the formkey of the Google form... The tutorial shows an old version of Google form and I wonder if the formkey is that yellow portion I highlighted in the URL. https://docs.google.com/forms/d/1RQRb3XwlONtrOwqJs1UBcrH9O8BWca6UulcT22NrF2U/formResponse?ifq&ygfd

I followed the instruction and typed in "https://spreadsheets.google.com/formResponse?formkey=1RQRb3XwlONtrOwqJs1UBcrH9O8BWca6UulcT22NrF2U &ifq&entry_751179938=sbc&submit=Submit"

This is what I get: We're sorry.
The spreadsheet at this URL could not be found. Make sure that you have the right URL and that the owner of the spreadsheet hasn't deleted it.

Find out more at the Google Docs Help Center.

S_sneeze:
Thanks ShapeShifter. This is the tutorial I found: http://www.open-electronics.org/how-send-data-from-arduino-to-google-docs-spreadsheet/

That looks like the same code you posted before. Why not start with something that is specifically written for the Yun?

Does it mean that it will work once I replace the client object with httpclient?

Yes, in the same way that your gasoline powered car will run on diesel fuel if you replace the engine. Of course, there's a lot more to it that that, and it's not as easy as it sounds. By the time you make that sketch Yun compatible, you'll have less than half of the original code left.

I followed the instruction and typed in "https://spreadsheets.google.com/formResponse?formkey=1RQRb3XwlONtrOwqJs1UBcrH9O8BWca6UulcT22NrF2U &ifq&entry_751179938=sbc&submit=Submit"

That's a good first step. Make sure you can do the update you want with a simple browser test like this. Until you can get this to work, there is no point in trying to program it -- it will be much easier to figure out what's going on by manually typing the requests for now. And yes, I would assume that yellow portion is the formkey.

Find out more at the Google Docs Help Center.

Did you try this? What did it say?

So far, at this point, it looks like you don't yet have a Yun programming problem, you have a basic issue communicating to Google. Work with the Google documentation until you can get a manually typed in request to work. Once you get that working, and you actually understand what the request is doing and what the various parts mean, then you can work on doing it programmatically on the Yun.

And when you get to the part of doing it on the Yun, it will be much easier if you start with an example that is actually written for the Yun.

Hey ShapeShifter! I used the code from the second link you gave me and I made it!!! Thank you so much, now I can go and play other stuffs.

This is what I did:

Click on view live form and submit the answer, the form key is in the URL from the thank you page( this formkey is different to what shows in the previous URL...I don't know why)

Here is the Arduino code:

#include <Process.h>

void setup()
{
  Bridge.begin();   // Initialize the Bridge
}

void loop()
{
  
  int value = 3;
 
  Process p;
  p.runShellCommand("curl \"https://docs.google.com/forms/d/1cSSbwCb0h97_RdoQkwja-11dqs48wExPoDRui_vYlYc/formResponse?entry.904254000=" + String(value) + "&submit=Submit\" -k");
  while(p.running()); 
  delay(2000);                
  
}

Thanks for the report, glad you got it working!

Wow, look at that code, I guess I was being conservative when I said only half of the other sketch would go away. See how much easier it is with the Yun? 8)

Yes, you're right. It is also good to know that it is ok to have https in Arduino. So there is no need to go through API... although there maybe some other reasons that people use API, just my lack of knowledge.