Skip to content

Instantly share code, notes, and snippets.

Created January 5, 2013 20:42
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 anonymous/4463549 to your computer and use it in GitHub Desktop.
Save anonymous/4463549 to your computer and use it in GitHub Desktop.
// Simple demo for feeding some random data to Pachube.
// 2011-07-08 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
#include <EtherCard.h>
#include <SHT1x.h>
#define PROXY 1 // set to 1 to enable proxy
#if PROXY
static byte proxy[] = { 192, 168, 1, 1 };
static uint16_t proxyport = 8118;
#endif
// change these settings to match your own setup
#define FEED "35832"
#define APIKEY "myapikey"
// Specify data and clock connections and instantiate SHT1x object
#define dataPin 4
#define clockPin 3
SHT1x sht1x(dataPin, clockPin);
// ethernet interface mac address, must be unique on the LAN
byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
char website[] PROGMEM = "api.pachube.com";
byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;
void setup () {
Serial.begin(115200);
Serial.println("\n[webClient]");
if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0)
Serial.println( "Failed to access Ethernet controller");
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
ether.printIp("SRV: ", ether.hisip);
}
void loop () {
ether.packetLoop(ether.packetReceive());
if (millis() > timer) {
timer = millis() + 10000;
float temp_c;
float temp_f;
float humidity;
// Read values from the sensor
temp_c = sht1x.readTemperatureC();
temp_f = sht1x.readTemperatureF();
humidity = sht1x.readHumidity();
// Print the values to the serial port
Serial.print("Temperature: ");
Serial.print(temp_c, 1);
Serial.print("C / ");
Serial.print(humidity);
Serial.println("%");
// generate two fake values as payload - by using a separate stash,
// we can determine the size of the generated message ahead of time
byte sd = stash.create();
stash.print("4,"); // 5 est le numero du datastream
stash.println( temp_c);
stash.print("5,");
stash.println( humidity);
stash.save();
// generate the header with payload - note that the stash size is used,
// and that a "stash descriptor" is passed in as argument using "$H"
Stash::prepare(PSTR("PUT http://$F/v2/feeds/$F.csv HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"X-PachubeApiKey: $F" "\r\n"
"Content-Length: $D" "\r\n"
"\r\n"
"$H"),
website, PSTR(FEED), website, PSTR(APIKEY), stash.size(), sd);
// send the packet - this also releases all stash buffers once done
ether.tcpSend();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment