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
Serial.begin(9600);
}
void loop(){
if (calibrated()) {
readTheSensor();
reportTheData();
}
}
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!");
Spark.publish("spark-hq/motion");
// 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
//delay(1000);
}
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;}
