B4J Tutorial [IoT] - Raspberry Pi CCTV with MQTT

Discussion in 'B4J Tutorials' started by Erel, Dec 21, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Simple solution that uses an embedded MQTT broker with a MQTT client to send the camera output to any connected client. The camera used is the standard RPi camera module.

    The program starts raspistill if it isn't already running. It configures it to take a shot every second.
    A timer sends the image every second.
    Code:
    Dim shl As Shell
    shl.Initialize(
    "shl""raspistill"Array("-o""temp.jpg""-t""0", _
       
    "-tl""1000""-w""640""-h""480""-q""75"))
    shl.Run(-
    1)
    Code:
    Sub Timer1_Tick
       client.Publish2(
    "image"Bit.InputStreamToBytes(File.OpenInput(File.DirApp, "temp.jpg")), 0False)
    End Sub
    Building a client that displays the image is trivial:

    upload_2015-12-21_17-45-55.png

    In this example I've implemented it with B4A.

    Note that raspistill will be left running if you kill the process.

    You can kill it with:
    Code:
    ps -a |grep raspistill
    kill <pid>
    You may need to update raspistill for it to work properly. I've tested it with v1.3.8.
     

    Attached Files:

    Last edited: Dec 21, 2015
  2. derez

    derez Expert Licensed User

    Thank you Erel !
    I would like to put the picture taken on an HTML page instead of b4a app.
    How can I add the image to an index.html file that I have (with radio switches and submit button) , working with Web socket ?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. derez

    derez Expert Licensed User

    Moved to a new thread
     
    Last edited: Dec 27, 2015
  5. Erel

    Erel Administrator Staff Member Licensed User

    @derez please start a new thread for this.
     
  6. derez

    derez Expert Licensed User

    It is possible to use a webcam instead of the raspberry camera : https://www.raspberrypi.org/documentation/usage/webcams/
    I use it with a timer and get a new picture every second or two. The RPi as server can send the picture per requests from users.

    Code:
    Sub tmr_tick
        
    Log("picture taken  " & DateTime.time(DateTime.now))
        shl.Initialize(
    "shl""fswebcam"Array("-r","640x480","--no-banner""temp.jpg"))
        shl.Run(-
    1)
    End Sub
    I have found that I need to init the shell every cycle ...(?)
     
    Last edited: Aug 17, 2016
  7. Erel

    Erel Administrator Staff Member Licensed User

    It probably ignores the option to take a screenshot every second.
     
  8. help.I have this error
    Waiting for debugger to connect...
    Program started.
    Error occurred on line: 35 (Main)
    java.io.IOException: Cannot run program "ps" (in directory "."): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at java.lang.Runtime.exec(Runtime.java:620)
    at org.apache.commons.exec.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:61)
    at org.apache.commons.exec.DefaultExecutor.launch(DefaultExecutor.java:279)
    at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:336)
    at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
    at anywheresoftware.b4j.objects.Shell.RunSynchronous(Shell.java:144)
    at b4j.example.main._processisrunning(main.java:121)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:614)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
    at b4j.example.main.main(main.java:29)
    Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
    at java.lang.ProcessImpl.start(ProcessImpl.java:137)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    ... 22 more
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this and post the relevant code.
     
  10. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    Instead of using an RPI, could I use my phone's camera? and how would it be?
    Thank you .
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Yes, but this is not the correct place for this question.
     
    Johan Hormaza likes this.
Loading...