Académique Documents
Professionnel Documents
Culture Documents
1 contributor
/**
Irrduino v0.8.2 by Joe Fernandez
Issues:
- need a nanny process to check a max run time for valves (regardless of commands or program)
- not working on an Arduino Ethernet board
Change Log:
- 2012-01-09 - changed reporting to point to /log (instead of /reports)
- 2012-01-07 - added run time in seconds (web command syntax option and
update to CommandDispatch handling)
- 2011-12-14 - added function to retrieve current settings
- 2011-12-13 - added REST command for system settings /settings?rr
- 2011-12-02 - added function to report zone runs checkAndPostReport()
- 2011-12-02 - add per-zone status reporting, with zone IDs and remaining times
- 2011-11-10 - add global status option
- 2011-10-27 - added code to turn on and blink an LED on pin 13 while zone running
- 2011-10-07 - fixed problem with home page not displaying after first request
- fixed problem mismatch between zone/pin selection
- 2011-10-05 - Version 0.6 completed
- 3 file split, commandDispatch[] infrastructure implemented
- fixed problem with zone command being run repeatedly
- 2011-09-28 - Split pde into parts for easier code management
- Introduced new commandDispatch[] object for storing command parameters
- 2011-09-25 - Added web UI for more zones and "ALL OFF" function
- 2011-09-xx - Entended parser to turn off zones indifvidually
- updated to use timer for executing irrigation zone runs; now runs can be interrupted
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetDNS.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xE2, 0xEB }; //physical mac address
byte ip[] = { 192, 168, 1, 15 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte dnsServerIp[] = { 192, 168, 1, 1}; // DNS server IP (typically your gateway)
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
// REST commands
const String REST_CMD_OFF = "off";
const String REST_CMD_STATUS = "status";
const String REST_CMD_ZONE = "zone";
const String REST_CMD_ZONES = "zones";
const String REST_CMD_SETTINGS = "settings";
const String REST_CMD_TESTREPORT = "testreport";
// Command codes
const int OBJ_CMD_ALL_OFF = 1;
const int OBJ_CMD_STATUS = 2;
const int OBJ_CMD_SETTINGS = 3;
const int OBJ_CMD_ZONES = 10;
const int OBJ_CMD_ZONE = 100;
const int OBJ_CMD_PROGRAMS = 20;
const int OBJ_CMD_PROGRAM = 200;
};
const int CR_PIN_ID = 0;
const int CR_END_TIME = 1;
const int CR_ZONE_ID = 2;
const int CR_START_TIME = 3;
// Command Report structure - for reporting completed runs (use CR_ constants for indexes)
int commandReportLength = 4;
unsigned long commandReport[] = {0, // pin ID, 0 for none
0, // run end time in miliseconds, 0 for none
0, // zone ID, 0 for none
0 // run start time in milliseconds, 0 for none
};
String jsonReply;
String reportData;
void setup(){
// Turn on serial output for debugging
Serial.begin(9600);
void loop(){
char clientLine[BUFSIZE];
int index = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
clientLine[index] = c;
index++;
// Serial.print("client-c: ");
// Serial.println(c);
continue;
}
Serial.print("http request: ");
Serial.println(clientLine);
if (urlString.lastIndexOf("TTP/1.1") < 0 ){
Serial.println("no HTTP/1.1, ignoring request");
// not a url request, ignore this
goto finish_http;
}
if (arg1 == NULL){
// we got no parameters. show default page
httpHomePage();
} else {
// start a json reply
jsonReply = String();
// identify the command
findCmdObject(arg1);
switch (commandDispatch[CD_OBJ_TYPE]) {
findZoneCommand(arg2);
switch (commandDispatch[CD_CMD_CODE]){
case CC_OFF:
endTimedRun();
break;
case CC_ON:
findZoneTimeValue(arg3);
cmdZoneTimedRun();
break;
case CC_STATUS:
cmdZoneStatus();
break;
}
break;
case OBJ_CMD_ZONES: // all zones
break;
case OBJ_CMD_PROGRAM: // program command
break;
case OBJ_CMD_PROGRAMS: // all programs
break;
default:
httpJsonReply("\"ERROR\":\"Command not recognized.\"");
}
}
}
}
}
} /// ========= end loop() =========
if (commandObject.startsWith(REST_CMD_ZONE)) {
commandDispatch[CD_OBJ_TYPE] = OBJ_CMD_ZONE; // command object type, 0 for none
jsonReply += "\"zone";
commandDispatch[CD_OBJ_TYPE] = OBJ_CMD_PROGRAM;
jsonReply += "\"program";
zoneCommand = zoneCommand.toLowerCase();
if (zoneTime.lastIndexOf("s") >= 0 ){
// zone run request is in seconds (e.g., 30s)
zoneTime = zoneTime.substring(0, zoneTime.lastIndexOf("s"));
commandDispatch[CD_VALUE_1] = stringToInt(zoneTime);
} else {
int time = atoi(zoneTimeValue);
commandDispatch[CD_VALUE_1] = time * 60;
}
Serial.println(arg);
if (label.compareTo("reportingHostName") == 0 ||
label.compareTo("rhn") == 0){ // label shortcut
if(jsonReply != NULL && jsonReply.length() > 0){
jsonReply += ",";
}
if (value.length() >= 4) {
value.toCharArray(reportingHostName, 512);
jsonReply += "\"reportingHostName\":\"";
jsonReply += value;
jsonReply += "\"";
}
return;
}
if (label.compareTo("reportingHostPort") == 0 ||
label.compareTo("rhp") == 0){ // label shortcut
if(jsonReply != NULL && jsonReply.length() > 0){
jsonReply += ",";
}
if (value.length() >= 1) {
reportingHostPort = stringToInt(value);
jsonReply += "\"reportingHostPort\":\"";
jsonReply += value;
jsonReply += "\"";
}
return;
}
}
// Utility functions
void clearCommandDispatch(){
for (int i = 0; i < commandDispatchLength; i++){
commandDispatch[i] = 0;
}
}
void clearCommandRunning(){
for (int i = 0; i < commandRunningLength; i++){
commandRunning[i] = 0;
}
}
void clearCommandReport(){
for (int i = 0; i < commandReportLength; i++){
commandReport[i] = 0;
}
}
Serial.println(logMsg);
2015 GitHub, Inc. Terms Privacy Security Contact Help Status API Training Shop Blog About Pricing