B4J Tutorial [WebApp][IoT] PiFace (Raspberry Pi) Server Example

Erel

Administrator
Staff member
Licensed User


This example demonstrates how a Raspberry Pi board can be controlled from the browser.

It tracks the switches state and allows the user to turn on and off the 8 leds.

The toggle buttons are based on this jQuery plugin: http://simontabor.com/labs/toggles/

The server code is quite simple. All the active connections are stored in a Map. This map is used to notify all connections about the switches events:

B4X:
Sub HandleSwitch(index As Int, State As Boolean)
   'notify all active connections about the switch state change.
   For Each wsm As WebSocketModule In connections.Keys
     CallSubDelayed3(wsm, "SwitchStateChanged", index, State)
   Next
End Sub
The leds are toggled with this code:
B4X:
Public Sub Led_Toggle(Params As Map)
   Dim id As String = Params.Get("id") 'id will be ledx
   Dim active As Boolean = Params.Get("active")
   Main.pface.GetLED(id.SubString(3)).State = active
End Sub
You should use B4J-Bridge to connect to the Raspberry Pi during development. Note that the www folder is not copied automatically during deployments. You need to copy the files whenever a static file is changed.



See this link for more information about jPi4J library: http://www.basic4ppc.com/android/forum/threads/jpi4j-raspberry-pi-gpio-controller.37493/#content
 

Attachments

rwblinn

Well-Known Member
Licensed User
Hi Erel,

thanks for sharing - very good = tested and works fine.

Q: Instead of accessing via a Browser, do you have an example of accessing via clients written in B4A and B4J?

Appreciated.
 

Erel

Administrator
Staff member
Licensed User
This example is built with the WebApp framework.

You can use the new WebSocket client library to create a similar solution with B4A. Another option is to add standard server handlers and communicate with HttpUtils2.

Why do you need to create a native solution? This solution is cross platform. You can change the UI to better fit mobile devices if you like.
 

monchanch

Member
Licensed User
Hello Erel, I am running it from my laptop. I have connected the b4j bridge between my laptop and raspberry pi. I guess i am missing out some things...PiFace Extension?
 

Erel

Administrator
Staff member
Licensed User
You can (and should) run it with B4J-Bridge. However this specific example requires the PiFace extension.
 

wenzelww

Member
Licensed User
Hello Erel, I tried this example but I could not get it to work stable. e.g. I connect with Android Phone and it works, but get the message "No Data available" when I try to access with Laptop. Sometimes it also works with the Laptop.
Do you have an idea what could be wrong?
 

Erel

Administrator
Staff member
Licensed User
Are you running it on a Pi board with the PiFace extension?
 

wenzelww

Member
Licensed User
Yes, Version B with PiFAce. And it works, but not always. Today it does not work at all. I get HTTP Error 404
 

Erel

Administrator
Staff member
Licensed User
404 means that the page is not found.

Are there any error messages in the server logs?
 

wenzelww

Member
Licensed User
after starting the program with B4JBridge, everything works fine, also when i later started the .jar directly.
Sorry i have no clue what happened.
 

wenzelww

Member
Licensed User
now i have the case that it works over android phone, but get error 404 when I access with laptop in same network (over private IP). The entries in the log file:
192.168.2.105 - - [12/Jan/2015:19:03:08 +0000] "GET /ws HTTP/1.1" 101 - "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
192.168.2.105 - - [12/Jan/2015:19:03:10 +0000] "GET /favicon.ico HTTP/1.1" 404 289 "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
192.168.2.105 - - [12/Jan/2015:19:03:28 +0000] "GET /favicon.ico HTTP/1.1" 404 289 "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
 

Erel

Administrator
Staff member
Licensed User
These 404 errors are not important. The browser is making additional calls to get the site icon and these calls fail.
 

wenzelww

Member
Licensed User
i have again the same situation: it works over android phone, but get error 404 when I access with laptop in same network (over private IP). This how it looks like:


ping to raspberry pi is ok
 

wenzelww

Member
Licensed User
meanwhile the problem could be solved. PiFaceSever runs for several days without errors. It was not an issue of the PiFaceSever software, but configuration and powermanagement (sleepmode) of wlan USB stick.
Thank's Erel for suppport!
 

Mark Turney

Active Member
Licensed User
BTW - this example is demo'd in the 7th B4J video tutorial to be released soon. See the B4J Video Tutorial thread.... :)
 
Top