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:

Madhav Tenneti

Member
Licensed User
Longtime User
Nice Step by Step instructions.

Having problems uploading Applications using b4j-bridge, during the upload the screen goes blank, looks like display driver gets effected, moving mouse slowly brings back the display on PI2, opening Tempjars folder is a Zip file, upon extracting this several folders are created however the Application jar file is missing, my RasPi Os is the current Jessie build.
 

derez

Expert
Licensed User
Longtime User
Managed to run a server app in my OrangePi, just followed the instructions.
Would be a good hint to remind that the public directory is in tempjars directory, not the application's.
In orange Pi the home directory is called root and you can run programs without sudo.
How do I close putty and not close the pi ? I want to start an app and keep it running after closing the putty interface (without having to connect it to real display).
Thanks
 

derez

Expert
Licensed User
Longtime User
I updated java jdk to 8 from the board using this
B4X:
sudo apt-get update && sudo apt-get install oracle-java8-jdk
I couldn't find the path_to_java so I wrote (with my app's name)
B4X:
nohup java -jar somejar.jar > nohup.out &
and it works, the program runs after closing putty.
Thank you !
 

derez

Expert
Licensed User
Longtime User
Strange - after I have my orangepi work with wifi, connect putty and winSCP, I see that the b4j servers I installed are not working. If I change to net by cable - they all work.
I have added to the /etc/wpa_supplicant/wpa_supplicant.conf file only these parameters (with my network names):
B4X:
network={
    ssid="testing"
    psk="testingPassword"
}
I have found that if I add more parameters like written here http://www.howtogeek.com/167425/how-to-setup-wi-fi-on-your-raspberry-pi-via-the-command-line/ it does not work at all.
It does not work also after connecting bridge (bridge is connected but the server does not react)
any hint ?
 
Last edited:

derez

Expert
Licensed User
Longtime User
Yes. I can run the server by the bridge but it does not get calls.
Loading the browser page does not reach the public folder to load index.html.
 
Last edited:

derez

Expert
Licensed User
Longtime User
Even stranger than that - if I connect the cable in addition and then remove it - it works, but never starts without the cable.
It is probably a problem with the dongle's driver.
 
Top