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
|
/*
* An unnamed project – client module
*
* Copyright (C) 2017 Gediminas Jakutis
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <poll.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <ncurses.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
static void init(const unsigned int port, int *sock);
static int getpacket(char *data, size_t buffsize, int *sock, struct sockaddr_in *sender);
static void draw_idle(void);
static void draw_busy(const char * const data);
static float digest_mpu_temp(const short int rawdata);
static float digest_thermistor_voltage(const short int rawdata);
int main(void)
{
static const char servermagic[] = "I love coffee!";
static const char clientmagic[] = "I love tea!";
struct sockaddr_in srvaddr = {0};
char buff[9001] = {0};
int sock;
int pstatus;
struct timespec lastreply;
struct timespec now;
init(2191, &sock);
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
lastreply = now;
lastreply.tv_sec -= 5;
do {
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
pstatus = getpacket(buff, sizeof(buff), &sock, &srvaddr);
switch (pstatus) {
case 0:
if ((now.tv_sec - lastreply.tv_sec) >= 5) {
draw_idle();
}
break;
case 1:
lastreply = now;
if (buff[0] == 'I') {
if (!(strcmp(buff, servermagic))) {
sendto(sock, clientmagic, sizeof(clientmagic),
MSG_DONTWAIT, (struct sockaddr *) &srvaddr, sizeof(srvaddr));
}
draw_idle();
continue;
}
draw_busy(buff);
break;
default:
; /* noop */
}
} while (getch() != 'q' && pstatus >= 0);
close(sock);
endwin();
return 0;
}
static void init(const unsigned int port, int *sock)
{
struct sockaddr_in bindaddress = {0};
bindaddress.sin_family = AF_INET;
bindaddress.sin_port = htons(port);
*sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
bind(*sock, (struct sockaddr*) &bindaddress, sizeof(bindaddress));
initscr();
nodelay(stdscr, 1);
noecho();
}
static int getpacket(char *data, size_t buffsize, int *sock, struct sockaddr_in *sender)
{
static struct pollfd pfd = {0};
int ret;
socklen_t sender_len = sizeof(*sender);
if (!pfd.fd) {
pfd.fd = *sock;
pfd.events = POLLIN;
pfd.revents = 0;
}
ret = poll(&pfd, 1, 25);
if (ret <= 0) {
return ret;
}
if (pfd.revents & POLLIN) {
recvfrom(*sock, data, buffsize, MSG_DONTWAIT, (struct sockaddr *) sender, &sender_len);
ret = 1;
} else {
ret = 0;
}
return ret;
}
static void draw_idle(void)
{
move(0, 0);
clrtoeol();
printw("sequence: [waiting for ESP8266...]");
move(1, 0);
clrtoeol();
mvprintw(1, 0, "mpu temp: [waiting for ESP8266...]");
move(2, 0);
clrtoeol();
mvprintw(2, 0, "thermistor voltage: [waiting for ESP8266...]");
move(3, 0);
clrtoeol();
mvprintw(3, 0, "press 'q' to exit");
refresh();
}
static void draw_busy(const char * const data)
{
unsigned int sequence;
short int mpu_data;
short int thermistor_data;
float mpu_temp;
float thermistor_voltage;
memcpy(&sequence, data + 2, sizeof(sequence));
memcpy(&mpu_data, data + 6, sizeof(mpu_data));
memcpy(&thermistor_data, data + 8, sizeof(thermistor_data));
mpu_temp = digest_mpu_temp(mpu_data);
thermistor_voltage = digest_thermistor_voltage(thermistor_data);
move(0, 0);
clrtoeol();
printw("sequence: %u", sequence);
move(1, 0);
clrtoeol();
mvprintw(1, 0, "mpu temp: %.3fC", mpu_temp);
move(2, 0);
clrtoeol();
mvprintw(2, 0, "thermistor voltage: %.3fV", thermistor_voltage);
move(3, 0);
clrtoeol();
mvprintw(3, 0, "press 'q' to exit");
refresh();
}
static float digest_mpu_temp(const short int rawdata)
{
float ret;
ret = rawdata / 340.0f + 36.53f;
return ret;
}
static float digest_thermistor_voltage(const short int rawdata)
{
float ret;
ret = rawdata * 0.003222656f;
return ret;
}
|