Using a remote Raspberry Pi's OS by PHP

This Smart Pascal program, inspired by James Farnsworth's remote control of the Raspberry Pi, uses PHP to execute commands on the Raspberry Pi. The second argument to PHP's exec function is an array which is filled with the line(s) of the result of the command. We add line endings so that the strings returned to the Smart Pascal program (by echo) display in the memo as if the command had been entered directly into the console on the Raspberry Pi.

The code of the PHP file and the Smart Pascal and XML code of the form follow the screenshot of the program in action. To replicate this demonstration, put the HTML output file and get-command-result.php in the /var/www/ folder of a Raspberry Pi that has been set up as a web server.

Application in Action

Application in Action

The user has the choice of selecting one of the tested commands in the combo box or typing a command into the edit box.

Code of get_command_result.php

<?php
$command = $_GET['c'];
exec($command, $results);
if (count($results) == 0)
{
  echo "No results returned";
}
else
{  
  foreach ($results as $line)
  {  
    echo $line;
    echo "\r\n";
  }
}
?>

Smart Pascal Code of Form

unit Form1;

interface

uses 
  SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms, 
  SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Controls.Button,
  SmartCL.Controls.EditBox, SmartCL.Controls.Memo, SmartCL.Time, SmartCL.Controls.CheckBox,
  SmartCL.Controls.Panel, SmartCL.Controls.ComboBox, SmartCL.Controls.Label;

type
  TForm1 = class(TW3Form)
    procedure W3EditBox1Changed(Sender: TObject);
    procedure W3Button1Click(Sender: TObject);
  private
    {$I 'Form1:intf'}
    strResult, strPHP: String;
    FTimerCommand: TW3Timer;
  protected
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure SendRequest;
  end;

implementation

procedure TForm1.SendRequest;
begin
  FTimerCommand.Enabled := True;
  asm
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
      @strResult = '';
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
        @strResult = xmlhttp.responseText;
      }
    }
    xmlhttp.open("GET",@strPHP,true);
    xmlhttp.send();
  end;
end;

procedure TForm1.W3Button1Click(Sender: TObject);
begin
  strPHP := 'get_command_result.php?c=' + W3EditBox1.Text;
  SendRequest;
end;

procedure TForm1.W3EditBox1Changed(Sender: TObject);
begin
  W3Button1Click(Sender);
end;

procedure TForm1.InitializeForm;
begin
  inherited;
  FTimerCommand := TW3Timer.Create;
  FTimerCommand.Enabled := False;
  FTimerCommand.Delay:= 100;
  FTimerCommand.OnTime := procedure (Sender: TObject)
    begin
      if W3Memo1.Text <> strResult then
         W3Memo1.Text := strResult;
      FTimerCommand.Enabled := False;
    end;
  W3Combobox1.Add('echo This is fun!');
  W3Combobox1.Add('pwd');
  W3Combobox1.Add('ls');
  W3Combobox1.Add('du -h');
  W3Combobox1.Add('uname -a');
  W3Combobox1.Add('date');
  W3Combobox1.Add('cal 8 2015');
  W3Combobox1.Add('grep "html" index.html');
  W3Combobox1.OnClick := procedure(Sender: TObject)
    begin
      strPHP := 'get_command_result.php?c=' + W3Combobox1.Items[W3Combobox1.SelectedIndex];
      SendRequest;
    end;
end;

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
end;
 
initialization
  Forms.RegisterForm({$I %FILE%}, TForm1);
end.

XML Code of Form

<SMART>
  <Form version="2" subversion="1">
    <Created>2015-03-01T13:33:43.698</Created>
    <Modified>2015-03-02T17:52:12.695</Modified>
    <object type="TW3Form">
      <Caption>Pi Commands</Caption>
      <Name>Form1</Name>
      <object type="TW3Button">
        <Caption>Send Command</Caption>
        <Width>136</Width>
        <Top>8</Top>
        <Left>203</Left>
        <Height>32</Height>
        <Name>W3Button1</Name>
        <OnClick>W3Button1Click</OnClick>
      </object>
      <object type="TW3Memo">
        <Width>488</Width>
        <Top>48</Top>
        <Left>8</Left>
        <Height>136</Height>
        <Name>W3Memo1</Name>
      </object>
      <object type="TW3EditBox">
        <Value></Value>
        <Text>ls -lh</Text>
        <Range></Range>
        <Width>112</Width>
        <Top>8</Top>
        <Left>91</Left>
        <Height>32</Height>
        <Name>W3EditBox1</Name>
        <OnChanged>W3EditBox1Changed</OnChanged>
      </object>
      <object type="TW3ComboBox">
        <Width>128</Width>
        <Top>8</Top>
        <Left>368</Left>
        <Height>32</Height>
        <Name>W3ComboBox1</Name>
      </object>
      <object type="TW3Label">
        <Caption>Command:</Caption>
        <Width>80</Width>
        <Top>8</Top>
        <Left>8</Left>
        <Height>32</Height>
        <Name>W3Label1</Name>
      </object>
    </object>
  </Form>
</SMART>
Programming - a skill for life!

Using Node.js for a server that updates connected clients in real time and remote use of a Raspberry Pi's OS