I have been building projects with PICAXE microp for five years and have moved on to the Photon, really excited! I want to trigger (a switch) to send an alert to my iphone via Pushover. I got the webhook working. In fact I can trigger an alert using the following code for the Photon but it hangs. Someone have a simple block of code I can use to get this little test project working?
void setup() {
pinMode(D0, INPUT);
}
void loop() {
if (digitalRead(D0) == HIGH) {
Particle.publish(“office-motion”, “OFFICE”, 60, PRIVATE);
while (digitalRead(D0) == HIGH); // hang tight here until motion stops
}
}
Here is my code for getting alerts via Ubidots when my PIR sensor was triggered. Not sure if it’s helpful but I can confirm it did work.
// This example is to save values with a setted data source name
#include "Ubidots/Ubidots.h"
#define TOKEN "TokenHere" // Put here your Ubidots TOKEN
#define DATA_SOURCE_NAME "PIR_Sensor_Test"
Ubidots ubidots(TOKEN);
bool value1 = LOW;
int ledPin = D7;
void setup() {
Serial.begin(115200);
ubidots.setDatasourceName(DATA_SOURCE_NAME);
pinMode(D0, INPUT_PULLDOWN); //Digital Input for PIR Sensor
pinMode(ledPin, OUTPUT); // Sets pin as output
}
void loop() {
bool value1 = LOW;
if (digitalRead(D0) == HIGH) {
digitalWrite(ledPin, HIGH); // Sets the LED on
value1 = HIGH;
ubidots.add("PIR Sensor 1", value1); // Change for your variable name
ubidots.sendAll();
//Particle.publish("office-motion", "OFFICE", 60, PRIVATE); //If you want to send a Webhook then you would use this line.
delay(60000);
value1 = LOW;
ubidots.add("PIR Sensor 1", value1); // Change for your variable name
ubidots.sendAll();
digitalWrite(ledPin, LOW); // Sets the LED off
while (digitalRead(D0) == HIGH); // hang tight here until motion stops
}
}
Thanks RWB! But, you are way ahead of my learning curve. I am just trying to flip a swt on the Photon and have it trigger an alert to Pushover. Something realllly simple!
One of the more important facts often gets overlooked.
Since you are dealing with a switch that does not supply a discrete voltage level when the button is open, your pin will float which can cause your digitalRead() may well be stuck HIGH even with the button untouched.
So you always need to apply pull-resistors (internal or external) with open pins.
As I assume from your code your button closes to 3.3V (don’t use Vin or any other 5V source) I’d write your original code like this
void setup() {
pinMode(D0, INPUT_PULLDOWN); // ensure a LOW signal with an open button
}
void loop() {
if (digitalRead(D0) == HIGH) {
Particle.publish("office-motion", "OFFICE", 60, PRIVATE);
while (digitalRead(D0) == HIGH); // hang tight here until motion stops
}
}
But to address some of the issues mentioned by @RWB and @BulldogLowell I’d slightly alter that code this way
void setup() {
pinMode(D0, INPUT_PULLDOWN); // ensure a LOW signal with an open button
}
void loop() {
if (digitalRead(D0)) {
uint32_t ms = millis();
Particle.publish("office-motion", "OFFICE", PRIVATE);
while (digitalRead(D0) || millis() - ms < 1000) // hang tight here until motion stops but at least one second
Particle.process(); // keep cloud connection alive while we are stuck in here
}
}
I think this should be the simplest working solution for what you asked for - without eny extra steepness in the learning curve.
Extra frontiers like the meaning of SYSTEM_THREAD(ENABLED) or how to us Ubidots or other useful tools can be postponed till you’re ready for that
ScruffR, the code above works great, thanks again. Now, to expand on this, how would I monitor two digital ports in this scenario and trigger different alterts? I have tried adding several lines to add D5 but the compiler does not like it!
Ok, I am trying to set up two switches on the Photon. I have the webhooks working but having some trouble with the code. Please see the error message. Thanks!
Looks like you may be missing a “;” after Particle.process() on line 19
When you see an error like this sometimes the line number it gives you like line 22 is right after the line that contains the error, so it’s a close hint to know where to look for the problem.