
Terug naar projecten
ReactPythonFlaskRaspberry PiLED
Automatische ARGB LED-strip Controller
Realtime besturing van ARGB LED-strips via een Python Flask API en React-interface op een Raspberry Pi.
Projectoverzicht
Begin 2024 ontwikkelde ik een systeem om ARGB LED-strips in realtime aan te sturen.
De controller combineert hardware, back-end en front-end tot één vloeiend werkend geheel:
- Raspberry Pi 4 als controller en API-host
- Python Flask API voor kleurcommando’s
- React-webapp voor live bediening
- Individueel adresseerbare ARGB-strips voor unieke lichteffecten
Technologie & Architectuur
- Raspberry Pi met de `board`- en `neopixel`-packages
- Communicatie via I2C naar de ARGB LED-controller
- Tot 400 individueel adresseerbare LEDs per strip
Codevoorbeelden
1// React: verzend kleuren naar de API
2async setLedColor(ledIndex, color) {
3 const apiUrl = this.getApiUrl(ledIndex);
4 color["synced"] = this.synced;
5 return new Promise((resolve, reject) => {
6 fetch(`${apiUrl}/set_color/${ledIndex}`, {
7 headers: {
8 Accept: "application/json",
9 "Content-Type": "application/json",
10 },
11 method: "POST",
12 body: JSON.stringify(color),
13 })
14 .then((response) => response.json())
15 .then((response) => {
16 resolve(response.message);
17 })
18 .catch((error) => {
19 console.error("Error:", error);
20 resolve(null);
21 });
22 });
23}1# Flask API endpoint
2@api_bp.route('/api/set_color/<int:light_id>', methods=['POST'])
3def set_color(light_id):
4 if light_id < len(all_led_strips):
5 data = request.get_json()
6 r, g, b = data['red'], data['green'], data['blue']
7 if not (0 <= r <= 255 and 0 <= g <= 255 and 0 <= b <= 255):
8 return jsonify({'message': 'Invalid color'}), 400
9 if data.get("synced"):
10 synced = data["synced"]
11 else:
12 synced = False
13
14 all_led_strips[light_id].set_color(r, g, b, synced)
15 return jsonify({'message': 'Color set successfully'}), 200
16 return jsonify({'message': 'LED not found'}), 404