Tuesday, May 19, 2015

Running Java EE Applications with Payara Micro on the Raspberry Pi

Payara Micro is a new way of running Java EE applications without having to install a full application server.

As Payara Micro requires no installation and has a <60MB runtime footprint it is ideal for running on small platforms like the Raspberry Pi.

This blog post (https://www.voxxed.com/blog/2015/05/running-javaee-applications-with-payara-micro-on-the-raspberrypi/) will give you a step by step rundown of getting a Payara Micro cluster running on a pair of Raspberry Pis. However the steps here can be used to test Payara Micro on any Linux platform.


Tuesday, May 12, 2015

Python socket server to send camera image to client (II)

Last exercise implement Python socket server to send camera image to client, wait client, send image file, and exit. In this exercise, the server will stay in the program and wait another connection from client, without exit.


pyCamJpgServer2.py
import io
import socket
import picamera
import atexit

def camServer():

    while True:
        print("wait...")
        conn, addr = server_socket.accept()
        if conn:
            print(conn)
            print(addr)
            connection = conn.makefile('wb')
            break

    print("Connecting")
    try:
        stream = io.BytesIO()
        camera.capture(stream, 'jpeg')
        stream.seek(0)
        connection.write(stream.read())
        stream.seek(0)
        stream.truncate()
    finally:
        print("close connection")
        connection.close()

def onExit():
    connection.close()
    server_socket.close()
    print("exit")

with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.start_preview()
    atexit.register(onExit)

    server_socket = socket.socket()
    server_socket.bind(('0.0.0.0', 8000))
    server_socket.listen(0)
    server_socket.setblocking(1)
    
    while True:
        camServer()


Monday, May 11, 2015

Python socket server to send camera image to client

Simple server run on Raspberry Pi to send jpg from Camera Module to client. Can be viewed on browsers at http://<Raspberry Pi IP>:8000.


pyCamJpgServer.py
import io
import socket
import picamera

server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8000))
server_socket.listen(0)
    
with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.start_preview()

    connection = server_socket.accept()[0].makefile('wb')
    print("Connecting")

    try:
        stream = io.BytesIO()
        camera.capture(stream, 'jpeg')
        stream.seek(0)
        connection.write(stream.read())
        stream.seek(0)
        stream.truncate()

    finally:
        connection.close()
        server_socket.close()
        camera.stop_preview()
        print("Connection closed")



In this example, the server will exit after image sent. To stay waiting next connection, refer to next post.

Friday, May 8, 2015

Python + RPi Camera Module: Capturing to a network stream, display on Tkinter GUI

This example modify from Picamera tutorial: Basic Recipes - 4.9. Capturing to a network streamHere we have two scripts: a server (presumably on a fast machine) which listens for a connection from the Raspberry Pi, and a client which runs on the Raspberry Pi and sends a continual stream of images to the server.

Instead of do nothing on server side, I create Tkinter GUI to display the received image.

This video show how both client and server run on Raspberry Pi 2, remotely with xrdp/remmina.


In server side, in order to run both Tkinter GUI and server connection in parallel, I have to implement both in Thread. For this part, reference to O'Reilly Python Cookbook (2002): Combining Tkinter and Asynchronous I/O with Threads.

pyCamServer.py
#!/usr/bin/python

import time
import threading
import random
import Queue
from PIL import Image, ImageTk
import Tkinter as tk
import socket
import struct
import io
import tkMessageBox

class GuiPart:
    
    def __init__(self, master, queue, endCommand):
        self.queue = queue
        self.endCommand = endCommand
        
        self.root=master
        self.root.title('My Pictures')

        imageFile = "pi.jpg"
        self.image1 = ImageTk.PhotoImage(Image.open(imageFile))
        w = 640
        h = 480
        x = 0
        y = 0
        self.root.geometry("%dx%d+%d+%d" % (w, h, x, y))

        self.panel1 = tk.Label(self.root, image=self.image1)

        self.panel1.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.YES)
        self.panel1.configure(image=self.image1)

        self.root.protocol("WM_DELETE_WINDOW", self.on_closing)

    def on_closing(self):
        if tkMessageBox.askokcancel("Quit", "Do you want to quit?"):
            self.endCommand()
            self.root.destroy()

    def processIncoming(self):
        """Handle all messages currently in the queue, if any."""
        while self.queue.qsize(  ):
            try:
                self.update_image(self.queue.get(0))
            except Queue.Empty:
                # just on general principles, although we don't
                # expect this branch to be taken in this case
                pass
    def update_image(self, newImage):
        self.image1=newImage
        self.panel1.configure(image=self.image1)

