Tuesday, August 26, 2014

Arduino IDE on Raspberry Pi

To install Arduino IDE on Raspberry Pi, enter the command:

$ sudo apt-get install arduino

After installed, run it with command:

$ arduino

This video show how to, and also show how Raspberry Pi connected Arduino Uno run.



Cross post with Arduino-er.

Shell script to check Raspberry Pi temperature

The following command print the Raspberry Pi CPU temperature:

$ cat /sys/class/thermal/thermal_zone0/temp

Shell script, chktemp.sh, with the following content print the CPU temperature:

temp=$(cat /sys/class/thermal/thermal_zone0/temp)
echo CPU Temp.= $(($temp/1000)) C

To make it executable:

$ sudo chmod +x chktemp.sh

Run it:

$ ./chktemp.sh

Shell script to check Raspberry Pi CPU frequency

The following command show the CPU current frequency:

$ cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

Create a shell script, chkspeed.sh, with content:

cur_freq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq)
echo Current CPU Freq.= $(($cur_freq/1000)) "(Mhz)"

To make it executable:

$ sudo chmod +x chkspeed.sh

Run it:

$ ./chkspeed.sh

Make shell script executable

To make shell scripts executable, enter the command:

sudo chmod +x <your shell script>


Monday, August 25, 2014

How much current Raspberry Pi draw

Shown in the video is a low cost USB Voltage Current Multimeter, it show the voltage and current at USB port alternatively. Maybe it's not a professional equipment, I can use it to monitor monitor and estimate power consumption on my RPi.

In order to eliminate the extra power draw by accessories, the Raspberry Pi connect USB power cable and network cable only, for sure with SD Card also.

Tuesday, August 12, 2014

Java/JavaFX Client/Server communication between Raspberry Pi and PC

It's from my another blog of Java/JavaFX Server and Client, to link Raspberry Pi and Android. With the same Java/JavaFX code, it can run on Raspberry Pi and/or PC to communicate as Client/Server. The code using ServerSocket, Socket, DataInputStream  and DataOutputStream. The code is listed at bottom also.

In this video, show the Server run on Raspberry Pi, and accept Client from PC.

In this another video show the Server run on PC, and accept Clients from Raspberry Pi.


