-
Notifications
You must be signed in to change notification settings - Fork 3
/
histlog2300.c
268 lines (201 loc) · 6.58 KB
/
histlog2300.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
/* open2300 - histlog2300.c
*
* Version 1.11
*
* Control WS2300 weather station
*
* Copyright 2003-2005, Kenneth Lavrsen
* This program is published under the GNU General Public license
*/
#include "rw2300.h"
/********************************************************************
* print_usage prints a short user guide
*
* Input: none
*
* Output: prints to stdout
*
* Returns: exits program
*
********************************************************************/
void print_usage(void)
{
printf("\n");
printf("histlog2300 - Log history data from WS-2300 to file.\n");
printf("Version %s (C)2003-2006 Kenneth Lavrsen.\n", VERSION);
printf("This program is released under the GNU General Public License (GPL)\n\n");
printf("Usage:\n");
printf("histlog2300 log_filename config_filename\n");
exit(0);
}
/********** MAIN PROGRAM ************************************************
*
* This program reads the history records from a WS2300
* weather station at a given record range
* and prints the data to stdout and to a file.
* Just run the program without parameters for usage.
*
* It uses the config file for device name.
* Config file locations - see open2300.conf-dist
*
***********************************************************************/
int main(int argc, char *argv[])
{
WEATHERSTATION ws2300;
FILE *fileptr;
char logline[3000] = "";
char tempstring[1000] = "";
int interval, countdown, no_records;
struct config_type config;
long counter;
char ch;
char datestring[50]; //used to hold the date stamp for the log file
struct timestamp time_last;
time_t time_lastlog, time_lastrecord;
struct tm time_lastlog_tm, time_lastrecord_tm;
int current_record, next_record, lastlog_record, new_records;
double temperature_in;
double temperature_out;
double dewpoint;
double windchill;
double pressure;
double pressure_term;
int humidity_in;
int humidity_out;
double rain;
double windspeed;
double winddir_degrees;
const char *directions[]= {"N","NNE","NE","ENE","E","ESE","SE","SSE",
"S","SSW","SW","WSW","W","WNW","NW","NNW"};
int temp_int1, temp_int2, i;
if (argc < 2 || argc > 3)
{
print_usage();
}
// Get serial port from config file. Use 2nd command line parameter
get_configuration(&config, argv[2]);
// Setup serial port
ws2300 = open_weatherstation(config.serial_device_name);
// Get in-data and select mode.
fileptr = fopen(argv[1], "ab+");
if (fileptr == NULL)
{
fprintf(stderr,"Cannot open file %s\n",argv[1]);
exit(EXIT_FAILURE);
}
fseek(fileptr, 1L, SEEK_END);
counter = 60;
do
{
counter++;
if (fseek(fileptr, -counter, SEEK_END) < 0 )
break;
ch = getc(fileptr);
} while (ch != '\n' && ch != '\r');
if (fscanf(fileptr,"%4d%2d%2d%2d%2d", &temp_int1, &temp_int2,
&time_lastlog_tm.tm_mday, &time_lastlog_tm.tm_hour,
&time_lastlog_tm.tm_min) == 5)
{
time_lastlog_tm.tm_year = temp_int1 - 1900;
time_lastlog_tm.tm_mon = temp_int2 - 1;
time_lastlog_tm.tm_sec = 0;
time_lastlog_tm.tm_isdst = -1;
}
else
{ //if no valid log we set the date to 1 Jan 1990 0:00
time_lastlog_tm.tm_year = 90;
time_lastlog_tm.tm_mon = 0;
time_lastlog_tm.tm_mday = 1;
time_lastlog_tm.tm_hour = 0;
time_lastlog_tm.tm_min = 0;
time_lastlog_tm.tm_sec = 0;
time_lastlog_tm.tm_isdst = -1;
}
time_lastlog = mktime(&time_lastlog_tm);
current_record = read_history_info(ws2300, &interval, &countdown, &time_last,
&no_records);
time_lastrecord_tm.tm_year = time_last.year - 1900;
time_lastrecord_tm.tm_mon = time_last.month - 1;
time_lastrecord_tm.tm_mday = time_last.day;
time_lastrecord_tm.tm_hour = time_last.hour;
time_lastrecord_tm.tm_min = time_last.minute;
time_lastrecord_tm.tm_sec = 0;
time_lastrecord_tm.tm_isdst = -1;
time_lastrecord = mktime(&time_lastrecord_tm);
pressure_term = pressure_correction(ws2300, config.pressure_conv_factor);
new_records = (int)difftime(time_lastrecord,time_lastlog) / (60 * interval);
if (new_records > 0xAF)
new_records = 0xAF;
if (new_records > no_records)
new_records = no_records;
lastlog_record = current_record - new_records;
if (lastlog_record < 0)
lastlog_record = 0xAE + lastlog_record + 1;
time_lastrecord_tm.tm_min -= new_records * interval;
for (i = 1; i <= new_records; i++)
{
next_record = (i + lastlog_record) % 0xAF;
read_history_record(ws2300, next_record, &config,
&temperature_in,
&temperature_out,
&pressure,
&humidity_in,
&humidity_out,
&rain,
&windspeed,
&winddir_degrees,
&dewpoint,
&windchill);
/* READ TEMPERATURE INDOOR */
sprintf(logline,"%.1f ", temperature_in);
/* READ TEMPERATURE OUTDOOR */
sprintf(tempstring,"%.1f ", temperature_out);
strcat(logline, tempstring);
/* CALCULATE DEWPOINT */
sprintf(tempstring,"%.1f ", dewpoint);
strcat(logline, tempstring);
/* READ RELATIVE HUMIDITY INDOOR */
sprintf(tempstring,"%d ", humidity_in);
strcat(logline, tempstring);
/* READ RELATIVE HUMIDITY OUTDOOR */
sprintf(tempstring,"%d ", humidity_out);
strcat(logline, tempstring);
/* READ WIND SPEED AND DIRECTION aND WINDCHILL */
sprintf(tempstring,"%.1f %.1f %s ", windspeed, winddir_degrees,
directions[(int)(winddir_degrees/22.5)]);
strcat(logline, tempstring);
/* READ WINDCHILL */
sprintf(tempstring,"%.1f ", windchill);
strcat(logline, tempstring);
/* READ RAIN 1H */
/* sprintf(logline,"%s%.2f ", logline,
rain_1h(ws2300, config.rain_conv_factor));
*/
/* READ RAIN 24H */
/*
sprintf(logline,"%s%.2f ", logline,
rain_24h(ws2300, config.rain_conv_factor));
*/
/* READ RAIN TOTAL */
sprintf(tempstring,"%.2f ", rain);
strcat(logline, tempstring);
/* READ RELATIVE PRESSURE */
sprintf(tempstring,"%.3f ", pressure + pressure_term);
strcat(logline, tempstring);
/* GET DATE AND TIME FOR LOG FILE, PLACE BEFORE ALL DATA IN LOG LINE */
// printf("time now: %d\n",time(&basictime));
// time_lastrecord_tm.tm_hour=time_last.hour;
time_lastrecord_tm.tm_min += interval;
mktime(&time_lastrecord_tm); //normalize time_lastlog_tm
strftime(datestring, sizeof(datestring), "%s %F %T%z",
localtime(&time_lastrecord_tm));
// Print out
fseek(fileptr, 0L, SEEK_END);
fprintf(fileptr, "%s %s\n", datestring, logline);
fflush(NULL);
}
// Goodbye and Goodnight
close_weatherstation(ws2300);
fclose(fileptr);
return(0);
}