#! /usr/bin/perl

# RootBear @ IrcNet
# oh3nwq @ ham radio

# reading 4 temperature sensors
# and sending data as UDP packets

use IO::Socket;
use strict;

#   remember to change the device addresses to your sensors
#   if you have less than 4 sensors, delete the extra lines
my $hit = `cat /sys/devices/w1_bus_master1/28-03168cffffff/w1_slave |grep "t=" | sed s/.*t=//` / 1000;
my $hat = `cat /sys/devices/w1_bus_master1/28-0516a5333333/w1_slave |grep "t=" | sed s/.*t=//` / 1000;
my $hot = `cat /sys/devices/w1_bus_master1/28-03168ceeeeee/w1_slave |grep "t=" | sed s/.*t=//` / 1000;
my $hut = `cat /sys/devices/w1_bus_master1/28-0516a5dddddd/w1_slave |grep "t=" | sed s/.*t=//` / 1000;

#   remember to change the port and address to match your server
my $sock = IO::Socket::INET->new(
    Proto    => 'udp',
    PeerPort => 1000,
    PeerAddr => '10.20.30.40',
) or die "Could not create socket: $!\n";

# printf ("Sensor 1:  %-8.3f°C\n", $hit);
# printf ("Sensor 2:  %-8.3f°C\n", $hat);
# printf ("Sensor 3:  %-8.3f°C\n", $hot);
# printf ("Sensor 4:  %-8.3f°C\n", $hut);

#   if you have less than 4 sensors, delete the extra lines
$sock->send("S1: $hit") or die "Send error: $!\n";
$sock->send("S2: $hat") or die "Send error: $!\n";
$sock->send("S3: $hot") or die "Send error: $!\n";
$sock->send("S4: $hut") or die "Send error: $!\n";
