forked from interlegis/Arduino-Zabbix-Agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino_Zabbix_Agent.ino
220 lines (205 loc) · 5.83 KB
/
Arduino_Zabbix_Agent.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//******************************************************************************
//* Purpose : Zabbix Sensor Agent - Environmental Monitoring Solution *
//* Git : https://github.com/interlegis/arduino-zabbix-agent
//* Author : Gabriel Ferreira and Marco Rougeth *
//* https://github.com/gabrielrf and
//* https://github.com/rougeth
//* Adapted from : Evgeny Levkov and Schotte Vincent *
//* Credits: *
#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <string.h>
//----------------------------- Network settings -------------------------------
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xE3, 0x1B };
//IPAddress ip(10, 1, 2, 235);
IPAddress ip(10, 89, 2, 223);
IPAddress gateway(10, 89, 2, 1);
IPAddress subnet(255, 255, 254, 0);
//------------ Pins 10, 11, 12 e 13 are used by ethernet shield! ---------------
#define MAX_CMD_LENGTH 25
#define LED_PIN 3 // LED pin with 1k resistor
#define DHT11_PIN 4 // DHT11 pin with a 10k resistor
#define ONE_WIRE_PIN 5 // One wire pin with a 4.7k resistor
#define PIR_PIN 6 // PIR pin
#define SOIL_PIN A0 // Soil humidity sensor pin
EthernetServer server(10050);
EthernetClient client;
OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature ds(&oneWire);
DeviceAddress dsTemp;
DHT dht(DHT11_PIN, DHT11);
boolean connected = false;
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
double temp = 0; // Temperature
double umid = 0; // Humidity
float celsius;
float oneWireTemp = 0;
String cmd; //FOR ZABBIX COMMAND
String serialNum;
int counter = 1; // For testing
int chk;
int presence = 0;
int lastPresence = 0;
int soil = 0; // Soil humidity
int limite = 1; // Command size. Using 1 for better performance.
int waitTime = 15000; // Default waiting time before reading sensor again.
int pirWaitTime = 200000; // Default waiting time for PIR.
unsigned long dhtLastCheck = 0;
unsigned long pirLastCheck = 0;
unsigned long oneWireLastCheck = 0;
// Read all DS18b20 and saves the result on variables every 15 seconds.
void readOneWire() {
if (millis() - oneWireLastCheck > waitTime) {
Serial.println("Reading OneWire");
ds.requestTemperatures();
oneWireTemp = ds.getTempC(dsTemp);
oneWireLastCheck = millis();
}
}
// Read DHT11 every 15 seconds and save values on variables
void readDHT11() {
if (millis() - dhtLastCheck > waitTime) {
temp = dht.readTemperature();
umid = dht.readHumidity();
dhtLastCheck = millis();
}
}
// Read command received.
void readTelnetCommand(char c) {
if (cmd.length() == MAX_CMD_LENGTH) {
cmd = "";
}
cmd += c;
if (c == '\n' || cmd.length() == limite) {
parseCommand();
}
else {
}
}
// Read soil humidity sensor.
void readSoil() {
soil = digitalRead(SOIL_PIN);
}
// Read PIR and if positive, keep the value for pirWaitTime seconds.
void readPresence() {
if (digitalRead(PIR_PIN)) {
pirLastCheck = millis();
lastPresence = 1;
}
if (digitalRead(PIR_PIN) && (lastPresence)) {
presence = 1;
}
else {
if (millis() - pirLastCheck > pirWaitTime) {
presence = 0;
lastPresence = 0;
}
}
}
//Commands received by agent on port 10050 parsing
void parseCommand() {
if (cmd.equals("")) { }
else {
counter = counter + 1;
Serial.print(" Tempo: ");
Serial.print(millis() / 1000);
Serial.print("\t");
Serial.print("Cmd: ");
Serial.print(cmd);
Serial.print("\t\t");
Serial.print("Resposta: ");
// AGENT ping
if (cmd.equals("p")) {
server.println("1");
} // Agent version
else if (cmd.equals("l")) {
//Serial.println("Version");
server.println("Arduino Zabbix Agent 1.0");
delay(100);
} // Agent soil humidity
else if (cmd.equals("q")) {
readSoil();
Serial.print(soil);
server.println(soil);
// NOT SUPPORTED
} // Agent air temperature
else if (cmd.equals("w")) {
readDHT11();
Serial.print(temp);
server.println(temp);
dhtLastCheck = millis() - waitTime;
} // Agent air humidity
else if (cmd.equals("e")) {
readDHT11();
server.println(umid);
Serial.print(umid);
} else if (cmd.equals("r")) {
server.println(oneWireTemp);
Serial.print(oneWireTemp);
} else if (cmd.equals("t")) {
server.println(presence);
Serial.print(presence);
oneWireLastCheck = oneWireLastCheck - (waitTime/3);
pirLastCheck = millis() - pirWaitTime;
} else { // Agent error
//server.print("ZBXDZBX_NOTSUPPORTED");
//server.print("Error");
}
cmd = "";
Serial.println("");
client.stop();
}
}
// The loop that Arduino runs forever after the setup() function.
void loop() {
client = server.available();
if (client) {
if (!connected) {
Serial.println("Conection not available");
client.flush();
connected = true;
client.stop();
}
if (client.available() > 0) {
Serial.println("Client Available");
Serial.println("Conection ok");
int clientread = client.read();
Serial.print(clientread);
char charcr = clientread;
digitalWrite(LED_PIN, HIGH);
readTelnetCommand(clientread);
digitalWrite(LED_PIN, LOW);
}
}
if (millis()%2) {
readPresence();
}
else {
readOneWire();
}
}
// This function runs once only, when the Arduino is turned on or reseted.
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
//Serial.begin(115200);
pinMode(SOIL_PIN, INPUT);
dht.begin();
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
ds.begin();
if (!ds.getAddress(dsTemp, 0)) {
Serial.println("Unable to find address for Device 0");
return;
}
ds.setResolution(dsTemp, 12);
Serial.println("Setup");
delay(1000);
}