Turn Relays Off If No Internet Connection

Hey Guys,

I am wanting to find out how to add to the “Internet_Relay” cod in the library a function that will turn off all the relays (particle relay shield) if the particle doesn’t receive a valid network signal. Could someone help out?

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

// Create an instance of the RelayShield library, so we have something to talk to
RelayShield myRelays;

// Create prototypes of the Spark.functions we'll declare in setup()
int relayOn(String command);
int relayOff(String command);

void setup() {
    //.begin() sets up a couple of things and is necessary to use the rest of the functions
    myRelays.begin();

    // Use myRelays.begin(2); if you have the square, white RelayShield (from the Core)
    // to use, just add a '2' between the parentheses in the code above.

    // Register Spark.functions and assign them names
    Particle.function("relayOn", relayOn);
    Particle.function("relayOff", relayOff);
}

void loop() {
    // Nothing needed here, functions will just run when called
}

int relayOn(String command){
    // Ritual incantation to convert String into Int
    char inputStr[64];
    command.toCharArray(inputStr,64);
    int i = atoi(inputStr);
    
    // Turn the desired relay on
    myRelays.on(i);
    
    // Respond
    return 1;    
}

int relayOff(String command){
    // Ritual incantation to convert String into Int
    char inputStr[64];
    command.toCharArray(inputStr,64);
    int i = atoi(inputStr);
    
    // Turn the desired relay off
    myRelays.off(i);
    
    // Respond
    return 1;    
}

Thanks,

Trey

Use an IF statement?
https://docs.particle.io/reference/firmware/photon/#ready-

To have your code run without WiFi/cloud connection you will also need to choose an appropriate SYSTEM_MODE() and may also use SYSTEM_THREAD(ENABLED)

1 Like