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