Help needed with code for a Relay Shield project

Hi people and many thanks for taking time out to read my request for help with a Relay Shield project.

I’m using my Spark Core and Relay Shield to switch some mains powered lamps sitting in our office. I’ve got everything set up and working nicely and we have a little web service that let’s people change pin states using a function exposed to Spark Cloud.

One problem I am having is that when the web service loads it does not know the current state of each pin. I want to create a new function, or a Spark.variable, that outputs the state of each pin, as a STRING array: “HIGH | LOW | HIGH | HIGH”, for example.

I’m having difficulty wrapping my head around the Variables because I can’t find much in the way of documentation. I can think of some long winded ways around this but in the interest of streamlining the code I am in the market for some advice.

Has anyone done this for another project they can point me to? If not, does anyone have suggestions?

Thanks in advance!

Assuming you want multiple people to control the device at the same time, I wouldn’t go with variables. Variables is a polling process, and is not very friendly when it comes to real time information, unless you poll every second, which is a waste of resources. I’d rather store the states of the pins on the Core, and use a Server Sent Event to publish the values as soon as one changes. This has the benefit of only using resources when something happens, as soon as something happens. Since this doesn’t require action on the client side, those can be updated real time, without needing to poll repeatedly.
To get the initial values when opening the web page, you could create a function that publishes the values, which gets called as soon as the page is loaded. I’m currently using this for my RGB lighting project, and it seems to work fine so far :slight_smile:
Let me know if you need any help, and I’ll see what I can do.

Hi @Moors7 and thanks for getting back to me with your suggestion. I’ll have a play today and see if I can work it out, and come back to you if (or more likely when) I get stuck. Thanks again.

1 Like

Hi @roomman

You might want to look at this thread where I helped another user with his relays controlling aquarium pumps. Both firmware code for the core and HTML are provided. For this application, the user wanted the durations in seconds rather than minutes or hours:minutes, but it could be easily changed.

Hi @bko, that’s fab. Many thanks for sharing. I’ll let you know how I get on.

Hi @bko / @Moors7 and thanks again for your messages.

Do Spark functions only return an integer or can they return a string?

Thanks in advance.

As per the docs:

The Spark function can return any integer;

What would you like to do with it? Perhaps we can offer useful alternatives.

Hi @Moors7,

I’d hoped the use some of the logic from the link @bko passed on, whereby I have an array (int relayState[4] = {LOW,LOW,LOW,LOW}:wink: and a function that loops through the four relay pins and assign a value (HIGH or LOW) to each position in the array, and then I’d hoped to output that to my web service.

Does that make sense - I have to confess that I’m an eager learner but have basic knowledge so might be trying to do something very naive here.

Thanks in advance!

It would be “hackish” but you could return a integer with your function call that represented the pin states as bits.

Ie, Imagine the current states are:

  1. Low (0)
  2. Low (0)
  3. High (1)
  4. Low (0)
  5. High (1)

Your function would return 0b10100 which would represent the pin states. It wouldn’t be too hard to extract the info on the other end either

Hi @harrisonhjones, thanks. What would the best non-‘hackish’ approach be? Four functions, one to check the state of each relay pin?

I’d probably go with @Moors’ solution. Something similar to:

  • Have your core emit an event every time a pin changes. This event’s data would simply be that binary number I mentioned.
  • Have a function which, when called, emits the event and then perhaps returns the binary number itself as the return value.

I said it was hackish because POST calls aren’t really supposed to have to return a value (other than success or failure). You can leverage the fact that it does but it just feels a little “dirty” to me.

Okay, many thanks for your help.

You could also make the event data structured as JSON for easy parsing on the receiving end, but I’m not sure if that’s necessary. The binary data should suffice.

1 Like