Part 1 of 3
#include <Time.h>
#include <MemoryFree.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// ethernet configuration
byte mac[] = {
0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
byte ip[] = {
192, 168, 0, 31 }; // ip in lan
byte gateway[] = {
192, 168, 0, 1 }; // internet access via router
byte subnet[] = {
255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); // port 80 is default for HTTP
// constants
const int AutoDownEnablePin = 6; // enable autodown switch
const int DoorClosedPin = 7; // door open switch
const int DoorOpenPin = 8; // door closed switch
const int OpenCloseRelayPin1 = 9;
const int OpenCloseRelayPin2 = 5;
//const int LED = 9; // led is connected to digital pin 9
const int timeZone = -5; // Central Standard Time (USA)
// NTP Servers:
IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov
// variables
boolean DoorOpenState;
boolean DoorClosedState;
int DoorPosition;
boolean RelayEnable1;
String TimeString;
long TempTime;
int SecondsOpen = 0;
int MinutesOpen;
boolean AutoDownSwitch;
boolean AutoDownEnable;
int AutoDownMinutes;
char c = 0; // received data
char command[2] = "\0"; // command
EthernetUDP Udp;
unsigned int localPort = 7777; // local port to listen for UDP packets
void setup()
{
// initialize serial communication:
Serial.begin(9600);
// initialize IOs
pinMode(AutoDownEnablePin, INPUT_PULLUP);
pinMode(DoorOpenPin, INPUT_PULLUP);
pinMode(DoorClosedPin, INPUT_PULLUP);
pinMode(OpenCloseRelayPin1, OUTPUT);
//pinMode(OpenCloseRelayPin2, OUTPUT);
//pinMode(LED, OUTPUT);
// initialize webserver
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();
// initialize udp for time sync
Udp.begin(localPort);
setSyncProvider(getNtpTime);
}
time_t prevDisplay = 0; // when the digital clock was displayed
void loop()
{
updatetime();
switchstatus();
webserver();
goto skipit;
Serial.print(F("freeMemory()="));
Serial.println(freeMemory());
skipit:
;
}
void switchstatus()
{
// read the state of the pushbutton value:
DoorOpenState = digitalRead(DoorOpenPin);
DoorClosedState = digitalRead(DoorClosedPin);
// calculate door status
if (DoorClosedState == 1 && DoorOpenState == 0){
DoorPosition = 0;
} // door is open
if (DoorClosedState == 0 && DoorOpenState == 0){
DoorPosition = 1;
} // door is in middle position
if (DoorClosedState == 0 && DoorOpenState == 1){
DoorPosition = 2;
} // door is open
if (DoorClosedState == 1 && DoorOpenState == 1){
DoorPosition = 3;
} // ERROR unknown state
goto skipit;
//Print the results
Serial.print(F("Closed="));
Serial.print(DoorClosedState);
Serial.print(F(",Open="));
Serial.print(DoorOpenState);
Serial.print(F(",Door="));
Serial.print(DoorPosition);
Serial.println();
skipit:
;
}