I have been experimenting lately with some Internet of Things (IoT) object. Some time ago, I attended a workshop at the MadLab at Manchester. Adrian McEwen presented his Bubblino, an Arduino bot that makes bubbles triggered by a Twitter message. He has written a book about designing IoT things. Based on all these examples I started experimenting myself. Working with some IoT myself, I started planning how user interfaces (UI) might affect how do we use the data that is streamed on the Web.
The Night Light Town
This is an experiment that reads the amount of light. The light data is sent to a server, which then is returned. Once the data is sent back, the Arduino reads the value and depending on the intensity, it turns on or off a LED.
To set up this you need:
- Arduino
- Arduino Ethernet/WiFi Shield
- Photocell
- 1k Ohm Resistor
- 220 Ohm Resistor
- A Breadboard
Here is the layout. You can use Fritzing to check more in detail how to set it up.
Alternatively you can download the Fritzing File:
Using Xively to Monitor Data
Xively is a service that allows you to monitor data and then make other services or object react. In this case I use it to dump the light data to it and then get it to send it back to me. I followed the basic tutorial from the Xively website. Nevertheless, they provided the code for the WiFi Arduino Shield, so I had to make some basic modifications. This final script was based on the ones provided by Xively and Paul Bellamy.
Here is MY final code. (Make sure you include the libraries)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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); | |
} | |
} | |