Skip to content

Instantly share code, notes, and snippets.

@jtuttas
Last active February 1, 2018 15:01
Show Gist options
  • Save jtuttas/a4b18e48cd77947be477439fc2c83d51 to your computer and use it in GitHub Desktop.
Save jtuttas/a4b18e48cd77947be477439fc2c83d51 to your computer and use it in GitHub Desktop.
. $PSScriptRoot/pi.ps1
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-type", 'application/json')
$key = Get-Content $PSScriptRoot/key
$headers.Add("X-AIO-Key", $key)
$temp = get-Temperature -slave 0
$data = "" | Select-Object -Property "value"
$data.value=$temp.value
Invoke-RestMethod 'https://io.adafruit.com/api/v2/jtuttas/feeds/pi2b/data' -Headers $headers -Method Post -Body (ConvertTo-Json $data)
<#
.SYNOPSIS
Get Temperature
.DESCRIPTION
Get the Temperature of a DS18B20
.EXAMPLE
Get-Temperature -save 0
Get the Temperature in degrees celsius from slave 0 (the first slave)
#>
function Get-Temperature {
[CmdletBinding()]
Param (
# GPIO Pin
[Parameter(Mandatory=$true,
Position=0)]
[int]$slave,
# master
[string]$master="w1_bus_master1"
)
begin {
$slaveAdress=""
[String[]]$slaveAdresses = Get-Content "/sys/bus/w1/devices/$master/w1_master_slaves"
Write-Verbose "SlaveAdresses ($($slaveAdresses.Length)): $($slaveAdresses)"
try {
$slaveAdress=$slaveAdresses.Get($slave)
Write-Verbose "Slave Adresse: $slaveAdress"
$data = Get-Content "/sys/bus/w1/devices/$slaveAdress/w1_slave"
[int]$value=$data[1].substring($data[1].indexof("t=")+2)
$s="" | Select-Object -Property "date","value","adress"
$s.date=Get-Date
$s.value=$value/1000
$s.adress=$slaveAdress
$s
}
catch {
Write-Error "Slave $slave not found!"
$null
}
}
}
<#
.SYNOPSIS
Set PI Pinmode
.DESCRIPTION
Set the Pin Mode at the Raspberry PI
.EXAMPLE
Set-Pinmode -pin 23 -mode OUTPUT
Set GPIO PIN 23 for Output Mode
.EXAMPLE
23,24,25 | Set-Pinmode -mode OUTPUT
Set GPIO Pins 23,24,25 for Output Mode
#>
function Set-Pinmode {
[CmdletBinding()]
Param (
# GPIO Pin
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String]$pin,
# mode
[Parameter(Mandatory=$true,Position=1,ValueFromPipelineByPropertyName=$true)]
[ValidateSet('INPUT','OUTPUT')]
[string]$mode
)
begin {
}
process {
Write-Verbose "Schreibe $pin in /sys/class/gpio/export"
$pin > "/sys/class/gpio/export"
sleep(0.5)
$success=$false
while (-not $success) {
try {
Write-Verbose "Schreibe in /sys/class/gpio/gpio$pin/direction"
if ($mode -eq "INPUT") {
"in" > "/sys/class/gpio/gpio$pin/direction"
}
else {
"out" > "/sys/class/gpio/gpio$pin/direction"
}
$success=$true
}
catch {
Write-Verbose $_
}
sleep(0.5)
}
}
end {
}
}
<#
.SYNOPSIS
Clear PI Pinmode
.DESCRIPTION
Clear the Pin Mode at the Raspberry PI
.EXAMPLE
Clear-Pinmode -pin 23
Clear GPIO PIN 23
.EXAMPLE
23,24,25 | Clear-Pinmode
Clear GPIO Pins 23,24,25
#>
function Clear-Pinmode {
[CmdletBinding()]
Param (
# GPIO Pin
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String]$pin
)
begin {
}
process {
Write-Verbose "Schreibe $pin in /sys/class/gpio/unexport"
$pin > "/sys/class/gpio/unexport"
sleep(0.5)
}
end {
}
}
<#
.SYNOPSIS
Get GPIO Input
.DESCRIPTION
Get the Value of a GPIO pin
.EXAMPLE
Get-Pin -pin 23
Returns the value of GPIO pin 23 (eg. $true for ON and $false for OFF)
.EXAMPLE
23,24,25 | Get-Pin
Returns the value of GPIO pin 23,24,25 (eg. $true for ON and $false for OFF)
#>
function Get-Pin {
[CmdletBinding()]
Param (
# GPIO Pin
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[int]$pin
)
begin {
}
process {
$c=Get-Content -Path "/sys/class/gpio/gpio$pin/value"
if ($c -contains "1") {
return $true
}
else {
return $false
}
}
end {
}
}
<#
.SYNOPSIS
Set GPIO Output
.DESCRIPTION
Set the OUTPUT Value
.EXAMPLE
Set-Pin -pin 23 -value ON
Set GPIO PIN 23 ON
.EXAMPLE
23,24,25 | Set-Pin -VALUE ON
Set GPIO Pins 23,24,25 to ON
#>
function Set-Pin {
[CmdletBinding()]
Param (
# GPIO Pin
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[int]$pin,
# m
[Parameter(Mandatory=$true,Position=1,ValueFromPipelineByPropertyName=$true)]
[ValidateSet('ON','OFF')]
[string]$value
)
begin {
}
process {
if ($value -eq "ON") {
Write-Verbose "Set Pin $pin to ON"
"1" > "/sys/class/gpio/gpio$pin/value"
}
else {
Write-Verbose "Set Pin $pin to OFF"
"0" > "/sys/class/gpio/gpio$pin/value"
}
}
end {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment