[SOLVED]Unable to access PIR sensor status

I copied this tutorial and wired the PIR sensor the same way.

When I try to access the published results with a curl command along with my access token, I get an “:ok” and blank lines on the screen.
curl -H “Authorization: Bearer hiddenaccesstoken” https://api.spark.io/v1/events/spark-hq/

Every so often there is a carriage return but the lines are blank. Why can’t I see any data?

I also tried this based on the sample code but get a “Not Found” error:

curl -H “Authorization: Bearer hiddenaccesstoken” https://api.spark.io/v1/events/spark-hq/motion
What am I doing wrong?

 * Connected sensor
 * Spark.publish() + PIR motion sensor = awesome
 * Thanks to Adafruit for the reference and inspiration

int inputPin = D0;              // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int calibrateTime = 10000;      // wait for the thingy to calibrate

void setup() {
  pinMode(inputPin, INPUT);     // declare sensor as input

void loop(){
  if (calibrated()) {

bool calibrated() {
  return millis() - calibrateTime > 0;

void readTheSensor() {
  val = digitalRead(inputPin);

void reportTheData() {
  if (val == HIGH) {
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
  } else {
    if (pirState == HIGH) {
      // we have just turned off
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;

My own attempt was this code. I see that the led lights whenever I move in front of the PIR. But when I access the state of the sensor pin with curl the response is that the status is LOW and then the LED never comes on again. Each curl command just shows LOW. I don’t understand why the tinker digital read sample code I got from the website changes the digital input to pulldown. I tried commenting out this line of code but it didn’t help.

PIR Sketch a passive infrared motion sensor on pin 0 lights led on pin 7
const int ledPin = 7;
const int inputPin = 0;

int tinkerDigitalRead(String pin);

void setup() {
Spark.function("digitalread", tinkerDigitalRead);   
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);

void loop() {
int val = digitalRead(inputPin);

if (val == HIGH)
    digitalWrite(ledPin, HIGH);
    delay (1000);
    digitalWrite(ledPin, LOW);
else { // inputPin == LOW
      digitalWrite(ledPin, LOW);

 // The processing occurs faster
  // than the response from the PIR, and adding this delay
  // eliminated a flickering on the LED

int tinkerDigitalRead(String pin) {
    int pinNumber = pin.charAt(1) - '0';
    if (pinNumber< 0 || pinNumber >7) return -1;
    if(pin.startsWith("D")) {
       pinMode(pinNumber, INPUT_PULLDOWN);
        return digitalRead(pinNumber);}
    else if (pin.startsWith("A")){
        pinMode(pinNumber+10, INPUT_PULLDOWN);
        return digitalRead(pinNumber+10);}
    return -2;}

I believe any event starting with “spark” is reserved for Spark-only use. That naming might be a problem, so try changing that to something other than “spark”. You can then paste this URL in your browser to see all your events: https://api.spark.io/v1/devices/events?access_token=PASTE_YOUR_ACCESSTOKEN_HERE
Let me know if that worked for you.

1 Like

That is progress, thanks.
I changed the Spark.publish("spark-hq/motion"); to Spark.publish("PIR/motion");

now I get this output whenever I move in front of the motion sensor. Something is happening during
the auto-calibrate perhaps?

event: PIR/motion
data: {"data":"null","ttl":"60","published_at":"2015-01-18T20:49:22.790Z","coreid":"hidden"}

I’m not sure what you’re expecting it to do other than this? This is the expected output from a Server Sent Event. Seems like it’s working :wink:

Thanks again. I thought the Serial.println(“Motion detected!”); was for the Spark.publish result.
Now I realize that when I connect a serial monitor I see the verbose output when there is motion.

Where can I read more about Spark.publish to understand the parameters data, null, ttl 60 etc?
I searched but didn’t find anything in the documentation to elaborate on this.

Well, that's why it says Serial :stuck_out_tongue:
I'll assume that you've seen the docs in regards to publishing, but I'll mention them here anyway: http://docs.spark.io/firmware/#spark-publish

It's a JSON response, and can be seen as such:

  name: 'TempHumi',
  data: '{"t":17.60,"h":48.50}',
  ttl: '60',
  published_at: '2015-01-19T10:49:07.856Z',
  coreid: 'HIDDEN' 

The name is basically the identifier which you can use to differentiate your event from others.
The data is where you'd put your data, if you have any.
The ttl is the Time To Live, which regulates how long the event is available. from the docs:

NOTE: The user-specified ttl value is not yet implemented, so changing this property will not currently have any impact.

The published_at is the time at which your Core published your event, so it's easy to now when it fired.
And the coreid is there to tell you who fired the event.

The nice thing about the data is that you can put your own data in there. The example above contains nested JSON, which is easy to parse. You can also just put a message in there, to be displayed somewhere. It's rather flexible.
I'm not sure what else there is to tell about this, but feel free to ask if there's anything specific you'd like to know, and I'll try my best to explain :smile: