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/9c94516c4af5f22d410d to your computer and use it in GitHub Desktop.

Select an option

Save sash13/9c94516c4af5f22d410d to your computer and use it in GitHub Desktop.
/*
Repeating Web client
This sketch connects to a a web server and makes a request
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
the Adafruit Ethernet shield, either one will work, as long as it's got
a Wiznet Ethernet module on board.
This example uses DNS, by assigning the Ethernet client with a MAC address,
IP address, and DNS address.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 19 Apr 2012
by Tom Igoe
http://arduino.cc/en/Tutorial/WebClientRepeating
This code is in the public domain.
*/
/* Used library
http://www.pjrc.com/teensy/td_libs_OneWire.html
https://github.com/milesburton/Arduino-Temperature-Control-Library
*/
#include <SPI.h>
#include <Ethernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// change these settings to match your own setup
#define FEED "000"
#define APIKEY "1234"
// 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);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
float temperature = 0.0;
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
IPAddress ip(192,168,1, 177);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
// initialize the library instance:
EthernetClient client;
const char server[] PROGMEM = "api.xively.com";
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 = 60*1000; // delay between updates, in milliseconds
String stringBuffer, stringNameValue;
void setup() {
// start serial port:
Serial.begin(9600);
// give the ethernet module time to boot up:
delay(1000);
// start the Ethernet connection:
Serial.println("Trying to get an IP address using DHCP");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// initialize the ethernet device not using DHCP:
Ethernet.begin(mac, ip, gateway, subnet);
}
// print your local IP address:
Serial.print("My IP address: ");
ip = Ethernet.localIP();
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(ip[thisByte], DEC);
Serial.print(".");
}
Serial.println();
// Start up the dallas library
sensors.begin();
stringBuffer = String ();
stringNameValue = String ("temp,");
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
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)) {
httpRequest();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// 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.print("Temperature for the device 1 (index 0) is: ");
Serial.println(temperature);
// if there's a successful connection:
if (client.connect(server, 80)) {
/*byte sd = stash.create();
stash.print("temp,");
stash.println(temperature);
stash.save();*/
char buffer[5];
String s = dtostrf(temperature, 1, 4, buffer);
stringBuffer = stringNameValue+s;
Serial.println("connecting...");
// send the HTTP PUT request:
//client.println("PUT http://"+PSTR(server)+"/v2/feeds/"+PSTR(FEED)+".csv HTTP/1.0");
client.print("PUT http://");
client.print(server);
client.print("/v2/feeds/");
client.print(FEED);
client.println(".csv HTTP/1.0");
//client.println("Host: " + server);
client.print("Host: ");
client.println(server);
//client.println("X-PachubeApiKey: " + PSTR(APIKEY));
client.print("X-PachubeApiKey: ");
client.println(APIKEY);
client.println("Content-Type: text/csv");
//client.println("Content-Length: " + stash.size());
client.print("Content-Length: ");
client.print(sizeof(stringBuffer));
client.println();
client.println("Connection: close");
client.println();
client.println(stringBuffer);
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment