### CPU Temperature

Measure the Raspberry Pi CPU Temperature by running the Shell Command "vcgencmd measure_temp".
App Type: B4J Non-UI (Console)
Parts Needed
• 1x Raspberry Pi
Description
Measure the Raspberry Pi CPU Temperature by running the Shell Command "vcgencmd measure_temp".
Additional Libraries
jShell
B4J Non-UI Example Code
`Sub Process_Globals
' NOT USED
End Sub`
`Sub AppStart (Args() As String)
Log($"Application Started"$)
GetCPUTemperature
'Do not forget to start the message loop
StartMessageLoop
End Sub`
' Handle shell process completed.
' StdOut example: temp=39.2'C which is converted to double 39.2
`Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success And ExitCode = 0 Then
' Log($"Shell command successfully executed. Output:${StdOut}"$)
Log("RPi CPU Temperature =" & ConvertCPUTemperature(StdOut))
Else
Log("Shell command error:")
Log(StdErr)
End If
ExitApplication
End Sub`
' Get the RPi CPU Temperature by running command vcgencmd with parameter measure_temp
`Sub GetCPUTemperature
Dim shl As Shell
Dim params As List
params.Initialize
params.Add("measure_temp")
shl.Initialize("shl", "vcgencmd", params)
shl.Run(-1)
End Sub`
' Convert the CPU Temperature String to a double.
' Example: convert temp=39.2'C to 39.2
`Sub ConvertCPUTemperature(str As String) As Double
Dim Result As Double = -1
Dim pattern As String
'one or more digits plus the dot and the decimal
pattern = "\d+\.\d+"
Dim Matcher1 As Matcher
Matcher1 = Regex.Matcher(pattern, str)
Do While Matcher1.Find
Result = Matcher1.Match
Loop
Return Result
End Sub`

Changed: 2018-04-14 18:46, Category: Raspberry Pi Sample Projects, File: T6C238M1736.htm, ID: 1736