InfluxDB 1

In fase di sviluppo Place holder In fase di sviluppo

Questa pagina non è ancora qui

In questa pagina sarà descritto come pubblicare dati su un database InfluxDB 1, per esempio installato di un RaspberryPi.

Per l'uso di un client InfluxDB 2: Client InfluxDB 2.

Il primo codice pubblica la concentrazione di CO2 nell'aria. Il sensore è un MH-Z19 collegato direttamente ad ESP32; ma dovrebbe funzionare anche con MH-Z14 (nota 1 e nota 2).

#include <WiFiMulti.h>
#include <InfluxDbClient.h>

// WiFi
#define WIFI_SSID "xxx"
#define WIFI_PASSWORD "xxx"
WiFiMulti wifiMulti;

// InfluxDb
#define INFLUXDB_URL "http://192.168.10.24:8086"
#define INFLUXDB_DB_NAME "xxx"
#define INFLUXDB_USER "xxx"
#define INFLUXDB_PASSWORD "xxx"
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME);

// MH-Z19
#define MHZ_LEN 9
#define CO2_SENSORID "MH-Z19"
const uint8_t ReadCO2[MHZ_LEN] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; // 0x86 - Read CO2 concentration

#define ESP_32_NAME "GW2"

Point sensor("mysensor");
int recordID = 0;
int measureID = 0;

void setup() {
  Serial.begin(115200);
  Serial2.begin(9600);

  Serial.println("Connessione al WiFi in corso");
  WiFi.mode(WIFI_STA);
  wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
  while (wifiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("Connesso");

  client.setConnectionParamsV1(INFLUXDB_URL, INFLUXDB_DB_NAME, INFLUXDB_USER, INFLUXDB_PASSWORD);
  if (client.validateConnection()) {
    Serial.print("Connessione a InfluxDB: ");
    Serial.println(client.getServerUrl());
    }
  else {
    Serial.print("Connesione a InfluxDB fallita: ");
    Serial.println(client.getLastErrorMessage());
  }
}

void loop() {
  uint8_t ReplayCO2[MHZ_LEN];
  int CO2ppm;

  Serial2.write(ReadCO2, MHZ_LEN);
  delay(50);
  if (Serial2.available() != MHZ_LEN){
    Serial.print("ERRORE: numero incostintente di byte ricevuti da MHZxx: ");
    Serial.println(Serial2.available());
    Serial2.flush();
  }
  else {
    for (int i = 0; i < MHZ_LEN; i++)
      ReplayCO2[i] = Serial2.read();

    if ( (ReplayCO2[0] == 0xFF) && (ReplayCO2[1] == 0x86) ){
      CO2ppm = ReplayCO2[2] * 256 + ReplayCO2[3];
      Serial.print("CO2: ");
      Serial.print(CO2ppm);
      Serial.println(" ppm");

      sensor.clearFields();
      sensor.clearTags();
      sensor.addField("CO2", CO2ppm );
      sensor.addField("measureID", measureID++);
      sensor.addField("RecordID", recordID++);
      sensor.addTag("GatewayID", ESP_32_NAME );
      sensor.addTag("SensorID", CO2_SENSORID);

      Serial.print("Scrivo: ");
      Serial.println(client.pointToLineProtocol(sensor));

      if (wifiMulti.run() != WL_CONNECTED) {
        Serial.println("Connessione Wifi persa");
      }
      if (!client.writePoint(sensor)) {
        Serial.print("Errore di scrittura su InfluxDB: ");
        Serial.println(client.getLastErrorMessage()); 
      }
      delay(5000);
    }
  }
}

Il secondo codice pubblica la temperatura misurata da alcuni PIC-WL-Sen via nRF24L01+.

#include <WiFiMulti.h>
#include <InfluxDbClient.h>

// WiFi
#define WIFI_SSID "xxx"
#define WIFI_PASSWORD "xxx"
WiFiMulti wifiMulti;

// InfluxDb
#define INFLUXDB_URL "http://192.168.10.24:8086"
#define INFLUXDB_DB_NAME "xxx"
#define INFLUXDB_USER "xx"
#define INFLUXDB_PASSWORD "xxx"
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME);

