-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
433 lines (390 loc) · 11.4 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* main.c -- A one wire temperature monitoring system for
* the Beaglebone Black
*
* This program monitors a couple of DS18B20 one wire temp probes
* and relays this information to a Storyboard UI.
*
* Copyright (C) 2014 Deric Panet-Raymond
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <gre/greio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <math.h>
#include "curl/curl.h"
#include "lcfg/lcfg.h"
#include "crankbrew.h"
#include <linux/fcntl.h>
#include <dirent.h>
#include <syslog.h>
#include <linux/inotify.h>
char * temp_wort;
char * temp_ambient;
char * web_address;
char * user;
char * pass;
char *heat_control = "/sys/class/gpio/gpio51/value";
char *cool_control = "/sys/class/gpio/gpio60/value";
int cool_fd;
int heat_fd;
struct MemoryStruct {
char *memory;
size_t size;
};
int read_setpoint(void *data)
{
thdata *thread_data = (thdata *)data;
char buffer[EVENT_BUF_LEN];
int fd;
int wd;
int length;
int i = 0;
syslog(LOG_DEBUG,"Starting to watch /tmp/set_point");
fd = inotify_init();
if ( fd < 0 ) {
syslog(LOG_ERR,"Inotify Init failed");
return 0;
}
wd = inotify_add_watch(fd,"/tmp/set_point",IN_CLOSE_WRITE | IN_CREATE);
while ( 1 ) {
i = 0;
syslog(LOG_DEBUG, "Waiting on inotify");
length = read(fd, buffer, EVENT_BUF_LEN);
if ( length < 0 ) {
syslog(LOG_ERR, "Inotify Read Failed");
return 0;
}
while ( i < length ) {
struct inotify_event *event = ( struct inotify_event * ) &buffer[i];
if ( event->mask & (IN_CLOSE_WRITE) ) {
FILE * t_fd = fopen("/tmp/set_point", "r");
char temp_buf[32];
long length;
if ( t_fd ) {
while ( fgets(temp_buf, 32, t_fd) != NULL ) {
}
}
fclose(t_fd);
if ( temp_buf ) {
thread_data->temp_data.set_point = atof(temp_buf);
syslog(LOG_DEBUG, "Setpoint converted to float is %f\n", thread_data->temp_data.set_point);
}
}
i += EVENT_SIZE + event->len;
}
}
inotify_rm_watch(fd,wd);
close(fd);
}
int log_temp(void *data)
{
thdata *thread_data = (thdata *)data;
CURL *curl;
CURLcode res;
char postdata[255];
struct MemoryStruct chunk;
chunk.memory = malloc(1);
chunk.size = 0;
char userpass[32];
snprintf(userpass,sizeof userpass,"%s:%s",user,pass);
while (1) {
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl && thread_data->good_read == 1) {
sprintf(postdata,"ambient=%f&wort=%f&sched=%d&heatcool=%d",thread_data->temp_data.temp2,thread_data->temp_data.temp1,2, thread_data->heatcool);
curl_easy_setopt(curl,CURLOPT_URL, web_address);
curl_easy_setopt(curl,CURLOPT_POSTFIELDS, postdata);
if ( thread_data->debug == 1 ) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
} else {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
}
curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1L);
curl_easy_setopt(curl,CURLOPT_HTTPAUTH,(long)CURLAUTH_ANY);
curl_easy_setopt(curl,CURLOPT_USERPWD,userpass);
res = curl_easy_perform(curl);
if ( res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed : %s\n", curl_easy_strerror(res));
}
thread_data->good_read = 0;
}
curl_easy_cleanup(curl);
curl_global_cleanup();
sprintf(postdata,"");
sleep(60);
}
return 0;
}
int temp_update(gre_io_t *send_handle, char * target, temp_update_t *event_data)
{
gre_io_serialized_data_t *nbuffer = NULL;
int ret;
/*
* Send a named event with an additional structure payload
*/
nbuffer = gre_io_serialize(NULL, target,
TEMP_UPDATE_EVENT,
TEMP_UPDATE_FMT,
event_data,
sizeof(temp_update_t));
ret = gre_io_send(send_handle, nbuffer);
//Free the allocated memory buffer for the serialized data
gre_io_free_buffer(nbuffer);
return ret;
}
int control_temp(char *heat,char *chill)
{
int ret;
int ret_1;
ret = write(heat_fd,heat,1);
ret_1 = write(cool_fd,chill,1);
return ret && ret_1;
}
float read_temp(int fp)
{
char tmpData[6];
unsigned char onewire[256];
float rounded_down;
int num_read = 0;
int failed_read = 0;
float float_val = 0.0;
memset(onewire,0,256);
memset(tmpData,0,6);
while((num_read = read(fp, onewire, 256)) > 0)
{
if ( strstr(onewire, "NO") == NULL ) {
strncpy(tmpData, strstr(onewire, "t=") + 2, 5);
float_val = strtof(tmpData, NULL);
float_val = float_val /1000;
//syslog(LOG_INFO,"Read Temp: %.3f C\n", float_val);
failed_read = 0;
} else {
failed_read = 1;
break;
}
}
if ( failed_read == 1 ) {
syslog(LOG_ERR, "Failed to read Wort temp sensor, skipping to next cycle");
return -999.0;
}
rounded_down = ceilf(float_val * 100) / 100;
if (rounded_down > 100.0f) {
return -999.0;
}
return rounded_down;
}
static void * check_temperature(void * data)
{
thdata *thread_data = (thdata *)data;
temp_update_t event_data;
int ret;
float old_ambient,old_wort;
int fp, fp1;
int a,b;
char hex_string[6];
int dec_val = 0;
float temp_read;
while (1)
{
fp = open(temp_wort, O_RDONLY);
fp1 = open(temp_ambient, O_RDONLY);
if ( fp != -1 && fp1 != -1 )
{
old_wort = event_data.temp1;
old_ambient = event_data.temp2;
// read wort then read ambient
temp_read = read_temp(fp);
if ( temp_read != -999.0 ) {
event_data.temp1 = temp_read;
} else {
event_data.temp1 = old_wort;
syslog(LOG_ERR, "one wire read failed for Wort, using old temp of %f",event_data.temp1);
}
temp_read = read_temp(fp1);
if ( temp_read != -999.0 ) {
event_data.temp2 = temp_read;
thread_data->good_read = 1;
} else {
event_data.temp2 = old_ambient;
syslog(LOG_ERR, "one wire read failed for Ambient, using old temp of %f",event_data.temp2);
}
if ( event_data.temp1 != old_wort || event_data.temp2 != old_ambient ) {
thread_data->temp_data.temp1 = event_data.temp1;
thread_data->temp_data.temp2 = event_data.temp2;
syslog(LOG_DEBUG,"GOT WORT TEMP OF : %f and AMBIENT OF %f and heatcool is %d", thread_data->temp_data.temp1,thread_data->temp_data.temp2,thread_data->heatcool);
// ret = temp_update(thread_data->send_handle, NULL, &event_data);
}
if ( thread_data->temp_data.temp1 < (thread_data->temp_data.set_point-0.3) && thread_data->temp_data.temp1 != -1 ) {
syslog(LOG_NOTICE,"Heat Enabled [ Setpoint %f : Wort Temp : %f\n", thread_data->temp_data.set_point,thread_data->temp_data.temp1);
if ( control_temp("1","0") ) {
thread_data->heatcool=2;
} else {
syslog(LOG_ERR,"Error, unable to enable heating");
}
}
if ( thread_data->temp_data.temp1 == thread_data->temp_data.set_point || thread_data->temp_data.temp1 == (thread_data->temp_data.set_point+0.1) || thread_data->temp_data.temp1 == (thread_data->temp_data.set_point-0.1) ) {
syslog(LOG_NOTICE,"Heating and Cooling disabled");
if ( control_temp("0","0") ) {
thread_data->heatcool=1;
} else {
syslog(LOG_ERR,"Unable to disable heating and cooling\n");
}
}
if ( thread_data->temp_data.temp1 > (thread_data->temp_data.set_point + 0.4) && thread_data->temp_data.temp2 >= 3 && thread_data->temp_data.temp1 != -1) {
syslog(LOG_NOTICE,"Chill Enabled [ Setpoint %f : Wort Temp : %f\n", thread_data->temp_data.set_point,thread_data->temp_data.temp1);
if ( control_temp("0","1") ) {
thread_data->heatcool=3;
} else {
syslog(LOG_ERR,"Unable to enable the chiller\n");
}
}
close(fp);
close(fp1);
sleep(thread_data->read_freq);
} else {
syslog(LOG_ERR, "unable to open temperature sensors");
}
}
}
enum lcfg_status read_cfg(const char *key, void *data, size_t len, void *user_data) {
int i;
char c;
printf("%s = \"", key);
for( i = 0; i < len; i++ ) {
c = *((const char *)(data + i));
printf("%c", isprint(c) ? c : '.');
}
puts("\"");
return lcfg_status_ok;
}
int main(int argc, char* argv[])
{
pthread_t temperature_thread,templog_thread,setpoint_thread;
int not_connect = 0;
int c;
char *channel = NULL;
pthread_mutex_t mutex;
pthread_cond_t condvar;
int ret;
int done = 0;
gre_io_t *send_handle;
thdata data1;
size_t len;
struct lcfg *cfg = lcfg_new("./brew.cfg");
//open the log
openlog("Fermtroller", LOG_PID, LOG_USER);
if ( lcfg_parse(cfg) != lcfg_status_ok ) {
syslog(LOG_INFO,"Error reading config file : %s\n", lcfg_error_get(cfg));
} else {
lcfg_accept(cfg, read_cfg,0);
}
lcfg_value_get(cfg, "wort_address",(void *)&temp_wort, &len);
lcfg_value_get(cfg, "ambient_address",(void *)&temp_ambient, &len);
if ( lcfg_value_get(cfg, "web_address", (void *)&web_address, &len) != lcfg_status_ok ) {
syslog(LOG_ERR,"Error reading web_address : %s\n", lcfg_error_get(cfg));
}
lcfg_value_get(cfg, "user",(void *)&user,&len);
lcfg_value_get(cfg,"pass",(void *)&pass,&len);
// assign the GPIO file descriptors
cool_fd = open(cool_control,O_WRONLY);
heat_fd = open(heat_control,O_WRONLY);
data1.temp_data.temp1 = 0.0;
data1.temp_data.temp2 = 0.0;
data1.good_read = 0;
data1.heatcool = 1;
data1.debug = 0;
data1.read_freq = 30; // default read frequency is 30 seconds
control_temp("0","0");
while ((c = getopt( argc, argv, "vc:p:t:r:")) != -1 )
{
switch(c)
{
case 'v':
data1.debug = 1;
break;
case 'c':
channel = optarg;
syslog(LOG_INFO,"Setting channel to : %s\n", channel);
break;
case 't':
data1.temp_data.set_point= atof(optarg);
syslog(LOG_INFO,"Setting Ferment temp to %f\n", data1.temp_data.set_point);
break;
case 'r':
data1.read_freq = atoi(optarg);
syslog(LOG_INFO,"Setting read frequency to %d\n", data1.read_freq);
break;
}
}
#ifdef STORYBOARD
if ( channel == NULL ) {
syslog(LOG_ERR,"You must supply a greio channel temp_driver -c <storyboard channel>\n");
exit(-1);
}
send_handle = gre_io_open(channel, GRE_IO_TYPE_WRONLY);
if(send_handle == NULL && channel != NULL ) {
syslog(LOG_ERR,"Can't open send handle\n");
while ( not_connect == 0 ) {
send_handle = gre_io_open(channel, GRE_IO_TYPE_WRONLY);
if ( send_handle != NULL ) {
not_connect = 1;
}
syslog(LOG_INFO,"Waiting to connect\n");
sleep(1);
}
}
// push the send handle into the thread data pointer so we can access it in our check_tmep thread.
data1.send_handle = send_handle;
#endif
FILE * t_fd = fopen("/tmp/set_point", "r");
char temp_buf[32];
long length;
if ( t_fd ) {
while ( fgets(temp_buf, 32, t_fd) != NULL ) {
}
}
fclose(t_fd);
if ( temp_buf ) {
data1.temp_data.set_point = atof(temp_buf);
syslog(LOG_DEBUG, "Setpoint converted to float is %f\n", data1.temp_data.set_point);
}
syslog(LOG_INFO,"Fermtroller starting");
pthread_create(&temperature_thread, NULL, (void *)&check_temperature, &data1);
sleep(1);
pthread_create(&templog_thread, NULL, (void *)&log_temp, &data1);
pthread_create(&setpoint_thread, NULL, (void *)&read_setpoint, &data1);
ret = pthread_mutex_init(&mutex, NULL);
if(ret != 0) {
syslog(LOG_ERR,"Mutex error\n");
return -1;
}
ret = pthread_cond_init(&condvar, NULL);
if(ret != 0) {
syslog(LOG_ERR,"Condvar error\n");
return -1;
}
//get_input(&data1);
while(!done) {
syslog(LOG_INFO,"waiting\n");
pthread_mutex_lock(&mutex);
pthread_cond_wait(&condvar, &mutex);
}
lcfg_delete(cfg);
free (temp_wort);
free (temp_ambient);
free (web_address);
free (user);
free (pass);
syslog(LOG_INFO,"Fermtroller shutting down\n");
close(cool_fd);
close(heat_fd);
closelog();
return 0;
}