Good Day everyone. I was wondering if someone might be able to help me with a piece of code. I probably should say to get the code to work on a standalone html page. The code is to trigger a latching relay and the code works with IDE and through the app.
The reason that I use a standalone HTML page is that I like to keep some people out of the app or web IDE because their fingers just never seem to do what they should be doing.
I was wondering could I do two html links, one to turn the relay on and one to turn the relay off.
#include "Particle.h"
// You control the relay from the cloud.
// Calling the relay function can perform two different functions:
// particle call Power *** relay on so the device power will be off
// particle call Power *** relay off so the device power will be on
// Replace Power *** with the name of your device.
// Set the pins you've connected your latching relay SET and UNSET to here:
const uint16_t SET_PIN = D0;
const uint16_t UNSET_PIN = D8;
void setRelay(bool state);
int relayHandler(String param);
void setup() {
pinMode(SET_PIN, OUTPUT);
digitalWrite(SET_PIN, LOW);
pinMode(UNSET_PIN, OUTPUT);
digitalWrite(UNSET_PIN, LOW);
Particle.function("relay", relayHandler);
}
void loop() {
}
void setRelay(bool state) {
uint16_t pin = (state ? SET_PIN : UNSET_PIN);
// To change the relay state, bring either SET_PIN or UNSET_PIN high for 10 milliseconds
digitalWrite(pin, HIGH);
delay(10);
digitalWrite(pin, LOW);
}
int relayHandler(String param) {
if (param.equals("on")) {
setRelay(true);
}
else
if (param.equals("off")) {
setRelay(false);
}
else {
return -1;
}
return 0;
}
I am not sure if youâve seen the Particle API, but you can make a request to Variables and Functions which returns a JSON response. You could use Axios or Fetch to make the request.
I like using html because it does not matter if my users are using a laptop, phone or any other device. Currently I have a group of non latching relays connected to borons. I keep it simple so that they can remotely reboot items and they are super happy.
I like to keep it the same way so that they are familiar with the steps but this time just a latching relay.
So with the html page I can hit the relay and the json push back gives me -1 which means that it is hitting the code but does not know what I am requesting. So I have to figure out how to turn on and off the relay and it probably has to do with /relay?