#include <RF24.h>
#include <printf.h>
#include <InfluxDbClient.h>

// nRF24L01+
#define RF 94 // 2494 MHz, WiFi free
#define ID_LEN 6
const byte address[6] = {0xC2, 0xC2, 0xC2, 0xC2, 0xC2}; // Indirizzo del pipe
struct frame_t { // Data to gateway frame structure
  uint8_t versione; // Frame version (FRAME_VERSION))
  uint8_t flagM; // 8 bits: 1: measure exist 0; empty
  uint16_t measureID; // Frame counter
  uint8_t DevID[ID_LEN]; // Sensor identifier (6 bytes read from ID Locations)
  uint16_t vdd; // Vdd [mV]
  int16_t temperature; // Temperature [°C * 1E-2. Example: 36,5°C -> 3650]
  uint16_t measure[6]; // Other measures... TODO
  uint32_t S; // Secure hash... TODO
  };
struct frame_t misure;

RF24 radio(4, 5); // Imposta CE e nCSN conformemente all'hardware

#define ESP_32_NAME "GW1"
Point sensor("mysensor");
int recordID = 0;

void setup() {
  Serial.begin(115200);
  printf_begin();

  Serial.println("Connessione al WiFi in corso");
  WiFi.mode(WIFI_STA);
  wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
  while (wifiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("Connesso");

  client.setConnectionParamsV1(INFLUXDB_URL, INFLUXDB_DB_NAME, INFLUXDB_USER, INFLUXDB_PASSWORD);
  if (client.validateConnection()) {
    Serial.print("Connessione a InfluxDB: ");
    Serial.println(client.getServerUrl());
  } else {
    Serial.print("Connesione a InfluxDB fallita: ");
    Serial.println(client.getLastErrorMessage());
  }

  radio.begin();
  radio.openReadingPipe(0, address); // Imposta l'indirizzo del pipe 0 di ricezione
  radio.setAutoAck(false); // Disattiva la richiesta di ACK
  radio.setChannel(RF);
  radio.setDataRate(RF24_2MBPS);
  radio.enableDynamicPayloads ();
  radio.printPrettyDetails();
  radio.startListening();
  Serial.println("nRF24L01+ Pronto...");
  }

void loop() {
  char SensorID[ID_LEN * 2 + 1];

  if (radio.available()) {
    radio.read( (unsigned char *) & misure, sizeof(misure));

    sensor.clearFields();
    sensor.clearTags();
    sensor.addField("Temperatura", (float)misure.temperature / 100);
    sensor.addField("VDD", (float)misure.vdd / 1000);
    sensor.addField("measureID", misure.measureID);
    sensor.addField("RecordID", recordID++);

    sprintf(SensorID, "%02X%02X%02X%02X%02X%02X", misure.DevID[0], misure.DevID[1], misure.DevID[2], misure.DevID[3], misure.DevID[4], misure.DevID[5] );

    sensor.addTag("SensorID", SensorID);
    sensor.addTag("GatewayID", ESP_32_NAME );

    Serial.print("Scrivo: ");
    Serial.println(client.pointToLineProtocol(sensor));

    if (wifiMulti.run() != WL_CONNECTED) {
      Serial.println("Connessione Wifi persa");
      }

    if (!client.writePoint(sensor)) {
      Serial.print("Errore di scrittura su InfluxDB: ");
      Serial.println(client.getLastErrorMessage());
    }
  }
}

Note

  1. La libreria MH-Z CO2 non funziona usando Serial2
  2. I fogli tecnici


Pagina creata nel febbraio 2022
Ultima modifica: 9 febbraio 2022


Pagina principaleAccessibilitàNote legaliPosta elettronicaXHTML 1.0 StrictCSS 3

Vai in cima