B4J Tutorial [IoT] - Raspberry Pi CCTV with MQTT

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.
B4X:
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)
B4X:
Sub Timer1_Tick
   client.Publish2("image", Bit.InputStreamToBytes(File.OpenInput(File.DirApp, "temp.jpg")), 0, False)
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:
B4X:
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.
 

Attachments

Last edited:

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 ?
 

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.

B4X:
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:
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
 

Erel

Administrator
Staff member
Licensed User
Please start a new thread for this and post the relevant code.
 
Top