2 * Weather Underground PWS Connect
4 * Copyright 2015 Andrew Mager
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7 * in compliance with the License. You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13 * for the specific language governing permissions and limitations under the License.
17 // This imports the Java class "DecimalFormat"
18 import java.text.DecimalFormat
21 name: "Weather Underground PWS Connect",
23 author: "Andrew Mager",
24 description: "Connect your SmartSense Temp/Humidity sensor to your Weather Underground Personal Weather Station.",
25 category: "Green Living",
26 iconUrl: "http://i.imgur.com/HU0ANBp.png",
27 iconX2Url: "http://i.imgur.com/HU0ANBp.png",
28 iconX3Url: "http://i.imgur.com/HU0ANBp.png",
34 section("Select a sensor") {
35 input "temp", "capability.temperatureMeasurement", title: "Temperature", required: true
36 input "humidity", "capability.relativeHumidityMeasurement", title: "Humidity", required: true
38 section("Configure your Weather Underground credentials") {
39 input "weatherID", "text", title: "Weather Station ID", required: true
40 input "password", "password", title: "Weather Underground password", required: true
45 log.debug "Installed with settings: ${settings}"
51 log.debug "Updated with settings: ${settings}"
60 Check to see if the sensor is reporting temperature, then run the updateCurrentWeather
63 if (temp.currentTemperature) {
64 runEvery5Minutes(updateCurrentWeather)
70 Updates the Weather Underground Personal Weather Station (PWS) Upload Protocol
71 Reference: http://wiki.wunderground.com/index.php/PWS_-_Upload_Protocol
73 def updateCurrentWeather() {
75 // Logs of the current data from the sensor
76 log.trace "Temp: " + temp.currentTemperature
77 log.trace "Humidity: " + humidity.currentHumidity
78 log.trace "Dew Point: " + calculateDewPoint(temp.currentTemperature, humidity.currentHumidity)
80 // Builds the URL that will be sent to Weather Underground to update your PWS
82 uri: "http://weatherstation.wunderground.com",
83 path: "/weatherstation/updateweatherstation.php",
88 "tempf": temp.currentTemperature,
89 "humidity": humidity.currentHumidity,
90 "dewptf": calculateDewPoint(temp.currentTemperature, humidity.currentHumidity),
91 "action": "updateraw",
92 "softwaretype": "SmartThings"
97 // Make the HTTP request using httpGet()
98 httpGet(params) { resp -> // This is how we define the "return data". Can also use $it.
99 log.debug "response data: ${resp.data}"
102 log.error "something went wrong: $e"
107 // Calculates dewpoint based on temperature and humidity
108 def calculateDewPoint(t, rh) {
109 def dp = 243.04 * ( Math.log(rh / 100) + ( (17.625 * t) / (243.04 + t) ) ) / (17.625 - Math.log(rh / 100) - ( (17.625 * t) / (243.04 + t) ) )
110 // Format the response for Weather Underground
111 return new DecimalFormat("##.##").format(dp)