/
thingithonmq.c
104 lines (90 loc) · 2.57 KB
/
thingithonmq.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
/* thingithonmq.c - publishes MAC addresses picked up by the hotspot monitor
* to the Thingitude server's MQTT broker.
*
* Copyright 2016 Mark Stanley.
*
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <mosquitto.h>
char *macFile;
char *timeNow;
int messagesToSend;
void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{
FILE *fp;
char buff[255]="";
char msgToSend[255]="";
char macString[255]="";
if(!result){
fp=fopen(macFile, "r");
while(!feof(fp)){
fgets(buff,255, (FILE *)fp);
if(!feof(fp)) {
strncpy(macString,buff,strlen(buff)-1);
sprintf(msgToSend,"{\"mac\":\"%s\",\"time\":\"%s\"}",macString, timeNow);
printf("Sending >> %s <<\n",msgToSend);
mosquitto_publish(mosq, NULL, "thingithon/mac", strlen(msgToSend), msgToSend, 2, 0);
}
}
fclose(fp);
}else{
fprintf(stderr, "Connect failed\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 my_publish_callback(struct mosquitto *mosq, void *userdata, int usernumber)
{
/* We've published so lets countdown the messages and exit nicely */
printf("Published message %d \n",messagesToSend);
messagesToSend--;
printf("Messages left to send %d \n",messagesToSend);
if(messagesToSend==0) {
mosquitto_disconnect(mosq);
}
}
int main(int argc, char *argv[])
{
char *host = "5.44.237.19";
int port = 1883;
int keepalive = 60;
bool clean_session = true;
struct mosquitto *mosq = NULL;
/* argv[1] is the filename, argv[2] is the number of MAC addresses */
/* and argv[3] is the timestamp in secs since the epoch */
if(argc==4) {
macFile=argv[1];
messagesToSend=atoi(argv[2]);
if(messagesToSend==0) {
return 1;
}
timeNow=argv[3];
} else {
printf("USAGE: thingithonmq macFileName numRecords timeStamp\n Where timeStamp is in seconds, like date +\%s\n");
return 1;
}
/* Start up Mosquitto */
mosquitto_lib_init();
mosq = mosquitto_new(NULL, clean_session, NULL);
if(!mosq){
fprintf(stderr, "Error: Out of memory.\n");
return 1;
}
mosquitto_log_callback_set(mosq, my_log_callback);
mosquitto_connect_callback_set(mosq, my_connect_callback);
mosquitto_publish_callback_set(mosq, my_publish_callback);
if(mosquitto_connect(mosq, host, port, keepalive)){
fprintf(stderr, "Unable to connect.\n");
return 1;
}
mosquitto_loop_forever(mosq, -1, 1);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}