<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.domoticz.com/index.php?action=history&amp;feed=atom&amp;title=Perl_for_Domoticz</id>
	<title>Perl for Domoticz - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.domoticz.com/index.php?action=history&amp;feed=atom&amp;title=Perl_for_Domoticz"/>
	<link rel="alternate" type="text/html" href="https://wiki.domoticz.com/index.php?title=Perl_for_Domoticz&amp;action=history"/>
	<updated>2026-09-18T23:49:06Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.0</generator>
	<entry>
		<id>https://wiki.domoticz.com/index.php?title=Perl_for_Domoticz&amp;diff=14238&amp;oldid=prev</id>
		<title>Rwaaren: /* Accessing device statuses in Domoticz */</title>
		<link rel="alternate" type="text/html" href="https://wiki.domoticz.com/index.php?title=Perl_for_Domoticz&amp;diff=14238&amp;oldid=prev"/>
		<updated>2020-06-12T19:38:16Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Accessing device statuses in Domoticz&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Introduction==&lt;br /&gt;
Perl may not be natively installed in the Domoticz binary, but it can be easily and extensively used with Domoticz if installed separately on your system.&lt;br /&gt;
Please remember that on Linux, any scripts need execution rights (chmod +x) and read rights and that line-endings should be UNIX-friendly (&amp;lt;LF&amp;gt; only). &lt;br /&gt;
Please also remember that any script should specify the correct interpreter path on the very first line of the file (shebang line). For Perl : &lt;br /&gt;
&lt;br /&gt;
 #!/usr/bin/perl&lt;br /&gt;
&lt;br /&gt;
You can run a perl script from the &amp;#039;On Action:&amp;#039; or &amp;#039;Off Action:&amp;#039; parameter of a switch. As an example:&lt;br /&gt;
&lt;br /&gt;
 script://coolscript.pl 0&lt;br /&gt;
&lt;br /&gt;
where &amp;#039;coolscript.pl&amp;#039; is a perl file in the &amp;#039;/home/pi/domoticz/scripts&amp;#039; folder and the &amp;#039;0&amp;#039; (or whatever) is an example parameter that the perl script will access via its @ARGV array.&lt;br /&gt;
&lt;br /&gt;
Be aware, however, that Domoticz will &amp;#039;&amp;#039;&amp;#039;wait&amp;#039;&amp;#039;&amp;#039; for this script to finish executing before it continues. This means that if there are any long loops with e.g. sleep statements in the perl script then your Domoticz event queue will be severely delayed. A simple way to get around this is to wrap the call to the perl script inside a bash script that uses the &amp;#039;&amp;amp;&amp;#039; notation to spawn off a subprocess to run the perl and return immediately. &lt;br /&gt;
&lt;br /&gt;
In your scripts directory, create a file called &amp;#039;exec.sh&amp;#039; with the following content:&lt;br /&gt;
&lt;br /&gt;
 #! /bin/sh&lt;br /&gt;
 /usr/bin/perl /home/pi/domoticz/scripts/$1.pl $2 $3 &amp;gt; /dev/null 2&amp;gt;&amp;amp;1 &amp;amp;&lt;br /&gt;
&lt;br /&gt;
You can now use the following syntax to call up the actual perl script as follows:&lt;br /&gt;
&lt;br /&gt;
 script://exec.sh coolscript 0&lt;br /&gt;
