Publish water usage in liters in MQTT
This commit is contained in:
parent
2db71059f6
commit
ba8f4b0ec9
3
cc1101.c
3
cc1101.c
@ -681,11 +681,10 @@ struct tmeter_data get_meter_data(void)
|
||||
meter_data_size=decode_4bitpbit_serial(rxBuffer, rxBuffer_size,meter_data);
|
||||
// show_in_hex(meter_data,meter_data_size);
|
||||
sdata = parse_meter_report(meter_data,meter_data_size);
|
||||
return sdata;
|
||||
}
|
||||
else
|
||||
{
|
||||
echo_debug(debug_out,"TMO on REC\r\n");
|
||||
}
|
||||
return;
|
||||
return sdata;
|
||||
}
|
||||
|
@ -6,9 +6,12 @@
|
||||
|
||||
#define MQTT_HOST "localhost"
|
||||
#define MQTT_PORT 1883
|
||||
#define MQTT_USER "homeassistant"
|
||||
#define MQTT_PASS "PASS"
|
||||
|
||||
|
||||
#define MQTT_KEEP_ALIVE 60
|
||||
#define MQTT_MSG_MAX_SIZE 512
|
||||
#define MQTT_TOPIC_NUM 3
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
@ -25,7 +28,7 @@ void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mo
|
||||
if(message->payloadlen){
|
||||
printf("%s %s", message->topic, (char *)message->payload);
|
||||
}else{
|
||||
printf("%s (null)\n", message->topic);
|
||||
//printf("%s (null)\n", message->topic);
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
@ -34,7 +37,7 @@ void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
|
||||
{
|
||||
if(!result){
|
||||
/* Subscribe to broker information topics on successful connect. */
|
||||
mosquitto_subscribe(mosq, NULL, "CCYY ", 2);
|
||||
mosquitto_subscribe(mosq, NULL, "WaterUsage ", 2);
|
||||
}else{
|
||||
fprintf(stderr, "Connect failed\n");
|
||||
}
|
||||
@ -54,6 +57,7 @@ void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const ch
|
||||
{
|
||||
/* Pring all log messages regardless of level. */
|
||||
printf("%s\n", str);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -72,11 +76,16 @@ int main(int argc, char *argv[])
|
||||
struct tmeter_data meter_data;
|
||||
struct mosquitto *mosq = NULL;
|
||||
char buff[MQTT_MSG_MAX_SIZE];
|
||||
char meter_id[12];
|
||||
char mqtt_topic[64];
|
||||
|
||||
sprintf(meter_id, "%i_%i", METER_YEAR, METER_SERIAL);
|
||||
|
||||
|
||||
mosquitto_lib_init();
|
||||
mosq = mosquitto_new(NULL, true, NULL);
|
||||
if(!mosq){
|
||||
printf("ERROR: Create MQTT client failed..\n");
|
||||
fprintf(stderr, "ERROR: Create MQTT client failed..\n");
|
||||
mosquitto_lib_cleanup();
|
||||
return 1;
|
||||
}
|
||||
@ -87,6 +96,7 @@ int main(int argc, char *argv[])
|
||||
mosquitto_message_callback_set(mosq, my_message_callback);
|
||||
mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
|
||||
|
||||
mosquitto_username_pw_set(mosq, MQTT_USER, MQTT_PASS);
|
||||
|
||||
//Connect to MQTT server
|
||||
if(mosquitto_connect(mosq, MQTT_HOST, MQTT_PORT, MQTT_KEEP_ALIVE)){
|
||||
@ -101,13 +111,15 @@ int main(int argc, char *argv[])
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
//mosquitto_publish(mosq,NULL,"CCYY ",strlen(buff)+1,buff,0,0);
|
||||
|
||||
IO_init();
|
||||
meter_data = get_meter_data();
|
||||
|
||||
sprintf(buff, "%d", meter_data.liters);
|
||||
sprintf(mqtt_topic, "homeassistant/sensor/cyblemeter_%s/state", meter_id);
|
||||
|
||||
printf("Liters: %i\n", meter_data.liters);
|
||||
mosquitto_publish(mosq, NULL, mqtt_topic, strlen(buff),buff,0,false);
|
||||
|
||||
|
||||
mosquitto_destroy(mosq);
|
||||
mosquitto_lib_cleanup();
|
||||
|
Loading…
x
Reference in New Issue
Block a user