moved publish after connect
This commit is contained in:
parent
3b741b13e2
commit
996bed526a
@ -33,6 +33,15 @@
|
|||||||
#include "everblu_meters.h"
|
#include "everblu_meters.h"
|
||||||
#include "cc1101.c"
|
#include "cc1101.c"
|
||||||
|
|
||||||
|
void IO_init(void)
|
||||||
|
{
|
||||||
|
wiringPiSetup();
|
||||||
|
pinMode (GDO2, INPUT);
|
||||||
|
pinMode (GDO0, INPUT);
|
||||||
|
|
||||||
|
cc1101_init();
|
||||||
|
}
|
||||||
|
|
||||||
void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
|
void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -49,6 +58,32 @@ void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
|
|||||||
if(!result){
|
if(!result){
|
||||||
/* Subscribe to broker information topics on successful connect. */
|
/* Subscribe to broker information topics on successful connect. */
|
||||||
mosquitto_subscribe(mosq, NULL, "WaterUsage ", 2);
|
mosquitto_subscribe(mosq, NULL, "WaterUsage ", 2);
|
||||||
|
|
||||||
|
struct tmeter_data meter_data = {
|
||||||
|
.liters = -1,
|
||||||
|
.battery_left = -1,
|
||||||
|
.reads_counter = -1,
|
||||||
|
.time_start = -1,
|
||||||
|
.time_end = -1,
|
||||||
|
};
|
||||||
|
char mqtt_topic[64];
|
||||||
|
char buff[MQTT_MSG_MAX_SIZE];
|
||||||
|
char meter_id[12];
|
||||||
|
|
||||||
|
sprintf(meter_id, "%i_%i", METER_YEAR, METER_SERIAL);
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
if(mosquitto_publish(mosq, NULL, mqtt_topic, strlen(buff),buff,1,false) != MOSQ_ERR_SUCCESS)
|
||||||
|
fprintf(stderr, "ERROR: could not publish message");
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
fprintf(stderr, "Connect failed\n");
|
fprintf(stderr, "Connect failed\n");
|
||||||
}
|
}
|
||||||
@ -72,32 +107,10 @@ void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const ch
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void IO_init(void)
|
|
||||||
{
|
|
||||||
wiringPiSetup();
|
|
||||||
pinMode (GDO2, INPUT);
|
|
||||||
pinMode (GDO0, INPUT);
|
|
||||||
|
|
||||||
cc1101_init();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
struct tmeter_data meter_data = {
|
|
||||||
.liters = -1,
|
|
||||||
.battery_left = -1,
|
|
||||||
.reads_counter = -1,
|
|
||||||
.time_start = -1,
|
|
||||||
.time_end = -1,
|
|
||||||
};
|
|
||||||
struct mosquitto *mosq = NULL;
|
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();
|
mosquitto_lib_init();
|
||||||
mosq = mosquitto_new(NULL, true, NULL);
|
mosq = mosquitto_new(NULL, true, NULL);
|
||||||
@ -130,17 +143,6 @@ int main(int argc, char *argv[])
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
if(mosquitto_publish(mosq, NULL, mqtt_topic, strlen(buff),buff,1,false) != MOSQ_ERR_SUCCESS)
|
|
||||||
fprintf(stderr, "ERROR: could not publish message");
|
|
||||||
|
|
||||||
mosquitto_destroy(mosq);
|
mosquitto_destroy(mosq);
|
||||||
mosquitto_lib_cleanup();
|
mosquitto_lib_cleanup();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user