1 Star 0 Fork 1

youxinweizhi/zabbix-trapper-arduino

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
zabbix-trapper-multi.ino 4.07 KB
一键复制 编辑 原始数据 按行查看 历史
root 提交于 2021-03-18 16:05 +08:00 . new projest
/*
The application sends increasing counter value to Zabbix in the form of traps.
To use in a meaningful manner, fire up your Zabbix UI or API, and create a
hostname "arduino", add an item with key "counter" of type "Zabbix trapper",
with type "Numeric".
*/
#include <Arduino.h>
#include <SPI.h>
#include <Ethernet.h>
#include "dht.h"
#define dht_apin A0 // Analog Pin sensor is connected to
dht DHT;
int counter;
byte mac[] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xdd };
IPAddress zabbix(192, 168, 7, 7);
int zabbixPort = 10051;
char host[] = "ethernet"; // hostname zabbix
char key[][11] = {"other","humidity","temp"};
int numeroDeVariaveis = 3; //COLOQUE AQUI A QUANTIDADE DE VARIÁVEIS QUE SERÃO ENVIADAS PARA O ZABBIX
void zabbix_send_trap(void);
void setup() {
Serial.begin(9600);
Serial.println("Obtaining IP address using DHCP...");
while (Ethernet.begin(mac) != 1) {
delay(10);
}
counter = 1;
Serial.println("setup done.");
printIPAddress();
}
void loop() {
zabbix_send_trap();
delay(2000*15);
counter++;
DHT.read11(dht_apin);
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
}
const char PROGMEM content_header[] = "{\"request\":\"sender data\",\"data\":[";
const char PROGMEM content_item[] = "{\"host\":\"%s\",\"key\":\"%s\",\"value\":\"%d\"}";
const char PROGMEM content_footer[] = "]}";
unsigned int prepare_content_hum(char *dst, char keyAtual[]) {
unsigned int len = 0;
memcpy(&dst[len], content_header, sizeof(content_header));
len += sizeof(content_header) - 1;
len += sprintf_P(&dst[len], content_item, host, keyAtual, DHT.humidity);
memcpy(&dst[len], content_footer, sizeof(content_footer));
len += sizeof(content_footer) - 1;
return len;
}
unsigned int prepare_content_temp(char *dst, char keyAtual[]) {
unsigned int len = 0;
memcpy(&dst[len], content_header, sizeof(content_header));
len += sizeof(content_header) - 1;
len += sprintf_P(&dst[len], content_item, host, keyAtual, DHT.temperature);
memcpy(&dst[len], content_footer, sizeof(content_footer));
len += sizeof(content_footer) - 1;
return len;
}
EthernetClient zbx_client;
char packet_header[] = "ZBXD\1"; //followed by unsigned long long content_len
unsigned long long content_len;
unsigned int payload_len;
char packet_content[256];
void zabbix_send_trap() {
content_len = prepare_content_temp(packet_content, key[2]);
payload_len = sizeof(content_header) + content_len + sizeof(content_footer);
if (zbx_client.connect(zabbix, zabbixPort)) {
Serial.println("connected to zabbix");
Serial.println("sending data");
zbx_client.write(packet_header, sizeof(packet_header) - 1);
zbx_client.write((char *)&content_len, sizeof(content_len));
zbx_client.write(packet_content, content_len);
delay(1);
zbx_client.stop();
Serial.println("disconnected");
}
delay(10);
content_len = prepare_content_hum(packet_content, key[1]);
payload_len = sizeof(content_header) + content_len + sizeof(content_footer);
if (zbx_client.connect(zabbix, zabbixPort)) {
Serial.println("connected to zabbix");
Serial.println("sending data");
zbx_client.write(packet_header, sizeof(packet_header) - 1);
zbx_client.write((char *)&content_len, sizeof(content_len));
zbx_client.write(packet_content, content_len);
delay(1);
zbx_client.stop();
Serial.println("disconnected");
}
}
void printIPAddress()
{
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/youxinweizhi/zabbix-trapper-arduino.git
git@gitee.com:youxinweizhi/zabbix-trapper-arduino.git
youxinweizhi
zabbix-trapper-arduino
zabbix-trapper-arduino
master

搜索帮助