Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
NWSWeather.h@0:4435c965d95d, 2014-03-16 (annotated)
- Committer:
- WiredHome
- Date:
- Sun Mar 16 15:37:16 2014 +0000
- Revision:
- 0:4435c965d95d
- Child:
- 1:e077d6502c94
Initial release of a NWSWeather class to gather current observation values.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WiredHome | 0:4435c965d95d | 1 | |
WiredHome | 0:4435c965d95d | 2 | #ifndef NWSWEATHER_H |
WiredHome | 0:4435c965d95d | 3 | #define NWSWEATHER_H |
WiredHome | 0:4435c965d95d | 4 | #include "mbed.h" |
WiredHome | 0:4435c965d95d | 5 | #include "HTTPClient.h" |
WiredHome | 0:4435c965d95d | 6 | |
WiredHome | 0:4435c965d95d | 7 | /// Return code interpretation. |
WiredHome | 0:4435c965d95d | 8 | typedef enum |
WiredHome | 0:4435c965d95d | 9 | { |
WiredHome | 0:4435c965d95d | 10 | noerror, ///< no error, or parameter has been updated. |
WiredHome | 0:4435c965d95d | 11 | nomemory, ///< insufficient memory to complete operation. |
WiredHome | 0:4435c965d95d | 12 | noresponse, ///< no response from server. |
WiredHome | 0:4435c965d95d | 13 | badparameter, ///< bad parameter to function (typically out of range value). |
WiredHome | 0:4435c965d95d | 14 | noupdate, ///< no update available. |
WiredHome | 0:4435c965d95d | 15 | noparamfound ///< no parameter found. |
WiredHome | 0:4435c965d95d | 16 | } NWSReturnCode_T; |
WiredHome | 0:4435c965d95d | 17 | |
WiredHome | 0:4435c965d95d | 18 | typedef enum |
WiredHome | 0:4435c965d95d | 19 | { |
WiredHome | 0:4435c965d95d | 20 | isFloat, |
WiredHome | 0:4435c965d95d | 21 | isInt, |
WiredHome | 0:4435c965d95d | 22 | isString |
WiredHome | 0:4435c965d95d | 23 | } TypeOf_T; |
WiredHome | 0:4435c965d95d | 24 | |
WiredHome | 0:4435c965d95d | 25 | typedef union |
WiredHome | 0:4435c965d95d | 26 | { |
WiredHome | 0:4435c965d95d | 27 | float fValue; // ValueType 0 |
WiredHome | 0:4435c965d95d | 28 | int iValue; // ValueType 1 |
WiredHome | 0:4435c965d95d | 29 | char * sValue; // ValueType 2 |
WiredHome | 0:4435c965d95d | 30 | } Value_T; |
WiredHome | 0:4435c965d95d | 31 | |
WiredHome | 0:4435c965d95d | 32 | typedef struct |
WiredHome | 0:4435c965d95d | 33 | { |
WiredHome | 0:4435c965d95d | 34 | char * name; |
WiredHome | 0:4435c965d95d | 35 | TypeOf_T typeis; |
WiredHome | 0:4435c965d95d | 36 | bool updated; |
WiredHome | 0:4435c965d95d | 37 | Value_T value; |
WiredHome | 0:4435c965d95d | 38 | } XMLItem_T; |
WiredHome | 0:4435c965d95d | 39 | |
WiredHome | 0:4435c965d95d | 40 | |
WiredHome | 0:4435c965d95d | 41 | /// NOAA's National Weather Service handler class. |
WiredHome | 0:4435c965d95d | 42 | /// |
WiredHome | 0:4435c965d95d | 43 | /// This class will fetch and parse weather data from an XML feed |
WiredHome | 0:4435c965d95d | 44 | /// using a URL such as "http://www.weather.gov/data/current_obs/KALO.xml". |
WiredHome | 0:4435c965d95d | 45 | /// |
WiredHome | 0:4435c965d95d | 46 | /// Only the essential parameters are gathered, which is generally those |
WiredHome | 0:4435c965d95d | 47 | /// from which you can derive others. It turns out this is also most of the |
WiredHome | 0:4435c965d95d | 48 | /// parameters. |
WiredHome | 0:4435c965d95d | 49 | /// \li observation_time_rfc822 |
WiredHome | 0:4435c965d95d | 50 | /// \li suggested_pickup |
WiredHome | 0:4435c965d95d | 51 | /// \li suggested_pickup_period |
WiredHome | 0:4435c965d95d | 52 | /// \li location |
WiredHome | 0:4435c965d95d | 53 | /// \li weather |
WiredHome | 0:4435c965d95d | 54 | /// \li temp_f |
WiredHome | 0:4435c965d95d | 55 | /// \li temp_c |
WiredHome | 0:4435c965d95d | 56 | /// \li relative_humidity |
WiredHome | 0:4435c965d95d | 57 | /// \li wind_degrees |
WiredHome | 0:4435c965d95d | 58 | /// \li wind_mph |
WiredHome | 0:4435c965d95d | 59 | /// \li pressure_mb |
WiredHome | 0:4435c965d95d | 60 | /// \li pressure_in |
WiredHome | 0:4435c965d95d | 61 | /// \li dewpoint_f |
WiredHome | 0:4435c965d95d | 62 | /// \li dewpoint_c |
WiredHome | 0:4435c965d95d | 63 | /// \li windchill_f |
WiredHome | 0:4435c965d95d | 64 | /// \li windchill_c |
WiredHome | 0:4435c965d95d | 65 | /// \li visibility_mi |
WiredHome | 0:4435c965d95d | 66 | /// \li icon_url_base |
WiredHome | 0:4435c965d95d | 67 | /// \li icon_url_name |
WiredHome | 0:4435c965d95d | 68 | /// \li latitude |
WiredHome | 0:4435c965d95d | 69 | /// \li longitude |
WiredHome | 0:4435c965d95d | 70 | /// |
WiredHome | 0:4435c965d95d | 71 | class NWSWeather |
WiredHome | 0:4435c965d95d | 72 | { |
WiredHome | 0:4435c965d95d | 73 | public: |
WiredHome | 0:4435c965d95d | 74 | |
WiredHome | 0:4435c965d95d | 75 | /// Constructor. |
WiredHome | 0:4435c965d95d | 76 | /// |
WiredHome | 0:4435c965d95d | 77 | /// Create the object to acquire the weather information from NOAA. |
WiredHome | 0:4435c965d95d | 78 | /// The full form of a valid url to acquire the data from is: |
WiredHome | 0:4435c965d95d | 79 | /// "http://www.weather.gov/data/current_obs/SITE.xml", where SITE |
WiredHome | 0:4435c965d95d | 80 | /// is replaced by the proper site location code. |
WiredHome | 0:4435c965d95d | 81 | /// |
WiredHome | 0:4435c965d95d | 82 | /// location codes are available at http://weather.noaa.gov/data/nsd_bbsss.txt, |
WiredHome | 0:4435c965d95d | 83 | /// and you can use the http://forecast.weather.gov/zipcity.php search tool |
WiredHome | 0:4435c965d95d | 84 | /// to search based on either zip code or city, state. |
WiredHome | 0:4435c965d95d | 85 | /// |
WiredHome | 0:4435c965d95d | 86 | /// It is possible to construct the NWSWeather object with an alternate |
WiredHome | 0:4435c965d95d | 87 | /// base url, but this is only useful if the alternate site is xml |
WiredHome | 0:4435c965d95d | 88 | /// compatible. |
WiredHome | 0:4435c965d95d | 89 | /// |
WiredHome | 0:4435c965d95d | 90 | /// @code |
WiredHome | 0:4435c965d95d | 91 | /// NWSWeather wx; |
WiredHome | 0:4435c965d95d | 92 | /// wx.get("KALO"}; |
WiredHome | 0:4435c965d95d | 93 | /// wx.PrintAllWeatherRecords(); |
WiredHome | 0:4435c965d95d | 94 | /// @endcode |
WiredHome | 0:4435c965d95d | 95 | /// |
WiredHome | 0:4435c965d95d | 96 | /// @code |
WiredHome | 0:4435c965d95d | 97 | /// NWSWeather wx("http://some.alternate.site/path/"); |
WiredHome | 0:4435c965d95d | 98 | /// wx.get("KALO"}; |
WiredHome | 0:4435c965d95d | 99 | /// wx.PrintAllWeatherRecords(); |
WiredHome | 0:4435c965d95d | 100 | /// @endcode |
WiredHome | 0:4435c965d95d | 101 | /// |
WiredHome | 0:4435c965d95d | 102 | /// @param baseurl is an optional parameter to set the base url. If this |
WiredHome | 0:4435c965d95d | 103 | /// is not set "http://www.weather.gov/data/current_obs/" is used. |
WiredHome | 0:4435c965d95d | 104 | /// If it is set, that alternate base url is used. |
WiredHome | 0:4435c965d95d | 105 | /// |
WiredHome | 0:4435c965d95d | 106 | NWSWeather(const char * baseurl = "http://www.weather.gov/data/current_obs/"); |
WiredHome | 0:4435c965d95d | 107 | |
WiredHome | 0:4435c965d95d | 108 | /// Destructor. |
WiredHome | 0:4435c965d95d | 109 | ~NWSWeather(); |
WiredHome | 0:4435c965d95d | 110 | |
WiredHome | 0:4435c965d95d | 111 | /// set an alternate base url after construction of the NWSWeather object. |
WiredHome | 0:4435c965d95d | 112 | /// |
WiredHome | 0:4435c965d95d | 113 | /// @param alternateurl is the new url to replace the baseurl. |
WiredHome | 0:4435c965d95d | 114 | /// @param returns success/failure code. @see NWSReturnCode_T. |
WiredHome | 0:4435c965d95d | 115 | /// |
WiredHome | 0:4435c965d95d | 116 | NWSReturnCode_T setAlternateURL(const char * alternateurl); |
WiredHome | 0:4435c965d95d | 117 | |
WiredHome | 0:4435c965d95d | 118 | /// get the weather data from the specified site. |
WiredHome | 0:4435c965d95d | 119 | /// |
WiredHome | 0:4435c965d95d | 120 | /// This does the work to fetch the weather data from the specified site, |
WiredHome | 0:4435c965d95d | 121 | /// using the baseurl established when the NWSWeather object is constructed. |
WiredHome | 0:4435c965d95d | 122 | /// |
WiredHome | 0:4435c965d95d | 123 | /// @param site is the site/location code of the site of interest. |
WiredHome | 0:4435c965d95d | 124 | /// @param responseSize sets the size of a temporarily allocated buffer |
WiredHome | 0:4435c965d95d | 125 | /// into which the data is received. |
WiredHome | 0:4435c965d95d | 126 | /// @param returns success/failure code. @see NWSReturnCode_T. |
WiredHome | 0:4435c965d95d | 127 | /// |
WiredHome | 0:4435c965d95d | 128 | NWSReturnCode_T get(const char * site, int responseSize = 3000); |
WiredHome | 0:4435c965d95d | 129 | |
WiredHome | 0:4435c965d95d | 130 | /// get the count of the number of weather parameters. |
WiredHome | 0:4435c965d95d | 131 | /// |
WiredHome | 0:4435c965d95d | 132 | /// @returns the count of the number of parameters for which |
WiredHome | 0:4435c965d95d | 133 | /// data is expected. |
WiredHome | 0:4435c965d95d | 134 | uint16_t count(void); |
WiredHome | 0:4435c965d95d | 135 | |
WiredHome | 0:4435c965d95d | 136 | /// determines if a specific parameter was updated from the last |
WiredHome | 0:4435c965d95d | 137 | /// acquisition. |
WiredHome | 0:4435c965d95d | 138 | /// |
WiredHome | 0:4435c965d95d | 139 | /// @param i is the item of interest. |
WiredHome | 0:4435c965d95d | 140 | /// @param returns noerror if the parameter is up to date. @see NWSReturnCode_T. |
WiredHome | 0:4435c965d95d | 141 | /// |
WiredHome | 0:4435c965d95d | 142 | NWSReturnCode_T isUpdated(uint16_t i); |
WiredHome | 0:4435c965d95d | 143 | |
WiredHome | 0:4435c965d95d | 144 | /// get one of the weather parameters. |
WiredHome | 0:4435c965d95d | 145 | /// |
WiredHome | 0:4435c965d95d | 146 | /// @code |
WiredHome | 0:4435c965d95d | 147 | /// // Iterate over each of the parameters |
WiredHome | 0:4435c965d95d | 148 | /// for (i=0; i<wx.count(); i++) { |
WiredHome | 0:4435c965d95d | 149 | /// char * name; TypeOf_T type; char * value; |
WiredHome | 0:4435c965d95d | 150 | /// if (wx.getParam(i, name, &type, value)) { |
WiredHome | 0:4435c965d95d | 151 | /// // print the values |
WiredHome | 0:4435c965d95d | 152 | /// } |
WiredHome | 0:4435c965d95d | 153 | /// } |
WiredHome | 0:4435c965d95d | 154 | /// @endcode |
WiredHome | 0:4435c965d95d | 155 | /// |
WiredHome | 0:4435c965d95d | 156 | /// @code |
WiredHome | 0:4435c965d95d | 157 | /// // Get the names of the available parameters. |
WiredHome | 0:4435c965d95d | 158 | /// for (i=0; i<wx.count(); i++) { |
WiredHome | 0:4435c965d95d | 159 | /// char * name; |
WiredHome | 0:4435c965d95d | 160 | /// if (wx.getParam(i, name)) { |
WiredHome | 0:4435c965d95d | 161 | /// // print the names of the parameters |
WiredHome | 0:4435c965d95d | 162 | /// } |
WiredHome | 0:4435c965d95d | 163 | /// } |
WiredHome | 0:4435c965d95d | 164 | /// @endcode |
WiredHome | 0:4435c965d95d | 165 | /// |
WiredHome | 0:4435c965d95d | 166 | /// @param i is index of the parameter of interest. |
WiredHome | 0:4435c965d95d | 167 | /// @param name is a pointer that is set to point to the name of parameter i. |
WiredHome | 0:4435c965d95d | 168 | /// @param value is an optional pointer that is then set to point to the value of parameter i. |
WiredHome | 0:4435c965d95d | 169 | /// @param typeis is an optional pointer that is set to indicate the type of the value. |
WiredHome | 0:4435c965d95d | 170 | /// @param returns success/failure code. @see NWSReturnCode_T. |
WiredHome | 0:4435c965d95d | 171 | /// |
WiredHome | 0:4435c965d95d | 172 | NWSReturnCode_T getParam(uint16_t i, char * name, Value_T *value = NULL, TypeOf_T *typeis = NULL); |
WiredHome | 0:4435c965d95d | 173 | |
WiredHome | 0:4435c965d95d | 174 | /// get one of the weather parameters. |
WiredHome | 0:4435c965d95d | 175 | /// |
WiredHome | 0:4435c965d95d | 176 | /// @code |
WiredHome | 0:4435c965d95d | 177 | /// TypeOf_T type; char * value; |
WiredHome | 0:4435c965d95d | 178 | /// if (wx.getParam("temp_f", &type, value)) { |
WiredHome | 0:4435c965d95d | 179 | /// // print the values |
WiredHome | 0:4435c965d95d | 180 | /// } |
WiredHome | 0:4435c965d95d | 181 | /// @endcode |
WiredHome | 0:4435c965d95d | 182 | /// |
WiredHome | 0:4435c965d95d | 183 | /// @param name is a const pointer to a string naming the parameter of interest. |
WiredHome | 0:4435c965d95d | 184 | /// @param value is a pointer that is set to point to the value of parameter i. |
WiredHome | 0:4435c965d95d | 185 | /// @param typeis is a pointer that is set to indicate the type of the value. |
WiredHome | 0:4435c965d95d | 186 | /// @param returns success/failure code. @see NWSReturnCode_T. |
WiredHome | 0:4435c965d95d | 187 | /// |
WiredHome | 0:4435c965d95d | 188 | NWSReturnCode_T getParam(const char *name, Value_T *value, TypeOf_T *typeis = NULL); |
WiredHome | 0:4435c965d95d | 189 | |
WiredHome | 0:4435c965d95d | 190 | /// Print to stdout the specified weather record. |
WiredHome | 0:4435c965d95d | 191 | /// |
WiredHome | 0:4435c965d95d | 192 | /// Prints the specified record as a "Parmeter = value\r\n" string. |
WiredHome | 0:4435c965d95d | 193 | /// |
WiredHome | 0:4435c965d95d | 194 | /// @param i specifies the record. |
WiredHome | 0:4435c965d95d | 195 | /// |
WiredHome | 0:4435c965d95d | 196 | void PrintWeatherRecord(uint16_t i); |
WiredHome | 0:4435c965d95d | 197 | |
WiredHome | 0:4435c965d95d | 198 | /// Printa all the records to stdout. |
WiredHome | 0:4435c965d95d | 199 | /// |
WiredHome | 0:4435c965d95d | 200 | /// calls PrintWeatherRecord for each parameter. |
WiredHome | 0:4435c965d95d | 201 | /// |
WiredHome | 0:4435c965d95d | 202 | void PrintAllWeatherRecords(void); |
WiredHome | 0:4435c965d95d | 203 | |
WiredHome | 0:4435c965d95d | 204 | private: |
WiredHome | 0:4435c965d95d | 205 | HTTPClient http; |
WiredHome | 0:4435c965d95d | 206 | char * m_baseurl; |
WiredHome | 0:4435c965d95d | 207 | NWSReturnCode_T ParseWeatherRecord(char * p); |
WiredHome | 0:4435c965d95d | 208 | void ParseWeatherXML(char * message); |
WiredHome | 0:4435c965d95d | 209 | void ClearWeatherRecords(void); |
WiredHome | 0:4435c965d95d | 210 | }; |
WiredHome | 0:4435c965d95d | 211 | #endif // NWSWEATHER_H |