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:

Cableguy

Expert
Licensed User
Longtime User
Thanks Klaus,

This will come in handy in a few weeks!
 

ilan

Expert
Licensed User
Longtime User
after reading your post here i have bought a raspberry pi 2, and to tell you the truth i have no clue what i have bought but it was to interesting for me.
i followed your tutorial and it is excellent. most tutorials i have seen will get you to a point where you are stuck because something is missing or wrong but yours is
really great. i managed to set my raspberry 2 and also connect to b4j bridge and run your very complex app ;)

thanx a lot, if @Erel will make it possible to run UI apps on raspberry it will be fantastic.

would love to run my clumsy bird example on my raspberry, do you have an idea what could i do ?? to make it run on the raspberry 2?

clumsy bird


raspberry.JPG
 

Roycefer

Well-Known Member
Licensed User
Longtime User
RasPi 2 on Raspbian can run JavaFX apps with some caveats: the Java version must be below 8u6, WebViews don't work, and there will be some other bugs. I've noticed some issues with window sizing and implementation of maximized windows and such. You can also write a server app and have your UI implemented in the browser.
 

Cableguy

Expert
Licensed User
Longtime User
From what I have read here in the forum, javafx is supported up to jfx v8 ... Something.... It was dropped by Oracle in the last 2 jfx upgrades I think.
So if you can put your hands on one of those jfx capable version and upload it/install it to the Raspi, then you will be able to reproduce your app on it


[EDIT] Roycefer beat me to the clock
 

ilan

Expert
Licensed User
Longtime User
the question is, if javafx is needed to run ui java apps? can erel make b4j ui java apps without javafx?
 

Roycefer

Well-Known Member
Licensed User
Longtime User
The older Java UI frameworks (Swing, AWT) are deprecated. Even Oracle doesn't recommend using them. They are probably still packaged into Java 8, though, so you could write a library that uses them (though that's no guarantee it would work on RasPi). I doubt Erel would incorporate them into the Visual Designer, though, just to meet a corner need of the RasPi community. I think the way to go is either with JavaFX on Java 8u6 or below or implement your UI in the browser. You could try out the new ABMaterial library.
 

ilan

Expert
Licensed User
Longtime User
B4J non-ui apps require Java 7+.

I stiil try to figure out how i could run my ui b4j apps on my raspberry pi 2.

Is there maybe another operating system i could use to run ui-apps?

Now i use raspbian.
 

Cableguy

Expert
Licensed User
Longtime User
you could search for an android version for Raspi, I remember reading about it, or try win10(IoT)...
 

ilan

Expert
Licensed User
Longtime User
I looked at win10 iot but its not a real operating system only a possibility to develope vs2015 apps and run on the raspberry.

I preffer to use b4x to develope my apps. I also read about android but could not find any tutorials or image file.
 
Last edited:

ilan

Expert
Licensed User
Longtime User
Btw i have a 8gb class 4 sandisk, if i buy a class10 sandisk will i see better performance on the raspberry?
 

ilan

Expert
Licensed User
Longtime User
You can use Java 7 or Java 8u6 to run UI apps. The layout will not be perfect but it should work.

thanx erel, but i dont understand how use java 8u6 on the raspberry.
do i need to check first what version is running on the raspberry? and then if its lower then java8u6 then install that java version?

cannot find any tutorials for that. maybe someone could make a simple tutorial how to install B4J ui-apps on the raspberry pi 2 :(
 

ilan

Expert
Licensed User
Longtime User
thank you very much erel, i will check it later when i get home.

just a small question, in your tutorial it says that raspberry pi already comes with java, will i be able to install java 8u60?

this is the version i need to download:
Linux x6457.03 MB
115899.gif
server-jre-8u60-linux-x64.tar.gz

right?
 
Top