diff --git a/wunderground.c b/wunderground.c index c64a3b1..7d754fb 100644 --- a/wunderground.c +++ b/wunderground.c @@ -5,6 +5,9 @@ #include #include #include +#include +#include +#include #include "wunderground.h" // file that contains weather underground station ID (first line) and @@ -20,16 +23,102 @@ // maximum command length #define CMDLENGTH 1024 +// number of samples of data per hour +// = 3600 seconds / 48 seconds +#define SAMPLESPERHOUR 75 + // module variables static char gStationId[STATIONIDLENGTH]; static char gPassword[PASSWORDLENGTH]; +static int CurrentDay = -1; +static double RainatStartofDay = 0.0; + +// keeps track of rain over the last hour +static int CurrentRainSample = -1; +static double LastRainSample = 0.0; +static double RainSamples[SAMPLESPERHOUR]; + +// gets rain in the last hour in mm +static double GetHourlyRainTotal + ( + double TotalRain + ) +{ + int i; + double Total; + + // no samples yet so initialize + if (CurrentRainSample == -1) + { + LastRainSample = TotalRain; + CurrentRainSample = 0; + return 0.0; + } + + // get new rainfail + double NewRain = TotalRain - LastRainSample; + LastRainSample = TotalRain; + + // store sample in circular buffer + RainSamples[CurrentRainSample] = NewRain; + CurrentRainSample++; + if (CurrentRainSample == SAMPLESPERHOUR) CurrentRainSample = 0; + + // get total rain in last hour + Total = 0.0; + for (i = 0; i < SAMPLESPERHOUR; i++) Total += RainSamples[i]; + + return Total; +} + +// gets the total amount of rain for today in mm +static double GetDailyRainTotal + ( + double TotalRain // total rain value from station + ) +{ + // get local time + time_t t = time(NULL); + struct tm *now = localtime(&t); + + // no daily rain information, so start anew + if (CurrentDay == -1) + { + CurrentDay = now->tm_yday; + RainatStartofDay = TotalRain; + return 0.0; + } + + // day has changed, start anew + if (CurrentDay != now->tm_yday) + { + CurrentDay = now->tm_yday; + RainatStartofDay = TotalRain; + return 0.0; + } + + // same day so get total rain since start of day + double RainToday = TotalRain - RainatStartofDay; + + // if negative value then transmitter batteries were changed + if (RainToday < 0.0) + { + RainatStartofDay = TotalRain; + return 0.0; + } + + return RainToday; +} + // initializes the module void WUnderground_Init ( void ) { + int i; + // read in credentials FILE *fp = fopen(CREDENTIALS, "r"); fgets(gStationId, STATIONIDLENGTH, fp); @@ -37,6 +126,9 @@ fgets(gPassword, PASSWORDLENGTH, fp); strtok(gPassword, "\n"); fclose(fp); + + // no rain data + for (i = 0; i < SAMPLESPERHOUR; i++) RainSamples[i] = 0.0; } // submits an observation @@ -52,7 +144,11 @@ ) { char Cmd[CMDLENGTH]; - char UTCTimestamp[20]; + char UTCTimestamp[40]; + + // get timestamp + time_t now = time(NULL); + strftime(UTCTimestamp, 40, "%Y-%m-%d+%H%%3A%M%%3A%S", gmtime(&now)); // get temperature in F double TemperatureF = (TemperatureC * 9.0 / 5.0) + 32.0; @@ -83,9 +179,12 @@ else if (!strcmp(WindDirection, "NW")) WindDegrees = 315; else if (!strcmp(WindDirection, "NNW")) WindDegrees = 337.5; - strcpy(UTCTimestamp, "timestampgoeshere"); + // get amount of rain today in mm + double RainToday = GetDailyRainTotal(TotalRainMm); + // get amount of rain in last hour in mm + double RainLastHour = GetHourlyRainTotal(TotalRainMm); - 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\"", + 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&dailyrainin=%f&rainin=%f\"", WUURL, gStationId, gPassword, @@ -96,8 +195,11 @@ WindGustMph, PressureHpa * 0.0295299830714, DewPointF, - WindDegrees + WindDegrees, + RainToday * 0.0393701, + RainLastHour * 0.0393701 ); printf("%s\n", Cmd); + system(Cmd); }