-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
229 lines (180 loc) · 6.5 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// Read Bosch BME680 registers and compute values
// author: [email protected]
// started: 2021/11/10
//
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <limits.h>
#include <MQTTClient.h>
#include "bme680.h"
#define ADDRESS "tcp://rbtmq01.duchess.burkholder.net:1883"
#define TOPIC "/beagle/bme680"
#define QOS 1
#define TIMEOUT 1000L
void main( int argc, char *argv[] ) {
if ( 3 == argc ) {
printf( "domo idx: %s, location %s\n", argv[ 1 ], argv[ 2 ] );
}
else {
printf( "need domo device idx, location\n" );
exit(EXIT_FAILURE);
}
int rc;
char szHostName[ HOST_NAME_MAX + 1 ];
rc = gethostname( szHostName, HOST_NAME_MAX + 1 );
if ( 0 != rc ) {
printf( "failure with gethostname\n" );
exit( EXIT_FAILURE );
}
int i2c_id_bus = 2; /* seeed beagleboard green bus number */
u8 i2c_addr_bme680 = 0x76; /* seeed bme680 board */
int fd_bme680; /* file descripter for device when opened */
fd_bme680 = open_i2c_device( i2c_id_bus, i2c_addr_bme680 );
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;
if ( ( rc = MQTTClient_create(
&client, ADDRESS, szHostName,
MQTTCLIENT_PERSISTENCE_NONE, NULL) ) != MQTTCLIENT_SUCCESS) {
printf("Failed to create client, return code %d\n", rc);
exit(EXIT_FAILURE);
}
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = "beagle"; // convert to command line
conn_opts.password = "beagle"; // convert to command line
if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) {
printf("Failed to connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}
#define max_buf_size 500
char szTopic[ max_buf_size ];
char szMessage[ max_buf_size ];
if ( 0 <= fd_bme680 ) {
int result;
result = check_chip_id( fd_bme680 );
if ( 0 < result ) {
// make sure iir filtering is off
result = write_register( fd_bme680, iir_filter_control, 0x00 );
// temperature seems a bit low by a degree or two
// pressure seems high - about 200 or 300 too high
// humidity seems low - a bit low by 4 or 5?
struct temperature t;
result = read_temperature_calibration( fd_bme680, &t );
struct pressure p;
result = read_pressure_calibration( fd_bme680, &p );
struct humidity h;
result = read_humidity_calibration( fd_bme680, &h );
time_t now;
struct tm timeinfo;
unsigned char szTimeInfo[ 50 ];
const unsigned char szTopic_nr[] = "/beagle/bme680";
const unsigned char szTopic_domo[] = "domoticz/in";
const unsigned char szTopic_apparition[] = "beagle";
while ( 1 ) {
// Get current time and publish it
time( &now );
localtime_r( &now, &timeinfo );
sprintf( szTimeInfo, "%04d-%02d-%02d %02d:%02d:%02d",
timeinfo.tm_year + 1900,
timeinfo.tm_mon + 1,
timeinfo.tm_mday,
timeinfo.tm_hour,
timeinfo.tm_min,
timeinfo.tm_sec);
result = measure_pth( fd_bme680, &t.raw, &p.raw, &h.raw );
compensate_temperature( &t );
compensate_pressure( &p, &t );
compensate_humidity( &h, &t );
double temperature = (double)t.compensated / 100.0;
double pressure = (double)p.compensated / 100.0;
double humidity = (double)h.compensated / 1000.0;
printf(
"p=%d(%0.2f) mbar, t=%d(%0.2f) degC, h=%d(%0.3f)%\n",
p.raw, pressure,
t.raw, temperature,
h.raw, humidity
);
int sizeTopic;
int sizeMessage;
// message for node-red
/*
sizeMessage = snprintf(
szMessage, max_buf_size,
"{\"device\":\"%s\",\"values\":{\"ti\":\"%s\",\"t\":%0.2f,\"p\":%0.2f,\"h\":%0.3f}}",
szHostName, szTimeInfo, temperature, pressure, humidity
);
pubmsg.payload = szMessage;
pubmsg.payloadlen = sizeMessage;
pubmsg.qos = QOS;
pubmsg.retained = 0;
if ( (rc = MQTTClient_publishMessage(client, szTopic_nr, &pubmsg, &token)) != MQTTCLIENT_SUCCESS) {
printf("Failed to publish node-red message, return code %d\n", rc);
}
else {
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Message %d: %s=%s\n", token, szTopic_nr, szMessage );
}
*/
// message for domoticz - to be deprecated
/*
sizeMessage = snprintf(
szMessage, max_buf_size,
"{\"command\":\"udevice\",\"idx\":%s,\"nvalue\":0,\"svalue\":\"%0.2f;%0.3f;0;%0.2f;0\"}",
argv[ 1 ], temperature, humidity, pressure
);
pubmsg.payload = szMessage;
pubmsg.payloadlen = sizeMessage;
pubmsg.qos = QOS;
pubmsg.retained = 0;
if ( (rc = MQTTClient_publishMessage(client, szTopic_domo, &pubmsg, &token)) != MQTTCLIENT_SUCCESS) {
printf("Failed to publish domoticz message, return code %d\n", rc);
}
else {
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Message %d: %s=%s\n", token, szTopic_domo, szMessage );
}
*/
// message for apparition
sizeMessage = snprintf(
szMessage, max_buf_size,
"{\"ti\":\"%s\",\"l\":\"%s\",\"t\":%0.2f,\"p\":%0.2f,\"h\":%0.3f}",
szTimeInfo, argv[ 2 ], temperature, pressure, humidity
);
sizeTopic = snprintf(
szTopic, max_buf_size,
"bb/%s/bme680", argv[ 1 ]
);
pubmsg.payload = szMessage;
pubmsg.payloadlen = sizeMessage;
pubmsg.qos = QOS;
pubmsg.retained = 0;
if ( (rc = MQTTClient_publishMessage(client, szTopic, &pubmsg, &token)) != MQTTCLIENT_SUCCESS) {
printf("Failed to publish apparition message, return code %d\n", rc);
}
else {
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Message %d: %s=%s\n", token, szTopic, szMessage );
}
// finish up
printf( "\n" );
sleep( 20 );
}
}
}
close( fd_bme680 );
if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
printf("Failed to disconnect, return code %d\n", rc);
MQTTClient_destroy(&client);
printf( "done\n" );
}