App help relay shield

I have a spark relay shield and have flashed it with my build code and my core seems to be okay. I just can’t figure out how to interact with my app !

Jedd, you need to explain a little more. What is your “build code” supposed to do? How do you expect to interact with it?

More importantly, what do you mean by ‘app’? Did you write an app yourself, and if so, on which platform?
If you mean the standard Tinker app, well, that’s not going to work. That is, not without modifying it. The app on your phone communicates with a sketch on your :spark:, dubbed “tinker” as well. You need to provide the expected code for your own sketch, if you want it to work together with the app. The sketch itself can be found here and should be identical to what is being flashed when a factory reset is executed (apart for changes made after manufacturing, but it’ll be highly similar I guess).
You should be able to paste in your own code so that it’ll respond to input from the app. Even better would be if you implemented the relevant code into your own sketch.

Best of luck, and let us know if you run into any problems.

Here’s the code I borrowed from defsdoor relay shield rev 2

int RELAY1 = D0;
int RELAY2 = D1;
int RELAY3 = D2;
int RELAY4 = D3;

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, LOW);
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY4, LOW);

//register the Spark function
Spark.function(“relay”, relayControl);
}

void loop()
{
// This loops for ever
}

int tokenise(char * command, char * tokens[], int max_tokens) {
int n = 0;
char * p = command;
while (n<max_tokens) {
tokens[n]=p;
while (*p != ‘\0’ && *p != ‘,’) { p++; };
if (*p == ‘\0’) return(n+1);
*p++ = ‘\0’;
n++;
}
return(n);
}

// command format r1,HIGH
int relayControl(String command)
{
char char_command[64];
char * tokens[20];
int token_count;
int relayNumber;
int relayState;
int n = 0;

command.toCharArray(char_command, 64);
token_count = tokenise(char_command, tokens, 20);
if (token_count >=20 || token_count == 0) return(-1);

while (n<token_count) {
if (tokens[n][0] == ‘R’ || tokens[n][0] == ‘r’) relayNumber = tokens[n][1] - ‘0’;
else relayNumber = tokens[n][0] - ‘0’;
n++;

relayState=-1;
if (strncmp(tokens[n],"HIGH", 4) == 0 || strncmp(tokens[n], "1", 1) ==0 ) relayState = 1;
else if (strncmp(tokens[n],"LOW",3) == 0 || strncmp(tokens[n], "0", 1) ==0 ) relayState = 0;

n++;

// do a sanity check
if (relayNumber < 1 || relayNumber > 4 || relayState==-1) continue;

digitalWrite(relayNumber-1, relayState);

}

return 1;
}

Jedd, I can’t vouch for whether the code is working or not but you can send commands using the Spark API helper to do your testing. :slight_smile:

Actually I couldn’t see a reason why the Relay Shield shouldn’t work with the Tinker App & Tinker firmware.
The Relay Shield only uses the HIGH/LOW states of the D0,D1,D2,D3 pins which you can easily controlled by setting the Tinker pins to digitalWrite and then toggle the states.
I guess this would actually be the easiest way to get started and to see if the Core and Shield are OK - since there is virtually no way to do anything wrong.
Take you fresh Core and fresh Relay Shield out the box or make a factory reset on the Core, get the Core connected to your network, push it into the Shield, start tinkering D0-D3.

ScriffR. you are absolutely right! I’m just not sure what Jedd is trying to do. :smile:

Sure, knowing that would help helping a lot :wink:

I just wanted to prevent some misconceptions to manifest in peoples minds and to show a quick and easy way to first test the equipment before diving into changing things and ending up with not being sure if it’s a HW problem out the box or a selfintroduced SW issue.

1 Like

You’re right of course. With the Tinker app & firmware, there shouldn’t be a problem. But since Jedd mentioned that he flashed it with his (own) build code, I was trying to explain that the Tinker app only works in conjunction with the relevant Tinker firmware. I, too, have spent quite some time fiddling with the Tinker app, to later find out it only works when the Tinker firmware is still installed, which is no longer the case as soon as you flash your own application.
So I was just trying to make sure he wasn’t making the same mistakes I was. :wink:

I got the app working with the web API link you sent me.

Device ID and Token fields populated. Then I did the following and press POST.

Function name = relay
Data = 1,1

My only remaining question is What does the URL look like for the example above?
Thanks for everyone’s help!

I see :smiley: , sorry I didn’t get this.
But to be even more specific - you’d not necessary need the actual Tinker FW running on the Core to have it work with Tinker App. Your own FW would just need to implement the Tinker interface (cloud functions digitalread, digitalwrite, analogread, analogwrite and the respective connected functions need to be able to parse the Tinker App commands).

@Jedd:
I’m not sure if this is a bad copy/paste or if you’re actually missing one important line in the code above. In int relayControl() you seem to be missing a statement that actually sets the relayNumber - (and some brackets).
And what do you actually mean with “What does the URL look like”?
First of, you can’t just put an URL into any browser address line to talk to the Core, since this would only create a POST request, but you need to do a GET request, which you could do via curl or the way it is done in Spark API helper (although it is a bit misleading that the buttons are labeld POST, where it rather triggers a getJSON() :wink: ) or …
Or do you mean how you’d have to GET the command across to the Core from a Smart Phone app?

BTW:
You stated that you got the app working.
What “app” did you mean? If you are refering to the software running on the Core, you could avoid ambiguity by using “firmware” or “FW” instead of “app”.
And if you meant the FW, did it actually work as expected, when you pressed POST? (Which I’d be surpriesed, given the relayNumber issue)

Sorry for the incorrect terminology.

FW is working.

Will I have to write an Iphone app like tinker to interact with FW I now have loaded on my core?

If you don’t want to write your own iApp, you could write your own HTML/Javascript page to interact with your core and put it on a private service like Dropbox. You then navigate to that page in the Dropbox app and tap to open it like a web page. That way your access token remains secret.

Another way would be to use a web page on any service but make it be a form that you need to add your access token to every time.

A third way would the Spark tools that some folks are using. You need to enter you key there so it can use it for you.

The one thing you should not do is make a web page for all to view that has your access token in it.