I have an electron I am trying to setup for an alarm for my 30’ enclosed car hauler. I’ve read through the docs, and checked some tutorials, but cannot seem to get the information I need, or I am overlooking something. Basically, I want a text message to my phone anytime a reed switch is activated by unauthorized entry. Here is my code:
//firmware
int door1 = D1;
int door2 = D2;
int door3 = D3;
void setup() {
pinMode(door1, INPUT);
pinMode(door2, INPUT);
pinMode(door3, INPUT);
}
void loop() {
if (digitalRead(door1) == HIGH) {
Particle.publish("Christine6923","Breakin Door1", PRIVATE);
}
if (digitalRead(door2) == HIGH) {
Particle.publish("Christine6923","Breakin Door2", PRIVATE);
}
if (digitalRead(door3) == HIGH) {
Particle.publish("Christine6923","Breakin Door3", PRIVATE);
}
}
It fires off a text as soon as the electron boots up and connects to the cloud (I assume this is normal) After that, it doesn’t seem to send a text when prompted, but if I leave it on during the day, I get 60+ random messages without anyone near the device.
your close to getting it right with code formatting, you have used the ’ instead of the tilda ` you should be able to edit your own post to fix it.
try changing INPUT to INPUT_PULLDOWN that will keep the input low unless its actually pulled up. otherwise it will float and every now and then go just high enough to trigger.
also the code will run very very fast and there is nothing stopping it continuously publishing, so you will flood the server very very quickly. not sure how many loops the electron does a second but its likely to be over 100!
try adding this code, its not perfect and the device will publish every 60 sec the door remains open, but its a good place to start. a better way would be to use interrupts. a search of the forums should help. debouncing should also be used
unsigned long Lockout; // used to stop flooding servers
//in setup()
Lockout = millis();
// in loop()
if (millis() >= Lockout){ // check how long its been since last opened
if(digitalRead(door1) == HIGH) { //read the door switch
delay(50);
if(digitalRead(door1) == HIGH) { // debounce check again to make sure
Lockout = millis() + 60000; // set up the lockout period 60 seconds before it will run again
RGB.control(true);
RGB.color(0,255,0); // make the LED go green so you know its triggered
Particle.publish("Christine6923","Breakin Door2", PRIVATE);
delay(1000); //so we can see the LED for 1 sec
RGB.control(false); // put the LED back to normal
}
}
}
Sorry for the nit picking It's not a tilde (~) but a grave accent and there are also no blanks allowed in the line.
Also, if you want to format text, like your json block, you'd use
Got it guys, looking at the tips and tricks, I thought it was a single quotation mark.
As far as the code, it looks like the changes @Hootie81 suggested did the trick, I won’t know for sure until I install it today, but I’m not getting random messages like before, at least not yet. Thanks for all your help.