Using Post command through browser but ultimately google script

i would like to be able to communicate with the spark core through google scripts so i can use the spreadsheets as a way to store data and then pass variables back and forth, i am having trouble sending the post request with google in a way that the spark core recognizes. I also have questions as to the exact way to send a post request without using google script to check the communication. the spark core code is below, right now i am just trying to pass back and forth the day and tmeofday which correspond to a row and column in my spreadsheet. any help would be greatly appreciated. i am able to read the temp off the cloud using urlfetch with google. so it is mainly the post functions that i have issue with.

thanks in advanced

// -----------------
// Read temperature
// -----------------

// Create a variable that will store the temperature value
float temperature = 0.0;
char tempstr[64];
char motionstr[64];
int ledh = D0;
int ledc = D1;
//Create variables that will track motion
int day = 9;
int tmeOfDay = 2;
int inMotion = 0;
int outMotion = 0;

void setup()
  // Register a Spark variable here
  Spark.variable("temperature", &tempstr, STRING);
  Spark.variable("outMotion", &motionstr, STRING);
  Spark.variable("day", &day, INT);
  Spark.variable("tmeOfDay", &tmeOfDay, INT);
  Spark.variable("inMotion", &inMotion, INT);
  // Register a Spark Function here
  Spark.function("inMotion", inMotionStore);

  // Connect the temperature sensor to A7 and configure it
  // to be an input
  pinMode(A7, INPUT);

void loop()
  int reading = 0;
  double voltage = 0.0;

  // Keep reading the sensor value so when we make an API
  // call to read its value, we have the latest one
  reading = analogRead(A7);

  // The returned value from the Core is going to be in the range from 0 to 4095
  // Calculate the voltage from the sensor reading
  voltage = (reading * 3.3) / 4095;

  // Calculate the temperature and update our static variable
  temperature = ((voltage - 0.5) * 100) * 1.8 + 32;
  sprintf(tempstr, "{\"temperature\":%2.1f}",temperature);
  outMotion = 1;
  sprintf(motionstr, "{\"day\":%d,\"tmeOfDay\":%d,\"outMotion\":%d}",day,tmeOfDay,outMotion);
  int inMotionStore(String command)
      int commandNumber = command.toInt();
      if(command.charAt(0) == 1)
       day = commandNumber - 10; 
      if(command.charAt(0) == 2 && command.length() == 3)
       tmeOfDay = commandNumber - 200;
      if(command.charAt(0) == 2 && command.length() == 2)
       tmeOfDay = commandNumber - 20;
      if(command.charAt(0) == 3)
       inMotion = commandNumber - 30;
     outMotion = inMotion;

I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy

First you need to drop the ampersand & in these two lines, since without [] it’s already an address.

  Spark.variable("temperature", &tempstr, STRING);  
  Spark.variable("outMotion", &motionstr, STRING);

And your function inMotionStore() should return an int value.
But the functionality in there looks a bit crude, but since I don’t actually know what you want to do with it, I guess it’s OK.

Unless you need your Spark.variable strings JSON encoded, you could just build them as you actually want them, since they will be wrapped up propperly by the Core firmware anyway.

Please read this thread how to format code propperly.

i am trying to use the function to pass variables to the spark core, if there is a better way to do this i am interested in understanding how. i have been unable to do a post request through a browser or google scripts fetchurl post. I am very new to this and am learning as i go but the information on the web for how to create a post request and have the spark core change the values of the variables that i am trying to modify is not going well. i am not sure if it is the setup of my spark code or how i am sending the POST. i checked what i was sending using request bin and it weened like i was sending the correct information but the variables were not updating.

This topic contains quite some useful information on this:

Read through it, and definitely try out the tools mentioned in there. They’ll allow you to easily test your functions and variables without having to worry about how to create certain requests. Once your code is known to be functional, you can move on to the whole POST/GET stuff.

Thanks @Moors7 I actually just found that topic and used Postman to send a POST that actually went through…didn’t update variables though so i will keep trying.
Thanks again

I’d suggest you put some sort of LED blink in your function, so you’ll get some visual confirmation things got through. The other tools mentioned in that topic are also worthwhile.

@gsheman, as I said in my previoius post

There are several ways to fix this.

  1. Since you already have a commandNumber, why not use this in your if statements instead of charAt()
  2. If you want to stick with charAt() you’d have to compare command.charAt(0) == '2' instead of the number 2 which would be a non-printable character ('2' = 50 = 0x32)
  3. switch() case would be also an option
1 Like

@ScruffR That did it, I missed the apostrophe when writing it the first time. I now have google scripts passing variables back and forth and recording the data. Thanks guys.

1 Like