B4J Code Snippet Raspberry Pi run Bash Script

Discussion in 'B4J Code Snippets' started by rwblinn, May 3, 2016.

  1. rwblinn

    rwblinn Well-Known Member Licensed User

    An example of running a Bash Script on a Raspberry Pi using the jShell Library.
    The Bash Script runs several commands which create output files. The content of these files are send to Home Automation solution.

    #Bash Script rpiinfo.sh
    Code:
    #/bin/bash
    #Note:
    #do not forget to x after change: sudo chmod a+x rpiinfo.sh. Then run ./rpiinfo.sh (to test prior using jShell Library)

    #Get CPU Temperature - divide by 1000 in the B4J application
    cat /sys/class/thermal/thermal_zone0/temp > rpicputemp.inf

    #Get Disc space used
    df -H | grep 
    'root' | awk '{ print $5 }' > rpidiscspaceused.inf

    #Get RAM used
    free -o -h | head -n2 | tail -n1 | awk 
    '{print $3}' | tr -d [=M=] > rpimemused.inf

    #Get RAM free
    free -o -h | head -n2 | tail -n1 | awk 
    '{print $4}' | tr -d [=M=] > rpimemfree.inf
    #B4J Code Snippet
    Code:
    'Get Raspberry Pi Information via Bash Script.
    'The Bash Scripts creates several information files (text format, extension .inf)
    Sub GetRasPiInfo
      
    Try
        shl.Initialize(
    "shl""./rpiinfo.sh"Null)
        shl.WorkingDirectory = 
    File.DirApp
        shl.Run(-
    1)
      
    Catch
        
    Log(LastException.Message)
      
    End Try
    End Sub

    Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
      
    'Process successfully completed
      'As an example start MQTT and update the RasPi information in Domoticz Home Automation after reading the Bash Script output files.
      If Success And ExitCode = 0 Then
        
    Log("Shell Command Successfull: MQTT connection initiated, data read and converted, published...")
      
    Else
        
    Log("Shell Command Error: " & StdErr)
      
    End If
    End Sub
     
    wirog80, luke2012, dar2o3 and 6 others like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice