Skip to content

Instantly share code, notes, and snippets.

@bkeating
Created March 20, 2015 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkeating/daeca0ca47115fc67990 to your computer and use it in GitHub Desktop.
Save bkeating/daeca0ca47115fc67990 to your computer and use it in GitHub Desktop.
Ben's Workshop Weather. Arduino sketch to push DHT11 sensor data to data.sparkfun.com
#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(10,0,1,130 );
IPAddress myDns(10,0,1,1);
EthernetClient client;
const String publicKey = "xxxxxxxxxxxxxxxxxxxx";
const String privateKey = "xxxxxxxxxxxxxxxxxxx";
char server[] = "data.sparkfun.com";
const byte NUM_FIELDS = 3;
const String fieldNames[NUM_FIELDS] = {"humidity", "temperature", "heatindex"};
String fieldData[NUM_FIELDS];
void setup() {
Serial.begin(9600);
dht.begin();
setupEthernet();
}
void loop() {
delay(900000);
float h = dht.readHumidity();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(f)) {
Serial.println("Failed to read from DHT sensor");
return;
}
float hi = dht.computeHeatIndex(f, h);
fieldData[0] = String(h);
fieldData[1] = String(f);
fieldData[2] = String(hi);
postData();
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hi);
Serial.println(" *F");
}
void postData()
{
if (client.connect(server, 80))
{
client.print("GET /input/");
client.print(publicKey);
client.print("?private_key=");
client.print(privateKey);
for (int i=0; i<NUM_FIELDS; i++)
{
client.print("&");
client.print(fieldNames[i]);
client.print("=");
client.print(fieldData[i]);
}
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
}
else
{
Serial.println(F("Connection failed"));
}
while (client.connected())
{
if ( client.available() )
{
char c = client.read();
Serial.print(c);
}
}
Serial.println();
client.stop();
}
void setupEthernet()
{
Serial.println("Setting up Ethernet...");
if (Ethernet.begin(mac) == 0) {
Serial.println(F("Failed to configure Ethernet using DHCP"));
Ethernet.begin(mac, ip);
}
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment