-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsensors.c
171 lines (147 loc) · 4.89 KB
/
sensors.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <pthread.h>
#include <errno.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <time.h>
pthread_t thread_askco2;
pthread_t thread_readco2;
pthread_t thread_readpm;
int UART_CO2 = -1, UART_PM = -1;
void msleep(unsigned long ms){
usleep(ms*1000);
}
void *askco2(void * pvoid){
unsigned char askco2[7] = {0x42, 0x4D, 0xE3, 0x00, 0x00, 0x01, 0x72};
while(1){
write(UART_CO2, (void*)askco2, 7);
sleep(1);
}
}
unsigned char read_one(int fd){
unsigned char rx_buffer[16]={0};
while (read(fd, (void*)rx_buffer, 1) < 0){
msleep(10);
}
return rx_buffer[0];
}
int co2 = 0, lastco2 = 0;
unsigned char co2s[12] = {0}, co2state = 0, update = 0;
void *readco2(void * pvoid){
//42 4D 00 08 0A 4D 3C 7F 0A EA 02 9D
while(1){
co2s[co2state++] = read_one(UART_CO2);
if( (co2state == 1) && (co2s[0] != 0x42) ) co2state = 0;
else if( (co2state == 2) && (co2s[1] != 0x4d) ) co2state = 0;
else if(co2state >= 12){
co2 = (co2s[4]<<8) + co2s[5];
co2state = 0;
if(co2 != lastco2){
lastco2 = co2;
update = 1;
}
}
}
}
unsigned char pm[32] = {0}, pmstate = 0;
int pm1_0=0, pm2_5=0, pm10=0;
void *readpm(void * pvoid){
// 42 4D 00 1C 00 09 00 0B 00 0C 00 09 00 0B 00 0C 09 5A 01 E3 00 2B 00 04 00 01 00 00 91 00 02 F3
while(1){
pm[pmstate++] = read_one(UART_PM);
if( (pmstate == 1) && (pm[0] != 0x42) ) pmstate = 0;
else if( (pmstate == 2) && (pm[1] != 0x4d) ) pmstate = 0;
else if(pmstate >= 32){
pm1_0 = (pm[10]<<8) + pm[11];
pm2_5 = (pm[12]<<8) + pm[13];
pm10 = (pm[14]<<8) + pm[15];
pmstate = 0;
update = 1;
}
}
}
void error(char *msg) {
perror(msg);
exit(0);
}
int sockfd_local = -1, sockfd_remote = -1;
struct sockaddr_in serveraddr_local, serveraddr_remote;
void udpconnect(){
struct hostent * server;
sockfd_local = socket(AF_INET, SOCK_DGRAM, 0);
server = (struct hostent *)gethostbyname("127.0.0.1");
bzero((char *) &serveraddr_local, sizeof(serveraddr_local));
serveraddr_local.sin_family = AF_INET;
bcopy((char *)(server->h_addr),
(char *)&serveraddr_local.sin_addr.s_addr, server->h_length);
serveraddr_local.sin_port = htons(20160);
sockfd_remote = socket(AF_INET, SOCK_DGRAM, 0);
server = (struct hostent *)gethostbyname("ypw.io");
bzero((char *) &serveraddr_remote, sizeof(serveraddr_remote));
serveraddr_remote.sin_family = AF_INET;
bcopy((char *)(server->h_addr),
(char *)&serveraddr_remote.sin_addr.s_addr, server->h_length);
serveraddr_remote.sin_port = htons(20160);
}
unsigned long long GetTimeStamp() {
struct timeval tv;
gettimeofday(&tv,NULL);
return (unsigned long long)tv.tv_sec*1000 + tv.tv_usec/1000;
}
char buf[128], buf2[128];
void udpsend(){
sprintf(buf, "PM1.0: %d, PM2.5: %d, PM10: %d, CO2: %d", pm1_0, pm2_5, pm10, co2);
sprintf(buf2, "%llu,%d,%d,%d,%d", GetTimeStamp(), pm1_0, pm2_5, pm10, co2);
sendto(sockfd_local, buf, strlen(buf), 0, (struct sockaddr *)&serveraddr_local, sizeof(serveraddr_local));
sendto(sockfd_remote, buf2, strlen(buf2), 0, (struct sockaddr *)&serveraddr_remote, sizeof(serveraddr_remote));
}
int main() {
UART_CO2 = open("/dev/ttyAMA2", O_RDWR | O_NOCTTY | O_NDELAY);
UART_PM = open("/dev/ttyAMA3", O_RDWR | O_NOCTTY | O_NDELAY);
if ( (UART_CO2 == -1) || (UART_PM == -1) ){
perror("Open Serial Port Error!\n");
return -1;
}
struct termios options_co2, options_pm;
tcgetattr(UART_CO2, &options_co2);
options_co2.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
options_co2.c_iflag = IGNPAR;
options_co2.c_oflag = 0;
options_co2.c_lflag = 0;
options_co2.c_cc[VTIME] = 0;
options_co2.c_cc[VMIN] = 1;
tcflush(UART_CO2, TCIFLUSH);
tcsetattr(UART_CO2, TCSANOW, &options_co2);
tcgetattr(UART_PM, &options_pm);
options_pm.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
options_pm.c_iflag = IGNPAR;
options_pm.c_oflag = 0;
options_pm.c_lflag = 0;
options_pm.c_cc[VTIME] = 0;
options_pm.c_cc[VMIN] = 1;
tcflush(UART_PM, TCIFLUSH);
tcsetattr(UART_PM, TCSANOW, &options_pm);
if(pthread_create(&thread_askco2, NULL, askco2, NULL) < 0)
error("create thread error\n");
if(pthread_create(&thread_readco2, NULL, readco2, NULL) < 0)
error("create thread error\n");
if(pthread_create(&thread_readpm, NULL, readpm, NULL) < 0)
error("create thread error\n");
udpconnect();
while(1){
if(update != 0){
update = 0;
udpsend();
printf("%s\n", buf);
msleep(500);
}
}
return 0;
}