B4J Question JkSSH2 get temp

luke2012

Well-Known Member
Licensed User
Longtime User
Hi to All,
is there a command to get the internal Pi temperature ?
I have to display it to the user.

Thanks in advance for the reply.
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Another option is to use
B4X:
/sys/class/thermal/thermal_zone0/temp

Full B4J Non-UI example using the Remote Connection jkSSH2 library - a great library for managing remote!
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
  Private SSH As jkSSH2
  Private hostname As String = "IPADDRESS"
  Private port As Int = 22
  Private username As String = "pi"
  Private password As String = "**********"                      
  Private command As String = "cat /sys/class/thermal/thermal_zone0/temp"
  Private temp As Double
End Sub

Sub AppStart (Args() As String)
    If SSH_Connect Then
        SSH_RunCmd(command)
    End If
    StartMessageLoop   
End Sub

Sub SSH_Connect As Boolean
    Dim Result As Boolean
    Try
'          Log($"Initialize the SSH Connection with Host ${hostname} and Port ${port}"$)
          SSH.Initialize("SSH", hostname, port)
          SSH.authenticateWithPassword(username, password)
          'Set debugoutput to false as debugging not supported by B4J. Debugging is by default using android/utils.
          SSH.DebugOutput = False
'          Log($"SSH Connection at ${DateTime.time(DateTime.now)}"$)
        Result = True
    Catch
          Log($"SSH Connection Error:${LastException.Message}"$)
        Result = False
    End Try
    Return Result
End Sub

Sub SSH_RunCmd(cmd As String)
  Try
    SSH.execCommand(cmd, port)
  Catch
    Log($"Error:${LastException.Message}"$)
  End Try
End Sub

Sub SSH_CmdExecuted (Success As Boolean, Result As List, TaskId As Int)
'    Log($"SSH Command Executed. Result entries ${Result.Size}"$)
      If Result.Size = 1 Then
          temp = NumberFormat(Result.Get(0) * 0.001, 0,2)
        Log($"Raspberry Pi CPU Temperature: ${temp}°C"$)
    End If
End Sub

Output Example:
Raspberry Pi CPU Temperature: 35.4°C
 
Upvote 0

inakigarm

Well-Known Member
Licensed User
Longtime User
Another option is to use
Output with this command (change rwblinn above code to)
B4X:
Private command As String = "cat /sys/class/thermal/thermal_zone0/temp"

B4X:
Sub SSH_CmdExecuted (Success As Boolean, Result As List, TaskId As Int)

      If Result.Size = 1 Then Log($"Raspberry Pi CPU Temperature: ${Result}°C"$)
 
End Sub


Output Example:
Raspberry Pi CPU Temperature: 59.1°C ---> on RPI3
 
Last edited:
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
@inakigarm I tryed your code and it return "[39007]°C".
It could be 39°C. I'm a little surprised to see these value

So I have to do / 1000 and the value is OK!
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…