Server code: (download HERE)
package javafx_server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFX_Server extends Application {

    TextField textTitle;
    Label labelSys, labelPort, labelIp;
    Label labelMsg;
    CheckBox optWelcome;

    ServerSocket serverSocket;

    String message = "";

    @Override
    public void start(Stage primaryStage) {

        textTitle = new TextField();
        labelSys = new Label();
        labelPort = new Label();
        labelIp = new Label();
        labelMsg = new Label();
        optWelcome = new CheckBox("Send Welcome when connect");
        optWelcome.setSelected(true);

        labelSys.setText(
            System.getProperty("os.arch") + "/"
            + System.getProperty("os.name"));
        labelIp.setText(getIpAddress());

        VBox mainLayout = new VBox();
        mainLayout.setPadding(new Insets(5, 5, 5, 5));
        mainLayout.setSpacing(5);
        mainLayout.getChildren().addAll(textTitle,
            labelSys, labelPort, labelIp,
            optWelcome, labelMsg);

        StackPane root = new StackPane();
        root.getChildren().add(mainLayout);

        Scene scene = new Scene(root, 300, 400);

        primaryStage.setTitle("Android-er: JavaFX Server");
        primaryStage.setScene(scene);
        primaryStage.show();

        Thread socketServerThread = new Thread(new SocketServerThread());
        socketServerThread.setDaemon(true); //terminate the thread when program end
        socketServerThread.start();
    }

    public static void main(String[] args) {
        launch(args);
    }

    private class SocketServerThread extends Thread {

        static final int SocketServerPORT = 8080;
        int count = 0;

        @Override
        public void run() {
            try {
                Socket socket = null;
                
                serverSocket = new ServerSocket(SocketServerPORT);
                Platform.runLater(new Runnable() {

                    @Override
                    public void run() {
                        labelPort.setText("I'm waiting here: "
                            + serverSocket.getLocalPort());
                    }
                });

                while (true) {
                    socket = serverSocket.accept();
                    count++;
                    
                    //Start another thread 
                    //to prevent blocked by empty dataInputStream
                    Thread acceptedThread = new Thread(
                        new ServerSocketAcceptedThread(socket, count));
                    acceptedThread.setDaemon(true); //terminate the thread when program end
                    acceptedThread.start();

                }
            } catch (IOException ex) {
                Logger.getLogger(JavaFX_Server.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }

    }

    private class ServerSocketAcceptedThread extends Thread {

        Socket socket = null;
        DataInputStream dataInputStream = null;
        DataOutputStream dataOutputStream = null;
        int count;

        ServerSocketAcceptedThread(Socket s, int c) {
            socket = s;
            count = c;
        }

        @Override
        public void run() {
            try {
                dataInputStream = new DataInputStream(
                    socket.getInputStream());
                dataOutputStream = new DataOutputStream(
                    socket.getOutputStream());

                //If dataInputStream empty, 
                //this thread will be blocked by readUTF(),
                //but not the others
                String messageFromClient = dataInputStream.readUTF();
                
                message += "#" + count + " from " + socket.getInetAddress()
                    + ":" + socket.getPort() + "\n"
                    + "Msg from client: " + messageFromClient + "\n";
                
                Platform.runLater(new Runnable() {
                    
                    @Override
                    public void run() {
                        labelMsg.setText(message);
                    }
                });
                
                if (optWelcome.isSelected()) {
                    
                    String msgReply = count + ": " + textTitle.getText();
                    dataOutputStream.writeUTF(msgReply);
                }
                
            } catch (IOException ex) {
                Logger.getLogger(JavaFX_Server.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException ex) {
                        Logger.getLogger(JavaFX_Server.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                if (dataInputStream != null) {
                    try {
                        dataInputStream.close();
                    } catch (IOException ex) {
                        Logger.getLogger(JavaFX_Server.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                if (dataOutputStream != null) {
                    try {
                        dataOutputStream.close();
                    } catch (IOException ex) {
                        Logger.getLogger(JavaFX_Server.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }

    }

    private String getIpAddress() {
        String ip = "";
        try {
            Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
                .getNetworkInterfaces();
            while (enumNetworkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = enumNetworkInterfaces
                    .nextElement();
                Enumeration<InetAddress> enumInetAddress = networkInterface
                    .getInetAddresses();
                while (enumInetAddress.hasMoreElements()) {
                    InetAddress inetAddress = enumInetAddress.nextElement();

                    if (inetAddress.isSiteLocalAddress()) {
                        ip += "SiteLocalAddress: "
                            + inetAddress.getHostAddress() + "\n";
                    }
                }
            }
        } catch (SocketException ex) {
            Logger.getLogger(JavaFX_Server.class.getName()).log(Level.SEVERE, null, ex);
        } 

        return ip;
    }

}

Client code: (download HERE)
package javafx_client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFX_Client extends Application {

    Label labelSys;
    TextField welcomeMsg;
    Label labelAddress;
    TextField textAddress;
    Label labelPort;
    TextField textPort;
    Button buttonConnect;
    Button buttonClear;
    Label textResponse;

    @Override
    public void start(Stage primaryStage) {

        labelSys = new Label();
        welcomeMsg = new TextField();
        labelAddress = new Label("IP Address");
        textAddress = new TextField();
        labelPort = new Label("Port");
        textPort = new TextField();
        buttonConnect = new Button("Connect");
        buttonClear = new Button("Clear");
        textResponse = new Label();

        labelSys.setText(
                System.getProperty("os.arch") + "/"
                + System.getProperty("os.name"));

        HBox buttonbox = new HBox();
        buttonbox.setSpacing(5);
        HBox.setHgrow(buttonConnect, Priority.ALWAYS);
        HBox.setHgrow(buttonClear, Priority.ALWAYS);
        buttonConnect.setMaxWidth(Double.MAX_VALUE);
        buttonClear.setMaxWidth(Double.MAX_VALUE);
        buttonbox.getChildren().addAll(buttonConnect, buttonClear);

        buttonConnect.setOnAction(buttonConnectEventHandler);
        
        buttonClear.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                textResponse.setText("");
            }
        });

        VBox mainLayout = new VBox();
        mainLayout.setPadding(new Insets(5, 5, 5, 5));
        mainLayout.setSpacing(5);
        mainLayout.getChildren().addAll(labelSys, welcomeMsg,
                labelAddress, textAddress, labelPort, textPort,
                buttonbox, textResponse);

        StackPane root = new StackPane();
        root.getChildren().add(mainLayout);

        Scene scene = new Scene(root, 300, 400);

        primaryStage.setTitle("Android-er: JavaFX Client");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    EventHandler<ActionEvent> buttonConnectEventHandler
        = new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            String tMsg = welcomeMsg.getText();
            if (tMsg.equals("")) {
                tMsg = null;
            }

            RunnableClient runnableClient
                = new RunnableClient(textAddress.getText(), 
                    Integer.parseInt(textPort.getText()),
                    tMsg);
            
            new Thread(runnableClient).start();
        }
    };

    class RunnableClient implements Runnable{
        
        String dstAddress;
        int dstPort;
        String response = "";
        String msgToServer;
        
        public RunnableClient(String addr, int port, String msgTo) {
            dstAddress = addr;
            dstPort = port;
            msgToServer = msgTo;
        }

        @Override
        public void run() {
            Socket socket = null;
            DataOutputStream dataOutputStream = null;
            DataInputStream dataInputStream = null;

            try {
                socket = new Socket(dstAddress, dstPort);
                dataOutputStream = new DataOutputStream(
                    socket.getOutputStream());
                dataInputStream = new DataInputStream(socket.getInputStream());

                if(msgToServer != null){
                    dataOutputStream.writeUTF(msgToServer);
                }
    
                response = dataInputStream.readUTF();
   

            } catch (IOException ex) { 
                Logger.getLogger(JavaFX_Client.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                
                Platform.runLater(new Runnable(){

                    @Override
                    public void run() {
                        textResponse.setText(response);
                    }
                    
                });
                
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException ex) {
                        Logger.getLogger(JavaFX_Client.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                
                if (dataOutputStream != null) {
                    try {
                        dataOutputStream.close();
                    } catch (IOException ex) {
                        Logger.getLogger(JavaFX_Client.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                
                if (dataInputStream != null) {
                    try {
                        dataInputStream.close();
                    } catch (IOException ex) {
                        Logger.getLogger(JavaFX_Client.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                
            }
        }
        
    }

}


Sunday, August 10, 2014

Cross compile Java/Swing program for Raspberry Pi, with Netbeans 8

This video show how to cross compile Java/Swing program for Raspberry Pi, on PC running Netbeans.



Currently, I cannot remote run the Java/Swing program on Raspberry Pi within Netbeans IDE, caused by "No X11 DISPLAY variable was set, but this program performed an operation which requires it". But it can create the runnable jar in Raspberry Pi. You can login Raspberry Pi and run the jar locally.

You need to:
Install JDK 8 on Raspberry Pi
Set up Java SE 8 Remote Platform on Netbeans 8 for Raspberry Pi
- and know how to Run/Build JavaFX application on Raspberry Pi remotely from Netbeans 8

Code of this example:
package helloworldswing;

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class HelloWorldSwing {

    private static void createAndShowGUI() {

        JFrame frame = new JFrame("Hello Swing@Raspberry Pi");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(400, 300));

        JLabel label = new JLabel();
        label.setText(
            "Hello: " 
            + System.getProperty("os.arch") + "/"
            + System.getProperty("os.name"));
        
        frame.getContentPane().add(label);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    
}



Updated@2015-12-06: Solution of "No X11 DISPLAY variable was set, but this program performed an operation which requires it":

Refer to karczas' comment in YouTube video, The solution of 0:50 error (No X11 DISPLAY variable was set, but this program performed an operation which requires it) which will allow you to run and debug GUI application directly from NetBeans and display their content on your RaspPi (or BeagleBone Black): 
https://mkarczewski.wordpress.com/2015/12/05/netbeans-remote-java-arm-debugging-with-gui



Remote debug Java program on Raspberry Pi using Netbeans 8

With JDK 8 installed on Raspberry Pi, and Set up Java SE 8 Remote Platform on Netbeans 8, you can Run JavaFX application on Raspberry Pi remotely from Netbeans 8, and also debug Java/JavaFX program running on Raspberry Pi remotely on PC with Netbeans 8.

This video show how to debug the last example "Java/JavaFX Client run on Raspberry Pi, link with Android Server" remotely with Netbeans 8.

Java/JavaFX Client run on Raspberry Pi, link with Android Server

It's the Java/JavaFX version of Android client run on Raspberry Pi (via Netbeans 8 Remote Platform), to link with Android Server.

In the video, the VLC Media Player display is the Raspberry Pi RCA video captured with USB Video Adapter.

The program code can be found here:
Java/JavaFX version of Android client
- Android Server

To know how to:
Set up Java SE 8 Remote Platform on Netbeans 8 for Raspberry Pi
Run JavaFX application on Raspberry Pi remotely from Netbeans 8


Wednesday, August 6, 2014

Try demo of Java SE 8 for ARM on Raspberry Pi

After Install JDK 8 on Raspberry Pi, you can try the Java SE Development Kit 8 Update 6 Demos and Samples for ARM on Raspberry Pi.

JDK 8 for ARM Downloads (http://www.oracle.com/technetwork/java/javase/downloads/jdk8-arm-downloads-2187472.html) provide download of Java SE Development Kit 8 Update 6 Demos and Samples for ARM.

The video below show the demo of Java2Demo run on Raspberry Pi.
  • This Java2D demo consists of a set of demos housed in one GUI framework that uses a JTabbedPane.  You can access different groups of demos by clicking the tabs at the top of the pane. There are demo groups for Arcs_Curves, Clipping, Colors, Composite, Fonts, Images, Lines, Mix, Paint, Paths and Transforms.  On the right-hand side of the pane, the GUI framework features individual and global controls for changing graphics attributes. There's also a memory-usage monitor, and a monitor for tracking the performance, in frames per second, of animation demos.

Download and unpack the Java SE Development Kit 8 Update 6 Demos and Samples for ARM, copy file /demo/jfc/Java2D/Java2Demo.jar to Raspberry Pi, run the command on Raspberry Pi in Terminal (under X-window environment).

java -jar Java2Demo.jar

Direct run on Raspberry Pi, output to HDMI:

Monday, August 4, 2014

VLC Media Player on Linux to capture Raspberry Pi RCA video output

The post "Capture Raspberry Pi RCA video output with USB Video Adapter" show how to run on Windows. On Linux platform, we can do the same thing to capture video from Easy CAPture USB Video Adapter with VLC Media Player.

Install and install VLC Media Player on Ubuntu, search vlc in Ubuntu Software Center.


Start VLC media player (Sound & Video -> VLC media player)

Media -> Open Capture Device...

- Select Video camera in Capture mode.
- Try select Video and Audio device name.
- Select NTSC in Video standard.
- Play.



Sunday, August 3, 2014

Run JavaFX application on Raspberry Pi remotely from Netbeans 8

With Remote Platform set up on Netbeans 8, we can also develop JavaFX application on host computer running Netbeans, run the application remotely on Raspberry Pi. In case of Java FX application running on Raspberry Pi, it will display on Raspberry Pi monitor, instead of console.

Set up Java SE 8 Remote Platform on Netbeans 8 for Raspberry Pi

With Netbeans 8 installed on host computer and Java SE 8 on Raspberry Pi, we can set up remote platform on Netbeans 8. Such that we can develop Java application using Netbeans on host and deploy on Raspberry Pi remotely, all within Netbeans IDE.

It's assumed Java SE 8 have been installed on your Raspberry Pi as in last post "Install JDK 8 on Raspberry Pi and set JAVA_HOME and PATH".

In host PC (Ubuntu in my case) start Netbeans 8.

Click Tools -> Java Platforms

Click Add Platform...


Select Remote Java Standard Edition and click Next >


Name your new Remote Platform, and enter other information:
- 192.168.1.104 is the IP address of your remote Raspberry Pi.
- pi, the user in Raspberry Pi, and also enter password.
- /opt/jdk1.8.0_06 in Remote JRE Path is the JDK 8 installed location on Raspberry Pi, in last post.

(Updated@2014-09-17) for New Raspbian (2014-09-09) come with Java 8, set Remote JRE Path to default location /usr. ~ Setup Netbeans Remote Platform for default Java 8 in new Raspbian

Then finish.



This video show the steps:


Deploy Java application on Raspberry Pi remotely from Netbeans 8

Create Java Application in Netbeans as normal case. To Run it using the Remote Platform (created in previous steps):

Right click on Project -> Propertiers.

Select Run tab. Select the new created Remote Platform in Runtime Platform, and name the new Configuration.


Then you can run the application on remote Raspberry Pi by running it in this new configuration.



Once Remote Platform setup in your Netbeans IDE, you can also
development JavaFX application on Nerbeans 8, and run it remotely on Raspberry Pi
- remote debug Java program on Raspberry Pi
Cross compile Java/Swing program for Raspberry Pi



Install JDK 8 on Raspberry Pi and set JAVA_HOME and PATH


Updated@2015-03-24:
The new Raspbian come with java 1.8.0 with path set, but no JAVA_HOME.
If you need set JAVA_HOME for the preload java in new Raspbian, refer to my updated post "Set JAVA_HOME for Raspbian pre-loaded java 1.8.0".



This post show how to download JDK 8 on a host computer running Ubuntu Linux (not on Raspberry Pi), then copy to Raspberry Pi, ssh to Pi to setup JDK 8 and set JAVA_HOME and PATH in .bashrc.



To download JDK 8 on a host computer, visit: http://www.oracle.com/technetwork/java/javase/downloads/index.html, follow the steps to download Java SE Development Kit (JDK) for ARM.

In the following steps,
  • jdk-8u6-linux-arm-vfp-hflt.tar.gz is the downloaded file
  • 192.168.1.104 is the IP address of Raspberry Pi
  • pi is the user in Raspberry Pi
Open Terminal, switch to the download folder.

Run the command to copy the downloaded file to Raspberry Pi:
$ scp jdk-8u6-linux-arm-vfp-hflt.tar.gz pi@192.168.1.104:

Log-in Raspberry Pi via ssh:
$ ssh -X 192.168.1.104 -l pi
(-X is option)

Un-pack the file to /opt
$ sudo tar -zxvf jdk-8u6-linux-arm-vfp-hflt.tar.gz -C /opt

Edit .bashrc, to set JAVA_HOME and PATH:
$ nano .bashrc

Add the two line
export JAVA_HOME=/opt/jdk1.8.0_06
export PATH=$JAVA_HOME/bin:$PATH

where /opt/jdk1.8.0_06 is the unpacked folder of the new JDK.

Finally reboot Raspberry Pi.



Related:
- Try demo of Java SE 8 for ARM on Raspberry Pi

Saturday, August 2, 2014

The MagPi magazine issue 26 released, with introduction of The Model B+

This month’s MagPi contains another great selection of hardware, software and programming articles. Michael Giles explains how to turn your Raspberry Pi into a magic wand with a fun hardware project that demonstrates the persistence of vision effect, while John Mahorney from Suncoast Science Center in Florida shows how a Raspberry Pi is used to display dynamic art. Big news this month is the launch of the Model B+ and MagPi writer Aaron Shaw has all the details for you on page 22.

Robotics is always a popular theme and Harry Gee continues on from last month’s article and shows how to add speech, voice control and facial recognition to your robot. Additionally, Rishi Deshpande describes how to use the SmartDrive controller board to easily control high current motors.

Another popular topic is beer and Sebastian Duell explains his hardware and software “Mashberry” project which he uses to optimise the mashing process. Karl-Ludwig Butte continues his series on using a Raspberry Pi and BitScope for electronic measurement plus we have an interesting article by Walberto Abad on how to set up your own VoIP telephone system.

Last, but definitely not least, Jon Silvera continues his series on learning how to program with FUZE BASIC. This is an exciting article with lots to discover. I certainly don’t remember ever being able to control sprites this easily with BASIC!

Visit: http://www.themagpi.com/issue/issue-26/

Friday, August 1, 2014

Java 8 and Embedded JavaFX

Presentation on using the new embedded features in Java 8, including JavaFX on devices like the Raspberry Pi and Lego Mindstorms EV3. Also covers JavaFX on tablets and smartphones.