IoT without the Breadboard Part 3

      No Comments on IoT without the Breadboard Part 3

Payara Micro and GrovePi+ Mashup

https://gitlab.com/omniprof/PayaraMicroGrovePiMashup

In this project I have combined two other projects. These are Steve Millidge’s, of Payara, proof of concept Java EE application that highlighted the use of the Payara Micro server on the Raspberry Pi. You can see the original article at http://blog.payara.fish/piyara-payara-micro-on-raspberry-pi-demo. The other project is Eduardo Moranchel’s IoTDevices project that presents how to work with the GrovePi with Java. You can find it at https://github.com/emoranchel/IoTDevices.

The Payara project presents a simulated stock ticker where the numbers are presented on a line graph. The IoTDevices project presents code to communicate with GrovePi modules using either the JavaME dio library or the Pi4J library. The mashup I created simply replaced the random number generator in the Payara project with readings of the DHT-11 temperature sensor using the IoTDevices code.

The changes in the two projects that I made were quite minor. For the IoTDevices I stripped away the JavaME dio code and removed the samples. For the Payara code all I changed was a few lines to read the sensor. For both projects I did upgrade their pom.xml files to how I expect my students to write these files.

Hardware

Here is the hardware you will need to run this project:

2x Raspberry Pi version 2 or 3. Version 1 might work but I did not test it.

1x GrovePi+. This is a Raspberry Pi shield or daughter board that interfaces with the Grove modules

1x Grove Temp&Humi Sensor

1x Grove – LCD RGB Backlight v2.0

I recommend purchasing the GrovePi+ Starter Kit for Raspberry Pi that includes a number of modules including the two for this project but not a Raspberry Pi.

Raspberry Pi Software

Here is the software you will need for the Raspberry Pi.

