From 982680a1c3cd3d0818d87b2c22754d20159c8bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Mon, 12 Feb 2024 21:55:08 +0100 Subject: [PATCH] added mosquitto setup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ebbe Baß --- README.md | 25 ++++++++++++++++++++++++- client/main.py | 1 + server/app.py | 12 ++++++++++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 731148a..2ef66fc 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ For the professionals out there, most of you will already know how to pixel cont ### Commands to install all the required packages: ``` -sudo apt update -y && sudo apt upgrade -y && sudo apt install python3 python3-pip git python3-flask apache2 php mariadb-server mariadb-client ola ola-python dnsmasq hostapd rfkill -y +sudo apt update -y && sudo apt upgrade -y && sudo apt install python3 python3-pip git python3-flask apache2 php mariadb-server mariadb-client ola ola-python dnsmasq hostapd rfkill mosquitto mosquitto-clients -y sudo rm /usr/lib/python3.11/EXTERNALLY-MANAGED pip3 install Flask pip3 install adafruit_circuitpython_neopixel @@ -236,6 +236,29 @@ exit; sudo systemctl restart apache2 ``` +### Setup a MQTT broker for the communication between the master and the tubes: + +Enable the service + +`sudo systemctl enable mosquitto.service` + +Now we are going to enable remote access to the broker. + +Edit the config file + +`sudo nano /etc/mosquitto/mosquitto.conf` + +Paste the following two lines in the config file: + +``` +listener 1883 +allow_anonymous true +``` + +Now save and exit and restart the services: + +`sudo systemctl restart mosquitto` + ### Clone the project to your Pi You can just clone the project itself into your home directory. diff --git a/client/main.py b/client/main.py index 077fe45..b6e944f 100644 --- a/client/main.py +++ b/client/main.py @@ -6,6 +6,7 @@ import requests import json import time from threading import Thread +import paho.mqtt.client as mqtt # Replace with your server's IP address and port SERVER_IP = '192.168.0.1' # Change to the actual IP of the PiXelTube Master diff --git a/server/app.py b/server/app.py index f9d3e70..0b918dc 100644 --- a/server/app.py +++ b/server/app.py @@ -1,6 +1,8 @@ from flask import Flask, render_template, request, jsonify import json import MySQLdb +import paho.mqtt.client as mqtt +import threading app = Flask(__name__) @@ -30,6 +32,8 @@ db = MySQLdb.connect( database=config['mysql']['database'], ) +mqttc = mqtt.Client("localhost", 1883, 60) + # Function to retrieve registered tubes from the database def get_tubes(): cur = db.cursor() @@ -79,8 +83,12 @@ def get_assigned_params(tube_unique_id): except Exception as e: return jsonify({'success': False, 'message': f'Error: {e}'}) -def main(): +def flask_api(): app.run(host='0.0.0.0', port=5000) +def mqtt_publisher(mqtt): + + if __name__ == "__main__": - main() + mqtt_publisher(mqtt) + flask_api()