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

Discussion in 'B4J Tutorials' started by Erel, Apr 17, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    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:

    Code:
    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:
    Code:
    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.

    [​IMG]

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

    Attached Files:

  2. rwblinn

    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.
     
  3. Erel

    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.
     
    joulongleu and jostr like this.
  4. monchanch

    monchanch Member Licensed User

    When i run the code i get errors. I have attached the error image of the same. What could be the matter?
    piface_error.png
     
    joulongleu likes this.
  5. Erel

    Erel Administrator Staff Member Licensed User

    Are you running it on a Pi board with PiFace extension?
     
    joulongleu likes this.
  6. monchanch

    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?
     
    joulongleu likes this.
  7. Erel

    Erel Administrator Staff Member Licensed User

    You can (and should) run it with B4J-Bridge. However this specific example requires the PiFace extension.
     
    joulongleu likes this.
  8. wenzelww

    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?
     
    joulongleu likes this.
  9. Erel

    Erel Administrator Staff Member Licensed User

    Are you running it on a Pi board with the PiFace extension?
     
  10. wenzelww

    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
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    404 means that the page is not found.

    Are there any error messages in the server logs?
     
  12. wenzelww

    wenzelww Member Licensed User

    i think you mean .log files in /home/pi/tempjars/logs
    there are no error messages in this files.
     
  13. wenzelww

    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.
     
    joulongleu likes this.
  14. wenzelww

    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"
     
    joulongleu likes this.
  15. Erel

    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.
     
    joulongleu likes this.
  16. wenzelww

    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:
    [​IMG] [​IMG]

    ping to raspberry pi is ok
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    The images are broken.
     
    joulongleu likes this.
  18. wenzelww

    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!
     
    joulongleu likes this.
  19. Mark Turney

    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.... :)
     
    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice