A camera on a robot sends its pictures to the laptop. Here each frame is a JPEG, base64 encoded, in an MQTT message, and the example decodes it with CameraMessage and shows it with OpenCV. A line following robot with a Raspberry Pi camera streams a Raspberry Pi camera over HTTP instead, which suits a continuous stream better.
What you need
- bitsim built with MQTT and OpenCV; see Installing bitsim;
- an MQTT broker on localhost:1883, such as Mosquitto;
- camera_feed.py in this example's directory, which needs Python, opencv-python and paho-mqtt, and a camera that OpenCV can open as device 0.
Running the example
mosquitto # the broker, if none is running
./mqtt_camera
python camera_feed.py # in another terminal
camera_feed.py publishes frames on the topic camera. The example shows each frame it receives in a window and waits for a key before the next one.
The code
#include "bitsim/bitsim_config.h"
#ifdef BITSIM_MQTT
#include <chrono>
#include <iostream>
#include <thread>
{
camera_subscriber.connect();
while (true)
{
auto message = camera_subscriber.poll(std::chrono::milliseconds(3000));
if (message.has_value())
{
if (reading.has_value())
{
auto img = reading.value().image;
if (img.empty())
{
std::cout << "Could not plot the received image: " << std::endl;
return 1;
}
cv::imshow("Image", img);
cv::waitKey(0);
}
}
std::this_thread::sleep_for(std::chrono::microseconds(200));
}
return 0;
}
#else
#include <iostream>
{
std::cerr << "This example requires MQTT and OpenCV to be enable. "
"Reconfigure bitsim with ENABLE_MQTT=ON and ENABLE_OPENCV=ON"
<< std::endl;
return 1;
}
#endif
Definition mqtt_subscriber.h:24
int main()
Definition discounted_returns.cpp:19
Definition dc_motor_backend_base.h:9
static std::optional< CameraMessage > parse(const std::string &base64jpeg)
Definition camera.cpp:12