im setting up a photon with a relay board to open a garage door, i have the code working to close relay for .3 sec to start door motor, from here im going to add a magnetic door switch to know when its closed, still yet to do or have code for,
i can use a curl function to operate the relay and also publish in the particle console, im just using a http widget on my phone
there are a few things i would like to do but unsure if its possible
can i get a push notification on my phone when the door is opened or closed reading the magnetic switch as reference
can i some how write an nfc tag to operate the door, i have the curl address and token that the http app is using but not quite sure what to use on the tag is possible and what happens then it is read?
also make it so only certain people can open it ie if it was outside
using samsung s21
any help or thoughts would be awesome still very new to this
thanks Karl
I had forgotten about this. You can, too. However, don't get your expectations too high here. It did not work too well for me, slow and inconvenient, but maybe there's much to improve in the way I did it or used it.
thank you, will take me a bit to work through this, currently trying to make the reed switch read, and publish closed or open when i active the relay but it loops between the both instead of only reading when i active the relay
Difficult to help without seeing your code, but a common mistake is to use INPUT without an external pull resistor on an open switch, which leaves the pin floating and potentially flip flop between HIGH and LOW.
i started off by just having a relay work momentary and publish garage door moving,
from there i got a magnetic reed switch, now would like to have something publish when door is closed or open but only the once when the door moves and not an endlessly publish, im thinking make relay work as it is and say door moving, then say where it is either open or closed,
but also be able to see if the door is operated by normal operation with a remote,
as i said before im not sure what im doing so code may not make any sence listed below, i have take out the loop code as it was not working so this is my current attempt
int relay = D6;
int magSwitch = D0;
void setup() {
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
Particle.function("SwitchRelay", SwitchRelay);
pinMode(magSwitch,INPUT_PULLUP);
}
int SwitchRelay(String command){
if (command == "open" || command == "OPEN" || command == "Open"){
digitalWrite(relay, HIGH);
delay(100);
digitalWrite(relay, LOW);
Particle.publish("garage door moving");
return 1;
}
return 0;
int switchState = digitalRead(magSwitch);{
if (switchState == LOW){
Particle.publish("Garage Door Closed");
}
else (switchState == HIGH);
Particle.publish("Garage Door Open");
}
}
void loop() {
Particle.function("SwitchRelay", SwitchRelay);
delay(3000);
}
If you had left the not working loop() code in place we could have spotted the error and made you aware of it and hence help you to make the same mistake again
Also Particle.function() must not be called multiple times for the same function name, but you do in loop() over and over - that call (usually) belongs in setup().
If you want to prevent the same state to be published over and over you need to store the state you last reported and only report whenever the current state diverges from the previous state.
I’m also not convinced the code you posted there would work at all, as the curly braces don’t seem to add up.
this is the extra code in loop, sorry didnt add it as i deleted it to stop it publishing,
as it sits it code verifys, i removed the particle.function,
i can control the relay still and displays in events saying door moving, but also prints open and closed every few sec
int relay = D6;
int magSwitch = D0;
void setup() {
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
pinMode(magSwitch,INPUT_PULLUP);
}
int SwitchRelay(String command){
if (command == "open" || command == "OPEN" || command == "Open"){
digitalWrite(relay, HIGH);
delay(100);
digitalWrite(relay, LOW);
Particle.publish("garage door moving");
return 1;
}
return 0;
int switchState = digitalRead(magSwitch);{
if (switchState == LOW){
Particle.publish("Garage Door Closed");
}
else (switchState == HIGH);
Particle.publish("Garage Door Open");
}
}
void loop() {
Particle.function("SwitchRelay", SwitchRelay);
delay(3000);
int switchState = digitalRead(magSwitch);
if (switchState == LOW) {
Particle.publish("Garage Door Closed");}
else (switchState == HIGH);
Particle.publish("Garage Door Open");
}
Incorporating the above advice (and some additional tweaks), this should solve the republishing issue.
void loop() {
/* Particle.function("SwitchRelay", SwitchRelay); <-- DO NOT DO THIS HERE -> it belongs in setup() */
static int prevState = -1; // store previously published state
static uint32_t msDelay = 0; // store timestamp of last publish
if (millis() - msDelay < 3000) return; // better than delay(3000) as it keeps the cloud connection responsive
msDelay = millis();
int curState = digitalRead(magSwitch);
if (curState != lastState) {
Particle.publish("Garage Door", curState ? "OPEN" : "CLOSED");
prevState = curState;
}
}
With this you can also remove the publishing calls from your SwitchRelay()
And instead of
you can write
if ( command.equalsIgnoreCase("open") ) {
This is simpler and matches all the possible upper/lower case combinations.
full code
const char event[] = "Garage Door";
const int pinRelay = D6;
const int pinReed = D0;
Timer timerReleaseRelay(100, releaseRelay, TRUE); // one-shot timer to release relay after 100ms
void setup() {
Particle.function("SwitchRelay", switchRelay);
pinMode(pinReed, INPUT_PULLUP);
pinMode(pinRelay, OUTPUT);
digitalWrite(pinRelay, LOW);
}
void loop() {
static int prevState = -1; // store previously published state
static uint32_t msDelay = 0; // store timestamp of last publish
if (millis() - msDelay < 3000) return; // better than delay(3000) as it keeps the cloud connection responsive
msDelay = millis();
int curState = digitalRead(pinReed);
if (curState != lastState) {
Particle.publish(event, curState ? "OPEN" : "CLOSED");
prevState = curState;
}
}
int switchRelay(String command){
if (command.equalsIgnoreCase("open")) {
digitalWrite(pinRelay, HIGH);
timerReleaseRelay.start();
Particle.publish(event, "MOVING");
return 1;
}
return 0;
}
void releaseRelay() {
digitalWrite(pinRelay, LOW);
}
ScruffR looking at what i was doing i had no hope of making this work,
the if ( command.equalsIgnoreCase(“open”) ) { is much better and easier
i have flashed the code and is work very well with some testing, pretty much everything in the loop im not sure how it works so i will be going through each step and following it,
but at the start i see you call the event “garage door” is this a better way so you can just write event? when it publishes, the event being garage door and state either open or closed is added, its nice and does it make it easier to have two options in the one line
For your simple project there is little advantage in that, but it's a good practice to adopt early on your path into programming to keep things maintainable and flexible.
When you scatter the same string literal over a huge project it will become inevitable that you may mistype it at least once and when you want to change it later on you will definitely fail to replace somewhere but will not get any indication about the issue as it will still be valid code as far it concerns the compiler.
Hence make it a global const at the top of your code and use that. If you mistype it the compiler will punish you for it immediately (but that's good as it will help eradicate the error there and then and not weeks after deployment )
Splitting the event name from the event information is also better practice.
Just like you wouldn't (normally) consider the cardboard box your gift comes in as intrinsic part of the gift itself.
I have read though so there post where nfc tag as used, now i can use a curl commard in cmd to active the relay so i now i have the correct token and id,
Question is
I am using an app, to try get nfc tag to send request,
Below is screen shot of 2 options,
I have tryed using both with curl command i use in cmd but still unable to make this work any thoughts or ideas i may of missed, the post request i use curl command up to token then parameters i write access token then token number,
Then another parameter put args and open,
It come up with a failure when i scan the nfc tag
Rather not have to pay a subscription to make this work
You post two screenshots but they don't show anything interesting at all.
You may want to fill in the form to show what exactly you did, not only that you downloaded an app and found some "empty" screens in it.
You might want to look at our RFID (NFC technology) card access system, at:
Re. Push notifications - we think that SMS texts to mobile phones are faster and more reliable than are push notifications. Furthermore, SMS does not require you to install any other app on the phone, and will even work with older style flip phones. You can do this with entirely free accounts that you already have: Particle, Google and your cell phone carrier. See the document in this account for details:
It shows history, and in with each time i tryed it shows one task executed with a time, nothing else,
On another note i have an http request app working so i have the correct info, i just cannot get it from an nfc tag to work, im unsure if its because its http instead of being https in the app or i need to do a login before it uses the command but the token and id are there so im unsure