B4J Tutorial [IoT] Raspberry 2 & B4J from the scratch...

This tutorial is about:

- Installing a brand new RaspBerry 2 (RB) (older models will do, too)
- configure WiFi on the RB
- get remote access to the RB from your PC via "Putty"
- share files between RB & PC via "WinSCP"
- prepare the B4J bridge on the RB
- write a B4J programm and test/run it on the RB

1. Buying a Raspberry

I recommend to buy a bundle. So you can be sure that f.e. the WiFi-dongle is working with your RB.

The one I bought (60€) came in with

- the RB Pi 2 (newest model with 1 GB RAM)
- a power supply with 2 ampere (less isn't good because it has 4 USB ports and HDMI)
- a WiFi dongle
- a pre installed SD-Card
- a hard case for the RB
- 3 coolers to stick on the cpu and other parts (you can put them on when you use a lot of CPU)


2. First install/run

Though the RB came in with a pre-installed SD-Card with Raspian OS it showed up as not usable. I think it was "some version they found". I strongly recommend that you wipe the card and install NOOBS.

To format the card use this tool (quick format) to be sure that it is formatted correct:

https://www.sdcard.org/downloads/formatter_4/

Get the NOOBS package from:

https://www.raspberrypi.org/downloads/noobs/

It seems that their server is unstable so my downloads didn't get through. I solved this by using a download manager: http://www.freedownloadmanager.org/

Just copy the link and click on "new download". Everthing will be done by that program.

When the download is finished, unzip the files and copy *all files an folders* to the fresh formatted sd-card and insert it in the RB.

3. Monitor, Mouse, keyboard & WiFi dongle -> power it on

The RB has a stabdard HDMI Output. You need a monitor (flatscreen is fine), a mouse and a keyboard to change the settings. Don't forget the WiFi dongle. First plug everything to the USB ports. The power supply comes last (the RB starts at once when it gets power).

4. Select Raspian

Wait 'till you see the mousepointer and the install Screen (desktop). Select Raspian to install and the language/keyboard layout and click install. Select "increase partition", too, to have more memory on the card.

This will take about 10 minutes. If you're asked to use SSH prompt it with "YES" (this is needed to get remote access later). If it ask's you to boot into UI or prompt chose "prompt".

The RB reboots after a while. My installation did not ask for a pw. If yes, the user is "pi" and the pw "raspberry" (standard). If you type the pw it will not be shown.

5. First start

If it boots into the desktop (UI) then jump to #6

If it boots into the prompt, type "startx" and press enter (this will start the desktop)

6. Configure WiFi (desktop)

Of course you can plug in a network cable but then you have to put the RB where the plug is.

In the right upper corner of the dektop you'll see a network symbol. Click on it (left or right). It took me some time to understand. First select the Router's name and then (with the other key of the mouse) you can set the passphrase. Very irritating. Just try a bit.

Now the RB is online. Exit the desktop with "Shutdown -> Exit to prompt". At any time you can get back to the desktop by typing "startx"

7. Updates (command prompt)

Now we *MUST* update everything by typing

B4X:
sudo apt-get update

Prompt everything with "y"

and

B4X:
sudo apt-get upgrade

Prompt everything with "y"

Don't think "I don't need that". Later you *WILL* get into troubles if you do not update!

8. Who is "sudo"?

sudo
stands for super user do, which is the admin of the RB. The normal user "pi" has just basic rights and may not change deeper system settings. So almost all commands start with sudo xxxx (like to start the B4J-bridge which I show later)

9. Remote access from your PC

Get the tool "Putty" for Windows from here: http://www.putty.org/

There's no need to install, just start it.

Config:

Connection -> Data use "pi" as Auto-login
Session -> the IP address of the Pi (take a look at your router's menu or type "ifconfig" at the prompt of your RB when it's connected to a monitor)

To save the profile type a name (Saved Sessions) and press "Save" to store it.

Now press "Open" at the bottom of the window to connect to the RB. Now you should see the RB's prompt.

If not (and you're sure that the ip address is correct): Connect the RB to a monitor and type
B4X:
sudo raspi-config

Navigate to Advanced Options -> SSH and activate it

The RB reboots. Try to connect again.

A new window opens and you will be asked for the password for user pi. Enter it and you'll see a screen like this:

B4X:
Using username "pi".
[email protected]'s password:

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Oct 29 22:42:44 2015 from klausi5.fritz.box
pi@raspberrypi ~ $

You can also use your Android device to connect to the RB. Donwload "Mobile SSH" from the Store. You only need to configure the IP address and press connect. You will be asked for the user. It's "pi" and the password.

10. Transfering files from/to the RB/PC

If find WinSCP very useful -> http://winscp.net/eng/download.php

Select "Commander" as the prefered view.

Configuration:

File Protocol -> SCP
User name -> pi
password -> optional (you will be asked for it)
Host name -> RB's ip address

Advanced -> SCP/Shell -> sudo su -

Click on "save" to save the profile. Chose a name for it and click on "login". You will be asked for the password after it's successfuly connected. Like in an FTP-Program you will then see two explorer windows. On the left your pc, on the right the Raspberry.

You can use Putty and WinSCP at the same time, so when I start to develop, I have both tools opened.

Putty: To mirror the screen of the RB
WinSCP: To exchange files

11. Folder structure on the Raspberry: Where am I?

Normally you log in with the user "pi". It's is a non-admin user and has access only to his own folder which is called like the user = "pi". This is the reason why we use "sudo" for a lot of commands. Otherwise it's not possible to create folders outside the home folder.

When you start Putty it will log in the user pi and you will navigated to the "pi" folder. Try to create a new folder:
B4X:
mkdir test
. No problem.

Now enter
B4X:
cd ..
to get one folder up. It's called "home". Enter
B4X:
mkdir test
again. Oops. Now it fails.

Enter
B4X:
sudo mkdir test
This works because now you're trying with the superuser. This is why we configure sudo in WinSCP to get access to all files and folders.

12. Time to "install" the B4J-bridge

I've put all tools for my RB in a folder on my pc. The B4J-bridge is just a *.jar file which has to be copied once to your RB.

Download the bridge from here www.b4x.com/b4j/files/b4j-bridge.jar and put it in the pc's folder. Open WinSCP and copy it to the home folder of the RB. I prefer NOT to put it into a new folder because you've got to type the full path which I'm too lazy to.

Another nice way to put files on the RB is to download files directly from a server from the RB's prompt with:
B4X:
sudo wget http://www.b4x.com/b4j/files/b4j-bridge.jar
.

Start the bridge with
B4X:
sudo java -jar b4j-bridge.jar
and you'll see

B4X:
B4J-Bridge v1.00
Waiting for connections (port=6790)...
My IP address is: 192.168.178.61

It's ready to connect now...

13. Debug time

Start B4J. Click on File -> New -> Non-UI (console) which creates a new console project. Save it to a new folder and give it a name you like.

Remove all lines and copy this code for a quick test:
B4X:
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Public t As Timer
End Sub

Sub AppStart (Args() As String)
    t.Initialize("T",2000)
    t.Enabled=True
    DateTime.DateFormat="dd.MM.yyyy"

    StartMessageLoop

End Sub

Sub T_Tick
    Log(DateTime.Time(DateTime.Now))
End Sub

It will create a timer and will print the actual time to the log.

Be sure that the bridge is running on your RB (see above).

In B4J click on Tools -> B4J-Bridge -> Connect and chose (or type in) the ip address of the pi. Forgot it? Stop the bridge on the RB (like any other program) with ctrl & c. Now type ifconfig (it's like ipconfig in windows) and you will see the address. Don't forget to start the bridge again after that.

Check if the bridge is really connected (in B4J in the left bottom corner) and run your program. What you see in the log is actually running on the RaspBerry.

On my first try I forgot the line
B4X:
StartMessageLoop
which causes the program to act like a script which exactly executes the lines ONCE and then ends.

14. Release time

Now that you have tested my very complicated example programm you may want to execute it on the RB directly. No prob.

First disconnect the bridge in B4J and stop the bridge in the RB's prompt with crtl & c.

Now Compile it in Release mode and take a look at the compiler's window. You will see where the *.jar file has been copied to on your pc: ...\Objects\Example.jar (I've called my program "Example").

Use WinSCP and create a new folder (for each app) under home. Copy the jar file (mine is called Example.jar) to that new folder.

Get back to Putty and navigate to the new folder "Example" by using the command cd:
B4X:
pi@raspberrypi /home $ cd Example
pi@raspberrypi /home/Example $

Start your programm with

B4X:
pi@raspberrypi /home/Example $ sudo java -jar Example.jar

The output will be the same as in the logs:

B4X:
pi@raspberrypi /home/Example $ cd ..
pi@raspberrypi /home $ clear
pi@raspberrypi /home $ cd Example
pi@raspberrypi /home/Example $ sudo java -jar Example.jar
17:18:13
17:18:15
17:18:17
17:18:19
17:18:21
17:18:23
17:18:25
17:18:27
17:18:29
17:18:31
17:18:33
17:18:35
17:18:37
17:18:39

To stop it, just use crtl & c...

15. Is there more than a simple program?

YES! A lot of B4A/B4J code can be run without modification (like httputils, encryption and other festures). Just browse the forum.

16. What about UI-Apps?

Yes, but... B4J uses JavaFX which will be removed at the next versions (8 and up). As long as you are good with Java 7 you can use it.

17. I need further informations

Just use Google. There are tons of documentation and videos. And of course here in the forum.

18. I would like to run the same application (.jar) on my Windows system. What must be changed?

Nothing. If you create a non UI app, there is nothing to change

- open a command line (WIN8/10):
- right click the windows start symbol
- click on run
- enter cmd
- locate your .jar file (in your project folder -> Objects
- hold shift and right click on the icon (to copy the full path)
- select copy as path
- in the cmd window type

B4X:
java -jar

- right click to insert the copied path
- press enter and your app runs

It can be closed by ctrl & c

Except the word "sudo" before "java -jar" it is the same statement to start it as on the RaspBerry
 
Last edited:

ilan

Expert
Licensed User
Longtime User
Ok i understand now what you mean. The b4j bridge can use different java version. So i start the bridge with a version that support javafx and like this i will be able to run b4j ui-apps.
 
Last edited:

Roycefer

Well-Known Member
Licensed User
Longtime User
If you are using Raspbian installed from NOOBS, then Java 8u0 comes pre-installed. Just use that. You can type
B4X:
java -version
in the terminal to see what Java version is installed. You can have whatever Java version you like on your PC, you only need Java 8u6 (or below) on the RasPi.
 

ilan

Expert
Licensed User
Longtime User
If you are using Raspbian installed from NOOBS, then Java 8u0 comes pre-installed. Just use that. You can type
B4X:
java -version
in the terminal to see what Java version is installed. You can have whatever Java version you like on your PC, you only need Java 8u6 (or below) on the RasPi.

i installed java jdk 1.7 and run it but when i debug my simple UI app nothings happens. also in logs nothing happens.
and bridge is connected
 

Roycefer

Well-Known Member
Licensed User
Longtime User
If you didn't uninstall Java 8, then Java 8 will probably override your Java 7 installation. Do the "java -version" terminal command I posted to see what Java version is reported. Also, I wouldn't use Java 7 if all you're trying to do is get JavaFX to work. Use Java 8u0. It comes with the Raspbian image. If I recall, Java 7 doesn't come with JavaFX built-in, you have to install JavaFX separately. Java 8 has it built-in (up to version 8u6 for ARM).
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
If you didn't uninstall Java 8, then Java 8 will probably override your Java 7 installation
That's not correct. You do not need to uninstall the existing java or install the new one. Just copy the files to the board and run b4j-bridge with the full path to java: [IoT] Linux Embedded Boards - Gettings started

Are you able to run a non-ui app?

You should use java 8u6.
 

ilan

Expert
Licensed User
Longtime User
Just copy the files to the board and run b4j-bridge with the full path to java

just download the java version and copy the files doesnot work for me.

i installed java 1.7.0 with: sudo apt-get install oracle-java7-jdk

and tried to run a ui-app but nothing happend.

i am able to run a non-ui app with java 1.8.0 as you can see in the image

rsp2.jpg

when i try t download the java version 8u6 (i donwload all linux versions jdk&jre because you did not said what version i should download and still no one worked)

when i copy the files and try to run the b4j-bridge with java 8u6 or even if i only ask for -version i get error

error 1: is "Permission denied"

then i changed the folder rights (incl all folder and files inside) to rwxr-xr-x

error 2: then i get this error: Exec format error

rsp1.png


i waste maybe 8 hours on it. so i give up, i hope there will be support for ui-apps in the future since this will make those cheap boards really usefull for us.
i know we can run non-ui server apps on it but i think run real programs would make raspberry much more interesting for us b4x community.

if someone was able to run ui-app i would be happy to get more informations.
 

ta1dr

Member
Licensed User
Longtime User
I have same problem.. :(
I bought RB pi 2 , 7" touch screen bla bla for my R&D project
project like a HMI I want read modbus ( LPG massflow meter) and display and write excel files
I wrote code B4J it works on PC but I want to work on RB2
Waiting for results with hope
 

ilan

Expert
Licensed User
Longtime User
You do not need to install Java. Follow the steps from this tutorial: https://www.b4x.com/android/forum/threads/60430/#content

Copy the tar file and unpack it.

as i said i have copied the files but i could not load the bridge with the java version i have downloaded.

first error was: you have no permission...
then i changed the directory rights and then i had another error: Exec format error

i have downloaded all jre linux java files and jdk linux java files and nothing worked.
only if i install a java version i can run b4j with that java version and thats why i tried java 1.7 (this is the only version i have installed)
all other versions i download then copy to my rsp2 via winSCP and try to run via putty

but i get errors!!
 

ta1dr

Member
Licensed User
Longtime User
thanks Erel
I download tar file on my pc and copy USB mem
after plud USB mem RB2 I see tar file ...this ok
but I didnt understand
copy tar file to RB2 download folder and after sudo tar bla bla
or which why..( sorry I am newbee for RB 2 also JAVA)

thanks again
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
then i changed the directory rights and then i had another error: Exec format error
You need to download the arm java.

Try this one:
1. Go to this page: http://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase8-2177648.html#
2. Find this one: Linux ARM v6/v7 Hard Float ABI :

SS-2015-11-20_10.07.18.png

Note that the title is NOT Java SE Development Kit 8u60.

@ta1dr a tar file is like a zip file. You need to unpack it. This is done with the command:
B4X:
tar -xf <tar name>
You need to run it from a terminal. Press Tab to complete the file name (after you enter a few characters).

 

ilan

Expert
Licensed User
Longtime User
ok, i have downloaded the same file as you show in the image above jdk-8-linux-arm-vfp-hflt.tar.gz (NOT ...Kit 8u60)

then i connect to the bridge with this java version and when i run in debug mode nothing happens. no logs... nothing

rsp3.png


what am i doing wrong??
 

ta1dr

Member
Licensed User
Longtime User
Thank you Erel for patiently answered I will try when goto workshop...
 

KMatle

Expert
Licensed User
Longtime User
sudo stands for super user do, which is the admin of the RB. The normal user "pi" has just basic rights and may not change deeper system settings. So almost all commands start with sudo xxxx (like to start the B4J-bridge which I show later)

Important:

The user "pi" ist a Standard user like in windowd (not admin) which is allowed to create directories and some other things ONLY in his user directories.

Best practice: Start every command with sudo

To Keep it simple: Please use the Noobs package (not "some Image I found on the net"). With this I had no Problems (even Java was updated to 1.8 automatically).
 
Top