#include <SPI.h>
#include <Ethernet.h>
#include <SHT1x.h>
#define dataPin 2
#define clockPin 3
SHT1x sht1x(dataPin, clockPin);
byte mac[] = {
0x90, 0xA2, 0xD2, 0x00, 0x17, 0xAA};
byte ip[] = {
192,168,1,20};
byte gateway[] = {
192,168,1,254};
byte subnet[] = {
255,255,255,0};
byte server[] = {
173,203,98,29};
Client client(server, 80);
long lastConnectionTime = 0;
boolean lastConnected = false;
const int postingInterval = 10000;
void setup()
{
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
delay(1000);
Serial.println("Starting up");
}
void loop()
{
float temp_c;
float temp_f;
float humidity;
temp_c = sht1x.readTemperatureC();
temp_f = sht1x.readTemperatureF();
humidity = sht1x.readHumidity();
/*
Serial.print("Temperature: ");
Serial.print(temp_c, DEC);
Serial.print("C / ");
Serial.print(temp_f, DEC);
Serial.print("F. Humidity: ");
Serial.print(humidity);
Serial.println("%");
delay(2000);
*/
char centigrados[20]="";
ftoa(centigrados,temp_c,3);
char faren[20]="";
ftoa(faren,temp_f,3);
char humedad[20]="";
ftoa(humedad,humidity,3);
String dataString = String(centigrados);
dataString += ",";
dataString += String(faren);
dataString += ",";
dataString += String(humedad);
if (client.available()) {
char c = client.read();
// Serial.print(c);
}
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// 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)) {
sendData(dataString);
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
void sendData(String thisData) {
// if there's a successful connection:
if (client.connect()) {
Serial.println("connecting...");
// send the HTTP PUT request.
// fill in your feed address here:
client.print("PUT /api/13310.csv HTTP/1.1\n");
client.print("Host: www.pachube.com\n");
// fill in your Pachube API key here:
client.print("X-PachubeApiKey: ____________________________________________________________ \n");
client.print("Content-Length: ");
client.println(thisData.length(), DEC);
// last pieces of the HTTP PUT request:
client.print("Content-Type: text/csv\n");
client.println("Connection: close\n");
// here's the actual content of the PUT request:
client.println(thisData);
Serial.println(thisData);
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
// Float support is hard on arduinos
// (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1164927646)
char *ftoa(char *a, double f, int precision)
{
long p[] = {
0,10,100,1000,10000,100000,1000000,10000000,100000000 };
char *ret = a;
long heiltal = (long)f;
itoa(heiltal, a, 10);
while (*a != '\0') a++;
*a++ = '.';
long desimal = abs((long)((f - heiltal) * p[precision]));
itoa(desimal, a, 10);
return ret;
}