Raspbian OS for the Pi (https://www.raspberrypi.org/downloads/raspbian/)

You only need the standard distribution, currently called Jessie, that already comes with Java 1.8. All other required libraries will be packaged in the application.

Payara Micro (http://www.payara.fish/payara_micro)

This is a single jar that you will need on each Raspberry Pi. You can download it directly on the Pi or download it to your dev system and copy it to the two Pi systems. Take note of its full name as you will need this in your WinSCP script. For simplicity I placed this jar in my home folder on both Pi systems.

Windows Development System Software

Here is the software you will need for your development system. I use Windows but the project will work from any Linux distro or Mac OSX system.

Java 1.8

The IoTDevices project uses Java 1.8 features such as lambdas.

NetBeans 8.1

While you could use pretty much any IDE I feel strongly that NetBeans is the best choice.

WinSCP (https://winscp.net/eng/index.php)

This is a windows application that allows you to connect to, upload to and execute on a remote Linux system. I experimented with some other options but I found WinSCP to be the best. It will run a script that Linux or Mac users should be able to turn into a shell script for their systems.

Raspberry Pi Setup

On one of the Pi systems attach the GrovePi+ shield. To the GrovePi+ attach the Temp&Humi Sensor and the LCD RGB Backlight module. The second Pi does not use a shield.

For my code I am using the default user and password for the Pi systems. You will need to know the IP number of each Pi. I use Ethernet rather than wireless but that should not make any difference. I assume that you have started up and verified the operation of each Pi. Either download on the Pi or copy to it the Payara Micro jar file.

Windows Dev System Setup

Verify that you have Java 1.8, download and install WinSCP, and download and install NetBeans. Run NetBeans and use its integrated Git support to clone this project. You will be confronted with many errors but you can ignore them as they will be resolved as you build each project.

Making it work

Clone the project from https://gitlab.com/omniprof/PayaraMicroGrovePiMashup. The four individual projects that appear in NetBeans when you clone my project are:

  • GrovePi-pi4j
  • GrovePi-spec
  • TemperatureTicker
  • TemperatureWeb

Before you build anything you need to edit the two WinSCP scripts. In NetBeans switch to File view and you find them in each project’s root.

TemperatureTicker is from Steve Millidge and is an EJB timer. I modified it to read from the GrovePi+ rather than generate a random number and display this value on the LCD display. The value is also sent our over a web socket. Its WinSCP script is called tickertopi.txt and contains:

open "scp://pi:raspberry@192.168.0.92" -hostkey="*"
put target\TemperatureTicker.jar
call java -jar payara-micro-4.1.1.162.jar --deploy TemperatureTicker.jar
close
exit

Change the IP number to that of the Pi with the GrovePi+ shield. Change the user name and password if necessary. Change the version number of the payara-micro jar file to match whatever you downloaded.

The TemperatureWeb, also from Steve Millidge, is a web socket client that receives data from the TemperatureTicker application and then feeds this to a JavaScript graphing application. Read Steve’s article and watch his video to understand how it all works. Its WinSCP script is called webtopi.txt and contains:

open "scp://pi:raspberry@192.168.0.85" -hostkey="*"
put target\TemperatureWeb.war
call java -jar payara-micro-4.1.1.162.jar --deploy TemperatureWeb.war
close
exit

Change the IP number to that of the Pi without the GrovePi+ shield. Change the user name and password if necessary. Change the version number of the payara-micro jar file to match whatever you downloaded.

IMPORTANT: This is not a secure project. It should not be using the default Raspberry Pi user and communication should be using SSL. This is a proof of concept only.

Building

All projects are Maven based so all you need is a working Internet connection on your development PC. You do not use the Run command. The first project that you will Clean and Build is GrovePi-spec. next, Clean and Build GrovePi-pi4j. They must be built in this order as GrovePi-pi4j depends on GrovePi-spec.

At this point both Raspberry Pi systems should be up and running. Now you can Clean and Build TemperatureTicker and TemperatureWeb. The order is not important as long as they are built after the GrovePi projects. The TemperatureTicker jar file will be large because it is packaged with all necessary libraries. This is why you don’t require a customized version of the Raspbian OS.

Seeing the results

Assuming that the build did not generate any errors and the WinSCP scripts ran without errors you will need to open a browser on your PC.

http://192.168.0.85:8080/TemperatureWeb

Change the IP number to match the Pi without the GrovePi+ shield. If all goes well it will look like:

I held the sensor and breathed on it to change the readings. Otherwise it was going to be 22 C constantly.

Graph

Stopping the Payara Server

You will need a remote terminal for each Raspberry Pi to kill each instance of the server. I have not learned how to do this from within NetBeans.

There you have it, the Payara/GrovePi+ mashup.

How to Run Maven Based Projects on a Remote Raspberry Pi Using NetBeans Part 2 of 2.

In the first part of this series I presented a solution for developing on a Windows PC a Maven managed Java applications with NetBeans and then deploying it to a Raspberry Pi. In this part I will look at at a solution that allows you to accomplish these tasks from within NetBeans. It will work with any version of Windows and Linux.

Solution #2 Running an external command from Maven

For this solution you will need an additional program on your Windows PC. You need a program that will allow you to interact with a Raspberry Pi or any Linux system from the command line in Windows. This is necessary because we will write a script that will carry out the required tasks.

For this solution I have chosen WinSCP as my tool for connecting to the Pi, uploading a file or files and then executing the file. It is not the only solution but its ability to run a script when executed makes it an ideal choice. You can find WinSCP at https://winscp.net/eng/index.php . Here is a direct link to the download: https://winscp.net/download/winscp577setup.exe . Once installed you should add its location to the System PATH so that it can be run easily from the command line.

Test it with a script. Here is a script from for my MavenWinToPi project from the previous article. Save the script as wintopi.txt in the root folder of the MavenWinToPi project.

open scp://pi:raspberry@192.168.0.92 -hostkey="*"
put target\MavenWinToPi-1.0-SNAPSHOT.jar
call java -cp java -cp MavenWinToPi-1.0-SNAPSHOT.jar com.kenfogel.mavenwintopi.MavenWinToPi
close
exit

This script will:

  1. Open a session between the PC and the Pi accepting any ssh key from the pi
  2. Copy the jar file from the PC to your home directory on the Pi. You can place it any folder by adding a path at the end of the put command.
  3. Execute command line java to run the jar file you copied. If you are using a directory other than home, then you will have to include the full directory path to the jar file.
  4. Close the session
  5. Exit WinSCP

There are two programs named WinSCP that are installed. One is WinSCP.exe that produces a GUI display similar to Filezilla. You could use WinSCP in place of Filezilla in solution #1. The other is WinSCP.com that is the command line version and is the one we want to use.

The script assumes that it is running in the root of the project so remember to save the script to this folder. The pom.xml file is also in the project root. Open a console, improperly called the Command Prompt by Microsoft, in the root folder of the project and test the script by entering at the command line:

WinSCP.com /script="wintopi.txt"

If it fails because WinSCP.com cannot be found, then use its location in the command such as it is on my system.

"C:\Program Files (x86)\WinSCP\WinSCP.com" /script="wintopi.txt"

winscp_script_x

If you can see “Hello Universe.” You know it has worked. Don’t panic over the WARNING! message. It is a result of using a wildcard for the -hotkey switch. Once you decide to go production you should carefully examine security. Now its time to make Maven execute WinSCP.com and the script.

Get Maven to do the work

To have Maven execute WinSCP and its script we need to add to the pom.xml file.  Here is the original pom.xml as generated by NetBeans.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.kenfogel</groupId>
    <artifactId>MavenWinToPi</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

We need to add a build section and in this section we need to add an exec plugin. Here is the new pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.kenfogel</groupId>
    <artifactId>MavenWinToPi</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.5.0</version>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>WinSCP.com</executable>
                    <arguments>
                        <argument>/script="wintopi.txt"</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

The Maven exec plugin is usually used to indicate which class contains the main method and or to add switches or arguments to the execution of a Java program. In my example I am using the plugin’s additional capability to run any type of executable. In this case the executable is WinSCP.com and the argument points to the script. To carry this out the command must be associated with a specific phase. In this case it is the ‘install’ phase when the fully assembled jar file is stored in the local maven repository.

Only use Build or Clean and Build

In NetBeans the install phase occurs at the end of a Java build. This means that if you do a Build or a Clean and Build the executable will run. Unfortunately, if you use the Run command in NetBeans it executes WinSCP.com but immediately declares a bad host error. Therefore, only do a Build or Clean and Build when using this technique for working with a Raspberry Pi

When you do the build you will see the output of the WinSCP program as well as the output of the program from the Raspberry Pi. It is almost indistinguishable from NetBeans’s native capability to run code remotely. Here is what should appear in the Output window of NetBeans:

Installing D:\NB_jgrove\MavenWinToPi\target\MavenWinToPi-1.0-SNAPSHOT.jar to E:\dev\.m2\com\kenfogel\MavenWinToPi\1.0-SNAPSHOT\MavenWinToPi-1.0-SNAPSHOT.jar

Installing D:\NB_jgrove\MavenWinToPi\pom.xml to E:\dev\.m2\com\kenfogel\MavenWinToPi\1.0-SNAPSHOT\MavenWinToPi-1.0-SNAPSHOT.pom

--- exec-maven-plugin:1.5.0:exec (default) @ MavenWinToPi ---
Searching for host...
Connecting to host...
Authenticating...
WARNING! Giving up security and accepting any host key as configured!
Using username "pi".
Authenticating with pre-entered password.
Authenticated.
Starting the session...
Session started.
Active session: [1] pi@192.168.0.92
target\MavenWinToPi-1.0-SNAPSHOT.jar |           2 KB |    0.0 KB/s | binary | 100%
Hello Universe.
Session 'pi@192.168.0.92' closed.
No session.
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 3.833s
Finished at: Sun Jul 17 19:56:59 EDT 2016
Final Memory: 14M/243M
------------------------------------------------------------------------

You know that all is well because you can see the ‘Hello Universe.’ Message.

Exception

The technique described her in part 2 will only work for applications that only have console output. If you must interact with the program in the console or if the program has a GUI then you will have to use solution #1.

In the remaining articles in my IoT without the Breadboard series I will be using this technique. If you know a better way to accomplish this, then let me know about it and I will update this information.

Acknowledgement

I’d like to thank fellow NetBeans Dream Team member Johannes Weigend for pointing out that the maven exec plugin could be a solution.