Update loft.groovy
[smartapps.git] / official / 04-weatherunderground-connect.groovy
1 /**
2  *  Weather Underground PWS Connect
3  *
4  *  Copyright 2015 Andrew Mager
5  *
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:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
14  *
15  */
16
17 // This imports the Java class "DecimalFormat"
18 import java.text.DecimalFormat
19  
20 definition(
21     name: "Weather Underground PWS Connect",
22     namespace: "co.mager",
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",
29     oauth: true)
30
31
32 preferences {
33     section("Select a sensor") {
34         input "temp", "capability.temperatureMeasurement", title: "Temperature", required: true
35         input "humidity", "capability.relativeHumidityMeasurement", title: "Humidity", required: true
36     }
37     section("Configure your Weather Underground credentials") {
38         input "weatherID", "text", title: "Weather Station ID", required: true
39         input "password", "password", title: "Weather Underground password", required: true
40     }
41 }
42
43 def installed() {
44     log.debug "Installed with settings: ${settings}"
45     initialize()
46 }
47
48
49 def updated() {
50     log.debug "Updated with settings: ${settings}"
51     unsubscribe()
52     initialize()
53 }
54
55
56 def initialize() {
57
58     /*
59       Check to see if the sensor is reporting temperature, then run the updateCurrentWeather
60       every 10 minutes
61     */
62     if (temp.currentTemperature) {
63         runEvery5Minutes(updateCurrentWeather)
64     }
65 }
66
67
68 /*
69   Updates the Weather Underground Personal Weather Station (PWS) Upload Protocol
70   Reference: http://wiki.wunderground.com/index.php/PWS_-_Upload_Protocol
71 */
72 def updateCurrentWeather() {
73
74     // Logs of the current data from the sensor
75     log.trace "Temp: " + temp.currentTemperature
76     log.trace "Humidity: " + humidity.currentHumidity
77     log.trace "Dew Point: " + calculateDewPoint(temp.currentTemperature, humidity.currentHumidity)
78
79     // Builds the URL that will be sent to Weather Underground to update your PWS
80     def params = [
81         uri: "http://weatherstation.wunderground.com",
82         path: "/weatherstation/updateweatherstation.php",
83         query: [
84             "ID": weatherID,
85             "PASSWORD": password,
86             "dateutc": "now",
87             "tempf": temp.currentTemperature,
88             "humidity": humidity.currentHumidity,
89             "dewptf": calculateDewPoint(temp.currentTemperature, humidity.currentHumidity),
90             "action": "updateraw",
91             "softwaretype": "SmartThings"
92         ]
93     ]
94     
95     try {
96         // Make the HTTP request using httpGet()
97         httpGet(params) { resp -> // This is how we define the "return data". Can also use $it.
98             log.debug "response data: ${resp.data}"
99         }
100     } catch (e) {
101         log.error "something went wrong: $e"
102     }
103
104 }
105
106 // Calculates dewpoint based on temperature and humidity
107 def calculateDewPoint(t, rh) {
108     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) ) ) 
109     // Format the response for Weather Underground
110     return new DecimalFormat("##.##").format(dp)
111 }
112