IoT and Event Streaming using Python-Flask with Apache Kafka

Leonardo Gamboa Uribe
2 min readMay 10, 2020

--

In this interesting article, we can experiment how to send data from a browser to an IoT device, in this case a robotic arm, with 4 servos.
The communication will be done through the Kafka broker, which allows us to write very quickly in a queue, using the PRODUCER module, and then read that queue, on demand, with the CONSUMER module.

Important. In order to carry out this activity, it is necessary to review the previous articles in my forum, because it is necessary to have installed and configured KAFKA on the RaspBerry Pi (3 or 4).

If we only want to do proof of concept, without having to mount the robot arm and the raspberry, we can do it all in the same machine linux. (ubuntu).

Install the kafka-python library. https://github.com/dpkp/kafka-python

In the following scheme we can identify the parts of the integration.

  1. WEB HTML 5. The Slider Control module. That allows in a simple way to send parameters (axis X, Y, Z, B; base, arm 1, arm 2, clamp). to the robotic arm. This html module is developed in HTML 5. (https://github.com/novagenio/proyectos_aws/blob/main/parteweb/html/slider/slider_post_ws_kfk.html).
  2. PRODUCER. This module is a “flask” web service that receives the parameters from the html (1), via POST. It takes these parameters and sends them to the Kafka topic. (https://github.com/novagenio/proyectos_aws/blob/main/parteweb/html/slider/slider_ws_kfk_01.py)

3. CONSUMER. This module is very simple. Read the KAFKA queue. We have two options.

The code consumer_kfk_test.py, only displays the messages on the screen.

The code consumer_kfk_16.py, work with the PCA9685 (Servo Driver-I2C PWM 16-channel interface). Using the Adafruit ServoKit library, to control the servos.

Here I put the scheme if you want to mount it completely on the raspberry.

Install ServoKit library https://pypi.org/project/adafruit-circuitpython-servokit/

Important: Is necessary activate the I2C interface in Raspberry configuration options.

--

--

Leonardo Gamboa Uribe
Leonardo Gamboa Uribe

Written by Leonardo Gamboa Uribe

PhD in AI student, Master's degree in Artificial Intelligence, Computer Science Engineering, Digital Transformation Expert

No responses yet