class ThreadedClient:
    def __init__(self, master):
        self.master = master
        self.queue = Queue.Queue(  )
        self.gui = GuiPart(master, self.queue, self.endApplication)

        # Set up the thread to do asynchronous I/O
        # More threads can also be created and used, if necessary
        self.running = 1
        self.thread1 = threading.Thread(target=self.workerThread1)
        self.thread1.start(  )

        # Start the periodic call in the GUI to check if the queue contains
        # anything
        self.periodicCall(  )

    def periodicCall(self):
        """
        Check every 200 ms if there is something new in the queue.
        """
        self.gui.processIncoming(  )
        if not self.running:
            # This is the brutal stop of the system. You may want to do
            # some cleanup before actually shutting it down.
            import sys
            sys.exit(1)
        self.master.after(200, self.periodicCall)

    def workerThread1(self):
        #set up server
        # Start a socket listening for connections on 0.0.0.0:8000 (0.0.0.0 means
        # all interfaces)
        self.server_socket = socket.socket()
        self.server_socket.bind(('0.0.0.0', 8000))
        self.server_socket.listen(0)

        # Accept a single connection and make a file-like object out of it
        self.connection = self.server_socket.accept()[0].makefile('rb')

        try:
            while self.running:
                # Read the length of the image as a 32-bit unsigned int. If the
                # length is zero, quit the loop
                image_len = struct.unpack('<L', self.connection.read(struct.calcsize('<L')))[0]
                if not image_len:
                    break
                # Construct a stream to hold the image data and read the image
                # data from the connection
                image_stream = io.BytesIO()
                image_stream.write(self.connection.read(image_len))

                image_stream.seek(0)
                
                newPhotoImage=ImageTk.PhotoImage(Image.open(image_stream))
                self.queue.put(newPhotoImage)
        finally:
            self.connection.close()
            self.server_socket.close()

    

    def endApplication(self):
        self.master.destroy()
        self.running = 0



root = tk.Tk(  )

client = ThreadedClient(root)
root.mainloop(  )


In client side, basically same as in Picamera tutorial: Basic Recipes - 4.9. Capturing to a network stream.

pyCamClient.py
#!/usr/bin/python

import io
import socket
import struct
import time
import picamera

# Connect a client socket to my_server:8000 (change my_server to the
# hostname of your server)
client_socket = socket.socket()
client_socket.connect(('192.168.1.111', 8000))

# Make a file-like object out of the connection
connection = client_socket.makefile('wb')
try:
    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        # Start a preview and let the camera warm up for 2 seconds
        camera.start_preview()
        time.sleep(2)

        # Note the start time and construct a stream to hold image data
        # temporarily (we could write it directly to connection but in this
        # case we want to find out the size of each capture first to keep
        # our protocol simple)
        start = time.time()
        stream = io.BytesIO()
        for foo in camera.capture_continuous(stream, 'jpeg'):
            # Write the length of the capture to the stream and flush to
            # ensure it actually gets sent
            connection.write(struct.pack('<L', stream.tell()))
            connection.flush()
            # Rewind the stream and send the image data over the wire
            stream.seek(0)
            connection.write(stream.read())
            # If we've been capturing for more than 30 seconds, quit
            if time.time() - start > 30:
                break
            # Reset the stream for the next capture
            stream.seek(0)
            stream.truncate()
    # Write a length of zero to the stream to signal we're done
    connection.write(struct.pack('<L', 0))
finally:
    connection.close()
    client_socket.close()



Raspbian 2015-05-05 is available

Raspbian 2015-05-05 is available now: https://www.raspberrypi.org/downloads/


Tuesday, May 5, 2015

Stream Raspberry Pi Camera Module video using raspivid and vlc

Here show how to stream video from Raspberry Pi Camera Module to network using raspivid and vlc.
Run on Raspberry Pi 2/Raspbian
View on desktop Ubuntu with VLC Media Player
- for sure, you have to enable camera module in your Raspberry Pi/Raspbian setup.

- install vlc:
$ sudo apt-get install vlc

- Run the command in Terminal, or you can make a sh file (remember to make it runnable):
$ raspivid -o - -t 99999 -w 640 -h 360 -fps 25|cvlc stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8090}' :demux=h264

Where 99999 is the length of the video, 99.999 seconds. You can change that to whatever you like. If you change it to 0 (zero) it will carry on indefinitely. (And CTRL+C to kill it at any time).

8090 is the port you assign.

Now you can play the stream video at http:<Raspberry Pi IP>:8090

(reference: http://raspi.tv/2013/how-to-stream-video-from-your-raspicam-to-your-nexus-7-tablet-using-vlc)

In my case, report error of "Invalid PCR value in ES_OUT_SET_(GROUP_)PCR !" Just ignore it, the stream video is playing, and show on RPi main monitor.



Tested on desktop Ubuntu, it can be played with VLC Media Player. On Android devices, Nexus 7 (2012) running Android 5.1, and Samsung Galaxy S3 running Android 4.3, it can be played using MX Player, but not VLC Player (black screen).

If you can develop Android app, you can play it on VideoView. Refer to my another blog: Android-er - Play stream video (from Raspberry Pi) on VideoView, with APK download.



Updated@2016-07-06:
- Re-test on Raspberry Pi 3 + Camera Module V2 NoIR + Raspbian Jessie, play in Windows 10 + VLC Media Player and Android App with VideoView (with APK download).


Monday, May 4, 2015

Wooden Robot Kit shown at //build 2015, featuring Windows 10 and Raspberry Pi 2.


Explore the awesome wooden Robot Kit shown at //build 2015, featuring Windows 10 and the Raspberry Pi 2.

Windows 10 + Raspberry Pi 2: //build IoT Core Maker Robot Kit


Go to http://www.windowsondevices.com to learn more and join the community of developers building an internet of your things.

Featured Board: Raspberry Pi 2
Frame Design: Paweł Szymczykowski @makenai
Wooden Parts: http://ponoko.com
Electronics: http://pololu.com

Subscribe to our channel for more lessons and demos.

We can’t wait to see what you make!

Narated and directed by @IoTDan
Video and music produced by @IoTMikeT

#MakeInventDo