HTML help for Latching relay - function

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.

my html link is

<form action="https://api.particle.io/v1/devices/*****DEVICE*****/relay?access_token=*****TOKEN*****" method="POST"> <button class="button">\\<form action="https://api.particle.io/v1/devices/<redacted>/relay?access_token=<redacted>" method="POST"> <button class="button">https://api.particle.io/v1/devices/*****device*****/relay?access_token=*****token*****" method="POST"></a>
		</form></a>
		</form>

This returns

{"id":"*****DEVICE*****","connected":true,"return_value":-1}

The code is not find and was found online.

#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;
}

Hello,

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.

Here you can see how to check a function: Cloud API | Reference Documentation | Particle.

Sorry, I didnt notice that my code excluded some info.
I update my html link, and this like is the request and the json response is also posted.

I have a html like that is

\ https://api.particle.io/v1/devices/device/relay?access_token=token" method=“POST”>

the json response is

{“id”:“ DEVICE ”,“connected”:true,“return_value”:-1}

Do you think that there is a way to make a relay turn “on” or “off” via the html code?

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.

Hello,

If I am understanding correctly, you’re trying to create a POST but the API does require a JSON encoded request.

Hi, this is old but may be of help:

Best

2 Likes

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?

I will be reading this tonight

I believe that this will work and I found it here. The JSON response looks correct but will have to wait until Thursday to test it out.

"https://api.particle.io/v1/devices/DEVICE/relay?access_token=TOKEN" method="POST"

Tell your device what to do!

 <input type="radio" name="arg" value="on">Turn the relay on.

 <input type="radio" name="arg" value="off">Turn the relay off.


 <input type="submit" value="Enter!">
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.