-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_debug.c
301 lines (235 loc) · 6.33 KB
/
arduino_debug.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
#include "arduino_funcs.h"
#include "server_helper.h"
// flag to determine if arduino is open or not
// for reading and writing
int is_open; // 0 if not open, 1 if open
/*
This code configures the file descriptor for use as a serial port.
*/
void configure(int fd) {
struct termios pts;
tcgetattr(fd, &pts);
cfsetospeed(&pts, 9600);
cfsetispeed(&pts, 9600);
tcsetattr(fd, TCSANOW, &pts);
}
/**
* function to get Arduino ready for reading
* @param p pointer to filename
*/
int get_started(char* file_name) {
// try to open the file for reading and writing
// you may need to change the flags depending on your platform
int ard_fd = open(file_name, O_RDWR | O_NOCTTY | O_NDELAY);
if (ard_fd < 0) {
printf("Cannot open file\n");
is_open = 0;
return 0;
// perror("Could not open file\n");
// exit(1);
}
else {
printf("Successfully opened %s for reading and writing\n", file_name);
is_open = 1;
}
configure(ard_fd);
return ard_fd;
}
/**
* function to keep on getting the
* current temperature from the Arduino
*
* will also write the temperature to a json file
*
* @param p packet with relevant data
*/
void* get_temps(void* p) {
// char* filename;
// int ard_fd;
// is_open = 0;
dict* d = malloc(sizeof(dict));
printf("In gettemps\n");
// packet* pack = (packet*) p;
// unpack packet
char* filename = "";
int ard_fd = -1;
// char* quit = NULL;
// read through a few times to get
// rid of any potential garbage that's
// being outputted
if (is_open) {
for (int i = 0; i < 10; i++) {
read_data(filename, ard_fd);
}
}
while (1) {
if (is_open) {
sleep(3);
write_to_arduino(ard_fd, 'b', filename);
sleep(3);
// write_to_arduino(ard_fd, 'b', filename);
// sleep(3);
// write_to_arduino(ard_fd, 'g', filename);
// sleep(3);
// write_to_arduino(ard_fd, 'r', filename);
// sleep(3);
// write_to_arduino(ard_fd, 'f', filename);
// sleep(3);
// write_to_arduino(ard_fd, 'l', filename);
// sleep(3);
// write_to_arduino(ard_fd, 's', filename);
// sleep(3);
}
// if connection to Arduino is open
if (is_open) {
char* tempo = read_data(filename, ard_fd);
/**** write temperature to file ****/
// get current time
char* curr_time = get_current_time();
strip_fat(curr_time);
char str_temp[20];
// convert temperature to string
if (tempo != NULL) {
strcpy(str_temp, tempo);
free(tempo);
}
else {
strcpy(str_temp, "OFFLINE");
}
// create key-value pair of the 2
kvp* k = make_pair(curr_time, str_temp);
// add to dictionary
add_to_dict(k, d);
// write dictionary to file
write_to_json("temperatures.json", d);
// free(f);
printf("completed readings\n");
sleep(2);
}
// if connection is not open
else {
/**** send message to file ****/
// get current time
char* curr_time = get_current_time();
strip_fat(curr_time);
// send offline message
char* message = malloc(sizeof(char) * (strlen("OFFLINE") + 1));
strcpy(message, "OFFLINE");
// create key-value pair of the 2
kvp* k = make_pair(curr_time, message);
// add to dictionary
add_to_dict(k, d);
// write to file
write_to_json("temperatures.json", d);
/******************************/
printf("Arduino is not connected. Will try to connect\n");
// try the first port option
int temp = get_started("/dev/ttyACM0");
if (is_open) {
// reset filename and ard_fd
filename = "/dev/ttyACM0";
ard_fd = temp;
// pack->ard_fd = ard_fd;
}
// otherwise, try second
if (!is_open) {
temp = get_started("/dev/ttyACM1");
if (is_open) {
// reset filename and ard_fd
filename = "/dev/ttyACM1";
ard_fd = temp;
// pack->ard_fd = ard_fd;
}
}
if (!is_open) {
sleep(5);
}
}
}
clear_dictionary(d);
pthread_exit(NULL);
}
/**
* funciton for single read of temperature from Arduino
* @param file_name Arduino filename
* @param fd file descriptor
* @return the temperature as a float, without any surrounding text
*/
char* read_data(char* file_name, int fd) {
char* out = malloc(sizeof(char) * 100);
int index = 0; // index to keep track of location in out[]
char buf[100]; // buffer to read in data
int end = 0; // flag to determine if are at end of message
int zero_count = 0;
while (end == 0) {
if (zero_count == 20) {
printf("Arduino has been disconnected\n");
is_open = 0;
return NULL;
}
int bytes_read = read(fd, buf, 100);
if (bytes_read == 0) {
zero_count++;
} else {
zero_count = 0;
}
for (int i = 0; i < bytes_read; i++) {
if (buf[i] == '\n') {
out[index] = '\0';
end = 1;
break;
}
out[index++] = buf[i];
}
}
printf("%s\n", out);
return out;
}
/**
* strip letters from the Arduino message
* @param str the Arduino message
* @return solely the temperature (as a float)
*/
float* strip_letters(char* str) {
float* f = malloc(sizeof(float));
char out[10];
int at_num = 0;
int index = 0;
for (int i = 0; i < strlen(str); i++) {
// anything that's a number or decimal
if ((str[i] >= '0' && str[i] <= '9') || str[i] == '.') {
out[index++] = str[i];
at_num = 1;
if (index == 9) {
break;
}
} else if (at_num != 0) {
break;
}
}
out[index] = '\0'; // null terminate
*f = atof(out);
return f;
}
void write_to_arduino(int fd, char c, char* filename) {
if (!is_open) {
printf("Arduino is not open for writing\n");
return;
}
if (write(fd, &c, sizeof(char)) != sizeof(char)) {
perror("Could not write to Arduino");
exit(1);
}
char* result = read_data(filename, fd);
while (strcmp(result, "r") != 0) {
free(result);
write(fd, &c, sizeof(char));
result = read_data(filename, fd);
}
free(result);
printf("\n\n\twriting %c to Arduino\n", c);
}
int main() {
get_temps(NULL);
return 0;
}