Skip to content

Instantly share code, notes, and snippets.

@gregoryfenton
Created July 30, 2012 00:14
Show Gist options
  • Select an option

  • Save gregoryfenton/3202791 to your computer and use it in GitHub Desktop.

Select an option

Save gregoryfenton/3202791 to your computer and use it in GitHub Desktop.
The file that is currently on an Arduino Uno with a serial number of 6413138333135171C0E0
#include <dht11.h>
#include "Ultrasonic.h"
#include <ShiftLCD.h>
#include <SPI.h>
#include <Ethernet.h>
#define APIKEY "" // your cosm api key
#define FEEDID // your feed ID
#define USERAGENT "Cosm Arduino Example ()" // user agent is the project name
//#define DEBUGOUTPUT
// assign a MAC address for the ethernet controller.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192,168,0,123);
// initialize the library instance:
EthernetClient client;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(216,52,233,121); // numeric IP for api.cosm.com
char server[] = "api.cosm.com"; // name address for cosm API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to cosm.com
// initialize the library with the numbers of the interface pins
ShiftLCD lcd(2, 4, 3);
Ultrasonic ultrasonic(6,7);
dht11 DHT11;
#define DHT11PIN 5
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}
//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}
// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}
// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}
void setup()
{
#ifdef DEBUGOUTPUT
Serial.begin(115200);
#endif
lcd.begin(16, 2);
lcd.print("testing...");
#ifdef DEBUGOUTPUT
Serial.println("testing...");
#endif
lcd.setCursor(0,1);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
lcd.print("Failed DHCP");
#ifdef DEBUGOUTPUT
Serial.println("Failed DHCP");
#endif
// DHCP failed, so use a fixed IP address:
Ethernet.begin(mac, ip);
}
}
unsigned int c = 0;
int chk;
int d = 0, t = 0, h = 0;
void loop()
{
int a = ultrasonic.Ranging(CM);
char str[64];
if (c == 0)
{
chk = DHT11.read(DHT11PIN);
#ifdef DEBUGOUTPUT
Serial.print("chk:");
Serial.println(chk);
#endif
t = DHT11.temperature;
h = DHT11.humidity;
d = dewPoint(t, h);
lcd.setCursor(0,1);
lcd.print("C");
lcd.print(t);
lcd.print(" ");
lcd.setCursor(14,0);
lcd.print(c);
lcd.print(" ");
lcd.setCursor(4,1);
lcd.print("H");
lcd.print(h);
lcd.print(" ");
lcd.setCursor(8,1);
lcd.print("D");
lcd.print(d);
lcd.print(" ");
lcd.setCursor(12,1);
lcd.print("F");
lcd.print((int)dewPointFast(t, h));
lcd.print(" ");
}
sprintf(str, "tempC,%d\nhumidity,%d\ndewPoint,%d",t,h,d);
lcd.setCursor(0, 0);
lcd.print("Dist:");
lcd.print(a);
lcd.print("cm ");
// if there's incoming data from the net connection.
// send it out the Serial port. This is for debugging
// purposes only:
while (client.available()) {
char r = client.read();
#ifdef DEBUGOUTPUT
Serial.print(r);
#endif
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
client.stop();
}
c=1;
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
//sprintf(str, "%d,%d,%d",t,h,d);
c = 0;
sendData(str);
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
//delay(100);
}
// this method makes a HTTP connection to the server:
void sendData(char str[64]) {
// if there's a successful connection:
if (client.connect(server, 80)) {
#ifdef DEBUGOUTPUT
Serial.println("connecting...");
#endif
// send the HTTP PUT request:
client.print("PUT /v2/feeds/");
client.print(FEEDID);
client.println(".csv HTTP/1.1");
client.println("Host: api.cosm.com");
client.print("X-ApiKey: ");
client.println(APIKEY);
client.print("User-Agent: ");
client.println(USERAGENT);
client.print("Content-Length: ");
// calculate the length of the sensor reading in bytes:
// 8 bytes for "sensor1," + number of digits of the data:
int thisLength = strlen(str);
client.println(thisLength);
// last pieces of the HTTP PUT request:
client.println("Content-Type: text/csv");
client.println("Connection: close");
client.println();
// here's the actual content of the PUT request:
//client.print("sensor1,");
client.println(str);
#ifdef DEBUGOUTPUT
Serial.println("finished update");
#endif
}
else {
// if you couldn't make a connection:
client.stop();
}
// note the time that the connection was made or attempted:
lastConnectionTime = millis();
}
// This method calculates the number of digits in the
// sensor reading. Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:
int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0) {
dividend = dividend /10;
digits++;
}
// return the number of digits:
return digits;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment