//-------------------------------------------------
// nico - august 2012
//-------------------------------------------------
//
// pin 4,5,6,7: relays
// pin A0 : input button
//
// usage:
// - input button cycles through relays
// - web interface
// - relays are turned off by safety timer
//-------------------------------------------------
#include "etherShield.h"
#include "TimerOne.h"
//#define WATER_DBG
// please modify the following two lines. mac and ip have to be unique on your LAN.
static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
static uint8_t myip[4] = {192,168,1,6};
static char baseurl[] = "http://192.168.1.6/";
static uint16_t mywwwport = 80; // listen port for tcp/www (max range 1-254)
#define NUM_SWITCH 3 // # of controlled circuits
int LED_PIN[NUM_SWITCH] = {4,5,6}; // {4,5,6,7};
#define MAX_TIME 60*60 // safety timer in seconds
#define BUFFER_SIZE 1000
static uint8_t buf[BUFFER_SIZE+1];
#define STR_BUFFER_SIZE 22
static char strbuf[STR_BUFFER_SIZE+1];
EtherShield es=EtherShield();
byte on_off[NUM_SWITCH];
int analogInPin = 14; // A0
int analogInGnd = 15; // A1
int analogInVcc = 16; // A2
int analogPin = 0;
// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf, byte* on_off);
int8_t analyse_cmd(char *str);
// timer --------------------------
int seconds = 0;
void callback()
{
char bufd[128];
sprintf(bufd, "seconds: %d", seconds);
Serial.println(bufd);
if (seconds == 1)
{
// turn everything off
sprintf(bufd, "turn everything off");
Serial.println(bufd);
int swi;
for(swi=0; swi200 OK"));
goto SENDTCP;
}
if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
plen=print_webpage(buf, on_off);
goto SENDTCP;
}
// end of example code -------------------------------------------
cmd=analyse_cmd((char *)&(buf[dat_p+5]));
if (cmd != -1){
// work out a cmd (2 or 3) and a switch number (0,1,2)
int ncmd = 2 + ((cmd - 2) % 2); // 2->2 3->3 4->2 5->3 6->2 7->3
int swi = (cmd - 2) / 2; // 2,3->0 4,5->1 6,7->2
if (ncmd==2)
on_off[swi]=HIGH;
else if (ncmd==3)
on_off[swi]=LOW;
seconds = MAX_TIME;
digitalWrite(LED_PIN[swi], on_off[swi]); // switch on LED
#ifdef WATER_DBG
sprintf(buft, "ncmd %d swi %d", ncmd, swi); Serial.println(buft);
#endif
}
}
// this code comes from an ethershield example -------------------
REFRESHPAGE:
plen=print_webpage(buf, on_off);
SENDTCP: es.ES_make_tcp_ack_from_any(buf); // send ack for http get
es.ES_make_tcp_ack_with_data(buf,plen); // send data
}
}
}
// The returned value is stored in the global var strbuf
uint8_t find_key_val(char *str,char *key)
{
uint8_t found=0;
uint8_t i=0;
char *kp;
kp=key;
while(*str && *str!=' ' && found==0){
if (*str == *kp){
kp++;
if (*kp == '\0'){
str++;
kp=key;
if (*str == '='){
found=1;
}
}
}else{
kp=key;
}
str++;
}
if (found==1){
// copy the value to a buffer and terminate it with '\0'
while(*str && *str!=' ' && *str!='&' && i 0x2f){
// is a ASCII number, return it
r=(*strbuf-0x30);
}
}
return r;
}
// Add the string str to the buffer one character at a time
int add_string(uint8_t*& Buf, char* str, uint16_t &plen)
{
int i = 0;
//Loop through each char
while (str[i]) {
// Add each char one by one to the buffer
Buf[TCP_CHECKSUM_L_P + 3 + plen] = str[i];
i++;
plen++;
}
}
// end of example code -------------------------------------------
uint16_t print_webpage(uint8_t *buf, byte* on_off)
{
uint16_t plen;
char txt[128];
plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));
int swi;
plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("