Skip to content

Instantly share code, notes, and snippets.

@trinkermedia
Last active December 20, 2015 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trinkermedia/6111466 to your computer and use it in GitHub Desktop.
Save trinkermedia/6111466 to your computer and use it in GitHub Desktop.
Xively
/*
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 photocell 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
##Paul Bellamy
https://github.com/xively/xively_arduino
##TrinkerMedia
*/
/////////////////////////////////
/////////////SETUP///////////////
/////////////////////////////////
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>
// MAC address for your Ethernet shield
byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
IPAddress ip(000,000,000,000);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
// Your Xively key to let you upload data
char xivelyKey[] = "**YOUR XIVELY KEY HERE**";
//your xively feed ID
#define xivelyFeed **FEED NUMBER HERE**
//datastreams
char sensorID[] = "LIGHT_SENSOR_CHANNEL";
char ledID[] = "LED_CHANNEL";
const float kColourSaturation = 0.9;
const float kColourLightness = 0.5;
// const int kLightSensorPin = 2;
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
#define sensorPin A2
//led connected pin
#define ledPin 9
// 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 */);
EthernetClient client;
XivelyClient xivelyclient(client);
void printEthernetStatus() {
// print your WiFi shield's IP address:
IPAddress ip = Ethernet.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
}
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 (Ethernet.begin(mac) != 1)
{
Serial.println("Error getting IP address via DHCP, trying again...");
delay(15000);
}
}
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[0]);
}else Serial.println("HTTP Error");
//write value to LED - change brightness
int level = feed[0].getFloat();
if(level < 300){
level = 255;
}else if(level > 301){
level = 0;
}
//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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment