Setup Zabbix connect to Arduino Uno whit sensor LDR, CO2, Tempeture and Humidity
Arduino connection to sensor
LDR Light Sensor connect to Digital 1 from bldr.org
SHT11 Temp/Humidity connect to clock = Digital2 / data = Digital3 bldr.org
CO2 MQ-7 Carbon Monoxide Sensor connect to analog pin 0 from wiring.org.co
Arduino program
Get Sensor value by command (SensorCmd03)
/*
= Serial Command v.0.03
= Get Sensor value by command
= Get Temp
= Temperature: 28.83
= Get Hum
= Humidity: 71.14
= GET L
= LDR :394
= Get CO2
= CO2: 0
*/
int LDR_Pin = A0; //analog pin 0
int MQ7_Pin = A1; //analog pin 1
int SHT_clockPin = 3; // pin used for clock
int SHT_dataPin = 2; // pin used for data
void setup(){
Serial.begin(9600);
}
void loop(){
String Serialcmd = SerialReaderln();
if (Serialcmd != NULL) {
CommandReq(Serialcmd);
}
}
void CommandReq (String command) {
String parameter;
command.toUpperCase();
if (command.startsWith("GET")) {
parameter = command.substring(4);
// Serial.println("Get Prameter:" + parameter); //DEBUG
if (parameter.startsWith("T")) {
//Get Temperature
Serial.print("Temperature: ");
Serial.println(getTemperature());
} else if (parameter.startsWith("H")) {
//Get Humidity
Serial.print("Humidity: ");
Serial.println(getHumidity());
} else if (parameter.startsWith("L")) {
//Get LDR
Serial.print("LDR: ");
Serial.println(getLDRSensor());
} else if (parameter.startsWith("C")) {
//Get CO2
Serial.print("CO2: ");
Serial.println(getCO2Sensor());
} else {
Serial.println("Get Invaid Parameter");
}
} else if (command.startsWith("PUT")) {
parameter = command.substring(4);
Serial.println("Put Command:" + parameter);
} else {
Serial.println("Invalid Command");
}
}
String SerialReaderln(){
char inChar;
String inText;
const int terminatingChar = 13; //Terminate lines with CR
delay(100);
while (Serial.available() > 0){ // As long as EOL not found and there's more to read, keep reading
inChar = Serial.read(); // Read next byte
if (inChar != '\n') {
inText += inChar;
} else {
inText.trim();
// Serial.println("inText:" + inText ); //DEBUG
return inText;
}
}
return NULL;
}
// ---------------------------------
// -- LDR Light Sensor --
// ---------------------------------
void testLDRSensor(){
delay(100);
int value = getLDRSensor();
Serial.print("LDR :");
Serial.println(value);
}
// Get LED Lighting Sensor Single
int getLDRSensor(){
int LDRValue = analogRead(LDR_Pin);
return LDRValue;
}
// ---------------------------------
// -- MQ7 Carbon Monoxide Sensor --
// ---------------------------------
void testCO2Sensor(){
delay(100);
int value = getCO2Sensor();
Serial.print("CO2: ");
Serial.println(value);
}
// Get MQ7 Sensor Signle
int getCO2Sensor(){
int CO2Value = analogRead(MQ7_Pin);
return CO2Value;
}
// -------------------------------------------
// -- SHT11 Temperature and Humidity Sensor --
// -------------------------------------------
void testSHT11(){
delay(100);
float temperature = getTemperature();
float humidity = getHumidity();
Serial.print("Temp: " );
Serial.println(temperature);
Serial.print("Humidity: ");
Serial.println(humidity);
}
float getTemperature(){
//Return Temperature in Celsius
SHT_sendCommand(B00000011, SHT_dataPin, SHT_clockPin);
SHT_waitForResult(SHT_dataPin);
int val = SHT_getData(SHT_dataPin, SHT_clockPin);
SHT_skipCrc(SHT_dataPin, SHT_clockPin);
//return (float)val * 0.01 - 40; //convert to celsius
return (float)val * 0.01 - 40.55; //convert to celsius
}
float getHumidity(){
//Return Relative Humidity
SHT_sendCommand(B00000101, SHT_dataPin, SHT_clockPin);
SHT_waitForResult(SHT_dataPin);
int val = SHT_getData(SHT_dataPin, SHT_clockPin);
SHT_skipCrc(SHT_dataPin, SHT_clockPin);
//return -4.0 + 0.0405 * val + -0.0000028 * val * val;
return -7.5 + 0.0405 * val + -0.0000028 * val * val;
}
void SHT_sendCommand(int command, int dataPin, int clockPin){
// send a command to the SHTx sensor
// transmission start
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
digitalWrite(dataPin, HIGH);
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, LOW);
digitalWrite(clockPin, LOW);
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, HIGH);
digitalWrite(clockPin, LOW);
// shift out the command (the 3 MSB are address and must be 000, the last 5 bits are the command)
shiftOut(dataPin, clockPin, MSBFIRST, command);
// verify we get the right ACK
digitalWrite(clockPin, HIGH);
pinMode(dataPin, INPUT);
if (digitalRead(dataPin)) Serial.println("ACK error 0");
digitalWrite(clockPin, LOW);
if (!digitalRead(dataPin)) Serial.println("ACK error 1");
}
void SHT_waitForResult(int dataPin){
// wait for the SHTx answer
pinMode(dataPin, INPUT);
int ack; //acknowledgement
//need to wait up to 2 seconds for the value
for (int i = 0; i < 1000; ++i){
delay(2);
ack = digitalRead(dataPin);
if (ack == LOW) break;
}
if (ack == HIGH) Serial.println("ACK error 2");
}
int SHT_getData(int dataPin, int clockPin){
// get data from the SHTx sensor
// get the MSB (most significant bits)
pinMode(dataPin, INPUT);
pinMode(clockPin, OUTPUT);
byte MSB = shiftIn(dataPin, clockPin, MSBFIRST);
// send the required ACK
pinMode(dataPin, OUTPUT);
digitalWrite(dataPin, HIGH);
digitalWrite(dataPin, LOW);
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
// get the LSB (less significant bits)
pinMode(dataPin, INPUT);
byte LSB = shiftIn(dataPin, clockPin, MSBFIRST);
return ((MSB << 8) | LSB); //combine bits
}
void SHT_skipCrc(int dataPin, int clockPin){
// skip CRC data from the SHTx sensor
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
digitalWrite(dataPin, HIGH);
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
}
= Serial Command v.0.03
= Get Sensor value by command
= Get Temp
= Temperature: 28.83
= Get Hum
= Humidity: 71.14
= GET L
= LDR :394
= Get CO2
= CO2: 0
*/
int LDR_Pin = A0; //analog pin 0
int MQ7_Pin = A1; //analog pin 1
int SHT_clockPin = 3; // pin used for clock
int SHT_dataPin = 2; // pin used for data
void setup(){
Serial.begin(9600);
}
void loop(){
String Serialcmd = SerialReaderln();
if (Serialcmd != NULL) {
CommandReq(Serialcmd);
}
}
void CommandReq (String command) {
String parameter;
command.toUpperCase();
if (command.startsWith("GET")) {
parameter = command.substring(4);
// Serial.println("Get Prameter:" + parameter); //DEBUG
if (parameter.startsWith("T")) {
//Get Temperature
Serial.print("Temperature: ");
Serial.println(getTemperature());
} else if (parameter.startsWith("H")) {
//Get Humidity
Serial.print("Humidity: ");
Serial.println(getHumidity());
} else if (parameter.startsWith("L")) {
//Get LDR
Serial.print("LDR: ");
Serial.println(getLDRSensor());
} else if (parameter.startsWith("C")) {
//Get CO2
Serial.print("CO2: ");
Serial.println(getCO2Sensor());
} else {
Serial.println("Get Invaid Parameter");
}
} else if (command.startsWith("PUT")) {
parameter = command.substring(4);
Serial.println("Put Command:" + parameter);
} else {
Serial.println("Invalid Command");
}
}
String SerialReaderln(){
char inChar;
String inText;
const int terminatingChar = 13; //Terminate lines with CR
delay(100);
while (Serial.available() > 0){ // As long as EOL not found and there's more to read, keep reading
inChar = Serial.read(); // Read next byte
if (inChar != '\n') {
inText += inChar;
} else {
inText.trim();
// Serial.println("inText:" + inText ); //DEBUG
return inText;
}
}
return NULL;
}
// ---------------------------------
// -- LDR Light Sensor --
// ---------------------------------
void testLDRSensor(){
delay(100);
int value = getLDRSensor();
Serial.print("LDR :");
Serial.println(value);
}
// Get LED Lighting Sensor Single
int getLDRSensor(){
int LDRValue = analogRead(LDR_Pin);
return LDRValue;
}
// ---------------------------------
// -- MQ7 Carbon Monoxide Sensor --
// ---------------------------------
void testCO2Sensor(){
delay(100);
int value = getCO2Sensor();
Serial.print("CO2: ");
Serial.println(value);
}
// Get MQ7 Sensor Signle
int getCO2Sensor(){
int CO2Value = analogRead(MQ7_Pin);
return CO2Value;
}
// -------------------------------------------
// -- SHT11 Temperature and Humidity Sensor --
// -------------------------------------------
void testSHT11(){
delay(100);
float temperature = getTemperature();
float humidity = getHumidity();
Serial.print("Temp: " );
Serial.println(temperature);
Serial.print("Humidity: ");
Serial.println(humidity);
}
float getTemperature(){
//Return Temperature in Celsius
SHT_sendCommand(B00000011, SHT_dataPin, SHT_clockPin);
SHT_waitForResult(SHT_dataPin);
int val = SHT_getData(SHT_dataPin, SHT_clockPin);
SHT_skipCrc(SHT_dataPin, SHT_clockPin);
//return (float)val * 0.01 - 40; //convert to celsius
return (float)val * 0.01 - 40.55; //convert to celsius
}
float getHumidity(){
//Return Relative Humidity
SHT_sendCommand(B00000101, SHT_dataPin, SHT_clockPin);
SHT_waitForResult(SHT_dataPin);
int val = SHT_getData(SHT_dataPin, SHT_clockPin);
SHT_skipCrc(SHT_dataPin, SHT_clockPin);
//return -4.0 + 0.0405 * val + -0.0000028 * val * val;
return -7.5 + 0.0405 * val + -0.0000028 * val * val;
}
void SHT_sendCommand(int command, int dataPin, int clockPin){
// send a command to the SHTx sensor
// transmission start
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
digitalWrite(dataPin, HIGH);
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, LOW);
digitalWrite(clockPin, LOW);
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, HIGH);
digitalWrite(clockPin, LOW);
// shift out the command (the 3 MSB are address and must be 000, the last 5 bits are the command)
shiftOut(dataPin, clockPin, MSBFIRST, command);
// verify we get the right ACK
digitalWrite(clockPin, HIGH);
pinMode(dataPin, INPUT);
if (digitalRead(dataPin)) Serial.println("ACK error 0");
digitalWrite(clockPin, LOW);
if (!digitalRead(dataPin)) Serial.println("ACK error 1");
}
void SHT_waitForResult(int dataPin){
// wait for the SHTx answer
pinMode(dataPin, INPUT);
int ack; //acknowledgement
//need to wait up to 2 seconds for the value
for (int i = 0; i < 1000; ++i){
delay(2);
ack = digitalRead(dataPin);
if (ack == LOW) break;
}
if (ack == HIGH) Serial.println("ACK error 2");
}
int SHT_getData(int dataPin, int clockPin){
// get data from the SHTx sensor
// get the MSB (most significant bits)
pinMode(dataPin, INPUT);
pinMode(clockPin, OUTPUT);
byte MSB = shiftIn(dataPin, clockPin, MSBFIRST);
// send the required ACK
pinMode(dataPin, OUTPUT);
digitalWrite(dataPin, HIGH);
digitalWrite(dataPin, LOW);
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
// get the LSB (less significant bits)
pinMode(dataPin, INPUT);
byte LSB = shiftIn(dataPin, clockPin, MSBFIRST);
return ((MSB << 8) | LSB); //combine bits
}
void SHT_skipCrc(int dataPin, int clockPin){
// skip CRC data from the SHTx sensor
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
digitalWrite(dataPin, HIGH);
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
}
Python Script to get Arduino from Serial port /dev/ttyACM0
root@linaro-alip:/usr/local/share/zabbix/externalscripts# apt-get install python-serialCreate python script at /usr/local/share/zabbix/externalscripts and chmod to 2755 (run script and root user)
root@linaro-alip:/usr/local/share/zabbix/externalscripts# usermod -a -G dialout zabbix
root@linaro-alip:/usr/local/share/zabbix/externalscripts# ls -l
-rwxr-sr-x 1 root root 388 Jan 12 01:12 SensorCO2.py
-rwxr-sr-x 1 root root 393 Jan 12 01:12 SensorHumidity.py
-rwxr-sr-x 1 root root 387 Jan 12 01:19 SensorLDR.py
-rwxr-sr-x 1 root root 438 Jan 12 11:27 SensorTemperature.py
# /usr/local/share/zabbix/externalscripts# chmod 2755 *.pySensorCO2.py
#!/usr/bin/env python2.7
import serial
import time
ser = serial.Serial('/dev/ttyACM0', 9600)
count = 0
while (count < 2):
time.sleep(2)
ser.write('Get C\n')
data = ser.readline()
data = data.strip()
if (data.startswith("CO2:")):
value = data[5:]
break
else:
value = 0
count = count + 1
print value.strip()
# print data #Debug
ser.close()
import serial
import time
ser = serial.Serial('/dev/ttyACM0', 9600)
count = 0
while (count < 2):
time.sleep(2)
ser.write('Get C\n')
data = ser.readline()
data = data.strip()
if (data.startswith("CO2:")):
value = data[5:]
break
else:
value = 0
count = count + 1
print value.strip()
# print data #Debug
ser.close()
SensorHumidity.py
#!/usr/bin/env python2.7
import serial
import time
ser = serial.Serial('/dev/ttyACM0', 9600)
count = 0
while (count < 2):
time.sleep(2)
ser.write('Get H\n')
data = ser.readline()
data = data.strip()
if (data.startswith("Humidity:")):
Value = data[10:]
break
else:
Value = 0
count = count + 1
print Value.strip()
#print data #Debug
ser.close()
import serial
import time
ser = serial.Serial('/dev/ttyACM0', 9600)
count = 0
while (count < 2):
time.sleep(2)
ser.write('Get H\n')
data = ser.readline()
data = data.strip()
if (data.startswith("Humidity:")):
Value = data[10:]
break
else:
Value = 0
count = count + 1
print Value.strip()
#print data #Debug
ser.close()
SensorLDR.py
#!/usr/bin/env python2.7
import serial
import time
ser = serial.Serial('/dev/ttyACM0', 9600)
count = 0
while (count < 2):
time.sleep(2)
ser.write('Get L\n')
data = ser.readline()
data = data.strip()
if (data.startswith("LDR:")):
Value = data[5:]
break
else:
Value = 0
count = count + 1
print Value.strip()
#print data #Debug
ser.close()
import serial
import time
ser = serial.Serial('/dev/ttyACM0', 9600)
count = 0
while (count < 2):
time.sleep(2)
ser.write('Get L\n')
data = ser.readline()
data = data.strip()
if (data.startswith("LDR:")):
Value = data[5:]
break
else:
Value = 0
count = count + 1
print Value.strip()
#print data #Debug
ser.close()
SensorTemperature.py
#!/usr/bin/env python2.7
import serial
import time
ser = serial.Serial('/dev/ttyACM0', 9600)
count = 0
while (count < 3):
time.sleep(2)
ser.write('Get T\n')
data = ser.readline() #Temperature: 29.15
data = data.strip()
if (data.startswith("Temperature:")):
Value = data[13:]
break
else:
Value = 0
count = count + 1
print Value.strip()
#print data #Debug
#print data.split(':');
ser.close()
Zabbix Config
Add New Template name ArduinoSensorConfiguration > Template and Add TemplateTemplate name : ArduinoSensorIn groups = TemplateAdd items to template
Add New Item to ArduinoSensorCofiguration > Template and select ArduinoSensor Click on Items (0)
Click "Create Item)Name: LDR SensorType : External checkKey: SensorLDR.pyType of information : Numeric(unsigned)DataType: DecimalNumber Interval: 100History : 90Applications : Sensor
Other Item detail
Add Host Arduino01 use template ArduinoSensor
Map element of Host label.
Name | DatType | Key | Interval | History | Trends | Type | Applications | Status | Unit |
LDR Sensor | Decimal | SensorLDR.py | 100 | 90 | 365 | External check | Sensor | Enabled | |
CO2 Sensor | Decimal | SensorCO2.py | 105 | 90 | 365 | External check | Sensor | Enabled | |
Humidity | NumberFloat | SensorHumidity.py | 110 | 90 | 365 | External check | Sensor | Enabled |
%
|
Temperature | SensorTemperature.py | 115 | 90 | 365 | External check | Sensor | Enabled |
°C
|
Add Host Arduino01 use template ArduinoSensor
Map element of Host label.
Temp:{{HOSTNAME}:SensorTemperature.py.last(0)}
Humidity:{{HOSTNAME}:SensorHumidity.py.last(0)}
Light:{{HOSTNAME}:SensorLDR.py.last(0)}
CO2:{{HOSTNAME}:SensorCO2.py.last(0)}
Nice, maybe you can upgrade you're system and use a ethernet shield to send you're data straigt to zabbix :
ReplyDeleteexample below
//*****************************************************************************************
//* Purpose : Zabbix Sender *
//* Author : Schotte Vincent *
//
//
// we are going to send a counter to zabbix
//
// create a hostname : arduinocounter
// item/key : counter Type : Zabbix trapper
// Type of Information : Numeric
//
//-----------------INCLUDES--------------------
#include
#include
#include
//--------------------------------------------
byte mac[] = { 0xDE, 0xAA, 0xBB, 0xEF, 0xFE, 0x92 };
IPAddress ip(192, 168, 1, 92); // ip address arduino
IPAddress gateway(255, 255, 255, 1);
IPAddress subnet(192, 168, 1, 1);
IPAddress zabbix(192,168,1,142); // you're zabbix server
char host[] = "arduinocounter"; // hostname zabbix
char base64host[200];
char key1[] = "counter"; // item zabbix
char base64key1[200];
char value1[10]; // value of counter
char base64value1[200];
int counter;
EthernetClient zabclient ;
void setup()
{
counter = 1 ; // init the counter
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
}
void loop()
{
sendzabbix(); // send counter value to zabbix
delay(60000); // wait one minute
counter = counter + 1;
}
//--------------------------------------------
void sendzabbix()
{
if (zabclient.connect(zabbix,10051))
{
base64_encode(base64host , host , sizeof(host)-1);
base64_encode(base64key1 , key1 , sizeof(key1)-1);
itoa(counter,value1,10); // transform int to char
base64_encode(base64value1 , value1 , sizeof(value1)-1);
Serial.println("Connected with zabbix , sending info");
zabclient.write("\n");
zabclient.write(" ");
zabclient.write(base64host);
zabclient.write("\n");
zabclient.write(" ");
zabclient.print(base64key1);
zabclient.write("\n");
zabclient.write(" ");
zabclient.write(base64value1);
zabclient.write("\n");
zabclient.write("\n");
delay(1);
zabclient.stop();
}
}
#include SPI.h
ReplyDelete#include Ethernet.h
#include Base64.h
includes are missing above
#include SPI.h
ReplyDelete#include Ethernet.h
#include Base64.h
includes are missing above
Nice, maybe you can upgrade you're system and use a ethernet shield to send you're data straigt to zabbix :
ReplyDeleteexample below
//*****************************************************************************************
//* Purpose : Zabbix Sender *
//* Author : Schotte Vincent *
//
//
// we are going to send a counter to zabbix
//
// create a hostname : arduinocounter
// item/key : counter Type : Zabbix trapper
// Type of Information : Numeric
//
//-----------------INCLUDES--------------------
#include
#include
#include
//--------------------------------------------
byte mac[] = { 0xDE, 0xAA, 0xBB, 0xEF, 0xFE, 0x92 };
IPAddress ip(192, 168, 1, 92); // ip address arduino
IPAddress gateway(255, 255, 255, 1);
IPAddress subnet(192, 168, 1, 1);
IPAddress zabbix(192,168,1,142); // you're zabbix server
char host[] = "arduinocounter"; // hostname zabbix
char base64host[200];
char key1[] = "counter"; // item zabbix
char base64key1[200];
char value1[10]; // value of counter
char base64value1[200];
int counter;
EthernetClient zabclient ;
void setup()
{
counter = 1 ; // init the counter
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
}
void loop()
{
sendzabbix(); // send counter value to zabbix
delay(60000); // wait one minute
counter = counter + 1;
}
//--------------------------------------------
void sendzabbix()
{
if (zabclient.connect(zabbix,10051))
{
base64_encode(base64host , host , sizeof(host)-1);
base64_encode(base64key1 , key1 , sizeof(key1)-1);
itoa(counter,value1,10); // transform int to char
base64_encode(base64value1 , value1 , sizeof(value1)-1);
Serial.println("Connected with zabbix , sending info");
zabclient.write("\n");
zabclient.write(" ");
zabclient.write(base64host);
zabclient.write("\n");
zabclient.write(" ");
zabclient.print(base64key1);
zabclient.write("\n");
zabclient.write(" ");
zabclient.write(base64value1);
zabclient.write("\n");
zabclient.write("\n");
delay(1);
zabclient.stop();
}
}
carbon monoxide is CO not CO2 !!!
ReplyDelete