B4J Question [Solved] RPi connect to a different wifi network

Mark Read

Well-Known Member
Licensed User
Longtime User
I have probably overseen something simple, if so forgive my question.

I am using a Rpi which is connected via bridge in a wifi network to develop my program. In my b4j program I need to change the wifi network and connect to a different wifi containing a mqtt broker.

My development network is 192.168.1.x and my mqtt network is 192.168.4.x.

My question: how to change the wifi network within b4j? When the program is finished, I reconnect to the original wifi network.

Thank you.
 

MarkusR

Well-Known Member
Licensed User
Longtime User
can u change the subnet mask to 255.255.0.0 ?
what is the role of your rpi?
i used the mosquitto mqtt broker direct at my rpi. my rpi have more the role of a server.
the mqtt broker can accessed by dyndns name from web :)
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Hello MarkusR.

can u change the subnet mask to 255.255.0.0 ?
Could do but it will not help.

i used the mosquitto mqtt broker direct at my rpi. my rpi have more the role of a server.
Not tried this but I know about it.

the mqtt broker can accessed by dyndns name from web
Internet is not available!

I am designing a system at work (development network) for use in the field (mqtt network). In the field, there is no network of any kind, so I am using an Node MCU in AP mode, running the µBroker library. So the system looks like this.

Node MCU - Wifi AP + MQTT Broker
Arduino 1 for Motor control
Arduino 2 for motor control
Arduino 3 for motor control and probably HC-SR04
Arduino 4 for custom industry measurement system (eg. wind speed and direction).
Rpi basically for the camera as the arduino camera is not very good. I need pictures as quick as possible.
Control system: either Windows + B4J or Android tablet (have both finished).

Why so many arduino's? The whole system is like a 3d printer but 6m wide and 30m long. I am trying to use the minumum of cables, at least keep them very short, as the environment is harsh - hot and dusty. I am also limited to the range of my intranet, as it should not disturb the surrounding equipment.

Everything is complete, I only need the RPi to finish.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
I have a solution!

At work, I have both networks available, so I connected to each one. This gives me the required info in the file /etc/wpa_supplicant/wpa_supplicant.conf on the rpi of course.

This file now looks like this for example.

B4X:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=GB

network={
    ssid="Rover AP"
    priority=1
    key_mgmt=NONE
}

network={
    ssid="VTTeck"
    psk="the password"
    priority=2
    key_mgmt=WPA-PSK
}

I thought that using the priority might work but it did not. However, the current network can be changed using the command:

B4X:
sudo wpa_cli select_network 0

for the first network and

B4X:
sudo wpa_cli select_network 1

for the second network. I have tried this in a terminal window and it works fine. Next step is to try it with shell. I think this should work:

B4X:
Private shl as Shell
shl.Initialize("shl", "sudo wpa_cli select_network 0", Null)
shl.Run(-1)

Will try if I get chance. Please correct me if I am wrong.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Sorry but I need a little help here. I have tried this code:

B4X:
Private shl As Shell
    shl.Initialize("shl", "sudo wpa_cli select_network 1", Null)
    shl.WorkingDirectory = "/home/pi/"
    shl.Run(-1)

But I have an error:

Error: org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "sudo wpa_cli select_network 1" (in directory "/home/pi"): error=2, No such file or directory)

If I try the command directly on the rpi in terminal it works.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
can u try to start a executable .sh script in Initialize?
in this script the row "sudo wpa_cli select_network 1"

your app have access to this folder?
 
Last edited:
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Yes! Created two files, wifi0.sh and wifi1.sh. Each contain a single line "sudo wpa_cli select_network x" and used this to check.

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private timer1 As Timer
    Private wifistate As Boolean=False
    Private counter As Int=0
End Sub

Sub AppStart (Args() As String)
    timer1.Initialize("timer1", 15000)
    timer1.Enabled=True
    StartMessageLoop
End Sub

Sub timer1_Tick
    counter=counter+1
    If wifistate=False Then
        Wifi0
        wifistate=Not(wifistate)
    Else
        Wifi1
        wifistate=Not(wifistate)
    End If
    If counter=2 Then
        ExitApplication
    End If
End Sub

Sub Wifi0
    Log("Switch to wifi0")
    Private shl As Shell
    shl.Initialize("shl", "./wifi0.sh", Null)
    shl.WorkingDirectory = "/home/pi/"
    shl.Run(-1)
End Sub

Sub Wifi1
    Log("Switch to wifi1")
    Private shl As Shell
    shl.Initialize("shl", "./wifi1.sh", Null)
    shl.WorkingDirectory = "/home/pi/"
    shl.Run(-1)
End Sub

Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
   If Success And ExitCode = 0 Then
     Log("Success")
     Log(StdOut)
   Else
     Log("Error: " & StdErr)
   End If
   'ExitApplication
End Sub

Works great, although 15 seconds is a little too quick to change.

Many thanks for the idea. Cheers.
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
B4X:
    shl.Initialize("shl", "sudo wpa_cli select_network 1", Null)

I think you should do it like this:

B4X:
shl.Initialize("shl", "sudo", Array as string("wpa_cli","select_network","1")

But using a shell file is better.

And how do you solve the sudo command? It needs you to input a password.

I have done this using the command below:

B4X:
echo password | sudo -S shutdown -h +30
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
And how do you solve the sudo command? It needs you to input a password.

I didn't need a password, it worked.

shl.Initialize("shl", "sudo", Array as string("wpa_cli","select_network","1")

I tried this and it didn't work either! Only with the shell files. But thanks for the tip anyway.
 
Upvote 0
Top