This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
everblu_meters.c
129 lines (98 loc) · 3.21 KB
/
everblu_meters.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* the radian_trx SW shall not be distributed nor used for commercial product*/
/* it is exposed just to demonstrate CC1101 capability to reader water meter indexes */
#define METER_YEAR 16
#define METER_SERIAL 123456
#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
#include <stdio.h>
#include <stdlib.h>
#include <mosquitto.h>
#include <string.h>
#include "everblu_meters.h"
#include "cc1101.c"
void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
if(message->payloadlen){
printf("%s %s", message->topic, (char *)message->payload);
}else{
//printf("%s (null)\n", message->topic);
}
fflush(stdout);
}
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, "WaterUsage ", 2);
}else{
fprintf(stderr, "Connect failed\n");
}
}
void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
{
int i;
printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
for(i=1; i<qos_count; i++){
printf(", %d", granted_qos[i]);
}
printf("\n");
}
void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str)
{
/* Pring all log messages regardless of level. */
printf("%s\n", str);
}
void IO_init(void)
{
wiringPiSetup();
pinMode (GDO2, INPUT);
pinMode (GDO0, INPUT);
cc1101_init();
}
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){
fprintf(stderr, "ERROR: Create MQTT client failed..\n");
mosquitto_lib_cleanup();
return 1;
}
//Set callback functions
mosquitto_log_callback_set(mosq, my_log_callback);
mosquitto_connect_callback_set(mosq, my_connect_callback);
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)){
fprintf(stderr, "ERROR: Unable to connect to MQTT broker.\n");
return 1;
}
//Start a thread, and call mosquitto? Loop() continuously in the thread to process network information
int loop = mosquitto_loop_start(mosq);
if(loop != MOSQ_ERR_SUCCESS)
{
fprintf(stderr, "ERROR: failed to create mosquitto loop");
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);
mosquitto_publish(mosq, NULL, mqtt_topic, strlen(buff),buff,0,false);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}