diff --git a/Makefile b/Makefile index 3eaf150..94122ee 100755 --- a/Makefile +++ b/Makefile @@ -3,8 +3,8 @@ all: wh1080_rf -wh1080_rf: wh1080_rf.o bcm2835.o bmp085.o - $(CC) -lm wh1080_rf.o bcm2835.o bmp085.o -o wh1080_rf +wh1080_rf: wh1080_rf.o bcm2835.o bmp085.o wunderground.o + $(CC) -lm wh1080_rf.o bcm2835.o bmp085.o wunderground.o -o wh1080_rf wh1080_rf.o: wh1080_rf.c $(CC) $(CFLAGS) wh1080_rf.c @@ -15,5 +15,8 @@ bmp085.o: bmp085.c $(CC) $(CFLAGS) bmp085.c +wunderground.o: wunderground.c + $(CC) $(CFLAGS) wunderground.c + clean: - rm -f wh1080_rf.o bcm2835.o bmp085.o wh1080_rf \ No newline at end of file + rm -f wh1080_rf.o bcm2835.o bmp085.o wunderground.o wh1080_rf diff --git a/wh1080_rf.c b/wh1080_rf.c index cdeb2e8..905f0d5 100755 --- a/wh1080_rf.c +++ b/wh1080_rf.c @@ -50,6 +50,7 @@ #include "wh1080_rf.h" #include "bcm2835.h" #include "rfm01.h" +#include "wunderground.h" uint16_t bw_scale[6] = {BW_67, BW_134, BW_200, BW_270, BW_340, BW_400}; @@ -178,13 +179,18 @@ return duty; } +// pressure function and value extern int read_bmp085(float altitude); +extern float pressure_hpa; int main(int argc, char *argv[]) { //unsigned char bytes2[] = {0xa1,0x82,0x0a,0x59,0x03,0x06,0x00,0x4e,0x06,0xc8}; //calculate_values(bytes2); //return -1; + + // initialize weather underground module + WUnderground_Init(); uint8_t packet_sig = 0xfa; @@ -433,7 +439,7 @@ read_bmp085(ALTITUDE_M); // read pressure, calculate for the given altitude #endif - calculate_values(bytes); + calculate_values(bytes, pressure_hpa); // Wait for remainder of 47 seconds in standard scheduler until we can expect the next read scheduler_standard(); @@ -470,7 +476,7 @@ char *direction_name[] = {"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"}; -void calculate_values(unsigned char *buf) { +void calculate_values(unsigned char *buf, float pressure_hpa) { unsigned short device_id = ((unsigned short)buf[0] << 4) | (buf[1] >> 4); unsigned short temperature_raw = (((unsigned short)buf[1] & 0x0f) << 8) | buf[2]; @@ -498,6 +504,10 @@ printf("Wind speed: %0.2f m/s, Gust Speed %0.2f m/s, %s\n", wind_avg_ms, wind_gust_ms, direction_str); printf("Wind speed: %0.1f mph, Gust Speed %0.1f mph, %s\n", wind_avg_mph, wind_gust_mph, direction_str); printf("Total rain: %0.1f mm\n", rain); + printf("Pressure (sea level): %0.1f hpa\n", pressure_hpa); + + // submit observation to weather underground + WUnderground_Observation(temperature, humidity, wind_avg_mph, wind_gust_mph, direction_str, rain, pressure_hpa); } /* diff --git a/wh1080_rf.h b/wh1080_rf.h index 17e4980..3371cff 100755 --- a/wh1080_rf.h +++ b/wh1080_rf.h @@ -1,8 +1,8 @@ //#define USE_BMP085 //#define ALTITUDE_M 210.0f -#define ALTITUDE_M 10.0f +#define ALTITUDE_M 6.0f void scheduler_realtime(); void scheduler_standard(); -void calculate_values(unsigned char *buf); +void calculate_values(unsigned char *buf, float pressure_hpa); diff --git a/wunderground.c b/wunderground.c new file mode 100644 index 0000000..0e277fa --- /dev/null +++ b/wunderground.c @@ -0,0 +1,99 @@ +/* Module for uploading observations to Weather Underground + (C) britishideas.com + GPL V2 - see license.txt */ + +#include +#include +#include +#include "wunderground.h" + +// file that contains weather underground station ID (first line) and +// password (second line) +#define CREDENTIALS "wunderground_creds.txt" + +// API url +#define WUURL "http://rtupdate.wunderground.com/weatherstation/updateweatherstation.php" + +// maximum lengths of station id and password +#define STATIONIDLENGTH 50 +#define PASSWORDLENGTH 50 +// maximum command length +#define CMDLENGTH 1024 + +// module variables +static char gStationId[STATIONIDLENGTH]; +static char gPassword[PASSWORDLENGTH]; + +// initializes the module +void WUnderground_Init + ( + void + ) +{ + // fixme - read station id and password from credentials file + strcpy(gStationId, "IHORNSEA3"); + strcpy(gPassword, "LIA80RvnuUY8WpO3peGC"); +} + +// submits an observation +void WUnderground_Observation + ( + double TemperatureC, // temperature in C + double Humidity, // as a percentage + double WindAveMph, + double WindGustMph, + char *WindDirection, // string "N", "NNE", "NE", "ENE", "E", etc. + double TotalRainMm, // total since battery change + double PressureHpa // pressure at sea level + ) +{ + char Cmd[CMDLENGTH]; + char UTCTimestamp[20]; + + // get temperature in F + double TemperatureF = (TemperatureC * 9.0 / 5.0) + 32.0; + + // calculate dew point in F + // see: http://www.gorhamschaffler.com/humidity_formulas.htm + double Es = 6.11 * pow(10.0, 7.5 * TemperatureC / (237.7 + TemperatureC)); + double E = Humidity * Es / 100.0; + double Tdc = (-430.22 + 237.7 * log(E)) / (-log(E) + 19.08); + double DewPointF = (Tdc * 9.0 / 5.0) + 32.0; + + // get wind direction + double WindDegrees = 0; + if (!strcmp(WindDirection, "N")) WindDegrees = 0; + else if (!strcmp(WindDirection, "NNE")) WindDegrees = 22.5; + else if (!strcmp(WindDirection, "NE")) WindDegrees = 45; + else if (!strcmp(WindDirection, "ENE")) WindDegrees = 67.5; + else if (!strcmp(WindDirection, "E")) WindDegrees = 90; + else if (!strcmp(WindDirection, "ESE")) WindDegrees = 112.5; + else if (!strcmp(WindDirection, "SE")) WindDegrees = 135; + else if (!strcmp(WindDirection, "SSE")) WindDegrees = 157.5; + else if (!strcmp(WindDirection, "S")) WindDegrees = 180; + else if (!strcmp(WindDirection, "SSW")) WindDegrees = 202.5; + else if (!strcmp(WindDirection, "SW")) WindDegrees = 225; + else if (!strcmp(WindDirection, "WSW")) WindDegrees = 247.5; + else if (!strcmp(WindDirection, "W")) WindDegrees = 270; + else if (!strcmp(WindDirection, "WNW")) WindDegrees = 292.5; + else if (!strcmp(WindDirection, "NW")) WindDegrees = 315; + else if (!strcmp(WindDirection, "NNW")) WindDegrees = 337.5; + + strcpy(UTCTimestamp, "timestampgoeshere"); + + sprintf(Cmd, "wget -b -a /tmp/wunderground.log -O /tmp/wunderground-result.html \"%s?action=updateraw&ID=%s&PASSWORD=%s&realtime=1&rtfreq=48&dateutc=%s&tempf=%f&humidity=%f&windspeedmph=%f&windgustmph=%f&baromin=%f,&dewptf=%f&winddir=%.1f\"", + WUURL, + gStationId, + gPassword, + UTCTimestamp, + TemperatureF, + Humidity, + WindAveMph, + WindGustMph, + PressureHpa * 0.0295299830714, + DewPointF, + WindDegrees + ); + + printf("%s\n", Cmd); +} diff --git a/wunderground.h b/wunderground.h new file mode 100644 index 0000000..f8971af --- /dev/null +++ b/wunderground.h @@ -0,0 +1,27 @@ +/* Module for uploading observations to Weather Underground + (C) britishideas.com + GPL V2 - see license.txt */ + +#ifndef _WUNDERGROUNDH_ +#define _WUNDERGROUNDH_ + +// initializes the module +extern void WUnderground_Init + ( + void + ); + +// submits an observation +extern void WUnderground_Observation + ( + double TemperatureC, // temperature in C + double Humidity, // as a percentage + double WindAveMph, + double WindGustMph, + char *WindDirection, // string "N", "NNE", "NE", "ENE", "E", etc. + double TotalRainMm, // total since battery change + double PressureHpa // pressure at sea level + ); + +#endif // _WUNDERGROUNDH_ +