Michele
Michele

Reputation: 27

UrlFetchApp bad request with IP address

I am trying to use a Google Apps Script to store analog input data from my Arduino Uno in a Google Spreadsheet. However, when I run my script, I receive a bad request error. Here is a screenshot of the error with the debugging panes at the bottom: Error screenshot.

I am following a tutorial to write the script: Daviom.com Arduino wi-fi server to Google Drive storage. I know that my Arduino is functioning properly because, per the tutorial, I am able to view the analog input data as a JSON object by navigating with my browser to my Arduino WiFi Shield's IP address and port.

Here is the code I am using:

/**

Google Drive Arduino fetcher

Query an Arduino Board and receive some data in JSON format: store everything
in a Spreadsheet

Author: Edoardo Scalafiotti [email protected]

*/

var spreadsheetId, arduinoIp;

spreadsheetId = '1m6KjZaAuFoj6nQOnPCzHQxSpCiGL-yNB4PcZrTfCzOU';
arduinoIp = 'http://192.168.1.103:8080';

function getData()
{

  var response, options, dataReceived, ss, sheet;

  options =
  {
    // Ensure we get a fresh copy of the site every time.
    headers : {'Cache-Control' : 'max-age=0'}
  };

  response = UrlFetchApp.fetch(arduinoIp,options);
  dataReceived = Utilities.jsonParse(response.getContentText());
  Logger.log(response.getContentText()); //comment out this line if you don't want debug

  ss = SpreadsheetApp.openById(spreadsheetId); // open the Spreadsheet
  sheet = ss.getSheetByName(dataReceived.sheet); // select the right sheet 
  sheet.getRange(sheet.getLastRow()+1, 1, 1, 3).setValues([[new Date(),dataReceived.projectid,dataReceived.value]]);
}

I am very inexperienced with Google Apps Script programming (and programming in general), so I am not sure what is going wrong. I tried defining arduinoIp and spreadsheetId inside the function loop, but that didn't seem to fix the problem. I also tried looking at previous stackoverflow questions, but I couldn't find any answers that seemed to fix this problem. Any help would be appreciated. Thank you, and please let me know if you need any further information.

Upvotes: 0

Views: 1391

Answers (1)

AshClarke
AshClarke

Reputation: 3078

You're trying to reach a local IP address which won't work because Apps Script executes on Google's servers.

You would need to forward a port on your router to your local arduino IP address so it's accessible from the the internet.

It's covered in the tutorial you're following under "Adjust your network settings"

Upvotes: 1

Related Questions