Skip to content

Instantly share code, notes, and snippets.

@mbafford
Created March 20, 2015 19:37
Show Gist options
  • Save mbafford/f6160d8abc28a62d4e5f to your computer and use it in GitHub Desktop.
Save mbafford/f6160d8abc28a62d4e5f to your computer and use it in GitHub Desktop.
Arduino Nano w// Ethernet Support - Litter Box Monitor
#include <EtherCard.h>
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
int PIN_DETECTED = 6;
int PIN_CYCLING = 5;
int PIN_WAITING = 4;
int PIN_LED = 13;
void flash(int n, int d) {
for ( int i = 0; i < n; i++ ) {
digitalWrite(PIN_LED, HIGH);
delay(d);
digitalWrite(PIN_LED, LOW);
delay(d);
}
}
byte Ethernet::buffer[700];
void setup () {
Serial.begin(57600);
Serial.println(F("\n[testDHCP]"));
Serial.print("MAC: ");
for (byte i = 0; i < 6; ++i) {
Serial.print(mymac[i], HEX);
if (i < 5) {
Serial.print(':');
}
}
Serial.println();
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_DETECTED, INPUT);
pinMode(PIN_CYCLING, INPUT);
pinMode(PIN_WAITING, INPUT);
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println(F("Failed to access Ethernet controller"));
Serial.println(F("Setting up DHCP"));
if (!ether.dhcpSetup()) {
Serial.println(F("DHCP failed"));
}
ether.printIp("My IP: ", ether.myip);
ether.printIp("Netmask: ", ether.netmask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
}
BufferFiller bfill;
void loop(){
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
if (pos) {
Serial.println(F("GOT HTTP REQUEST, RETURNING STATUS"));
ether.httpServerReply(statusMessage()); // send web page data
}
}
static word statusMessage() {
int detected = digitalRead(PIN_DETECTED);
int cycling = digitalRead(PIN_CYCLING );
int waiting = digitalRead(PIN_WAITING );
long t = millis() / 1000;
word h = t / 3600;
byte m = (t / 60) % 60;
byte s = t % 60;
bfill = ether.tcpOffset();
bfill.emit_p(PSTR(
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"{\"detected\": $D, \"cycling\": $D, \"waiting\": $D}"),
detected, cycling, waiting);
return bfill.position();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment