comparison wh1080.h @ 0:9dab44dcb331

Initial commit of Greg's code from http://www.lemis.com/grog/tmp/wh1080.tar.gz
author Daniel O'Connor <darius@dons.net.au>
date Tue, 09 Feb 2010 13:44:25 +1030
parents
children b22a888eb975
comparison
equal deleted inserted replaced
-1:000000000000 0:9dab44dcb331
1 /*
2 * WH-1080 data input utility.
3 *
4 * Greg Lehey, 16 November 2009
5 *
6 * $Id: wh1080.h,v 1.9 2010/02/07 03:41:07 grog Exp $
7 */
8
9 #include <errno.h>
10 #include <stdio.h>
11 #include <math.h>
12 #include <sys/file.h>
13 #include <sys/stat.h>
14 #include <sys/param.h>
15 #include <stdint.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <usb.h>
19 #include <mysql/mysql.h>
20 #include <sys/mman.h>
21 #include <pwd.h>
22
23 /* Station description */
24 #include "wh1080_dev.h"
25
26 /* Program-dependent stuff. */
27 #define WH1080_MAX_RETRIES 5
28 #define WH1080_READ_TIMEOUT 1000
29 #define WH1080_WRITE_TIMEOUT 1000
30
31 #define STAMPSIZE 32 /* size of time stamp to print out */
32 #define DATETEXTLENGTH 48 /* more than maximum size of date text */
33 #define SECSPERHOUR 3600 /* seconds per hour */
34 #define SECSPERDAY 86400 /* and seconds per day */
35
36 /* Conversion factors to archaic units, for Wunderground and friends */
37 #define KM_TO_MILES 0.6213711922
38 #define MM_TO_IN 0.039370078
39 #define C_TO_F(C) (C * 1.8 + 32)
40 #define hPa_TO_inHg 0.0295299801
41 /*
42 * For some reason, Wunderground doesn't adhere to this constant. By
43 * observation, the constant must be something like this.
44 *
45 * 29.87 / 1011.4 = 0.029533320
46 * 29.88 / 1011.7 = 0.029534446
47 * 29.89 / 1012.1 = 0.029532654
48 * 29.90 / 1012.4 = 0.029533781
49 * Average 0.02953553
50 *
51 */
52 #define Wunder_hPa_TO_inHg 0.02953553
53
54
55 /* OK, this one's not so archaic */
56 #define KELVIN(C) (C + 273.15)
57
58 struct usb_dev_handle {
59 int fd;
60
61 struct usb_bus *bus;
62 struct usb_device *device;
63
64 int config;
65 int interface;
66 int altsetting;
67
68 /* Added by RMT so implementations can store other per-open-device data */
69 int *impl_info;
70 };
71
72 /*
73 * Local configuration, to be read from configuration file.
74 * If this changes, also change the table definition in db/mkdb.
75 */
76 struct config
77 {
78 int version; /* format version, for DB */
79 #define CONFIG_VERSION 1 /* current format version */
80 float latitude; /* In ° */
81 float longitude;
82 float elevation; /* metres */
83 float pressure_error; /* difference between actual and real pressure, hPa */
84 int poll_interval; /* time, in seconds, between reading the device */
85 char *station_id; /* this is the one we use for the database */
86 char *address; /* Address to display on web pages */
87 char *wunderground_station_id;
88 char *wunderground_passwd;
89 int wunderground_report_interval; /* how many seconds between reports to Wunderground */
90 char *weatheforyou_station_id;
91 char *weatheforyou_passwd;
92 int weatherforyou_report_interval; /* how many seconds between reports to Weather for you */
93 int website_generation_interval; /* how many seconds between generating PHP header file */
94 char *db_host; /* database on this host */
95 char *db_user; /* user ID on host */
96 char *db_passwd; /* and password */
97 char *db; /* database name */
98 char *db_table; /* table */
99 char *php_header; /* php header file with weather data */
100 char *comparefile; /* file to compare local weather stations */
101 };
102
103 extern struct config config;
104 /* Command line options, defined in db.c */
105 extern int debug; /* -d */
106 extern int update; /* -n turns updating off */
107 extern int recover; /* -r: recover missed archive data (wh1080 only) */
108 extern int verbose; /* -v */
109 extern int once; /* -1 */
110
111 /* MySQL stuff */
112 extern MYSQL *mysql;
113 extern MYSQL_RES *mysql_result;
114 extern MYSQL_ROW mysql_row;
115 extern char mysql_querytext [1024]; /* build up queries here */
116
117
118 /*
119 * Readings in sanitized form. These are effectively the readings from the
120 * station in a format that's easier for us to use.
121 */
122
123 struct readings
124 {
125 int page; /* page from whence the readings came */
126 time_t timestamp; /* time of reading */
127 int last_save_mins; /* last save minutes (?) */
128 float inside_temp; /* inside temperature */
129 int inside_humidity; /* humidity in percent */
130 float inside_dewpoint; /* dewpoint temperature inside */
131 float outside_temp; /* outside temperature */
132 int outside_humidity; /* humidity in percent */
133 float outside_dewpoint; /* dewpoint temperature outside */
134 float pressure; /* absolute pressure, hPa */
135 float pressure_sea_level; /* relative sea level pressure, hPa */
136 float page1_abs_pressure; /* absolute pressure, hPa, from page 1 */
137 float page1_rel_pressure; /* relative pressure, hPa, from page 1 */
138 float wind_speed; /* wind speed in km/h */
139 float wind_gust; /* wind gust speed in km/h */
140 float wind_direction; /* wind direction, as angle */
141 #define INVALID_DIRECTION 1024 /* set if we get invalid readings */
142 char wind_direction_text [4]; /* wind direction, in text */
143 /*
144 * We keep two rain counters. rain is the current rainfall, incremented on
145 * every reading. This is similar to, but not the same as, what the station
146 * does internally. This field is only changed by the wh1080 program.
147 *
148 * The other field, 'previous_rain', is set to 'rain' by the report program
149 * after reporting the rainfall. It is only changed by report. This approach
150 * avoids any kind of locking.
151 */
152 float rain; /* rainfall in mm */
153 float previous_rain; /* previous rainfall in mm */
154 };
155
156 extern struct readings current_readings;
157
158 struct sharefile
159 {
160 #define SHARED_MAGIC 0x539
161 int magic; /* magic number */
162 struct readings current_readings; /* current readings */
163 };
164
165 extern struct sharefile *sharefile; /* file to map */
166 extern int sharefd; /* and descriptor */
167 extern float print_previous_rain;
168
169 /* MySQL stuff, in db.c */
170 extern MYSQL *mysql;
171 extern MYSQL_RES *mysql_result;
172 extern MYSQL_ROW mysql_row;
173 extern char mysql_querytext [1024]; /* build up queries here */
174 extern char *wind_directions []; /* in util.c */
175
176 /* in db.c */
177 void db_login ();
178 int doquery (char *query);
179 int domyquery (char *query, MYSQL_RES **result);
180 char *mycopy (char *);
181 int read_config (int argc, char *argv []);
182
183 void print_readings (struct readings *readings);
184 float dewpoint (float temp, int humidity);
185 void set_dewpoints (struct readings *readings);
186 void set_sea_level_pressure (struct readings *readings);
187 void datetext (time_t, char *, char *);