A few years ago my company was having issues with the air conditioning failing in our server room. We were dependent on our facilities department to inform us when this happened. I just happened to have a Raspberry Pi sitting on my desk, so I decided to use it to solve this problem. We had just started using Graylog as our log consolidation tool. So the project started there.

I chose TI’s TMP102 sensor because it was very cheap. I had originally hacked together a small python script. The script used another blog post for instructions on wiring and also had a code snippet for getting the temperature. I believe this was the blog post I used. I then used a GELF (the protocol used by Graylog to send logs) library to send the log data. After the script part of the project was finished, I added the temp to one of our dashboards in Graylog, then created alerts from there. I will write a post about using Graylog with PowerShell scripts later.

Fast forward a few years, the PowerShell team first released PowerShell 6 for Raspbian, then released the Microsoft PowerShell IOT module. When I saw this, I was itching to remove the only Python job/script running in our environment. Thus, the PowerShell module for the TMP102 sensor was created.

Prerequisites

Getting Started

  1. Image Rasbian Stretch
  2. Install PowerShell 6
  3. Install PowerShell IOT
  4. Install PowerShell TMP102 Module
  5. Install PowerShell PSGELF Module ( or ‘Install-Module PSGELF’)
  6. Wire the sensor. I used this blog post to wire and test the sensor.
  7. Download the example script here
  8. Schedule the script to be ran with cron every X number of minutes.

Example Script

$ErrorActionPreference = "Stop"
$HostName = "tempsensor.yourdomain.com"

Try {
    Import-Module PowerShell.IoT.TMP102
    Import-Module PSGELF

    $temp = Get-TMP102Temp
    Send-PSGelfUDP -GelfServer graylog.yourdomain.com `
        -Port 12201 `
        -HostName $HostName `
        -ShortMessage "The temp in Farenheit is $($temp.Fahrenheit)" `
        -AdditionalField @{temperature = $temp.Fahrenheit}
} Catch {
    #Send an email, if there are any other errors in the script.
    Send-MailMessage `
        -From "sysadmin@yourdomain.com" `
        -To 'jmcgee@yourdomain.com' `
        -Subject "An error occured with a Temp Sensor $HostName" `
        -BodyAsHtml "<p>There was an issue with the tempsensor $HostName.<br /> $($_) </p>" `
        -SmtpServer smtp.yourdomain.com
        $_

}

Function Output

screenshot

Raspberry Pi Pictures

pi1

pi2

graylogsnap