Skip to content

Instantly share code, notes, and snippets.

@xseignard
Created June 12, 2015 13:15
Show Gist options
  • Select an option

  • Save xseignard/f024914e1c47d6535bcf to your computer and use it in GitHub Desktop.

Select an option

Save xseignard/f024914e1c47d6535bcf to your computer and use it in GitHub Desktop.
#include <Ethernet.h>
#include <SPI.h>
#include <Button.h>
#include <elapsedMillis.h>
elapsedMillis doorElapsed;
unsigned int doorInterval = 100;
Button door = Button(A0, INPUT_PULLUP);
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x04 };
EthernetClient client;
void setup() {
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
delay(1000);
}
void loop() {
if (door.uniquePress() && doorElapsed > doorInterval) {
sendEvent("door", "1", "door");
doorElapsed = 0;
}
}
void sendEvent(String name, String value, String unit) {
String data = "name=" + name + "&value=" + value + "&unit=" + unit;
data += "&token=MY_TOKEN";
Serial.println(data);
if (client.connect("sidlee.herokuapp.com",80)) {
client.println("POST /api/1/event HTTP/1.1");
client.println("Host: sidlee.herokuapp.com");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
}
if (client.connected()) {
client.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment