Skip to content

Instantly share code, notes, and snippets.

@circuitsenses
Created August 29, 2014 14:36
Show Gist options
  • Save circuitsenses/1ec1196cf6945e233247 to your computer and use it in GitHub Desktop.
Save circuitsenses/1ec1196cf6945e233247 to your computer and use it in GitHub Desktop.
Arduino + WizNet Ethernet + Xively
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h> // I2C needed for sensors
#include "MPL3115A2.h" // Pressure sensor
#include "HTU21D.h" // Humidity sensor
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { "Your MAC" };
IPAddress server(216,52,233,120);
IPAddress ip( "Your IP" );
EthernetClient client;
MPL3115A2 myPressure; //Create an instance of the pressure sensor
HTU21D myHumidity; //Create an instance of the humidity sensor
const byte STAT1 = 7;
const byte STAT2 = 8;
const byte LIGHT = A1;
const byte REFERENCE_3V3 = A3;
float humidity = 0; // [%]
float tempf = 0; // [temperature F]
float pressure = 0;
float light_lvl = 455; //[analog value from 0 to 1023]
#define WEBSITE "api.xively.com"
#define API_key "Your APIKey"
#define feedID "Your FeedID"
//Returns the voltage of the light sensor based on the 3.3V rail
//This allows us to ignore what VCC might be (an Arduino plugged into USB has VCC of 4.5 to 5.2V)
float get_light_level()
{
float operatingVoltage = analogRead(REFERENCE_3V3);
float lightSensor = analogRead(LIGHT);
operatingVoltage = 3.3 / operatingVoltage; //The reference voltage is 3.3V
lightSensor = operatingVoltage * lightSensor;
return(lightSensor);
}
//Calculates each of the variables that wunderground is expecting
void calcWeather()
{
//Calc humidity
humidity = myHumidity.readHumidity();
//float temp_h = myHumidity.readTemperature();
//Serial.print(" TempH:");
//Serial.print(temp_h, 2);
//Calc tempf from pressure sensor
tempf = myPressure.readTempF();
tempf = (tempf - 32.0)/1.8000;
//Serial.print(" TempP:");
//Serial.print(tempf, 2);
tempf = 0;
humidity = 0;
//Calc pressure
//pressure = (myPressure.readPressure())/100000;
//Calc light level
light_lvl = get_light_level() * 100;
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// DHCP failed, so use a fixed IP address:
Ethernet.begin(mac, ip);
}
}
void loop() {
calcWeather();
digitalWrite(STAT1, HIGH); //Blink stat LED
// Prepare JSON for Xively & get length
int length = 0;
String data = "";
data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"Humidity\",\"current_value\" : \"" + String(humidity) + "\"},"
+ "{\"id\" : \"Luminosity\",\"current_value\" : \"" + String(light_lvl) + "\"}]}";
length = data.length();
digitalWrite(STAT1, HIGH); //Blink stat LED
if (client.connect(server, 80)) {
Serial.println("Client Connected");
if (client.connected())
{
Serial.println("Connected!");
client.println("PUT /v2/feeds/" + String(feedID) + ".json HTTP/1.0");
client.println("Host: api.xively.com");
client.println("X-ApiKey: " + String(API_key));
client.println("Content-Length: " + String(length));
client.print("Connection: close");
client.println();
client.print(data);
client.println();
digitalWrite(STAT1, LOW); //Blink stat LED
digitalWrite(STAT2, LOW); //Blink stat LED
}
else {
Serial.println(F("Connection failed"));
client.stop();
return;
}
while (client.connected()) {
while (client.available()) {
char c = client.read();
// Serial.print(c);
}
client.stop();
}
delay(1000);
}
else {
Serial.println("Client Failure");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment