-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor_node.c
160 lines (125 loc) · 3.87 KB
/
sensor_node.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
/********************************************************************************
* File Name: sensor_node.c
* Author: Jeroen Van Aken
* Organizaion: KU_Leuven-GroupT
* Date: 2015-05-10
* E-mail: jeroen.vanaken@kuleuven.be
* Description: .c file of virtual sensor node
*******************************************************************************/
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "tcpsocket.h"
#include <signal.h>
// conditional compilation option to control the number of measurements this sensor node wil generate
#if (LOOPS > 1)
#define UPDATE(i) (i--)
#else
#define LOOPS 1
#define UPDATE(i) (void)0 //create infinit loop
#endif
// conditional compilation option to log all sensor data to a text file
#ifdef LOG_SENSOR_DATA
#define LOG_FILE "sensor_log"
#define LOG_OPEN() \
FILE *fp_log; \
do { \
fp_log = fopen(LOG_FILE, "w"); \
if ((fp_log)==NULL) { \
printf("%s\n","couldn't create log file"); \
exit(EXIT_FAILURE); \
} \
} while(0)
#define LOG_PRINTF(sensor_id,temperature,timestamp) \
do { \
fprintf(fp_log, "%" PRIu16 " %g %ld\n", (sensor_id), (temperature), (long int)(timestamp)); \
fflush(fp_log); \
} while(0)
#define LOG_CLOSE() fclose(fp_log);
#else
#define LOG_OPEN(...) (void)0
#define LOG_PRINTF(...) (void)0
#define LOG_CLOSE(...) (void)0
#endif
#define INITIAL_TEMPERATURE 20
#define TEMP_DEV 5 // max afwijking vorige temperatuur in 0.1 celsius
int running = 1;
Socket client;
uint16_t sensor_id;
void print_help(void);
time_t timestamp;
double temperature = INITIAL_TEMPERATURE;
/*
* argv[1] = sensor ID
* argv[2] = sleep time
* argv[3] = server IP
* argv[4] = server port
*/
void my_handler(){
signal(SIGINT,SIG_DFL);
printf("sensor_node %d was closed\n",sensor_id);
running = 0;
double k = 100.001;
tcp_send( client, (void *)&sensor_id, sizeof(sensor_id));
tcp_send( client, &k, sizeof(temperature));
tcp_send( client, (void *)×tamp, sizeof(timestamp));
}
//argv[]
int main( int argc, char *argv[] )
{
long int sleep_time;
int server_port;
char server_ip[] = "000.000.000.000";
LOG_OPEN();
if (argc != 5)
{
print_help();
exit(EXIT_SUCCESS);
}
else
{
// to do: user input validation!
sensor_id = atoi(argv[1]);
sleep_time = atoi(argv[2]);
strncpy(server_ip, argv[3],strlen(server_ip));
server_port = atoi(argv[4]);
//printf("%d %ld %s %d\n", sensor_id, sleep_time, server_ip, server_port);
}
srand48( time(NULL) );
// open TCP connection to the server; server is listening to SERVER_IP and PORT
client = tcp_active_open( server_port, server_ip );
int i=LOOPS;
signal(SIGINT, my_handler);
while(running)
{
temperature = temperature + TEMP_DEV * ((drand48() - 0.5)/10);
time(×tamp);
// send data to server in this order (!!): <sensor_id><temperature><timestamp>
// remark: don't send as a struct!
tcp_send( client, (void *)&sensor_id, sizeof(sensor_id));
tcp_send( client, (void *)&temperature, sizeof(temperature));
tcp_send( client, (void *)×tamp, sizeof(timestamp));
LOG_PRINTF(sensor_id,temperature,timestamp);
sleep(sleep_time);
UPDATE(i);
}
i = 0;
tcp_close( &client );
LOG_CLOSE();
exit(EXIT_SUCCESS);
}
void print_help(void)
{
printf("Use this program with 4 command line options: \n");
printf("\t%-15s : a unique sensor node ID\n", "\'ID\'");
printf("\t%-15s : node sleep time (in sec) between two measurements\n","\'sleep time\'");
printf("\t%-15s : TCP server IP address\n", "\'server IP\'");
printf("\t%-15s : TCP server port number\n", "\'server port\'");
}