-
-
Save toddtreece/2e0d994922425ae8f2d1 to your computer and use it in GitHub Desktop.
dog_waterer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <NewSoftSerial.h> | |
#include <AF_XPort.h> | |
#include <string.h> | |
#include <EEPROM.h> | |
#include <avr/pgmspace.h> | |
#include <WString.h> | |
#define XPORT_RX 2 | |
#define XPORT_TX 3 | |
#define XPORT_RESET 4 | |
#define XPORT_DTR 5 | |
#define XPORT_CTS 6 | |
#define XPORT_RTS 7 | |
#define RELAY_1 8 | |
#define RELAY_2 9 | |
#define RELAY_3 10 | |
#define RELAY_4 11 | |
#define RELAY_5 12 | |
#define BEEP 13 | |
AF_XPort xport = AF_XPort(XPORT_RX, XPORT_TX, XPORT_RESET, XPORT_DTR, XPORT_RTS, XPORT_CTS); | |
const char http_404header[] PROGMEM = "HTTP/1.1 404 Not Found\nServer: arduino\nContent-Type: text/html\n\n<html><head><title>404</title></head><body><h1>404: Sorry, that page cannot be found!</h1></body>"; | |
const char http_header[] PROGMEM = "HTTP/1.0 200 OK\nServer: arduino\nContent-Type: text/html\n\n"; | |
String sendstring = String(30); | |
char linebuffer[128]; | |
void setup() { | |
pinMode(RELAY_1, OUTPUT); | |
pinMode(RELAY_2, OUTPUT); | |
pinMode(RELAY_3, OUTPUT); | |
pinMode(RELAY_4, OUTPUT); | |
pinMode(RELAY_5, OUTPUT); | |
pinMode(BEEP, OUTPUT); | |
xport.begin(9600); | |
xport.reset(); | |
Serial.begin(9600); | |
} | |
void runCommand(){ | |
Serial.println(sendstring); | |
if(sendstring.equals("water")){ | |
digitalWrite(BEEP, HIGH); | |
delay(50); | |
digitalWrite(BEEP, LOW); | |
delay(50); | |
digitalWrite(BEEP, HIGH); | |
delay(50); | |
digitalWrite(BEEP, LOW); | |
delay(1000); | |
digitalWrite(RELAY_5, HIGH); | |
delay(3000); | |
digitalWrite(RELAY_5, LOW); | |
} else if(sendstring.equals("door")) { | |
digitalWrite(BEEP, HIGH); | |
delay(1500); | |
digitalWrite(BEEP, LOW); | |
delay(1500); | |
digitalWrite(BEEP, HIGH); | |
delay(1500); | |
digitalWrite(BEEP, LOW); | |
delay(1000); | |
digitalWrite(RELAY_4, HIGH); | |
delay(500); | |
digitalWrite(RELAY_4, LOW); | |
} else if(sendstring.equals("light_on")) { | |
digitalWrite(RELAY_3, HIGH); | |
} else if(sendstring.equals("light_off")) { | |
digitalWrite(RELAY_3, LOW); | |
} | |
} | |
uint16_t requested(void) { | |
uint8_t read, x; | |
char *found; | |
while (1) { | |
read = xport.readline_timeout(linebuffer, 128, 200); | |
if (read == 0) | |
return 0; | |
if (read) | |
Serial.println(linebuffer); | |
if (strstr(linebuffer, "GET / ")) { | |
return 200; | |
} | |
if (strstr(linebuffer, "GET /?")) { | |
return 200; | |
} | |
if(strstr(linebuffer, "GET ")) { | |
return 404; | |
} | |
} | |
} | |
void processData(void) { | |
char *found=0; | |
// Look for a ? style GET command | |
found = strstr(linebuffer, "?command="); | |
if (found) { | |
Serial.println("received command"); | |
found += 9; | |
char *tosend = strtok(found, " "); | |
sendstring = tosend; | |
runCommand(); | |
} | |
} | |
void loop() { | |
uint16_t ret; | |
ret = requested(); | |
if (ret == 404) { | |
xport.flush(250); | |
xport.ROM_print(http_404header); | |
xport.disconnect(); | |
} else if (ret == 200) { | |
processData(); | |
respond(); | |
delay(1000); | |
pinMode(XPORT_RESET, HIGH); | |
delay(50); | |
pinMode(XPORT_RESET, LOW); | |
} | |
} | |
void respond(){ | |
uint8_t temp; | |
xport.flush(50); | |
xport.ROM_print(http_header); | |
xport.flush(255); | |
xport.disconnect(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment