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

SS-2014-04-17_10.51.25.png


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.

SS-2014-04-17_10.59.22.png


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

Attachments

  • PiFaceServer.zip
    6 KB · Views: 1,306

rwblinn

Well-Known Member
Licensed User
Longtime 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

B4X founder
Staff member
Licensed User
Longtime 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
Longtime 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?
 

wenzelww

Member
Licensed User
Longtime 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?
 

wenzelww

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

wenzelww

Member
Licensed User
Longtime User
i think you mean .log files in /home/pi/tempjars/logs
there are no error messages in this files.
 

wenzelww

Member
Licensed User
Longtime 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
Longtime 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"
 

wenzelww

Member
Licensed User
Longtime 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:
C:\DOKUME~1\WILHEL~1.N60\LOKALE~1\Temp\msohtml1\01\clip_image002.jpg
C:\DOKUME~1\WILHEL~1.N60\LOKALE~1\Temp\msohtml1\01\clip_image002.jpg


ping to raspberry pi is ok
 

wenzelww

Member
Licensed User
Longtime 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
Longtime User
BTW - this example is demo'd in the 7th B4J video tutorial to be released soon. See the B4J Video Tutorial thread.... :)
 
Top