Balcony Bit

Erica, Shatha, James, Dania

icon-car.pngKML-LogoFullscreen-LogoQR-code-logoGeoJSON-LogoGeoRSS-LogoWikitude-Logo
Balcony Bit Sensor

loading map - please wait...

Balcony Bit Sensor 31.947180, 35.930805

BALCONY BIT SCRIPT

/* THIS IS A WORKING VERSION OF THE BALCONY BIT SCRIPT WHICH COLLECTS FORCE,
LIGHT, AND MOTION DATA AND TRIGGERS LED WHEN EITHER FORCE OR MOTION ARE ACTIVATED
DEPENDING ON THE LEVEL OF LIGHT NEARBY
*/
#include <SPI.h>
#include <WiFi.h>
#include <HttpClient.h>
#include <Xively.h>

char ssid[] = “TP-LINK_MR3040_8F7B3C”; // your network SSID (name)
char pass[] = “14507366”; // your network password (use for WPA, or use as key for WEP)
//int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

// Your Xively key to let you upload data
char xivelyKey[] = “HLHhsrTwQPjNbW3yXA2KyyLMNjabhJ2g7FPyWPIvxYRlK5zZ”;
//your xively feed ID
#define xivelyFeed 400894261
//datastreams
char sensorIDl[] = “LightCell”;
char sensorIDm[] = “MotionCell”;
char sensorIDf[] = “ForceCell”;
char ledID[] = “LEDlight”;

// Analog pin which we’re monitoring (0 and 1 are used by the Ethernet shield)
#define sensorPin_l A0
#define sensorPin_m 3
#define sensorPin_f A1
//led connected pin
#define ledPin 9

// Define the strings for our datastream IDs
XivelyDatastream datastreams[] = {
XivelyDatastream(sensorIDl, strlen(sensorIDl), DATASTREAM_FLOAT),
XivelyDatastream(sensorIDm, strlen(sensorIDm), DATASTREAM_FLOAT),
XivelyDatastream(sensorIDf, strlen(sensorIDf), DATASTREAM_FLOAT),
XivelyDatastream(ledID, strlen(ledID), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
XivelyFeed feed(xivelyFeed, datastreams, 4 /* number of datastreams */);

WiFiClient client;
XivelyClient xivelyclient(client);

void printWifiStatus() {
// print the SSID of the network you’re attached to:
Serial.print(“SSID: “);
Serial.println(WiFi.SSID());

// print your WiFi shield’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print(“IP Address: “);
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.print(rssi);
Serial.println(” dBm \n”);
}

//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;

//the time when the sensor outputs a low impulse
long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//pin setup
pinMode(sensorPin_l, INPUT);
pinMode(sensorPin_m, INPUT);
pinMode(sensorPin_f, INPUT);
pinMode(ledPin, OUTPUT);

digitalWrite(sensorPin_m, LOW);

Serial.println(“Starting single datastream upload to Xively…”);
Serial.println();

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print(“Attempting to connect to SSID: “);
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println(“Connected to wifi”);
printWifiStatus();
//give the sensor some time to calibrate
Serial.print(“calibrating sensor “);
for(int i = 0; i < calibrationTime; i++){
Serial.print(“.”);
delay(1000);
}
Serial.println(“done”);
Serial.println(“SENSOR ACTIVE”);
delay(50);
}
void loop() {
//adjust LED level. set from Xively
int getReturn = xivelyclient.get(feed, xivelyKey); //get data from xively
if(getReturn > 0){
Serial.println(“LED Datastream”);
Serial.println(feed[1]);
}else Serial.println(“HTTP Error”);

///////////////////////////////////////////////////////
//read sensor values
int sensorValue_l = analogRead(sensorPin_l);
datastreams[0].setFloat(sensorValue_l);

if(digitalRead(sensorPin_m) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
int sensorValue_m = 1023;
datastreams[1].setFloat(sensorValue_m);
}
if(digitalRead(sensorPin_m) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
int sensorValue_m = 0;
datastreams[1].setFloat(sensorValue_m);
}

int sensorValue_f = analogRead(sensorPin_f);
datastreams[2].setFloat(sensorValue_f);

//LED as function of force
int Value=map(sensorValue_f,0,1023,0,255);
Value=constrain(Value,0,255);
Serial.print(“value”);
Serial.println(Value);
analogWrite(9,Value-255);

//print the sensor valye
Serial.print(“light cell”);
Serial.println(datastreams[0].getFloat());
Serial.print(“motion cell”);
Serial.println(datastreams[1].getFloat());
Serial.print(“force cell”);
Serial.println(datastreams[2].getFloat());

//send value to xively
Serial.println(“Uploading it to Xively”);
int ret = xivelyclient.put(feed, xivelyKey);
//return message
Serial.print(“xivelyclient.put returned “);
Serial.println(ret);
Serial.println(“”);

//delay between calls
delay(15000);
}