I don't understand how this all works

So I have a Particle and a relay shield.

How do I access the Particle from the internet? I have used an Arduino Yun and it ran its own web server so connecting to it was trivial. Do I have to run my own web server?

Error:

relay1.ino: In function 'void setup()':
relay1.ino:8:12: error: 'RELAY1' was not declared in this scope
relay1.ino:9:12: error: 'RELAY2' was not declared in this scope
relay1.ino:10:12: error: 'RELAY3' was not declared in this scope
relay1.ino:11:12: error: 'RELAY4' was not declared in this scope
relay1.ino: In function 'int relayControl(String)':
relay1.ino:43:11: error: expected '}' at end of input
../build/module.mk:267: recipe for target '../build/target/user/platform-6-msrc/relay1.o' failed
make[2]: *** [../build/target/user/platform-6-msrc/relay1.o] Error 1
make[2]: Leaving directory '/firmware/user'
../../../build/recurse.mk:11: recipe for target 'user' failed
make[1]: *** [user] Error 2`
make[1]: Leaving directory '/firmware/modules/photon/user-part'
../build/recurse.mk:11: recipe for target 'modules/photon/user-part' failed
make: *** [modules/photon/user-part] Error 2

Also the sample code for the reals shield will not compile

// This #include statement was automatically added by the Particle IDE.
#include <RelayShield.h>


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 Particle function
   Particle.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+2, relayState);
  return 1;

You are missing a } to close the function called relayControl(String command)

In addition to @kennethlimcp’s comment, the error messages are rather clear.

You haven’t declared any of the RELAYx variables in your code, and the #define statements present in the library are probably not visible in your .ino file, but even if they were they are not defined as all capital letters.

If you look at the examples in the library, you’ll see how to write the firmware for the Relay Shield
https://build.particle.io/libs/RelayShield/0.0.6/tab/example/3_Internet_Relays.cpp

And to check the function via the internet, you can go to
https://console.particle.io/devices
and trigger the exposed functions

To see how you can control your device otherwise, you can have a look at this tutorial
Tutorial: Spark Variable and Function on One Web Page

2 Likes

Interesting all this stuff is missing as this code was copied and pasted directly from

https://docs.particle.io/datasheets/particle-shields/#relay-shield-sample-code

But I did add the library.

What the point of sample code that does not work?

What does “exposed functions” mean, I see that term used a lot in the docs but don’t know what it means.

So I do have to set up my own webserver to use this?

Hi @Bobzilla

The sample code does indeed work fine, you just didn't cut and paste all of it. Not a big deal. The relays are defined at the top of the sample:

int RELAY1 = D3;
int RELAY2 = D4;
int RELAY3 = D5;
int RELAY4 = D6;

An "exposed function" is something you declare using Particle.function(). It is a way of letting you use the Particle cloud to call a function remotely on your Photon or Electron. A Particle function always takes a String argument and returns and int that generally indicates success or failure of the call, but you can decide what it means.

Here is a link to a tutorial I wrote a long time ago using both functions and variables. Note that Spark.function is now Particle.function and a few other small changes.

You will see that you don't need your own webserver to use any of this--any modern browser will work. If you do have a webserver, that can let you do more advanced things, but it is not required.

4 Likes