Hi Everybody,
I recently got my spark core and have been loving it so far, but have hit a stumbling block with my latest project. Here goes…
I have an eight channel relay that I am trying to control with the Spark, so far it works fine from the tinker app. However, my ultimate goal is controlling it from a website, so I used the available relay shield code, see below:
int RELAY1 = A0;
int RELAY2 = A1;
int RELAY3 = A2;
int RELAY4 = A3;
void setup()
{
//Initilize the relay control pins as output
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
// Initialize all relays to an OFF state
digitalWrite(RELAY1, HIGH);
digitalWrite(RELAY2, HIGH);
digitalWrite(RELAY3, HIGH);
digitalWrite(RELAY4, HIGH);
//register the Spark function
Spark.function("relay", relayControl);
}
void loop()
{
// This loops for ever
}
// command format r1,HIGH
int relayControl(String command)
{
int relayState = 0;
// parse the relay number
int relayNumber = command.charAt(1) - '0';
// do a sanity check
if (relayNumber < 1 || relayNumber > 4) return -1;
// find out the state of the relay
if (command.substring(3,7) == "HIGH") relayState = 1;
else if (command.substring(3,6) == "LOW") relayState = 0;
else return -1;
// write to the appropriate relay
digitalWrite(relayNumber-1, relayState);
return 1;
}
But it doesn’t work, I have tried using curl in the terminal and Spark Helper both give me success messages but nothing happens on the Spark. Here is an example of a curl command and return:
curl https://api.spark.io/v1/devices/{MY-ID}/relay -d access_token={MY TOKEN} -d args=r1,LOW
{
"id": "MY-ID",
"name": "core-1",
"last_app": null,
"connected": true,
"return_value": 1
}
Same arguments in Spark Helper, I get a success message, but no cigar on the Spark.
Anybody have any ideas?