forked from 1000io/OregonPi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
108 lines (92 loc) · 3.18 KB
/
test.cpp
File metadata and controls
108 lines (92 loc) · 3.18 KB
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
/* ===================================================
C code : test.cpp
* ===================================================
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include "RCSwitch.h"
#include "RcOok.h"
#include "Sensor.h"
/* inlcude MQTT */
#include <mosquitto.h>
int main(int argc, char* argv[])
{
int RXPIN = 2;
int TXPIN = 0;
int loggingok; // Global var indicating logging on or off
FILE* fp; // Global var file handle
/* default values */
char MQTT_HOSTNAME[] = "localhost\0";
char MQTT_USERTNAME[] = "admin\0";
char MQTT_PASSWORD[] = "password\0";
char MQTT_TOPIC[] = "433MHz\0";
char* host = MQTT_HOSTNAME;
int port = 1883;
char* usr = MQTT_USERTNAME;
char* pswd = MQTT_PASSWORD;
char* topic = MQTT_TOPIC;
struct mosquitto* mosq = NULL;
// Initialize the Mosquitto library
mosquitto_lib_init();
// Create a new Mosquitto runtime instance with a random client ID,
// and no application-specific callback data.
mosq = mosquitto_new(NULL, true, NULL);
if (!mosq) {
fprintf(stderr, "Can't initialize Mosquitto library\n");
exit(-1);
}
mosquitto_username_pw_set(mosq, usr, pswd);
// Establish a connection to the MQTT server. Do not use a keep-alive ping
int ret = mosquitto_connect(mosq, host, port, 0);
if (ret) {
fprintf(stderr, "Can't connect to Mosquitto server\n");
exit(-1);
}
if (argc == 2) {
fp = fopen(argv[1], "a"); // Log file opened in append mode to avoid destroying data
loggingok = 1;
if (fp == NULL) {
perror("Failed to open log file!"); // Exit if file open fails
exit(EXIT_FAILURE);
}
}
else {
loggingok = 0;
}
if (wiringPiSetup() == -1)
return 0;
RCSwitch* rc = new RCSwitch(RXPIN, TXPIN);
while (1) {
if (rc->OokAvailable()) {
char message[100];
rc->getOokCode(message);
printf("%s\n", message);
Sensor* s = Sensor::getRightSensor(message);
if (s != NULL) {
printf("Temp : %f\n", s->getTemperature());
printf("Humidity : %f\n", s->getHumidity());
printf("Channel : %d\n", s->getChannel());
if ((loggingok) && (s->getChannel() > 0)) {
fprintf(fp, "OREGON#{\"channel\":%d,\"temp\":%f,\"humidity\":%f}\n", s->getChannel(), s->getTemperature(), s->getHumidity());
fflush(fp);
fflush(stdout);
}
}
char valueStr[240];
sprintf(valueStr, "OREGON#{\"message\":\"%s\",\"channel\":%d,\"temp\":%f,\"humidity\":%f}", message,s->getChannel(), s->getTemperature(), s->getHumidity());
ret = mosquitto_publish(mosq, NULL, &MQTT_TOPIC[0u], strlen(valueStr), valueStr, 0, false);
if (ret) {
fprintf(stderr, "Can't publish to Mosquitto server\n");
exit(-1);
}
else {
fprintf(stdout, "MQTT: %s\n", valueStr);
}
printf("\n");
delete s;
}
delay(1000);
}
}