Arduino sending data to Grafana

Hello everyone, I just registered on the Grafana website because I’m interested in testing it for future projects. At the moment, I’m using the free plan just to run some tests. My intention is to send measurements from an Arduino Nano ESP32. As a data source, I’m trying to use Prometheus, but it seems that the measurements (currently just random values) are not being sent

#include <WiFi.h>
#include <HTTPClient.h>

// Configurazioni WiFi
#define WIFI_SSID "xxx"
#define WIFI_PASSWORD "xxxx"

// Configurazioni Prometheus Pushgateway
#define PUSHGATEWAY_URL xxxxxx"
#define API_KEY "xxxxxx"

void setup() {
  // Inizializza la comunicazione seriale
  Serial.begin(9600);
  while (!Serial);

  // Connessione WiFi
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connessione al WiFi...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" Connesso!");
}

void loop() {
  // Genera 5 valori casuali tra 0 e 10
  int valore1 = random(0, 11);
  int valore2 = random(0, 11);
  int valore3 = random(0, 11);
  int valore4 = random(0, 11);
  int valore5 = random(0, 11);

  // Crea il payload per Prometheus
  String payload = "";
  payload += "# TYPE valore1 gauge\n";
  payload += "valore1 " + String(valore1) + "\n";
  payload += "# TYPE valore2 gauge\n";
  payload += "valore2 " + String(valore2) + "\n";
  payload += "# TYPE valore3 gauge\n";
  payload += "valore3 " + String(valore3) + "\n";
  payload += "# TYPE valore4 gauge\n";
  payload += "valore4 " + String(valore4) + "\n";
  payload += "# TYPE valore5 gauge\n";
  payload += "valore5 " + String(valore5) + "\n";

  // Invia i dati al Pushgateway
  if (sendToPrometheus(payload)) {
    Serial.println("Dati inviati con successo!");
  } else {
    Serial.println("Errore nell'invio dei dati.");
  }

  // Attendi 5 secondi prima di inviare nuovi dati
  delay(5000);
}

bool sendToPrometheus(String payload) {
  WiFiClient client;
  HTTPClient http;

  // Configura la richiesta HTTP
  http.begin(client, PUSHGATEWAY_URL);
  http.addHeader("Content-Type", "text/plain");
  http.addHeader("Authorization", "Bearer " + String(API_KEY));

  // Invia la richiesta POST
  int httpResponseCode = http.POST(payload);

  // Stampa la risposta del server (per debug)
  if (httpResponseCode > 0) {
    String response = http.getString();
    Serial.println("Risposta del server: " + response);
  } else {
    Serial.print("Errore HTTP: ");
    Serial.println(httpResponseCode);
  }

  // Chiudi la connessione
  http.end();

  // Verifica la risposta
  return (httpResponseCode == 202);
}

I don’t quite understand what parameters to enter in the Arduino code. It always returns 'Error in sending data. HTTP Error: -1"

I ask for your forgiveness, but I’m just getting started

Thank you all, have a great day

1 Like

Hi, thanks for the instructions.
I think I entered the correct data but Arduino (on the serial port) gives me this result:

Starting
Free Mem Before Setup: 317764
Connecting Wifi
Connecting to ‘xxxxxx’ …connected
IP address: xxx.xxx.x.xxx
Setting up sntp and setting time from: pool.ntp.org
.Time set succesfully
Free Mem After Setup: 277744
1740580018781
1740580033781
1740580048781
1740580063781
Begin serialization: Free Heap: 277744
Bytes used for serialization: 298
After serialization: Free Heap: 277744
After Compression Init: Free Heap: 277216
Required buffer size for compression: 379
Compressed Len: 205
After Compression: Free Heap: 277720
Sending To Prometheus
Connecting…
Connected.
Sent, waiting for response
Timed out connecting to Loki

I took the files from the site you indicated to me, the only thing I configured is the config.h file by inserting the access data for the WiFi network and those for Grafana:

#define WIFI_SSID "XXX"
#define WIFI_PASSWORD "XXXXXXXX"
// For more information on where to get these values see: https://github.com/grafana/diy-iot/blob/main/README.md#sending-metrics
#define GC_URL "https://prometheus-prod-24-prod-eu-west-2.grafana.net"
#define GC_PATH "/api/prom/push"
#define GC_PORT 443
#define GC_USER "2286XXX"
#define GC_PASS "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

I don’t understand where the problem is, the WiFi connects. Prometheus seems connected.

I took the data following this guide:

[diy-iot/README.md at main · grafana/diy-iot · GitHub]

Do you have any suggestions?
Thank you all