Arduino
Sensor Data
Apache Kafka
Server Communication
Data Streaming

Push sensor data from arduino to apache kafka server directly.

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Pushing sensor data from an Arduino to an Apache Kafka server can enable powerful real-time data processing and integration capabilities within IoT applications. This setup involves capturing data from sensors connected to an Arduino, and then pushing this data directly into a Kafka topic. Here's how you can achieve this, including technical details and examples.

Understanding the Components

Arduino: A popular open-source electronics platform based on easy-to-use hardware and software. It's great for creating devices that can sense and control the physical world.

Apache Kafka: A distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log.

Requirements

  • Arduino Board: Any Arduino with networking capabilities (e.g., Arduino Uno with an Ethernet shield, Arduino MKR1000, or similar).
  • Apache Kafka: Set up and running either locally or on a server.
  • Sensor: Any basic sensor compatible with Arduino (temperature, humidity, etc.).

Setting up the Arduino

Connect your sensor to the Arduino as per the sensor's datasheet. For simplicity, assume it's a temperature sensor connected via an analog pin.

Sample Arduino Code

Upload the following example code to the Arduino. It reads data from the sensor and sends it over a network.

cpp
1#include <SPI.h>
2#include <Ethernet.h>
3
4// Ethernet and Kafka configuration
5byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
6char server[] = "192.168.1.100"; // Kafka server IP
7int port = 9092; // Kafka server port
8
9EthernetClient client;
10
11void setup() {
12  Ethernet.begin(mac);
13  Serial.begin(9600);
14
15  while (!client.connect(server, port)) {
16    Serial.println("Connection failed, retrying...");
17    delay(5000);
18  }
19}
20
21void loop() {
22  int sensorValue = analogRead(A0);
23  float voltage = sensorValue * (5.0 / 1023.0);
24  float temperature = (voltage - 0.5) * 100;
25
26  // Create a simple JSON payload
27  String payload = "{\"temperature\": " + String(temperature, 2) + "}";
28  
29  if(client.connected()) {
30    client.println(payload);
31  } else {
32    Serial.println("Lost connection, reconnecting...");
33    client.stop();
34    client.connect(server, port);
35  }
36
37  delay(1000); // Publish every second
38}

Setting Up Apache Kafka

Ensure your Kafka broker is set up with a topic named for instance sensor-data.

Kafka Producer Configuration

While Arduino does not support Kafka client libraries due to resource constraints, you can use a simple TCP connection to send data to a Kafka broker by wrapping your payloads in Kafka’s protocol. However, in practice, this is complex and not recommended for beginners.

Instead, consider using a small intermediary service, typically running on a Raspberry Pi or a server, which listens to simple TCP payloads from Arduino and forwards them to Kafka:

python
1import socket
2from kafka import KafkaProducer
3
4producer = KafkaProducer(bootstrap_servers='localhost:9092')
5
6server_socket = socket.socket()
7server_socket.bind(('0.0.0.0', 9093))
8server_socket.listen()
9
10while True:
11    client_socket, addr = server_socket.accept()
12    data = client_socket.recv(1024).decode()
13    producer.send('sensor-data', data.encode())
14    client_socket.close()

This Python script acts as a bridge by receiving sensor data from Arduino and publishing it to the Kafka topic sensor-data.

Integrating and Scaling

This setup can be expanded by integrating more sensors and Arduinos, offering scalability and centralized data management. Data fed into Kafka can then be processed with systems like Apache Flink, Spark, or processed with machine learning algorithms for advanced analytics.

Summary Table

ComponentRoleDetails
ArduinoData CollectionReads sensor data and sends over TCP.
SensorData SourceProvides real-time physical measurements.
Apache KafkaData Streaming and ProcessingManages real-time data ingestion and processing.
Python ScriptIntermediary for Data TransmissionReceives data from Arduino and forwards it to Kafka.

Final Insights

The described setup allows you to push sensor data from Arduino to Apache Kafka, facilitating sophisticated data processing pipelines in IoT applications. It achieves a balance between simplicity and functionality, ensuring scalability and efficient data handling.


Course illustration
Course illustration

All Rights Reserved.