Trying out my first app on a Photon-Help!

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

Not exactly what your trying to do but close.

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!

Since you are holding in that while loop, the device isn’t getting to housekeeping the system (WiFi) functions.

Try adding this to the top of your program:

SYSTEM_THREAD(ENABLED)

and find some non-blocking method to get rid of that while loop… you will need to get into that habit using this device.

Look at the Arduino State Change example…

Understood.

Checkout Ubidots though because it’s the easiest way to get data into an online dashboard and database that I have found so far. It’s free also!

Do you need to set up a webhook to use this service? Or just the code you have included above?

That code at the top includes the Ubidots library which allows you to use the Ubidots functions in the code.

Ubidots has Particle examples you can try out to get a better understanding of how it works.

It's easy and well worth your time to setup. You send SMS and email alerts based on any settings you want for data you send over to it.

That turns on the system thread, which will be able to process all of the Particle services sort-of behind-the-scenes.

More on System Thread here

Try it and let us know!

Well, it does not hang now and I can trigger once then it goes into a solid blue mode. Have to reset it and it will repeat this? Solid blue light?

Do you know how to get rid of that while loop?

Another thing to consider is that your PIR sensor may be too sensitive and just stay Alerting which may be why you never see the BLUE led turn off.

What type of PIR sensor are you using? Is it’s sensitivity adjustable?

Where is the PIR sensor being pointed towards?

you can try like this:

SYSTEM_THREAD(ENABLED)

void setup() 
{
  pinMode(D0, INPUT);
}

void loop() 
{
  static int lastState = LOW;
  int currentState = digitalRead(D0);
  if(lastState != currentState)
  {
    if(currentState == HIGH)
    {
      Particle.publish("office-motion", "OFFICE", 60, PRIVATE);
      delay(1000);
    }
    else
    {
      Particle.publish("no motion", "OFFICE", 60, PRIVATE);
      delay(1000);
    }
    lastState = currentState;
  }
}

not tested/compiled

Thanks!
Got to drop it for the night but will pick up tomorrow and will try it then.

1 Like

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 :wink:

1 Like

ScruffR,
That did it! Thanks a bunch!

2 Likes

Thanks @ScruffR, @RWB, @BulldogLowell for the help!

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!

Showing your code and the error messages would be a first step. We don't usually spoon feed solutions :wink:

1 Like

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 :slight_smile:

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.

1 Like