Skip to content

Instantly share code, notes, and snippets.

@xseignard
Created January 25, 2015 20:07
Show Gist options
  • Save xseignard/2d4e00fd3a762d393606 to your computer and use it in GitHub Desktop.
Save xseignard/2d4e00fd3a762d393606 to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <Ethernet.h>
#include <SPI.h>
#include <elapsedMillis.h>
#define PIN_TEMP 0
#define PIN_DB 2
elapsedMillis timeElapsed;
const unsigned int interval = 20000;
boolean canSend = true;
elapsedMillis canSendDelay;
const unsigned int canSendInterval = 2000;
const int numberOfSamples = 9;
const int dB[] = {30, 32, 45, 60, 69, 72, 74, 76, 80};
const double analogValues[] = {0.0, 0.14, 0.6, 1.39, 2.6, 3.68, 5.2, 6.24, 130.2};
const int threshold = 25;
int previousdB = 0;
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x05 };
EthernetClient client;
void setup() {
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println(F("Failed to configure Ethernet using DHCP"));
}
// start and configure lux sensor
if(!tsl.begin()) {
Serial.print(F("Failed to configure lux sensor"));
}
tsl.enableAutoRange(true);
//tsl.setGain(TSL2561_GAIN_16X);
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);
delay(1000);
}
void loop() {
int currentdB = getDb(PIN_DB);
if (abs(currentdB - previousdB) > threshold && canSend) {
sendEvent("sound", String(currentdB, DEC), "dB");
canSend = false;
canSendDelay = 0;
}
previousdB = currentdB;
if (canSendDelay > canSendInterval) {
canSend = true;
}
if (timeElapsed > interval) {
sendEvent("degrees", getTemp(PIN_TEMP), "C");
sendEvent("sound", String(currentdB, DEC), "dB");
int lux = getLux();
if (lux > -1) {
sendEvent("light", String(lux, DEC), "lux");
}
timeElapsed = 0;
}
}
int getDb(int dBPin) {
int i;
double value = 0;
// take 1000 measures to get a more meaningful value
for(i=0; i<1000; i++) {
value += analogRead(dBPin);
}
value = value / 1000;
// constraint it to the min and max values the sensor is able to measure
value = constrain(value, analogValues[0], analogValues[numberOfSamples-1]);
// if the value is the maximun one return it
if (value == analogValues[numberOfSamples-1]) {
return dB[numberOfSamples-1];
}
// else extrapolate to a measure between the range of the closest samples
else {
for(i=0; i<numberOfSamples-1; i++) {
if(analogValues[i] <= value && analogValues[i+1] > value) {
return dB[i] + int((value * (dB[i+1] - dB[i])) / analogValues[i+1]);
}
}
}
}
String getTemp(int tempPin) {
float temp = ((500.0 * analogRead(tempPin)) /1024.0);
// keep only 2 decimals
String t = String(temp, DEC);
t = t.substring(0, t.indexOf('.') + 2);
return t;
}
int getLux() {
sensors_event_t event;
tsl.getEvent(&event);
if(event.light) {
return (int) event.light;
}
else {
return -1;
}
}
void sendEvent(String name, String value, String unit) {
String data = "name=" + name + "&value=" + value + "&unit=" + unit;
Serial.println(data);
if (client.connect("XXXX.herokuapp.com",80)) {
client.println(F("POST /api/1/event HTTP/1.1"));
client.println(F("Host: XXXX.herokuapp.com"));
client.println(F("Content-Type: application/x-www-form-urlencoded"));
client.print(F("Content-Length: "));
client.println(data.length());
client.println();
client.print(data);
}
if (client.connected()) {
client.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment