hw3 :: delirious home :: step 1 :: motion sensor

Dali Atomicus, photo by Philippe Halsman (1948)

If our  everyday artifacts and objects were to become capable of sensing and perceiving what they are detecting, so how can we make sure the response is shaping the networks of our environments the way we want?

So I am imagining a delirious home where the internet of things is out of control… something like this :

Prototype :

As a starting point I am working with a motion sensor that I will place at the entrance door of the apartment to detect people, pets… passing by my door… the sensor will send data to the internet when motion is detected. I  will work with the data and affect another object in the home. I am using the Arduino Yun to connect to the internet and send a data stream on sparkfun.

The motion sensor is now working and lighting up pin13 on the Yun.

However, the data doesn’t seem to want to go online. So I’m still working on that.

Here’s the code

/*
Simple data.sparkfun.com example

go there to get the public and private keys needed to make this example work

*/

// include all Libraries needed:
//#include <Process.h>
#include <Bridge.h>
#include <HttpClient.h>
#include “password.h”      // contains my passwords, see below

/*
NOTE: passwords.h is not included with this repo because it contains my passwords.
You need to create it for your own version of this application.  To do so, make
a new tab in Arduino, call it passwords.h, and include the following variables and constants:

#define APIKEY        “foo”                  // replace your sparkfun secret key here
#define FEEDID        “0000”                  // replace your sparkfun public key here
*/

// set up net client info:
const unsigned long postingInterval = 10000;  //delay between updates to xively.com
unsigned long lastRequest = 0;      // when you last made a request
String dataString = “”;

//pin motion sensor
int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = HIGH;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

void setup() {

//setup motion sensor
pinMode(ledPin, OUTPUT);      // declare LED as output
pinMode(inputPin, INPUT);     // declare sensor as input

// start serial port:
Bridge.begin();
Console.begin();

while (!Console);   // wait for Network Serial to open
Console.println(“data.sparkfun client”);

// Do a first update immediately
updateData();
sendData();
lastRequest = millis();

Serial.begin(9600);
}

void loop() {
// get a timestamp so you can calculate reading and sending intervals:
long now = millis();

// if the sending interval has passed since your
// last connection, then connect again and send data:

val = digitalRead(inputPin);  // read input value
Serial.println(val);
Console.println(val);
if (val == HIGH) {            // check if the input is HIGH
digitalWrite(ledPin, HIGH);  // turn LED ON
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;
updateData();
sendData();
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println(“Motion ended!”);
// We only want to print on the output change, not state
pirState = LOW;
}
}
}

void updateData() {
// convert the readings to a String to send it:
dataString = “motion=”;  //field names
dataString += val; //random number send –> data from sensor
}

// this method makes a HTTP connection to the server:
void sendData() {
// form the string for the API header parameter:
// form the string for the URL parameter:
String url = “http://data.sparkfun.com/input/”;
url += FEEDID;
url += “?private_key=”;
url += APIKEY;
url += “&”;
url += dataString;

// Send the HTTP GET request

HttpClient client;
//Process xively;
Console.print(“\n\nSending data… “);
Console.println(url);
client.get(url);
Console.println(“done!”);

// If there’s incoming data from the net connection,
// send it out the Console:
while (client.available()) {
char c = client.read();
Console.print(c);
}
Console.flush();

}

Leave a Reply

Your email address will not be published. Required fields are marked *