-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWatchRat.ino
executable file
·139 lines (110 loc) · 4.04 KB
/
WatchRat.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Many thanks to author of https://github.com/jcw/ethercard/ !!! Long Life!!!
// 2011-06-12 <[email protected]> http://opensource.org/licenses/mit-license.php
#include <EtherCard.h>
#define DEAD_HAND_TIMEOUT 50000000
#define TURNON_TIMEOUT 5000
#define BOOT_TIMEOUT 250000000
#define SYSLOG_PORT 514
#define RELAY_ON 0
#define RELAY_OFF 1
#define Relay_1 4
#define Relay_2 5
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
byte Ethernet::buffer[700];
Stash stash;
static uint32_t timer;
static uint32_t DeadTimerForFirstRelayServer;
static bool firstServerBooting = false;
static byte myip[] = {10, 254, 3, 254};
static byte netmask[] = {255, 255, 255, 255};
static byte gateway[] = {10, 254, 3, 100};
static byte firstDns[] = {10, 254, 3, 100};
static byte firstRelayServer[] = {10, 254, 3, 100};
static byte syslogServer[] = {10, 254, 3, 200};
static byte syslogSession;
//timing variable
int res = 100; // was 0
// called when a ping comes in (replies to it are automatic)
static void gotPinged (byte* ptr) {
ether.printIp(">>> ping from: ", ptr);
}
void setup () {
Serial.begin(115200);
initialize_ethernet();
digitalWrite(Relay_1, RELAY_OFF);
pinMode(Relay_1, OUTPUT);
digitalWrite(Relay_2, RELAY_OFF);
pinMode(Relay_2, OUTPUT);
timer = -9999999; // start timing out right away
DeadTimerForFirstRelayServer = micros();
}
void loop () {
word len = ether.packetReceive(); // go receive new packets
word pos = ether.packetLoop(len); // respond to incoming pings
const char* reply = ether.tcpReply(syslogSession);
if (reply != 0) {
Serial.println("Got a response!");
Serial.println(reply);
}
if (!firstServerBooting) {
// ping a remote server once every few seconds
if (micros() - timer >= 5000000) {
ether.copyIp(ether.hisip, firstRelayServer);
ether.printIp("Pinging: ", ether.hisip);
timer = micros();
ether.clientIcmpRequest(ether.hisip);
}
// report whenever a reply to our outgoing ping comes back
if (len > 0 && ether.packetLoopIcmpCheckReply(ether.hisip)) {
DeadTimerForFirstRelayServer = micros();
Serial.print(" ");
Serial.print((micros() - timer) * 0.001, 3);
Serial.println(" ms");
}
if (DEAD_HAND_TIMEOUT < (micros() - DeadTimerForFirstRelayServer)) {
Serial.println("Lol, first server died. Time to click-click relay #1!!! Send to syslog");
firstServerBooting = true;
//---*syslog*---
Stash::prepare(PSTR("<000> First server didn't answer on ping. Try to powerOn." "\r\n"));
ether.copyIp(ether.hisip, syslogServer);
ether.hisport = SYSLOG_PORT;
syslogSession = ether.tcpSend();
//---*syslog*---
digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
delay(TURNON_TIMEOUT); // wait for powerup
digitalWrite(Relay_1, RELAY_OFF);// set the Relay ON
DeadTimerForFirstRelayServer = micros();
}
} else {
if (BOOT_TIMEOUT < (micros() - DeadTimerForFirstRelayServer) ) {
firstServerBooting = false;
DeadTimerForFirstRelayServer = micros();
}
}
}
void initialize_ethernet(void) {
for (;;) { // keep trying until you succeed
//Reinitialize ethernet module
//pinMode(5, OUTPUT); // do notknow what this is for, i ve got something elso on pin5
//Serial.println("Reseting Ethernet...");
//digitalWrite(5, LOW);
//delay(1000);
//digitalWrite(5, HIGH);
//delay(500);
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println(F("Failed to access Ethernet controller."));
if (!ether.staticSetup(myip, gateway, firstDns, netmask))
Serial.println(F("Failed to setup IP."));
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
// call this to report others pinging us
ether.registerPingCallback(gotPinged);
//reset init value
res = 180;
break;
}
}