Aegidius
 Plüss   Aplulogo
   
serverstatus
 www.aplu.ch
 www.java-online.eu
     Print Text 
© 2018, V10.0
 
  TCPCom Module
 
TCP With The Raspberry Pi
 

 

The communication between two running programs, one on the Raspberry Pi (RPi) and one on a PC, is of special interest in many situations. For example the PC can act as a remote controller to request information acquired by RPi sensors or the camera module and pan and tilt the camera using servo motors. It is not necessary that the same programming language is used. A TCP server running on the RPi can be programmed in Python while the control program on the PC is written in Java in order to include a smart and elegant graphical user interface (GUI).

In the following example the principle is demonstrated without the use of real sensors. With a period of 1 second, the RPi sends the state of a pushbutton attached to a GPIO port to the PC that writes the information to the console window. If the button is held down for 3 cycles, the server stops.

Python server running on Raspberry Pi:

from tcpcom import TCPServer
import time
import RPi.GPIO as GPIO

def onStateChanged(state, msg):
    print "State:", state, "Msg:", msg

P_BUTTON1 = 16 # Switch pin number
dt = 1  # 1 s period
port = 5000 # IP port

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(P_BUTTON1, GPIO.IN, GPIO.PUD_UP)
server = TCPServer(port, stateChanged = onStateChanged)
n = 0
while True:
    if server.isConnected():
        rc = GPIO.input(P_BUTTON1)
        if rc == 0:
            server.sendMessage("pressed")
            n += 1
            if n == 3:
                break
        else:
            server.sendMessage("released")
            n = 0
        time.sleep(dt)
server.terminate()
print "Server terminated"

Again we use easygui to display a model input dialog to enter the server's IP address. Because no easy modeless dialog is available, we write the results from the server to the console.

Python client running on PC:

from tcpcom import TCPClient
from easygui import msgbox, enterbox

def onStateChanged(state, msg):
    print "State: " + state + ". Message: " + msg

port = 5000 # IP port
host= enterbox("Button Server IP Address?", "Button Client", "", True)
if host != None:
    client = TCPClient(host, port, stateChanged = onStateChanged)
    rc = client.connect()
    if rc:
        msgbox("Button client running. OK to stop","Button Client")
        client.disconnect()

In Java the package ch.aplu.util (download from here) provides modal and modeless dialogs. The results are written into a ModelessOptionPane.

Java client running on PC:

import ch.aplu.tcpcom.*;
import ch.aplu.util.*;

public class ButtonClient implements TCPClientListener
{
  private ModelessOptionPane mop;

  public ButtonClient()
  {
    int port = 5000;
    InputDialog id = new InputDialog("Button Client", "Host?");
    String host = id.readString();
    if (host == null)
      return;
    TCPClient client = new TCPClient(host, port);
    boolean success = client.connect();
    if (!success)
    {
       mop = new ModelessOptionPane("Connection failed");
       return;
    }
    mop = new ModelessOptionPane("Information from server:");
    mop.setTitle("Button Client");
    client.addTCPClientListener(this);
  }

  public void onStateChanged(String state, String msg)
  {
     mop.setText("State: " + state + ". Message: " + msg);
  }
  
  public static void main(String[] args)
  {
    new ButtonClient();
  }
}

Execute the program locally using WebStart

 

tcpbutton