2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-19 18:56:03 +00:00

50_HP1000.pm: add support to forward data to Weather Underground

git-svn-id: https://svn.fhem.de/fhem/trunk@11435 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
jpawlowski 2016-05-13 19:17:10 +00:00
parent a41fe38681
commit 74ef726a12

View File

@ -68,7 +68,7 @@ sub HP1000_Initialize($) {
$hash->{DefFn} = "HP1000_Define"; $hash->{DefFn} = "HP1000_Define";
$hash->{UndefFn} = "HP1000_Undefine"; $hash->{UndefFn} = "HP1000_Undefine";
$hash->{AttrList} = $readingFnAttributes; $hash->{AttrList} = "wu_push:1,0 wu_id wu_password " . $readingFnAttributes;
} }
################################### ###################################
@ -81,11 +81,11 @@ sub HP1000_Define($$) {
return "Usage: define <name> HP1000 [<ID> <PASSWORD>]" return "Usage: define <name> HP1000 [<ID> <PASSWORD>]"
if ( int(@a) < 2 ); if ( int(@a) < 2 );
my $name = $a[0]; my $name = $a[0];
$hash->{ID} = $a[2] if (defined($a[2])); $hash->{ID} = $a[2] if ( defined( $a[2] ) );
$hash->{PASSWORD} = $a[3] if (defined($a[3])); $hash->{PASSWORD} = $a[3] if ( defined( $a[3] ) );
return "Device already defined: " . $modules{HP1000}{defptr}{NAME} return "Device already defined: " . $modules{HP1000}{defptr}{NAME}
if (defined($modules{HP1000}{defptr})); if ( defined( $modules{HP1000}{defptr} ) );
$hash->{fhem}{infix} = "updateweatherstation"; $hash->{fhem}{infix} = "updateweatherstation";
@ -134,7 +134,7 @@ sub HP1000_CGI() {
# get device name # get device name
$name = $data{FWEXT}{"/updateweatherstation"}{deviceName} $name = $data{FWEXT}{"/updateweatherstation"}{deviceName}
if (defined($data{FWEXT}{"/updateweatherstation"})); if ( defined( $data{FWEXT}{"/updateweatherstation"} ) );
# return error if no such device # return error if no such device
return ( "text/plain; charset=utf-8", return ( "text/plain; charset=utf-8",
@ -151,8 +151,12 @@ sub HP1000_CGI() {
$webArgs->{$p} = $v; $webArgs->{$p} = $v;
} }
return ("text/plain; charset=utf-8", "Insufficient data") return ( "text/plain; charset=utf-8", "Insufficient data" )
if (!defined($webArgs->{softwaretype}) || !defined($webArgs->{dateutc}) || !defined($webArgs->{ID}) || !defined($webArgs->{PASSWORD}) || !defined($webArgs->{action})) if ( !defined( $webArgs->{softwaretype} )
|| !defined( $webArgs->{dateutc} )
|| !defined( $webArgs->{ID} )
|| !defined( $webArgs->{PASSWORD} )
|| !defined( $webArgs->{action} ) );
} }
# no data received # no data received
@ -165,11 +169,22 @@ sub HP1000_CGI() {
$hash->{SWVERSION} = $webArgs->{softwaretype}; $hash->{SWVERSION} = $webArgs->{softwaretype};
$hash->{SYSTEMTIME_UTC} = $webArgs->{dateutc}; $hash->{SYSTEMTIME_UTC} = $webArgs->{dateutc};
if (defined($hash->{ID}) && defined($hash->{PASSWORD}) && ($hash->{ID} ne $webArgs->{ID} || $hash->{PASSWORD} ne $webArgs->{PASSWORD})) { if (
defined( $hash->{ID} )
&& defined( $hash->{PASSWORD} )
&& ( $hash->{ID} ne $webArgs->{ID}
|| $hash->{PASSWORD} ne $webArgs->{PASSWORD} )
)
{
Log3 $name, 4, "HP1000: received data containing wrong credentials:"; Log3 $name, 4, "HP1000: received data containing wrong credentials:";
return ("text/plain; charset=utf-8", "Wrong credentials"); return ( "text/plain; charset=utf-8", "Wrong credentials" );
} else { }
else {
Log3 $name, 5, "HP1000: received data:\n" . Dumper($webArgs); Log3 $name, 5, "HP1000: received data:\n" . Dumper($webArgs);
HP1000_PushWU( $hash, $webArgs )
if AttrVal( $name, "wu_push", 0 ) eq "1";
delete $webArgs->{ID}; delete $webArgs->{ID};
delete $webArgs->{PASSWORD}; delete $webArgs->{PASSWORD};
delete $webArgs->{dateutc}; delete $webArgs->{dateutc};
@ -180,28 +195,29 @@ sub HP1000_CGI() {
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
# write general readings # write general readings
while ( (my $p, my $v) = each %$webArgs ) { while ( ( my $p, my $v ) = each %$webArgs ) {
# ignore those values # ignore those values
next if ($v eq ""); next if ( $v eq "" );
# name translation # name translation
$p = "humidityIndoor" if ($p eq "inhumi"); $p = "humidityIndoor" if ( $p eq "inhumi" );
$p = "temperatureIndoor" if ($p eq "intemp"); $p = "temperatureIndoor" if ( $p eq "intemp" );
$p = "humidity" if ($p eq "outhumi"); $p = "humidity" if ( $p eq "outhumi" );
$p = "temperature" if ($p eq "outtemp"); $p = "temperature" if ( $p eq "outtemp" );
$p = "luminosity" if ($p eq "light"); $p = "luminosity" if ( $p eq "light" );
$p = "pressure" if ($p eq "relbaro"); $p = "pressure" if ( $p eq "relbaro" );
$p = "pressureAbs" if ($p eq "absbaro"); $p = "pressureAbs" if ( $p eq "absbaro" );
$p = "rain" if ($p eq "rainrate"); $p = "rain" if ( $p eq "rainrate" );
$p = "rainDay" if ($p eq "dailyrain"); $p = "rainDay" if ( $p eq "dailyrain" );
$p = "rainWeek" if ($p eq "weeklyrain"); $p = "rainWeek" if ( $p eq "weeklyrain" );
$p = "rainMonth" if ($p eq "monthlyrain"); $p = "rainMonth" if ( $p eq "monthlyrain" );
$p = "rainYear" if ($p eq "yearlyrain"); $p = "rainYear" if ( $p eq "yearlyrain" );
$p = "uv" if ($p eq "UV"); $p = "uv" if ( $p eq "UV" );
$p = "windChill" if ($p eq "windchill"); $p = "windChill" if ( $p eq "windchill" );
$p = "windDir" if ($p eq "winddir"); $p = "windDir" if ( $p eq "winddir" );
$p = "windGust" if ($p eq "windgust"); $p = "windGust" if ( $p eq "windgust" );
$p = "windSpeed" if ($p eq "windspeed"); $p = "windSpeed" if ( $p eq "windspeed" );
readingsBulkUpdate( $hash, $p, $v ); readingsBulkUpdate( $hash, $p, $v );
} }
@ -210,20 +226,25 @@ sub HP1000_CGI() {
# #
# dewpointIndoor # dewpointIndoor
if (defined($webArgs->{intemp}) && defined($webArgs->{inhumi})) { if ( defined( $webArgs->{intemp} ) && defined( $webArgs->{inhumi} ) ) {
my $v = int( dewpoint_dewpoint($webArgs->{intemp}, $webArgs->{inhumi}) + 0.5 ); my $v = int(
dewpoint_dewpoint( $webArgs->{intemp}, $webArgs->{inhumi} ) + 0.5 );
readingsBulkUpdate( $hash, "dewpointIndoor", $v ); readingsBulkUpdate( $hash, "dewpointIndoor", $v );
} }
# humidityAbs # humidityAbs
if (defined($webArgs->{outtemp}) && defined($webArgs->{outhumi})) { if ( defined( $webArgs->{outtemp} ) && defined( $webArgs->{outhumi} ) ) {
my $v = int( dewpoint_absFeuchte($webArgs->{outtemp}, $webArgs->{outhumi}) + 0.5 ); my $v =
int( dewpoint_absFeuchte( $webArgs->{outtemp}, $webArgs->{outhumi} ) +
0.5 );
readingsBulkUpdate( $hash, "humidityAbs", $v ); readingsBulkUpdate( $hash, "humidityAbs", $v );
} }
# humidityIndoorAbs # humidityIndoorAbs
if (defined($webArgs->{intemp}) && defined($webArgs->{inhumi})) { if ( defined( $webArgs->{intemp} ) && defined( $webArgs->{inhumi} ) ) {
my $v = int( dewpoint_absFeuchte($webArgs->{intemp}, $webArgs->{inhumi}) + 0.5 ); my $v =
int( dewpoint_absFeuchte( $webArgs->{intemp}, $webArgs->{inhumi} ) +
0.5 );
readingsBulkUpdate( $hash, "humidityIndoorAbs", $v ); readingsBulkUpdate( $hash, "humidityIndoorAbs", $v );
} }
@ -237,19 +258,27 @@ sub HP1000_CGI() {
# brightness in % ?? # brightness in % ??
# uv_index, uv_risk # uv_index, uv_risk
if (defined($webArgs->{UV})) { if ( defined( $webArgs->{UV} ) ) {
my $wavelength = $webArgs->{UV}; my $wavelength = $webArgs->{UV};
} }
$result = "T: ".$webArgs->{outtemp} if (defined($webArgs->{outtemp})); $result = "T: " . $webArgs->{outtemp} if ( defined( $webArgs->{outtemp} ) );
$result .= " H: ".$webArgs->{outhumi} if (defined($webArgs->{outhumi})); $result .= " H: " . $webArgs->{outhumi}
$result .= " Ti: ".$webArgs->{intemp} if (defined($webArgs->{intemp})); if ( defined( $webArgs->{outhumi} ) );
$result .= " Hi: ".$webArgs->{inhumi} if (defined($webArgs->{inhumi})); $result .= " Ti: " . $webArgs->{intemp}
$result .= " W: ".$webArgs->{windspeed} if (defined($webArgs->{windspeed})); if ( defined( $webArgs->{intemp} ) );
$result .= " R: ".$webArgs->{rainrate} if (defined($webArgs->{rainrate})); $result .= " Hi: " . $webArgs->{inhumi}
$result .= " WD: ".$webArgs->{winddir} if (defined($webArgs->{winddir})); if ( defined( $webArgs->{inhumi} ) );
$result .= " D: ".$webArgs->{dewpoint} if (defined($webArgs->{dewpoint})); $result .= " W: " . $webArgs->{windspeed}
$result .= " P: ".$webArgs->{relbaro} if (defined($webArgs->{relbaro})); if ( defined( $webArgs->{windspeed} ) );
$result .= " R: " . $webArgs->{rainrate}
if ( defined( $webArgs->{rainrate} ) );
$result .= " WD: " . $webArgs->{winddir}
if ( defined( $webArgs->{winddir} ) );
$result .= " D: " . $webArgs->{dewpoint}
if ( defined( $webArgs->{dewpoint} ) );
$result .= " P: " . $webArgs->{relbaro}
if ( defined( $webArgs->{relbaro} ) );
readingsBulkUpdate( $hash, "state", $result ); readingsBulkUpdate( $hash, "state", $result );
readingsEndUpdate( $hash, 1 ); readingsEndUpdate( $hash, 1 );
@ -257,6 +286,159 @@ sub HP1000_CGI() {
return ( "text/plain; charset=utf-8", "success" ); return ( "text/plain; charset=utf-8", "success" );
} }
###################################
sub HP1000_PushWU($$) {
#
# See: http://wiki.wunderground.com/index.php/PWS_-_Upload_Protocol
#
my ( $hash, $webArgs ) = @_;
my $name = $hash->{NAME};
my $timeout = AttrVal( $name, "timeout", 7 );
my $http_noshutdown = AttrVal( $name, "http-noshutdown", "1" );
my $wu_user = AttrVal( $name, "wu_id", "" );
my $wu_pass = AttrVal( $name, "wu_password", "" );
Log3 $name, 5, "HP1000 $name: called function HP1000_PushWU()";
if ( $wu_user eq "" && $wu_pass eq "" ) {
Log3 $name, 4,
"HP1000 $name: missing attributes for Weather Underground transfer: wu_user and wu_password";
my $return = "error: missing attributes wu_user and wu_password";
readingsSingleUpdate( $hash, "wu_state", $return, 1 )
if ( ReadingsVal( $name, "wu_state", "" ) ne $return );
return;
}
$webArgs->{ID} = $wu_user;
$webArgs->{PASSWORD} = $wu_pass;
my $cmd;
while ( my ( $key, $value ) = each %{$webArgs} ) {
$value = urlEncode($value)
if ( $key eq "softwaretype" || $key eq "dateutc" );
if ( $key eq "windspeed" ) {
$key = "windspeedmph";
$value = $value / 1.609344;
}
if ( $key eq "windgust" ) {
$key = "windgustmph";
$value = $value / 1.609344;
}
if ( $key eq "inhumi" ) {
$key = "indoorhumidity";
}
if ( $key eq "intemp" ) {
$key = "indoortempf";
$value = $value * 9 / 5 + 32;
}
if ( $key eq "intempf" ) {
$key = "indoortempf";
}
if ( $key eq "outhumi" ) {
$key = "humidity";
}
if ( $key eq "outtemp" ) {
$key = "tempf";
$value = $value * 9 / 5 + 32;
}
if ( $key eq "outtempf" ) {
$key = "tempf";
}
if ( $key eq "rain" ) {
$key = "rainin";
$value = $value / 25.4;
}
if ( $key eq "dailyrain" ) {
$key = "dailyrainin";
$value = $value / 25.4;
}
if ( $key eq "dewpoint" ) {
$key = "dewptf";
$value = $value * 9 / 5 + 32;
}
if ( $key eq "absbaro" ) {
$key = "baromin";
$value = $value * 100 * 0.000295299830714;
}
if ( $key eq "light" ) {
$key = "solarradiation";
$value = $value;
}
$cmd .= "$key=" . $value . "&";
}
Log3 $name, 4, "HP1000 $name: pushing data to WU: " . $cmd;
HttpUtils_NonblockingGet(
{
url =>
"http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?"
. $cmd,
timeout => $timeout,
noshutdown => $http_noshutdown,
data => undef,
hash => $hash,
callback => \&HP1000_ReturnWU,
}
);
return;
}
###################################
sub HP1000_ReturnWU($$$) {
my ( $param, $err, $data ) = @_;
my $hash = $param->{hash};
my $name = $hash->{NAME};
# device not reachable
if ($err) {
my $return = "error: connection timeout";
Log3 $name, 4, "HP1000 $name: WU HTTP " . $return;
readingsSingleUpdate( $hash, "wu_state", $return, 1 )
if ( ReadingsVal( $name, "wu_state", "" ) ne $return );
}
# data received
elsif ($data) {
my $logprio = 5;
my $return = "ok";
if ( $data !~ m/^success.*/ ) {
$logprio = 4;
$return = "error";
$return .= " " . $param->{code} if ( $param->{code} ne "200" );
$return .= ": $data";
}
Log3 $name, $logprio,
"HP1000 $name: WU HTTP return: " . $param->{code} . " - $data";
readingsSingleUpdate( $hash, "wu_state", $return, 1 )
if ( ReadingsVal( $name, "wu_state", "" ) ne $return );
}
return;
}
1; 1;
=pod =pod
@ -274,7 +456,7 @@ sub HP1000_CGI() {
<div> <div>
<code>define &lt;WeatherStation&gt; HP1000 [&lt;ID&gt; &lt;PASSWORD&gt;]</code><br> <code>define &lt;WeatherStation&gt; HP1000 [&lt;ID&gt; &lt;PASSWORD&gt;]</code><br>
<br> <br>
Provides webhook receiver for weather station HP1000 and WH2600 of Fine Offset Electronics.<br> Provides webhook receiver for weather station HP1000 and WH2600 of Fine Offset Electronics (e.g. also known as Ambient Weather WS-1001-WIFI).<br>
There needs to be a dedicated FHEMWEB instance with attribute webname set to "weatherstation".<br> There needs to be a dedicated FHEMWEB instance with attribute webname set to "weatherstation".<br>
No other name will work as it's hardcoded in the HP1000/WH2600 device itself!<br> No other name will work as it's hardcoded in the HP1000/WH2600 device itself!<br>
<br> <br>
@ -289,9 +471,30 @@ sub HP1000_CGI() {
# to send this ID and PASSWORD for data to be accepted<br> # to send this ID and PASSWORD for data to be accepted<br>
define WeatherStation HP1000 MyHouse SecretPassword</code> define WeatherStation HP1000 MyHouse SecretPassword</code>
</div><br> </div><br>
IMPORTANT: In your HP1000/WH2600 device, make sure you use a DNS name as most revisions cannot handle IP addresses correctly.<br> IMPORTANT: In your HP1000/WH2600 hardware device, make sure you use a DNS name as most revisions cannot handle IP addresses correctly.<br>
</div><br> </div><br>
</div> </div>
<br>
<a name="HP1000Attr" id="HP10000Attr"></a> <b>Attributes</b>
<div>
<ul>
<a name="wu_id"></a>
<li>wu_id<br>
Weather Underground (Wunderground) station ID
</li>
<br>
<a name="wu_password"></a>
<li>wu_password<br>
Weather Underground (Wunderground) password
</li>
<br>
<a name="wu_push"></a>
<li>wu_push<br>
Enable or disable to push data forward to Weather Underground (defaults to 0=no)
</li><br>
</ul>
</div>
=end html =end html
@ -308,9 +511,9 @@ sub HP1000_CGI() {
<div> <div>
<code>define &lt;WeatherStation&gt; HP1000 [&lt;ID&gt; &lt;PASSWORD&gt;]</code><br> <code>define &lt;WeatherStation&gt; HP1000 [&lt;ID&gt; &lt;PASSWORD&gt;]</code><br>
<br> <br>
Stellt einen Webhook f&uuml;r die HP1000 oder WH2600 Wetterstation von Fine Offset Electronics bereit.<br> Stellt einen Webhook f&uuml;r die HP1000 oder WH2600 Wetterstation von Fine Offset Electronics bereit (z.B. auch bekannt als Ambient Weather WS-1001-WIFI).<br>
Es muss noch eine dedizierte FHEMWEB Instanz angelegt werden, wo das Attribut webname auf "weatherstation" gesetzt wurde.<br> Es muss noch eine dedizierte FHEMWEB Instanz angelegt werden, wo das Attribut webname auf "weatherstation" gesetzt wurde.<br>
Kein anderer Name funktioniert, da dieser hard im HP1000/WH2600 Ger%auml;t hinterlegt ist!<br> Kein anderer Name funktioniert, da dieser hard im HP1000/WH2600 Ger&auml;t hinterlegt ist!<br>
<br> <br>
Da die URI ebenfalls fest kodiert ist, kann mit einer einzelnen FHEM Installation maximal eine HP1000/WH2600 Station gleichzeitig verwendet werden.<br> Da die URI ebenfalls fest kodiert ist, kann mit einer einzelnen FHEM Installation maximal eine HP1000/WH2600 Station gleichzeitig verwendet werden.<br>
<br> <br>
@ -323,10 +526,30 @@ sub HP1000_CGI() {
# diese ID und PASSWORD sendet, damit Daten akzeptiert werden<br> # diese ID und PASSWORD sendet, damit Daten akzeptiert werden<br>
define WeatherStation HP1000 MyHouse SecretPassword</code> define WeatherStation HP1000 MyHouse SecretPassword</code>
</div><br> </div><br>
WICHTIG: Im HP1000/WH2600 Ger&auml; muss sichergestellt sein, dass ein DNS Name statt einer IP Adresse verwendet wird, da einige Revisionen damit nicht umgehen k&ouml;nnen.<br> WICHTIG: Im HP1000/WH2600 Ger&auml;t selbst muss sichergestellt sein, dass ein DNS Name statt einer IP Adresse verwendet wird, da einige Revisionen damit nicht umgehen k&ouml;nnen.<br>
</div><br> </div><br>
</div> </div>
<a name="HP1000Attr" id="HP10000Attr"></a> <b>Attributes</b>
<div>
<ul>
<a name="wu_id"></a>
<li>wu_id<br>
Weather Underground (Wunderground) Stations ID
</li>
<br>
<a name="wu_password"></a>
<li>wu_password<br>
Weather Underground (Wunderground) Passwort
</li>
<br>
<a name="wu_push"></a>
<li>wu_push<br>
Pushen der Daten zu Weather Underground aktivieren oder deaktivieren (Standard ist 0=aus)
</li><br>
</ul>
</div>
=end html_DE =end html_DE
=cut =cut