Good day,
I have a rainfall tipping bucket from DFRobot which uses a library that provides raw data (number of tips), rainfall in the previous hour, total sensor up time and total rainfall during sensor uptime.
I want to use the pubsub library to send the data through to Home Assistant, where I want to historize the rainfall data.
Code for Raspberry Pi Pico connected via I2C to Rainfall Sensor:
#include "DFRobot_RainfallSensor.h"
#include <PubSubClient.h>
#include <WiFi.h>
DFRobot_RainfallSensor_I2C Sensor(&Wire);
// Replace with your network credentials
const char* ssid = "My SSID";
const char* password = "My Password";
// MQTT broker details
const char* mqtt_server = "My HA IP Address with MQTT Broker";
const int mqtt_port = 1883;
const char* mqtt_topic_rain = "rainfall";
const char* mqtt_username = "mqttUser";
const char* mqtt_password = "password1";
uint32_t prevRain = 0;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback); // Define the callback function
delay(1000);
while(!Sensor.begin()){
Serial.println("Sensor init err!!!");
delay(1000);
}
Serial.print("vid:\t");
Serial.println(Sensor.vid,HEX);
Serial.print("pid:\t");
Serial.println(Sensor.pid,HEX);
Serial.print("Version:\t");
Serial.println(Sensor.getFirmwareVersion());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
Serial.println("connected");
client.subscribe(mqtt_topic_rain);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
uint32_t rain;
//Get the sensor working time, unit: hour
Serial.print("Sensor WorkingTime:\t");
Serial.print(Sensor.getSensorWorkingTime());
Serial.println(" H");
//Get the accumulated rainfall during the sensor working time
Serial.print("Rainfall:\t");
Serial.println(Sensor.getRainfall());
//rain = Sensor.getRawData();
//Get the accumulated rainfall within 1 hour of the system (function parameter optional 1-24)
Serial.print("1 Hour Rainfall:\t");
Serial.print(Sensor.getRainfall(1));
Serial.println(" mm");
//Get the raw data, the number of tipping buckets for rainfall, unit: times
Serial.print("rainfall raw:\t");
Serial.println(Sensor.getRawData());
rain = Sensor.getRawData();
delay(1000);
//client.publish(mqtt_topic_rain, rain);
if(rain > prevRain){
client.publish(mqtt_topic_rain, "0.2974");
}
prevRain = rain;
}
If I listen to the MQTT topic “rainfall” on HA, I get a 0.2794 every time I tip the bucket, which is what I want. However, the sensor entity I set up, only updates the first time I tip, and never again after that.
YAML configuration for MQTT Sensor data:
mqtt:
sensor:
- name: "Omeya Rain"
state_topic: "rainfall"
state_class: total_increasing
device_class: precipitation
unit_of_measurement: "mm"
unique_id: "OmeyaRain"
It seems like the way I have set up my sensor, or the fact that client.publish sends a string, is preventing me from totalizing my data.
I would appreciate some help or advice.
Thank you