Skip to content

Instantly share code, notes, and snippets.

@sash13
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save sash13/73cd624352339cea1341 to your computer and use it in GitHub Desktop.

Select an option

Save sash13/73cd624352339cea1341 to your computer and use it in GitHub Desktop.
// used library
// https://github.com/milesburton/Arduino-Temperature-Control-Library
// https://github.com/jcw/ethercard
#include <OneWire.h>
#include <DallasTemperature.h>
#include <EtherCard.h>
// change these settings to match your own setup
#define FEED "000"
#define APIKEY "xxx"
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
const char website[] PROGMEM = "api.xively.com";
byte Ethernet::buffer[350];
uint32_t timer;
Stash stash;
byte session;
//timing variable
int res = 0;
float temperature = 0;
void setup () {
Serial.begin(57600);
Serial.println("\n[Xively example]");
//Initialize Ethernet
initialize_ethernet();
sensors.begin();
}
void loop () {
//if correct answer is not received then re-initialize ethernet module
if (res > 220){
initialize_ethernet();
}
res = res + 1;
ether.packetLoop(ether.packetReceive());
//200 res = 10 seconds (50ms each res)
if (res == 200) {
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
temperature = sensors.getTempCByIndex(0);
Serial.println(temperature);
// 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("temp,");
stash.println(temperature);
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
session = ether.tcpSend();
}
const char* reply = ether.tcpReply(session);
if (reply != 0) {
res = 0;
Serial.println(reply);
}
delay(50);
}
void initialize_ethernet(void){
for(;;){ // keep trying until you succeed
//Reinitialize ethernet module
pinMode(5, OUTPUT);
Serial.println("Reseting Ethernet...");
digitalWrite(5, LOW);
delay(1000);
digitalWrite(5, HIGH);
delay(500);
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0){
Serial.println( "Failed to access Ethernet controller");
continue;
}
if (!ether.dhcpSetup()){
Serial.println("DHCP failed");
continue;
}
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);
//reset init value
res = 0;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment