Skip to content

Commit 5d1ac8e

Browse files
committed
Initial commit
0 parents  commit 5d1ac8e

File tree

6 files changed

+362
-0
lines changed

6 files changed

+362
-0
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Temperature and humidity Arduino monitor
2+
3+
This is the source code for a very simple temperature and humidity logger based
4+
on an Arduino and the DHT22 sensor.
5+
6+
## Hardware
7+
8+
You will need:
9+
10+
* Arduino board (with an USB cable)
11+
* DHT22 sensor
12+
13+
## Arduino source code
14+
15+
The Arduino source code is in the `src` directory, and can be compiled using
16+
the Arduino IDE.
17+
18+
You also need to install the [DHT11/DHT22
19+
library](https://github.com/adafruit/DHT-sensor-library) into your `libraries`
20+
folder.
21+
22+
## Monitor script
23+
24+
You will need the `pyserial` module to use the monitoring script in the
25+
`monitoring` directory:
26+
27+
pip install pyserial
28+
29+
You can then read temperature and humidity data from the Arduino connected by
30+
an USB cable, by running:
31+
32+
python monitor/arduino_monitor.py
33+
34+
## Zabbix template
35+
36+
You must first install the template found in the `zabbix` directory, and add it
37+
to the host the Arduino board is attached to.
38+
39+
To send temperature and humidity data to Zabbix, you will also need the
40+
`py-zabbix` package:
41+
42+
pip install py-zabbix
43+
44+
Copy the script somewhere:
45+
46+
mkdir -pv /var/lib/zabbix/scripts
47+
cp monitor/arduino_monitor.py /var/lib/zabbix/scripts
48+
49+
Copy the systemd service:
50+
51+
cp systemd/arduino-sensors.service /etc/systemd/system
52+
53+
You can optionally edit the `--host`, `--temperature-item` and
54+
`--humidity-item` arguments to match the names of the host and items you use on
55+
your Zabbix installation.
56+
57+
Reload, enable and start:
58+
59+
systemctl daemon-reload
60+
systemctl enable arduino-sensors.service
61+
systemctl start arduino-sensors.service

arduino/sensors.ino

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "DHT.h"
2+
3+
#define DHTPIN 2
4+
#define DHTTYPE DHT22
5+
#define INTERVAL_SEC 10
6+
7+
DHT dht(DHTPIN, DHTTYPE);
8+
9+
void setup() {
10+
Serial.begin(9600);
11+
Serial.println("-- Humidity/Temperature monitor");
12+
Serial.print("-- Printing every ");
13+
Serial.print(INTERVAL_SEC);
14+
Serial.println("s");
15+
Serial.println("-- Values: H (in %),T (in Celsius)");
16+
dht.begin();
17+
}
18+
19+
void loop() {
20+
float h = dht.readHumidity();
21+
float t = dht.readTemperature();
22+
23+
if (isnan(h) || isnan(t)) {
24+
Serial.println("Failed to read from DHT sensor");
25+
return;
26+
}
27+
28+
Serial.print(h);
29+
Serial.print(",");
30+
Serial.print(t);
31+
Serial.print("\n");
32+
33+
delay(10000);
34+
}

monitor/arduino_monitor.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/python
2+
# coding=utf-8
3+
4+
# Base Python File (arduino_monitor.py)
5+
# Created: Sat 24 Oct 2015 12:53:17 PM CEST
6+
# Version: 1.0
7+
#
8+
# This Python script was developped by François-Xavier Thomas.
9+
# You are free to copy, adapt or modify it.
10+
# If you do so, however, leave my name somewhere in the credits, I'd appreciate it ;)
11+
#
12+
# (ɔ) François-Xavier Thomas <fx.thomas@gmail.com>
13+
14+
"""Zabbix monitor for temperature and humidity on an Arduino DHT11/DHT22 sensor"""
15+
16+
import argparse
17+
import logging
18+
import sys
19+
import os
20+
21+
from serial import Serial
22+
23+
parser = argparse.ArgumentParser(description=__doc__)
24+
parser.add_argument("--device", "-d", default="/dev/ttyACM0",
25+
help="TTY device on which the Arduino is connected")
26+
parser.add_argument("--host", "-s", default=None,
27+
help="Send to Zabbix host on which the trap metric is defined")
28+
parser.add_argument("--temperature-item", "-t", default="arduino.temperature",
29+
help="Temperature item name")
30+
parser.add_argument("--humidity-item", "-m", default="arduino.humidity",
31+
help="Humidity item name")
32+
parser.add_argument("--verbose", "-v", action="store_true")
33+
args = parser.parse_args()
34+
35+
36+
# Setup logging
37+
class NamedLoggerAdapter(logging.LoggerAdapter):
38+
def process(self, msg, kwargs):
39+
if args.host:
40+
return '%s: %s' % (args.host, msg), kwargs
41+
else:
42+
return msg, kwargs
43+
logging_level = logging.DEBUG if args.verbose else logging.INFO
44+
logging.basicConfig(stream=sys.stderr, level=logging_level)
45+
log = NamedLoggerAdapter(logging.getLogger(), {})
46+
47+
# Connect to the serial device
48+
if not os.path.exists(args.device):
49+
log.error("Device %s does not exist, exiting.", args.device)
50+
sys.exit(1)
51+
serial = Serial(args.device, 9600)
52+
53+
# Try to setup Zabbix
54+
if args.host:
55+
try:
56+
from zabbix.sender import ZabbixMetric, ZabbixSender
57+
sender = ZabbixSender(use_config=True)
58+
log.info("Zabbix support enabled for host: %s", args.host)
59+
except ImportError:
60+
log.error("Zabbix support not enabled: py-zabbix not found.")
61+
sys.exit(2)
62+
63+
# Read from serial forever
64+
while True:
65+
66+
# Try and parse a line
67+
data = serial.readline()
68+
try:
69+
hum, temp = map(float, data.strip().split(b","))
70+
71+
# If the output cannot be parsed, print it as is
72+
except:
73+
log.info(data.decode("ascii").strip())
74+
75+
# Otherwise, assume we got temp/humidity values
76+
else:
77+
78+
# Write temperature/humidity data to stderr
79+
log.info("Read %s: %s%%", args.humidity_item, hum)
80+
log.info("Read %s: %s°C", args.temperature_item, temp)
81+
82+
# Send to Zabbix, if --host is present
83+
if args.host:
84+
sender.send([
85+
ZabbixMetric(args.host, args.humidity_item, str(hum)),
86+
ZabbixMetric(args.host, args.temperature_item, str(temp)),
87+
])

monitor/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
py-zabbix
2+
pyserial

systemd/arduino-sensors.service

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Unit]
2+
Description=Arduino Sensor monitoring service
3+
Wants=zabbix-server.service
4+
After=zabbix-server.service
5+
6+
[Service]
7+
Type=simple
8+
ExecStart=/usr/bin/python /var/lib/zabbix/scripts/arduino_monitor.py --host %H
9+
StandardOutput=syslog
10+
StandardError=syslog
11+
12+
[Install]
13+
WantedBy=multi-user.target

zabbix/arduino_template.xml

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<zabbix_export>
3+
<version>2.0</version>
4+
<date>2016-01-14T22:25:21Z</date>
5+
<groups>
6+
<group>
7+
<name>Templates</name>
8+
</group>
9+
</groups>
10+
<templates>
11+
<template>
12+
<template>Template Arduino Sensors</template>
13+
<name>Template Arduino Sensors</name>
14+
<description/>
15+
<groups>
16+
<group>
17+
<name>Templates</name>
18+
</group>
19+
</groups>
20+
<applications>
21+
<application>
22+
<name>Arduino Sensors</name>
23+
</application>
24+
</applications>
25+
<items>
26+
<item>
27+
<name>Arduino Humidity Sensor</name>
28+
<type>2</type>
29+
<snmp_community/>
30+
<multiplier>0</multiplier>
31+
<snmp_oid/>
32+
<key>arduino.humidity</key>
33+
<delay>0</delay>
34+
<history>90</history>
35+
<trends>365</trends>
36+
<status>0</status>
37+
<value_type>0</value_type>
38+
<allowed_hosts/>
39+
<units>%</units>
40+
<delta>0</delta>
41+
<snmpv3_contextname/>
42+
<snmpv3_securityname/>
43+
<snmpv3_securitylevel>0</snmpv3_securitylevel>
44+
<snmpv3_authprotocol>0</snmpv3_authprotocol>
45+
<snmpv3_authpassphrase/>
46+
<snmpv3_privprotocol>0</snmpv3_privprotocol>
47+
<snmpv3_privpassphrase/>
48+
<formula>1</formula>
49+
<delay_flex/>
50+
<params/>
51+
<ipmi_sensor/>
52+
<data_type>0</data_type>
53+
<authtype>0</authtype>
54+
<username/>
55+
<password/>
56+
<publickey/>
57+
<privatekey/>
58+
<port/>
59+
<description/>
60+
<inventory_link>0</inventory_link>
61+
<applications>
62+
<application>
63+
<name>Arduino Sensors</name>
64+
</application>
65+
</applications>
66+
<valuemap/>
67+
<logtimefmt/>
68+
</item>
69+
<item>
70+
<name>Arduino Temperature Sensor</name>
71+
<type>2</type>
72+
<snmp_community/>
73+
<multiplier>0</multiplier>
74+
<snmp_oid/>
75+
<key>arduino.temperature</key>
76+
<delay>0</delay>
77+
<history>90</history>
78+
<trends>365</trends>
79+
<status>0</status>
80+
<value_type>0</value_type>
81+
<allowed_hosts/>
82+
<units>°C</units>
83+
<delta>0</delta>
84+
<snmpv3_contextname/>
85+
<snmpv3_securityname/>
86+
<snmpv3_securitylevel>0</snmpv3_securitylevel>
87+
<snmpv3_authprotocol>0</snmpv3_authprotocol>
88+
<snmpv3_authpassphrase/>
89+
<snmpv3_privprotocol>0</snmpv3_privprotocol>
90+
<snmpv3_privpassphrase/>
91+
<formula>1</formula>
92+
<delay_flex/>
93+
<params/>
94+
<ipmi_sensor/>
95+
<data_type>0</data_type>
96+
<authtype>0</authtype>
97+
<username/>
98+
<password/>
99+
<publickey/>
100+
<privatekey/>
101+
<port/>
102+
<description/>
103+
<inventory_link>0</inventory_link>
104+
<applications>
105+
<application>
106+
<name>Arduino Sensors</name>
107+
</application>
108+
</applications>
109+
<valuemap/>
110+
<logtimefmt/>
111+
</item>
112+
</items>
113+
<discovery_rules/>
114+
<macros/>
115+
<templates/>
116+
<screens/>
117+
</template>
118+
</templates>
119+
<graphs>
120+
<graph>
121+
<name>Arduino Sensors</name>
122+
<width>900</width>
123+
<height>200</height>
124+
<yaxismin>0.0000</yaxismin>
125+
<yaxismax>100.0000</yaxismax>
126+
<show_work_period>1</show_work_period>
127+
<show_triggers>1</show_triggers>
128+
<type>0</type>
129+
<show_legend>1</show_legend>
130+
<show_3d>0</show_3d>
131+
<percent_left>0.0000</percent_left>
132+
<percent_right>0.0000</percent_right>
133+
<ymin_type_1>0</ymin_type_1>
134+
<ymax_type_1>0</ymax_type_1>
135+
<ymin_item_1>0</ymin_item_1>
136+
<ymax_item_1>0</ymax_item_1>
137+
<graph_items>
138+
<graph_item>
139+
<sortorder>0</sortorder>
140+
<drawtype>0</drawtype>
141+
<color>000099</color>
142+
<yaxisside>0</yaxisside>
143+
<calc_fnc>2</calc_fnc>
144+
<type>0</type>
145+
<item>
146+
<host>Template Arduino Sensors</host>
147+
<key>arduino.humidity</key>
148+
</item>
149+
</graph_item>
150+
<graph_item>
151+
<sortorder>1</sortorder>
152+
<drawtype>0</drawtype>
153+
<color>C80000</color>
154+
<yaxisside>1</yaxisside>
155+
<calc_fnc>2</calc_fnc>
156+
<type>0</type>
157+
<item>
158+
<host>Template Arduino Sensors</host>
159+
<key>arduino.temperature</key>
160+
</item>
161+
</graph_item>
162+
</graph_items>
163+
</graph>
164+
</graphs>
165+
</zabbix_export>

0 commit comments

Comments
 (0)