&lt;br /&gt;
This way, control is immediately returned to Domoticz while your perl script runs in the background. It can still interact with Domoticz using JSON calls as per usual, it&amp;#039;s just running quietly.&lt;br /&gt;
&lt;br /&gt;
== Accessing device statuses in Domoticz ==&lt;br /&gt;
The essence of interacting with Domoticz from a perl script is by using the JSON-call possibilities (which are documented here: [[JSON]https://www.domoticz.com/wiki/Domoticz_API/JSON_URL&amp;#039;s]. You can make these calls from perl via e.g. the CPAN modules &amp;#039;LWP::UserAgent&amp;#039; and &amp;#039;JSON::RPC::Client&amp;#039;, while modules such as &amp;#039;JSON::XS&amp;#039; make decoding the returned JSON easy. Here&amp;#039;s an example of switching a light on or off from perl:&lt;br /&gt;
&lt;br /&gt;
 #!/usr/bin/perl&lt;br /&gt;
 &lt;br /&gt;
 no warnings &amp;#039;uninitialized&amp;#039;;&lt;br /&gt;
 use LWP::UserAgent;&lt;br /&gt;
 &lt;br /&gt;
 $url{domo} =&amp;#039;http://192.168.1.12:8080&amp;#039;;&lt;br /&gt;
 &lt;br /&gt;
 $idx =301;&lt;br /&gt;
 $cmd =&amp;#039;On&amp;#039;; # or &amp;#039;Off&amp;#039;&lt;br /&gt;
 &lt;br /&gt;
 $switch_url = $url{domo}.&amp;#039;/json.htm?type=command&amp;amp;param=switchlight&amp;amp;idx=&amp;#039;.$idx.&amp;#039;&amp;amp;switchcmd=&amp;#039;.$cmd;&lt;br /&gt;
 $ua=LWP::UserAgent-&amp;gt;new; $ua-&amp;gt;timeout(5); $res=$ua-&amp;gt;put($switch_url);&lt;br /&gt;
 unless ($res-&amp;gt;is_success) { warn $res-&amp;gt;status_line };&lt;br /&gt;
&lt;br /&gt;
or to retrieve the status of a switch from perl:&lt;br /&gt;
&lt;br /&gt;
 #!/usr/bin/perl&lt;br /&gt;
 &lt;br /&gt;
 no warnings &amp;#039;uninitialized&amp;#039;;&lt;br /&gt;
 use LWP::UserAgent;&lt;br /&gt;
 use JSON::XS;&lt;br /&gt;
 &lt;br /&gt;
 $url{domo} = &amp;#039;http://192.168.1.12:8080&amp;#039;;&lt;br /&gt;
 &lt;br /&gt;
 $idx = 301;&lt;br /&gt;
 &lt;br /&gt;
 $ua = LWP::UserAgent-&amp;gt;new; $ua-&amp;gt;timeout(5);&lt;br /&gt;
 $retrieve = $ua-&amp;gt;get($url{domo}.&amp;#039;/json.htm?type=devices&amp;amp;rid=&amp;#039;.$idx);&lt;br /&gt;
 $res = $retrieve-&amp;gt;decoded_content;&lt;br /&gt;
 if ($retrieve-&amp;gt;is_success) { $jres = JSON::XS-&amp;gt;new-&amp;gt;allow_nonref-&amp;gt;decode($res)  } else { warn $retrieve-&amp;gt;status_line };&lt;br /&gt;
 $state = $$jres{result}[0]-&amp;gt;{Status}; # &amp;#039;On&amp;#039;, &amp;#039;Off&amp;#039;, &amp;#039;Open&amp;#039;, &amp;#039;Closed&amp;#039;, etc&lt;br /&gt;
&lt;br /&gt;
An example of using JSON::RPC::Client is provided here [[LMS] https://www.domoticz.com/wiki/Logitech_Media_Server#Audio_Alerts_via_Squeezebox_Players]&lt;br /&gt;
&lt;br /&gt;
=== Further examples ===&lt;br /&gt;
This script is the basis of interacting with Domoticz, because good practice dictates that you should test current device status before performing an action (no need to change the state to something it&amp;#039;s already at). All the scripts below will rely on this building block.&lt;br /&gt;
&lt;br /&gt;
=== Prerequisite ===&lt;br /&gt;
 sudo apt-get install libjson-perl libdatetime-perl libwww-perl&lt;br /&gt;
&lt;br /&gt;
=== Script example ===&lt;br /&gt;
&lt;br /&gt;
liststatuses.pl&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
 #!/usr/bin/perl&lt;br /&gt;
 use v5.14;&lt;br /&gt;
 use LWP::Simple;                # From CPAN&lt;br /&gt;
 use JSON qw( decode_json );     # From CPAN&lt;br /&gt;
 use Data::Dumper;               # Perl core module&lt;br /&gt;
 use strict;                     # Good practice&lt;br /&gt;
 use warnings;                   # Good practice&lt;br /&gt;
 use utf8;&lt;br /&gt;
 use feature     qw&amp;lt; unicode_strings &amp;gt;;&lt;br /&gt;
 my $IP=&amp;quot;192.168.0.24&amp;quot;;&lt;br /&gt;
 my $PORT=&amp;quot;8080&amp;quot;;   &lt;br /&gt;
 my $trendsurl = &amp;quot;http://$IP:$PORT/json.htm?type=devices&amp;amp;filter=all&amp;amp;used=true&amp;amp;order=Name&amp;quot;; &lt;br /&gt;
 my $json = get( $trendsurl ); &lt;br /&gt;
 die &amp;quot;Could not get $trendsurl!&amp;quot; unless defined $json;   &lt;br /&gt;
 # Decode the entire JSON &lt;br /&gt;
 my $decoded = JSON-&amp;gt;new-&amp;gt;utf8(0)-&amp;gt;decode( $json );&lt;br /&gt;
 # you&amp;#039;ll get this (it&amp;#039;ll print out); comment this when done.&lt;br /&gt;
 #print Dumper $decoded_json;&lt;br /&gt;
 my @results = @{ $decoded-&amp;gt;{&amp;#039;result&amp;#039;} };&lt;br /&gt;
 foreach my $f ( @results ) {&lt;br /&gt;
   if ($f-&amp;gt;{&amp;quot;SwitchType&amp;quot;}) {&lt;br /&gt;
         print $f-&amp;gt;{&amp;quot;idx&amp;quot;} . &amp;quot; &amp;quot; . $f-&amp;gt;{&amp;quot;Name&amp;quot;} . &amp;quot; &amp;quot; . $f-&amp;gt;{&amp;quot;Status&amp;quot;} . &amp;quot;\n&amp;quot;;&lt;br /&gt;
   } elsif ($f-&amp;gt;{&amp;quot;Type&amp;quot;} eq &amp;quot;Group&amp;quot;) {&lt;br /&gt;
        print $f-&amp;gt;{&amp;quot;idx&amp;quot;} . &amp;quot; &amp;quot; . $f-&amp;gt;{&amp;quot;Name&amp;quot;} . &amp;quot; &amp;quot; . $f-&amp;gt;{&amp;quot;Status&amp;quot;} . &amp;quot;\n&amp;quot;;&lt;br /&gt;
   } else {&lt;br /&gt;
         print $f-&amp;gt;{&amp;quot;idx&amp;quot;} . &amp;quot; &amp;quot; . $f-&amp;gt;{&amp;quot;Name&amp;quot;} . &amp;quot; &amp;quot; . $f-&amp;gt;{&amp;quot;Data&amp;quot;} . &amp;quot;\n&amp;quot;;&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ping several machines ==&lt;br /&gt;
&lt;br /&gt;
I started pinging machines from LUA but it quickly appeared that it made shift the clock for time scripts in LUA because of ping timeout wait if a device was absent. Thus I removed the scripts and added a perl script run from crontab that achieves the same and keeps the LUA script time working seamlessly...&lt;br /&gt;
&lt;br /&gt;
Please remember to configure the couple (idx,IP) where idx is the device number of the virtual/dummy light-switch associated.&lt;br /&gt;
&lt;br /&gt;
=== Prerequisite ===&lt;br /&gt;
&lt;br /&gt;
 sudo apt-get install libjson-perl libdatetime-perl libwww-perl&lt;br /&gt;
&lt;br /&gt;
=== Proposed Crontab entry ===&lt;br /&gt;
&lt;br /&gt;
I propose to run it every 2 minutes&lt;br /&gt;
&lt;br /&gt;
 */2 * * * * /home/pi/ping_by_ip.pl 2&amp;gt;&amp;amp;1 &amp;gt;&amp;gt; /dev/null&lt;br /&gt;
&lt;br /&gt;
=== Script example ===&lt;br /&gt;
ping_by_ip.pl&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
 #!/usr/bin/perl&lt;br /&gt;
 use v5.14;&lt;br /&gt;
 use LWP::Simple;                # From CPAN&lt;br /&gt;
 use JSON qw( decode_json );     # From CPAN&lt;br /&gt;
 use Data::Dumper;               # Perl core module&lt;br /&gt;
 use strict;                     # Good practice&lt;br /&gt;
 use warnings;                   # Good practice&lt;br /&gt;
 use utf8;    &lt;br /&gt;
 use feature     qw&amp;lt; unicode_strings &amp;gt;;&lt;br /&gt;
 #Change settings below to your server&lt;br /&gt;
 my $IP=&amp;quot;192.168.0.24&amp;quot;;&lt;br /&gt;
 my $PORT=&amp;quot;8080&amp;quot;;&lt;br /&gt;
 # PUT your devices here such as it shows their idx (swichlight) and IP&lt;br /&gt;
 my %IP=(39=&amp;gt;&amp;#039;192.168.0.23&amp;#039;,&lt;br /&gt;
        40=&amp;gt;&amp;#039;192.168.0.22&amp;#039;,&lt;br /&gt;
        10=&amp;gt;&amp;#039;192.168.0.25&amp;#039;); &lt;br /&gt;
 my $trendsurl = &amp;quot;http://$IP:$PORT/json.htm?type=devices&amp;amp;filter=all&amp;amp;used=true&amp;amp;order=Name&amp;quot;;&lt;br /&gt;
 my $json = get( $trendsurl );&lt;br /&gt;
 die &amp;quot;Could not get $trendsurl!&amp;quot; unless defined $json;&lt;br /&gt;
 # Decode the entire JSON&lt;br /&gt;
 my $decoded = JSON-&amp;gt;new-&amp;gt;utf8(0)-&amp;gt;decode( $json );&lt;br /&gt;
 my @results = @{ $decoded-&amp;gt;{&amp;#039;result&amp;#039;} };&lt;br /&gt;
 my @tab;&lt;br /&gt;
 foreach my $f ( @results ) {&lt;br /&gt;
  if ($f-&amp;gt;{&amp;quot;SwitchType&amp;quot;}) {&lt;br /&gt;
        $tab[$f-&amp;gt;{&amp;quot;idx&amp;quot;}]=$f-&amp;gt;{&amp;quot;Status&amp;quot;};&lt;br /&gt;
  }&lt;br /&gt;
 }       &lt;br /&gt;
 foreach my $k (keys %IP) {&lt;br /&gt;
        my $ip=$IP{$k};&lt;br /&gt;
        my $res=system(&amp;quot;sudo ping $ip -w 3 2&amp;gt;&amp;amp;1 &amp;gt; /dev/null&amp;quot;); &lt;br /&gt;
  #print &amp;quot;--&amp;gt;&amp;quot;.$k.&amp;quot; &amp;quot;.$res.&amp;quot; &amp;quot;.$tab[$k].&amp;quot;\n&amp;quot;;      &lt;br /&gt;
        if (($res==0)&amp;amp;&amp;amp;($tab[$k] eq &amp;#039;Off&amp;#039;)) {&lt;br /&gt;
                print &amp;quot;$k is On\n&amp;quot;;&lt;br /&gt;
                `curl -s &amp;quot;http://192.168.0.24:8080/json.htm?type=command&amp;amp;param=switchlight&amp;amp;idx=$k&amp;amp;switchcmd=On&amp;quot;`; &lt;br /&gt;
        } elsif (($res!=0)&amp;amp;&amp;amp;($tab[$k] eq &amp;#039;On&amp;#039;)) {&lt;br /&gt;
                print &amp;quot;$k is Off\n&amp;quot;;&lt;br /&gt;
                `curl -s &amp;quot;http://192.168.0.24:8080/json.htm?type=command&amp;amp;param=switchlight&amp;amp;idx=$k&amp;amp;switchcmd=Off&amp;quot;`; &lt;br /&gt;
        } else {&lt;br /&gt;
                print &amp;quot;do nothing: $k is &amp;quot;.$tab[$k].&amp;quot;\n&amp;quot;;&lt;br /&gt;
        }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Accessing sqlite3 ==&lt;br /&gt;
&lt;br /&gt;
=== Prerequisite ===&lt;br /&gt;
&lt;br /&gt;
 sudo apt-get install libdbd-sqlite3-perl libdbi-perl sqlite3&lt;br /&gt;
&lt;br /&gt;
=== Script example ===&lt;br /&gt;
&lt;br /&gt;
 use DBI;&lt;br /&gt;
 my $dbh = DBI-&amp;gt;connect(          &lt;br /&gt;
    &amp;quot;dbi:SQLite:dbname=domoticz.db&amp;quot;, &lt;br /&gt;
    &amp;quot;&amp;quot;,                          &lt;br /&gt;
    &amp;quot;&amp;quot;,                          &lt;br /&gt;
    { RaiseError =&amp;gt; 1 },         &lt;br /&gt;
 ) or die $DBI::errstr;&lt;br /&gt;
 my $sth = $dbh-&amp;gt;prepare( &amp;quot;SELECT * FROM DeviceStatus LIMIT 5&amp;quot; );  &lt;br /&gt;
 $sth-&amp;gt;execute();&lt;br /&gt;
      &lt;br /&gt;
 my $row;&lt;br /&gt;
 while($row = $sth-&amp;gt;fetchrow_hashref()) {&lt;br /&gt;
    print &amp;quot;$row-&amp;gt;{Id} $row-&amp;gt;{HardwareId} $row-&amp;gt;{Name}\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
 #$stmt = qq(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)&lt;br /&gt;
 #      VALUES (3, &amp;#039;Teddy&amp;#039;, 23, &amp;#039;Norway&amp;#039;, 20000.00 ));&lt;br /&gt;
 #$rv = $dbh-&amp;gt;do($stmt) or die $DBI::errstr;&lt;br /&gt;
 $sth-&amp;gt;finish();&lt;br /&gt;
 $dbh-&amp;gt;disconnect();&lt;br /&gt;
&lt;br /&gt;
== Pachube / Cosm / Xively ==&lt;br /&gt;
&lt;br /&gt;
Posting data to Xively can be interesting in the IoT (Internet of Things) approach, and maybe you are already doing so for some devices.&lt;br /&gt;
&lt;br /&gt;
Here is a simple way that extracts all sensor values, simple or multiple, makes a bundle and senda it to Xively to create/update Channels of an existing device.&lt;br /&gt;
&lt;br /&gt;
To achieve this we had to remove spaces and reserved chars, but we can post the unit type this way !&lt;br /&gt;
&lt;br /&gt;
=== Prerequisite ===&lt;br /&gt;
&lt;br /&gt;
 sudo apt-get install libjson-perl libfile-slurp-unicode-perl libwww-perl libxml-simple-perl&lt;br /&gt;
&lt;br /&gt;
=== Proposed Crontab entry ===&lt;br /&gt;
&lt;br /&gt;
I propose to run it every 10 minutes&lt;br /&gt;
&lt;br /&gt;
 */10 * * * * /home/pi/cosmodom.pl 2&amp;gt;&amp;amp;1 &amp;gt;&amp;gt; /dev/null&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Script example ===&lt;br /&gt;
&lt;br /&gt;
cosmodom.pl&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
 #!/usr/bin/perl&lt;br /&gt;
 use v5.14;&lt;br /&gt;
 use LWP::Simple;                # From CPAN&lt;br /&gt;
 use JSON ;    # From CPAN&lt;br /&gt;
 use File::Slurp;&lt;br /&gt;
 use LWP::UserAgent;&lt;br /&gt;
 use Crypt::SSLeay;&lt;br /&gt;
 use Data::Dumper;               # Perl core module&lt;br /&gt;
 use strict;                     # Good practice&lt;br /&gt;
 use warnings;                   # Good practice&lt;br /&gt;
 use utf8;&lt;br /&gt;
 use feature     qw&amp;lt; unicode_strings &amp;gt;;&lt;br /&gt;
 my $COSM_API_KEY = &amp;#039;7W0cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5lyS&amp;#039;;&lt;br /&gt;
 my $COSM_FEED = &amp;quot;xxxxxx&amp;quot;;&lt;br /&gt;
 my $IP=&amp;#039;&amp;#039;192.168.0.24&amp;quot;;&lt;br /&gt;
 my $PORT=&amp;quot;8080&amp;quot;;&lt;br /&gt;
 my $feed = { &amp;#039;version&amp;#039; =&amp;gt; &amp;#039;1.0.0&amp;#039;, &amp;#039;datastreams&amp;#039; =&amp;gt; [] }; &lt;br /&gt;
 # Create an HTTP client&lt;br /&gt;
 my $ua = LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
 $ua-&amp;gt;agent(&amp;#039;RaspberryPiDomoticz/1.0 &amp;#039;);&lt;br /&gt;
 my $trendsurl = &amp;quot;http://$IP:$PORT/json.htm?type=devices&amp;amp;filter=all&amp;amp;used=true&amp;amp;order=Name&amp;quot;;&lt;br /&gt;
 my $json = get( $trendsurl );&lt;br /&gt;
 die &amp;quot;Could not get $trendsurl!&amp;quot; unless defined $json;&lt;br /&gt;
 # Decode the entire JSON&lt;br /&gt;
 my $decoded = JSON-&amp;gt;new-&amp;gt;utf8(0)-&amp;gt;decode( $json ); &lt;br /&gt;
 my @results = @{ $decoded-&amp;gt;{&amp;#039;result&amp;#039;} };&lt;br /&gt;
 foreach my $f ( @results ) {&lt;br /&gt;
   if ($f-&amp;gt;{&amp;quot;SwitchType&amp;quot;}) {&lt;br /&gt;
         #print $f-&amp;gt;{&amp;quot;idx&amp;quot;} . &amp;quot; &amp;quot; . $f-&amp;gt;{&amp;quot;Name&amp;quot;} . &amp;quot; &amp;quot; . $f-&amp;gt;{&amp;quot;Status&amp;quot;} . &amp;quot;\n&amp;quot;;&lt;br /&gt;
  } elsif ($f-&amp;gt;{&amp;quot;Type&amp;quot;} eq &amp;quot;Group&amp;quot;) {&lt;br /&gt;
        #print $f-&amp;gt;{&amp;quot;idx&amp;quot;} . &amp;quot; &amp;quot; . $f-&amp;gt;{&amp;quot;Name&amp;quot;} . &amp;quot; &amp;quot; . $f-&amp;gt;{&amp;quot;Status&amp;quot;} . &amp;quot;\n&amp;quot;;&lt;br /&gt;
  } else {&lt;br /&gt;
        my $te=$f-&amp;gt;{&amp;quot;Data&amp;quot;};&lt;br /&gt;
        my $name=$f-&amp;gt;{&amp;quot;Name&amp;quot;};&lt;br /&gt;
        next if $te=~/;/;&lt;br /&gt;
        #next if $f-&amp;gt;{&amp;quot;idx&amp;quot;}==3;&lt;br /&gt;
        my @tab=split(/,/,$te);&lt;br /&gt;
        foreach my $tem (@tab) {&lt;br /&gt;
                my ($temp,$unit)=($tem=~/(\d*.?\d*)(.*)/);&lt;br /&gt;
                $unit=~s/\s*//;&lt;br /&gt;
                $temp=~s/\s*//;&lt;br /&gt;
                my $nam;&lt;br /&gt;
                if ($te=~/,/) { $nam=$name.&amp;#039;_&amp;#039;.$unit; } else {$nam=$name;}&lt;br /&gt;
                $nam=~s/\s/_/;&lt;br /&gt;
                $nam=~s/\//_/;&lt;br /&gt;
                $nam=~s/%/P/;&lt;br /&gt;
                print &amp;quot;$nam/$temp/$unit\n&amp;quot;;&lt;br /&gt;
                push(@{$feed-&amp;gt;{&amp;#039;datastreams&amp;#039;}}, {&amp;#039;id&amp;#039; =&amp;gt; $nam, &amp;#039;current_value&amp;#039; =&amp;gt; scalar($temp), &amp;#039;units&amp;#039; =&amp;gt; $unit});&lt;br /&gt;
        }&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 # Create a HTTP request&lt;br /&gt;
 my $req = HTTP::Request-&amp;gt;new(PUT =&amp;gt; &amp;quot;https://api.xively.com/v2/feeds/$COSM_FEED&amp;quot;);&lt;br /&gt;
 $req-&amp;gt;header(&amp;#039;X-ApiKey&amp;#039; =&amp;gt; $COSM_API_KEY);&lt;br /&gt;
 $req-&amp;gt;content_type(&amp;#039;application/json&amp;#039;);&lt;br /&gt;
 $req-&amp;gt;content(encode_json($feed));&lt;br /&gt;
 # Make the request&lt;br /&gt;
 my $res = $ua-&amp;gt;request($req);&lt;br /&gt;
 unless ($res-&amp;gt;is_success) {&lt;br /&gt;
                print STDERR $res-&amp;gt;status_line, &amp;quot;\n&amp;quot;;&lt;br /&gt;
                print STDERR $res-&amp;gt;content, &amp;quot;\n&amp;quot;;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sen.Se ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Using:&lt;br /&gt;
 SendToSenSe(25.0000,1234);&lt;br /&gt;
 &lt;br /&gt;
GetFromSenSe(1234);&lt;br /&gt;
&lt;br /&gt;
=== Prerequisite ===&lt;br /&gt;
 sudo apt-get install libjson-perl libdatetime-perl libwww-perl&lt;br /&gt;
&lt;br /&gt;
=== script example ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
 use LWP::UserAgent;&lt;br /&gt;
 use JSON;&lt;br /&gt;
 use strict;&lt;br /&gt;
 use warnings;&lt;br /&gt;
 my $ua = LWP::UserAgent-&amp;gt;new;&lt;br /&gt;
 my $SENSE_API_KEY = &amp;#039;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&amp;#039; ;&lt;br /&gt;
 sub SendToSenSe {&lt;br /&gt;
        # Get parameters&lt;br /&gt;
        my $value = $_[0];&lt;br /&gt;
        # Remove the newline....&lt;br /&gt;
        chomp $value;&lt;br /&gt;
        my $feed_id = $_[1];&lt;br /&gt;
        chomp $feed_id;&lt;br /&gt;
        my %datalist = (&amp;#039;feed_id&amp;#039; =&amp;gt;  $feed_id, &amp;#039;value&amp;#039;=&amp;gt;  $value );&lt;br /&gt;
        my $json = encode_json \%datalist;&lt;br /&gt;
        #print $json, &amp;quot;\n&amp;quot;;&lt;br /&gt;
        # Create a request&lt;br /&gt;
        my $req = HTTP::Request-&amp;gt;new(POST =&amp;gt; &amp;quot;http://api.sen.se/events/?sense_key=&amp;quot;.$SENSE_API_KEY);&lt;br /&gt;
        $req-&amp;gt;content_type(&amp;#039;application/json&amp;#039;);&lt;br /&gt;
        $req-&amp;gt;content($json);&lt;br /&gt;
        # Pass request to the user agent and get a response back&lt;br /&gt;
        my $res = $ua-&amp;gt;request($req);&lt;br /&gt;
        # Check the outcome of the response&lt;br /&gt;
        if ($res-&amp;gt;is_success) {&lt;br /&gt;
            return $res-&amp;gt;content, &amp;quot;\n&amp;quot;;&lt;br /&gt;
        }&lt;br /&gt;
        else {&lt;br /&gt;
        return $res-&amp;gt;status_line, &amp;quot;\n&amp;quot;;&lt;br /&gt;
        }&lt;br /&gt;
 }&lt;br /&gt;
 sub GetFromSenSe {&lt;br /&gt;
        my $feed_id = $_[0];&lt;br /&gt;
        # Create the request&lt;br /&gt;
        my $req = HTTP::Request-&amp;gt;new(GET =&amp;gt; &amp;quot;http://api.sen.se/feeds/$feed_id/last_event/?sense_key=&amp;quot;.$SENSE_API_KEY);&lt;br /&gt;
        $req-&amp;gt;content_type(&amp;#039;application/json&amp;#039;);&lt;br /&gt;
        #$req-&amp;gt;content($json);&lt;br /&gt;
        # Pass request to the user agent and get a response back&lt;br /&gt;
        my $res = $ua-&amp;gt;request($req);&lt;br /&gt;
        # Check the outcome of the response&lt;br /&gt;
        if ($res-&amp;gt;is_success) {&lt;br /&gt;
            return $res-&amp;gt;content, &amp;quot;\n&amp;quot;;&lt;br /&gt;
        }&lt;br /&gt;
        else {&lt;br /&gt;
        return $res-&amp;gt;status_line, &amp;quot;\n&amp;quot;;&lt;br /&gt;
        }&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== GPIO access ==&lt;br /&gt;
&lt;br /&gt;
=== Device::BCM2835 ===&lt;br /&gt;
&lt;br /&gt;
This must be done as root. To change to the root user:&lt;br /&gt;
&lt;br /&gt;
 sudo su -&lt;br /&gt;
&lt;br /&gt;
Supports GPIO and SPI interfaces. You must also get and install the bcm2835 library. Details and downloads from  http://www.open.com.au/mikem/bcm2835 You must then get and install the Device::BCM2835 perl library from CPAN  http://search.cpan.org/~mikem/Device-BCM2835-1.0/lib/Device/BCM2835.pm&lt;br /&gt;
&lt;br /&gt;
 use Device::BCM2835;&lt;br /&gt;
 use strict;&lt;br /&gt;
 # call set_debug(1) to do a non-destructive test on non-RPi hardware&lt;br /&gt;
 #Device::BCM2835::set_debug(1);&lt;br /&gt;
 Device::BCM2835::init() &lt;br /&gt;
  || die &amp;quot;Could not init library&amp;quot;;&lt;br /&gt;
 # Blink pin 11:&lt;br /&gt;
 # Set RPi pin 11 to be an output&lt;br /&gt;
 Device::BCM2835::gpio_fsel(&amp;amp;Device::BCM2835::RPI_GPIO_P1_11, &lt;br /&gt;
                            &amp;amp;Device::BCM2835::BCM2835_GPIO_FSEL_OUTP);&lt;br /&gt;
 while (1)&lt;br /&gt;
 {&lt;br /&gt;
    # Turn it on&lt;br /&gt;
    Device::BCM2835::gpio_write(&amp;amp;Device::BCM2835::RPI_GPIO_P1_11, 1);&lt;br /&gt;
    Device::BCM2835::delay(500); # Milliseconds&lt;br /&gt;
    # Turn it off&lt;br /&gt;
    Device::BCM2835::gpio_write(&amp;amp;Device::BCM2835::RPI_GPIO_P1_11, 0);&lt;br /&gt;
    Device::BCM2835::delay(500); # Milliseconds&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wiring a perl script as a watchdog [http://electrorun.blogspot.fr/2013/06/using-gpio-with-raspberry-pi-and-perl.html}&lt;br /&gt;
&lt;br /&gt;
=== HiPi ===&lt;br /&gt;
&lt;br /&gt;
To download it (as root):&lt;br /&gt;
 wget http://raspberry.znix.com/hipifiles/hipi-install&lt;br /&gt;
 perl hipi-install&lt;br /&gt;
&lt;br /&gt;
More details here: &lt;br /&gt;
[http://raspberry.znix.com/]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Reading serial port - Arduino ==&lt;br /&gt;
&lt;br /&gt;
Arduino reports to a ttyUSB* or ttyACM* ata 115200 bauds. The below scripts shown a simple way (tested but work in progress) to read and write to USB port.&lt;br /&gt;
&lt;br /&gt;
=== Prerequisite ===&lt;br /&gt;
&lt;br /&gt;
 sudo apt-get install libdevice-serialport-perl libdatetime-perl&lt;br /&gt;
&lt;br /&gt;
=== Writing to serial port ===&lt;br /&gt;
&lt;br /&gt;
Here is a snippet to write to a USB port:&lt;br /&gt;
&lt;br /&gt;
 my $msg = &amp;quot;$name;$value1;4;13;M\n&amp;quot;;&lt;br /&gt;
 my $co = $ob-&amp;gt;write($msg);&lt;br /&gt;
 warn &amp;quot;write failed\n&amp;quot; unless ($co);&lt;br /&gt;
 print &amp;quot;$date W ($co) : $msg \n&amp;quot;;&lt;br /&gt;
 $ob-&amp;gt;write_drain;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Script (Work in progress) ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
 #!/usr/bin/perl&lt;br /&gt;
 use warnings;&lt;br /&gt;
 use strict;&lt;br /&gt;
 use POSIX qw(strftime);&lt;br /&gt;
 use Device::SerialPort;&lt;br /&gt;
 use IO::Handle;&lt;br /&gt;
 use DateTime;&lt;br /&gt;
 use Scalar::Util qw(looks_like_number);&lt;br /&gt;
 my $ccnt;&lt;br /&gt;
 my $port = &amp;#039;/dev/ttyUSB0&amp;#039;;&lt;br /&gt;
 my $conf = &amp;#039;~/.conf-pasha&amp;#039;;&lt;br /&gt;
 my $ob = Device::SerialPort-&amp;gt;new($port, 1) || die &amp;quot;Can&amp;#039;t open $port: $ +!&amp;quot;; &lt;br /&gt;
 my $STALL_DEFAULT = 10;&lt;br /&gt;
 my $timeout = $STALL_DEFAULT;&lt;br /&gt;
 my $arb  = $ob-&amp;gt;can_arbitrary_baud;&lt;br /&gt;
 my $data = $ob-&amp;gt;databits(8);&lt;br /&gt;
 my $baud = $ob-&amp;gt;baudrate(115200);&lt;br /&gt;
 my $parity = $ob-&amp;gt;parity(&amp;quot;none&amp;quot;);&lt;br /&gt;
 my $hshake = $ob-&amp;gt;handshake(&amp;quot;rts&amp;quot;);&lt;br /&gt;
 my $stop = $ob-&amp;gt;can_stopbits;&lt;br /&gt;
 my $rs = $ob-&amp;gt;is_rs232;&lt;br /&gt;
 my $total = $ob-&amp;gt;can_total_timeout;&lt;br /&gt;
 $ob-&amp;gt;stopbits(1);&lt;br /&gt;
 $ob-&amp;gt;buffers( 4096, 4096 );&lt;br /&gt;
 $ob-&amp;gt;write_settings();&lt;br /&gt;
 my ($count, $string, $name, $value);&lt;br /&gt;
 $ob-&amp;gt;close || warn &amp;quot;close failed&amp;quot;;;&lt;br /&gt;
 $ob = Device::SerialPort-&amp;gt;new($port, 1) || die &amp;quot;Can&amp;#039;t open $port: $ +!&amp;quot;;&lt;br /&gt;
 $ob-&amp;gt;databits(8);&lt;br /&gt;
 $ob-&amp;gt;baudrate(115200);&lt;br /&gt;
 $ob-&amp;gt;parity(&amp;quot;none&amp;quot;);&lt;br /&gt;
 $ob-&amp;gt;stopbits(1);&lt;br /&gt;
 $ob-&amp;gt;buffers( 4096, 4096 );&lt;br /&gt;
 $ob-&amp;gt;write_settings();&lt;br /&gt;
 my $sleep = 5;&lt;br /&gt;
 print &amp;quot;: write_settings() done\n: sleeping $sleep second to let arduino get ready...\n&amp;quot;;&lt;br /&gt;
 sleep $sleep;&lt;br /&gt;
 my @vals;&lt;br /&gt;
 open(FIC,&amp;quot;&amp;gt;&amp;gt;log-gw.txt&amp;quot;)||die $!;&lt;br /&gt;
 print FIC &amp;quot;Starting\n&amp;quot;;&lt;br /&gt;
 FIC-&amp;gt;autoflush(1);&lt;br /&gt;
 while(1) {&lt;br /&gt;
        $ccnt++;&lt;br /&gt;
        $ob-&amp;gt;lastline(&amp;quot;\n&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        ($count, $string) = $ob-&amp;gt;read(255);&lt;br /&gt;
&lt;br /&gt;
        #print &amp;quot;$ccnt $string\n\n&amp;quot;;&lt;br /&gt;
        @vals  = split(&amp;quot;\n&amp;quot;, $string);&lt;br /&gt;
        foreach (@vals) {&lt;br /&gt;
                $_=~ s/\t/\=/;&lt;br /&gt;
                $_=~ s/\r//;&lt;br /&gt;
                $_=~ s/\n//;&lt;br /&gt;
        }&lt;br /&gt;
        sleep(1);&lt;br /&gt;
 }&lt;br /&gt;
 close(FIC);&lt;br /&gt;
 $ob-&amp;gt;write_drain;&lt;br /&gt;
 $ob-&amp;gt;close;&lt;br /&gt;
 undef $ob;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[Category:Perl]]&lt;/div&gt;</summary>
		<author><name>Rwaaren</name></author>
	</entry>
</feed>