Athina, Nour, Haitham, Foaz
Arduino Code for unloading sensor date online
/*
##Xively WiFi Sensor Tutorial##
This sketch is designed to take sensors (from photocell) and upload the values to Xively
at consistant intervals. At the same time it gets a setable value from Xively to adjust the brigthness
of an LED. This sketch is reusable and can be adapted for use with many different sensors.
Derived from Xively Ardino Sensor Client by Sam Mulube.
By Calum Barnes 3-4-2013
BSD 3-Clause License – [http://opensource.org/licenses/BSD-3-Clause]
Copyright (c) 2013 Calum Barnes
*/
#include <SPI.h>
#include <WiFi.h>
#include <HttpClient.h>
#include <Xively.h>
char ssid[] = “awesome”; // your network SSID (name)
char pass[] = “”; // 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[] = “l3A3385tsgfSfT8DF6rlUR2WHLsJ6y8sQ7PykUdncpVVlQkG”;
//your xively feed ID
#define xivelyFeed 1395314825
//datastreams
char sensorID[] = “FLEXSENSOR”;
char ledID[] = “LED”;
// Analog pin which we’re monitoring (0 and 1 are used by the Ethernet shield)
#define sensorPin A5
//led connected pin
#define ledPin 13
// Define the strings for our datastream IDs
XivelyDatastream datastreams[] = {
XivelyDatastream(sensorID, strlen(sensorID), DATASTREAM_FLOAT),
XivelyDatastream(ledID, strlen(ledID), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
XivelyFeed feed(xivelyFeed, datastreams, 2 /* 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”);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//pin setup
pinMode(sensorPin, INPUT);
pinMode(ledPin, OUTPUT);
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);
//keyIndex,, pass
// wait 10 seconds for connection:
delay(10000);
}
Serial.println(“Connected to wifi”);
printWifiStatus();
}
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”);
//write value to LED – change brightness
int level = feed[1].getFloat();
if(level < 0){
level = 0;
}else if(level > 255){
level = 255;
}
//actually write the value
digitalWrite(ledPin, level);
///////////////////////////////////////////////////////
//read sensor values
int sensorValue = analogRead(sensorPin);
datastreams[0].setFloat(sensorValue);
//print the sensor valye
Serial.print(“Read sensor value “);
Serial.println(datastreams[0].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);
}
Arduino code for flex sensor triggering LED
/*
Flex Sensor and LEDs created by ScottC on 23rd May 2011
updated on 16/05/2012.
—————————————————–*/
//Flex Sensor Pin (flexPin)
//the analog pin the Flex Sensor is connected to
int flexPin = 0;
void setup() {
for (int i=4; i<14; i++){
pinMode(i, OUTPUT); //sets the led pins 4 to 13 to output
}
}
void loop(){
//Ensure to turn off ALL LEDs before continuing
for (int i=4; i<14; i++){
digitalWrite(i, LOW);
}
/* Read the flex Level
Adjust the value 130 to 275 to span 4 to 13
The values 130 and 275 may need to be widened to suit
the minimum and maximum flex levels being read by the
Analog pin */
//I got the range of sensitivity via a code that reads sensor values
//int flexReading = map(analogRead(flexPin), 145, 290, 4, 13);
int flexReading = map(analogRead(flexPin), 175, 185, 4,13);
// Make sure the value does not go beyond 4 or 13
int LEDnum = constrain(flexReading, 4, 13);
/*Call the blink function: this will turn the LED on for 10 milliseconds, and keep it
off for only 1 millisecond. You can change the blink rate by changing these values,
however, I want a quick response time when the flex sensor bends, hence the small
values. LEDnum determines which LED gets turned on.*/
blink(LEDnum,10,1);
}
// The blink function – used to turn the LEDs on and off
void blink(int LEDPin, int onTime, int offTime){
// Turn the LED on
digitalWrite(LEDPin, HIGH);
// Delay so that you can see the LED go On.
delay(100);
// Turn the LED Off
digitalWrite(LEDPin, LOW);
// Increase this Delay if you want to see an actual blinking effect.
delay(0);
}
//offTime
Arduino code for readings of the flex sensor
/*
##Testing Environment for Xively Arduino Tutorial##
This program is designed to test the sensing circuit created
in the Xively Wi-Fi tutorial. It tests the flex sensor as well as the
LED output. This sketch can be adapted to take sensor readings from any analog sensor.
Derived from basicSensorTestEnv by Calum Barnes and AnalogInput by Tom Igoe
By Calum Barnes 3-4-2013
MIT License – [http://opensource.org/licenses/MIT]
Copyright (c) 2013 Calum Barnes
*/
/////////////////////////////////
/////////////SETUP///////////////
/////////////////////////////////
//you need to upload the code first then open the serial monitor for reading the values
const int analogInPin = A0; // Analog input pin that sensors is attached to (DEFAULT=A2)
int readingDelay = 10; // Delay between each reading (DEFAULT=10)
int readingsPerSample = 10; // Number of reaings per sample / loop (DEFAULT=10)
boolean singleRead = false; // Series of readings (False) or single reading (TRUE) (DEFAULT=FALSE)
boolean enableLED = true; // LED
const int ledPin = 9; // Pin that Anode of LED is attached to (DEFAULT=13)
/////////////////////////////////
//vars
int sensorValue = 0; // value read from the pot
int outputValue = 0;
int ledValue = 0;
int sval;
int sensorAvg;
int tenTot;
void setup() {
// initialize serial communications at 19200 bps:
Serial.begin(19200);
pinMode(analogInPin, INPUT); //configure pin as input
pinMode(ledPin, OUTPUT);
}
void loop() {
if(!singleRead){
//SAMPLE OF 10 READINGS
for (int i=0; i<readingsPerSample; i++){ //repeat number of times defined in setup
sval = analogRead(analogInPin); //take single reading
tenTot = tenTot + sval; //add up readings
delay(readingDelay); //delay between readings as defined in setup. should be non 0
}
sensorAvg = (tenTot / 10); //divide the total
tenTot = 0; //reset total variable
outputValue = sensorAvg; //define smoothed, averaged reading
}else{
//STRAIGHT READINGS //only do this if singleRead=true
sensorValue = analogRead(analogInPin);
outputValue = sensorValue;
}
//print the data
Serial.print(“Flex value= “);
Serial.println(outputValue); //print the final value to serial mon
//turn on LED to level of the light
ledValue = map(outputValue, 0, 1023, 255, 0); //changing output to single byte for pin
analogWrite(ledPin, ledValue); //turn on led to specified level
}