MQTT, InfluxDB and Grafana

I have followed and read every post for the following Example.

I can interact with the database and confirmed Grafana can communicate with the database. However, I cannot get any points into the database from MQTT.

I have two readings shown in Paperspace UI I would like to graph.

I think the database is empty because the ESP8266 is sending string data over MQTT and InfluxDB does not accept strings for data.

Is there a way to write directly into InfluxDB from MQTT? I would rather not install another piece of software if this can be accomplished by changing my Arduino sketch.

#include <ESP8266WiFi.h>
#include <Wire.h>
#include <PubSubClient.h>
#include <Adafruit_HDC1000.h>

#define wifi_ssid "YOUR WIFI SSID"
#define wifi_password "WIFI PASSWORD"

#define mqtt_server "YOUR_MQTT_SERVER_HOST"
#define mqtt_user "your_username"
#define mqtt_password "your_password"

#define humidity_topic "sensor/humidity"
#define temperature_topic "sensor/temperature"

WiFiClient espClient;
PubSubClient client(espClient);
Adafruit_HDC1000 hdc = Adafruit_HDC1000();

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);

  // Set SDA and SDL ports
  Wire.begin(2, 14);

  // Start sensor
  if (!hdc.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    // If you do not want to use a username and password, change next line to
    // if (client.connect("ESP8266Client")) {
    if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

bool checkBound(float newValue, float prevValue, float maxDiff) {
  return !isnan(newValue) &&
         (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
}

long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 1.0;

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 1000) {
    lastMsg = now;

    float newTemp = hdc.readTemperature();
    float newHum = hdc.readHumidity();

    if (checkBound(newTemp, temp, diff)) {
      temp = newTemp;
      Serial.print("New temperature:");
      Serial.println(String(temp).c_str());
      client.publish(temperature_topic, String(temp).c_str(), true);
    }

    if (checkBound(newHum, hum, diff)) {
      hum = newHum;
      Serial.print("New humidity:");
      Serial.println(String(hum).c_str());
      client.publish(humidity_topic, String(hum).c_str(), true);
    }
  }
}

“You don’t”
That is to say, using openHAB persistence you persist Item values. The persistence service doesn’t care how the Items got populated.

So there’s two areas to look at -
Your Items, have you got any, are they getting updated from MQTT?
You persistence, is it persisting the Items that you expect?

Here is a shot of my Items from Paperspace

I have the InfluxDB installed as an Addon and using it as default Persisence in System Configuration.

How can I check to see if Items are being persisted?

Thanks for your helpl.

Your Items are of string type, not suitable for charting with Grafana.

That is what I thought…Is there a way to send items through MQTT to InfluxDB by changing the Arduino code? Or is there a way to reformat the data prior to it being stored into InfluxDB without adding another service/software?

Configure the MQTT Channels to be number instead of text types. Change the type of the Items to Number instead of String. You will probably have to delete the old entries in InfluxDB as most databases don’t like it when you change the type of an Item. Using a new Item name would also work.

1 Like

That was it. I assumed it was necessary to make the Things and Items strings due to the MQTT being sent with a format of string. I guess the values are transformed in Openhab?

Thanks a bunch!
WJones

Docs describing supported channels of this binding

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.