-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZabduino-dht22.ino
249 lines (209 loc) · 5.95 KB
/
Zabduino-dht22.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Zabbix agent, based on Arduino
#include <DHT.h>
#include <EtherCard.h>
// Uncomment for activate console messages
//#define DEBUG
#define AGENT_VERSION "Zas v1.0" // Zabbix Arduino Sensors
#define AGENT_TYPE "zabduino"
#define AGENT_PORT 10050
#define LEDPIN 9 // Debug led
#define DHTPIN1 2
#define DHTPIN2 3
#define DHTPIN3 4
#define DHTPIN4 5
#define DHTPIN5 6
#define DHTPIN6 7
#define DHTCOUNT 6
#define DHTTYPE DHT22
DHT* sensors[DHTCOUNT] = {
new DHT(DHTPIN1, DHTTYPE),
new DHT(DHTPIN2, DHTTYPE),
new DHT(DHTPIN3, DHTTYPE),
new DHT(DHTPIN4, DHTTYPE),
new DHT(DHTPIN5, DHTTYPE),
new DHT(DHTPIN6, DHTTYPE),
};
byte mymac[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };
const char host[] PROGMEM = "zabbix.local"; // Zabbix server
byte Ethernet::buffer[512];
static BufferFiller bfill;
typedef void(*zabbix_cb)(BufferFiller &buf, String &cmd);
typedef struct {
const char * cmd; // Name of the item
const zabbix_cb callback; // Callback handler
} ZabbixConfig;
// Yeah, stupid way...
static const char cmd_agent_1[] PROGMEM = "agent.ping";
static const char cmd_agent_2[] PROGMEM = "agent.version";
static const char cmd_agent_3[] PROGMEM = "agent.type";
static const char cmd_s_1_t[] PROGMEM = "s.1.t";
static const char cmd_s_1_h[] PROGMEM = "s.1.h";
static const char cmd_s_2_t[] PROGMEM = "s.2.t";
static const char cmd_s_2_h[] PROGMEM = "s.2.h";
static const char cmd_s_3_t[] PROGMEM = "s.3.t";
static const char cmd_s_3_h[] PROGMEM = "s.3.h";
static const char cmd_s_4_t[] PROGMEM = "s.4.t";
static const char cmd_s_4_h[] PROGMEM = "s.4.h";
static const char cmd_s_5_t[] PROGMEM = "s.5.t";
static const char cmd_s_5_h[] PROGMEM = "s.5.h";
static const char cmd_s_6_t[] PROGMEM = "s.6.t";
static const char cmd_s_6_h[] PROGMEM = "s.6.h";
static const ZabbixConfig zabbix_config[] = {
{ cmd_agent_1, &zbx_agent_ping },
{ cmd_agent_2, &zbx_agent_version },
{ cmd_agent_3, &zbx_agent_type },
{ cmd_s_1_t, &zbx_dht },
{ cmd_s_1_h, &zbx_dht },
{ cmd_s_2_t, &zbx_dht },
{ cmd_s_2_h, &zbx_dht },
{ cmd_s_3_t, &zbx_dht },
{ cmd_s_3_h, &zbx_dht },
{ cmd_s_4_t, &zbx_dht },
{ cmd_s_4_h, &zbx_dht },
{ cmd_s_5_t, &zbx_dht },
{ cmd_s_5_h, &zbx_dht },
{ cmd_s_6_t, &zbx_dht },
{ cmd_s_6_h, &zbx_dht },
};
void sendZabbixResponse(BufferFiller &buf, const char* cmd) {
char header[9];
byte i, len;
len = strlen(cmd);
// Create the header
for (i = 0; i<9; i++) {
switch (i) {
case 0:
header[i] = 0x01; break;
case 1:
header[i] = len; break;
default:
header[i] = 0;
}
}
buf = ether.tcpOffset();
buf.emit_p(PSTR("ZBXD"));
buf.emit_raw(header, 9);
buf.emit_raw(cmd, len);
ether.httpServerReply(buf.position());
#ifdef DEBUG
Serial.print("<< ");
Serial.println(cmd);
#endif // DEBUG
}
void zbx_agent_ping(BufferFiller &buf, String &cmd) {
sendZabbixResponse(buf, "1");
}
void zbx_agent_version(BufferFiller &buf, String &cmd) {
sendZabbixResponse(buf, AGENT_VERSION);
}
void zbx_agent_type(BufferFiller &buf, String &cmd) {
sendZabbixResponse(buf, AGENT_TYPE);
}
void zbx_dht(BufferFiller &buf, String &cmd) {
char buffer[8];
// s.1.t = Sensor, #1, Temperature
char type = cmd.charAt(4);
char tmp = cmd.charAt(2);
int num = strtol(&tmp, NULL, 10); // 10 - base (2, 8, 10, 16)
if (type == 't')
dtostrf(sensors[num - 1]->readTemperature(), 2, 1, buffer);
else
dtostrf(sensors[num - 1]->readHumidity(), 2, 1, buffer);
sendZabbixResponse(buf, buffer);
}
void serviceZabbixRequest(BufferFiller &buf, word pos) {
byte i;
String cmd = String((char *)Ethernet::buffer + pos);
String check;
const char *ptr;
char c;
for (i = 0; i<(sizeof zabbix_config) / (sizeof(ZabbixConfig)); i++) {
// read from the eeprom
check = "";
ptr = zabbix_config[i].cmd;
while ((c = pgm_read_byte(ptr++))) {
check += c;
}
check += '\n'; // every command ends with a newline
// now check contains the command name
// Check it agains the current command
if (cmd == check) {
#ifdef DEBUG
Serial.print(">> ");
Serial.print(cmd); // newline already in cmd string.
#endif // DEBUG
(zabbix_config[i].callback)(buf, cmd);
return;
}
}
sendZabbixResponse(buf, "ZBX_NOTSUPPORTED");
}
int memoryTest() {
int byteCounter = 0;
byte *byteArray;
while ((byteArray = (byte*)malloc(byteCounter * sizeof(byte))) != NULL)
{
byteCounter++;
free(byteArray);
}
free(byteArray);
return byteCounter;
}
static void gotPinged(byte* ptr) {
#ifdef DEBUG
ether.printIp(">> ping from: ", ptr);
#endif // DEBUG
}
void setup() {
#ifdef DEBUG
Serial.begin(115200); // 9600 by default
Serial.println("[SETUP]");
#endif // DEBUG
// Setup led
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, HIGH); // Config start, enable led
// Setup ethernet
if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0) {
#ifdef DEBUG
Serial.println(F("ETH: NO ACCESS"));
#endif // DEBUG
}
if (!ether.dhcpSetup()) {
#ifdef DEBUG
Serial.println(F("ETH: NO DHCP"));
#endif // DEBUG
}
ether.hisport = AGENT_PORT;
ether.dnsLookup(host);
#ifdef DEBUG
ether.printIp("Eth.Ip: ", ether.myip);
ether.printIp("Eth.Gw: ", ether.gwip);
ether.printIp("Eth.Dns: ", ether.dnsip);
ether.printIp("Eth.Srv: ", ether.hisip);
#endif // DEBUG
ether.registerPingCallback(gotPinged);
ether.persistTcpConnection(false);
for (int i = 0; i < DHTCOUNT; i++)
{
sensors[i]->begin(); // Start sensors.
}
#ifdef DEBUG
Serial.print("Mem.Free: ");
Serial.println(memoryTest());
Serial.println("[DONE]");
#endif // DEBUG
delay(2000); // 2 sec for sensors init
digitalWrite(LEDPIN, LOW); // End of config, disable led.
}
void loop() {
word len = ether.packetReceive();
//word pos;
word pos = ether.packetLoop(len);
if (len) {
if ((pos = ether.accept(AGENT_PORT, len))) {
digitalWrite(LEDPIN, HIGH);
serviceZabbixRequest(bfill, pos);
digitalWrite(LEDPIN, LOW);
}
}
}