0

Following is the zabbix documentation with the query header format. Document Zabbix

I have difficulty implementing this header in C ++ (Arduino),

I saw an implementation of what I need in this forum here , however it is in Russian and is implemented in C #, I tried to translate it but to no avail.

So I need someone to help me write the function of the header examples in C ++ (Arduino).

Follows full Arduino code after @EdgarBonet change suggestion

#include <ArduinoJson.h>
#include <SPI.h>
#include <Ethernet.h>

// Config IP
IPAddress ip(192, 168, 0, 130);
IPAddress gw(192, 168, 0, 1);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
float contador = 0;

EthernetClient client;

//Config Server address
const char* server = "192.168.0.135";
int porta = 10051;

void setup() {
    Serial.begin(9600);
    Ethernet.begin(mac, ip);
}

void loop() {
    // Begin Json
    const size_t capacity = JSON_ARRAY_SIZE(1)
                          + JSON_OBJECT_SIZE(2)
                          + JSON_OBJECT_SIZE(3);
    DynamicJsonBuffer jsonBuffer(capacity);

    JsonObject& root = jsonBuffer.createObject();
    root["request"] = "sender data";

    JsonArray& data = root.createNestedArray("data");

    JsonObject& data_0 = data.createNestedObject();
    data_0["host"] = "ethernet";
    data_0["key"] = "variavel1";
    data_0["value"] = contador;

    String json = "";
    root.printTo(json);

    const uint8_t *dataSend = (const uint8_t *) json.c_str();
    uint64_t data_length = json.length();
    uint8_t packet[13 + data_length];  // 13-byte header + payload

    // Write the 5-byte signature.
    memcpy(packet, "ZBXD\1", 5);

    // Add the 8-byte packet length, LSB first.
    for (int i = 0; i < 8; i++) {
        packet[5+i] = data_length >> 8*i;
    }

    // Add the payload.
    memcpy(packet + 13, dataSend, data_length);
    if (client.connect(server, porta)) {
        Serial.println("Enviando ao servidor");
        delay(1000);
        // Send the packet.
        client.write(packet, sizeof packet);
        delay(1000);
        contador ++;
    }
}

Following server response:

2034:20191003:101411.196 Message size 2066888786 from 192.168.0.130 exceeds the maximum size 1073741824 bytes. Message ignored.
  • 1
    Your Arduino code does not compile. Please provide us with a Minimal, Reproducible Example of the issue: stackoverflow.com/help/minimal-reproducible-example. Have you seen this: zabbix.com/integrations/arduino – VE7JRO Oct 2 at 22:22
  • Thank you for editing your question. When I try to compile the sketch I get the following error message: DynamicJsonBuffer is a class from ArduinoJson 5. Please see arduinojson.org/upgrade to learn how to upgrade your program to ArduinoJson version 6. I'm not sure if that is related to the issue you are having, but you might want to consider updating the library. – VE7JRO Oct 3 at 20:48
0

The problem is quite explicit in the error message:

expected 808529969 bytes

You told the server that you were going to give it 808529969 bytes, yet you provided much less. Why did you tell it you were going to send so much data? Maybe we can get a clue by rewriting this number in hexadecimal:

808529969 = 0x30313031

Breaking this into individual bytes, LSB first (as this is how the server expects it), you have

0x31 0x30 0x31 0x30

These are the bytes you sent to the server, and they all happen to be ASCII codes for digits, which make up the string "1010". I don't know how you came up with this, as the code you posted is not the one you tested (the posted code does not even compile!). But it seems at one point you tried to convert something to ASCII binary, and indeed String(10, BIN) produces exactly the string above.

The server expects the data length to be provided as binary bytes, not as an ASCII string. I personally try to avoid the String class, which can create memory issues. In this situation I would just work with an array of bytes, typed as uint8_t (the same as the Arduino byte type, only more standard). In the code below, I assume for simplicity that you keep a String for building the payload, but then I switch to a raw byte array:

const uint8_t *data = (const uint8_t *) json.c_str();
uint64_t data_length = json.length();
uint8_t packet[13 + data_length];  // 13-byte header + payload

// Write the 5-byte signature.
memcpy(packet, "ZBXD\1", 5);

// Add the 8-byte packet length, LSB first.
for (int i = 0; i < 8; i++) {
    packet[5+i] = data_length >> 8*i;
}

// Add the payload.
memcpy(packet + 13, data, data_length);

// Send the packet.
client.write(packet, sizeof packet);

Note that when working with binary, rather than text data, you have to use memcpy() instead of strcpy(), and client.write() instead of client.print().

  • Hello Mr @EdgarBonet, first I want to thank you for the help, finally I put your code inside my project, but the server is reporting another error, now says that the message is still too big, according to the documentation, this message may have in 128MB maximum, and according to the error code code Message size 2066888786 from 192.168.0.130 exceeds the maximum size 1073741824 bytes. Message ignored. is exceeding this limit, as I don`t have enough reputation to answer here in the forum I forwarded my entire code to link[/link]. Thank – Venicius Back Oct 3 at 13:22
  • 1
    @VeniciusBack: You probably have enough reputation to edit your question. And you wouldn't want to answer anyway... unless you do have an answer! – Edgar Bonet Oct 3 at 13:37
  • 1
    @VeniciusBack: Also, when you provide your code and report an error message, make double sure that the error message your report is generated by the exact same version of the code you are showing. Otherwise you are wasting everyone's time. I am pretty sure the error message and the code your are showing do not match. – Edgar Bonet Oct 3 at 14:35
  • 1
    I edited the post, with the full code of the arduino I am sending and the response of that code provided by the zabbix server. Thanks! – Venicius Back Oct 3 at 14:55
  • 1
    @VeniciusBack: Do not edit your answer, which was deleted by VE7JRO for a very good reason (it's not really an answer). Edit your question. – Edgar Bonet Oct 3 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.