From 5e3b242723aa50bfc0d0ac4dcf4cd6cf6fb27ba3 Mon Sep 17 00:00:00 2001 From: Jesus Alcocer Tagua Date: Sat, 17 Feb 2024 13:18:23 +0100 Subject: [PATCH] adding mqtt info + nodered docker --- Z_MQTT/C/mqtt_publish_server.c | 50 ++++++ Z_MQTT/C/readme.md | 14 ++ Z_MQTT/Node-Red/Docker-compose.yml | 55 +++++++ Z_MQTT/Python/Python_push_distribution.py | 30 ++++ Z_MQTT/Readme.md | 0 _posts/2021-07-21-getting-started.md | 6 +- _posts/2024-02-15-rpi-mqtt.md | 184 +++++++++++++++++++++- _posts/2024-12-11-rpi-gps-superset.md | 1 + 8 files changed, 332 insertions(+), 8 deletions(-) create mode 100644 Z_MQTT/C/mqtt_publish_server.c create mode 100644 Z_MQTT/C/readme.md create mode 100644 Z_MQTT/Node-Red/Docker-compose.yml create mode 100644 Z_MQTT/Python/Python_push_distribution.py create mode 100644 Z_MQTT/Readme.md diff --git a/Z_MQTT/C/mqtt_publish_server.c b/Z_MQTT/C/mqtt_publish_server.c new file mode 100644 index 0000000..d67f390 --- /dev/null +++ b/Z_MQTT/C/mqtt_publish_server.c @@ -0,0 +1,50 @@ +#include +#include +#include "MQTTClient.h" + +#define ADDRESS "tcp://192.168.3.200:1883" +#define CLIENTID "ExampleClientPub" +#define TOPIC "c/mqtt" +#define PAYLOAD "Hello from C!" +#define QOS 1 +#define TIMEOUT 10000L + +// int main(int argc, char* argv[]) { +// // MQTTClient declaration and other code for connecting, publishing, and disconnecting +// } + +int main(int argc, char* argv[]) { + MQTTClient client; + MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; + MQTTClient_message pubmsg = MQTTClient_message_initializer; + MQTTClient_deliveryToken token; + int rc; + + // Initialize the client + MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL); + conn_opts.keepAliveInterval = 20; + conn_opts.cleansession = 1; + + // Connect to the MQTT broker + if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) { + printf("Failed to connect, return code %d\n", rc); + exit(-1); + } + + // Prepare and publish the message + pubmsg.payload = PAYLOAD; + pubmsg.payloadlen = strlen(PAYLOAD); + pubmsg.qos = QOS; + pubmsg.retained = 0; + MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token); + printf("Waiting for up to %ld seconds for publication of %s\n" + "on topic %s for client with ClientID: %s\n", + TIMEOUT / 1000, PAYLOAD, TOPIC, CLIENTID); + rc = MQTTClient_waitForCompletion(client, token, TIMEOUT); + printf("Message with delivery token %d delivered\n", token); + + // Disconnect + MQTTClient_disconnect(client, 10000); + MQTTClient_destroy(&client); + return rc; +} diff --git a/Z_MQTT/C/readme.md b/Z_MQTT/C/readme.md new file mode 100644 index 0000000..c5965d3 --- /dev/null +++ b/Z_MQTT/C/readme.md @@ -0,0 +1,14 @@ + +```sh +sudo apt-get update +#sudo apt-get install libpaho-mqtt-dev +sudo apt-get install build-essential git cmake + + + +gcc -o mqtt_publish mqtt_publish.c -lpaho-mqtt3c +./mqtt_publish +#gcc -o mqtt_publish_server mqtt_publish_server.c -lpaho-mqtt3c +#./mqtt_publish_server + +``` \ No newline at end of file diff --git a/Z_MQTT/Node-Red/Docker-compose.yml b/Z_MQTT/Node-Red/Docker-compose.yml new file mode 100644 index 0000000..e84d311 --- /dev/null +++ b/Z_MQTT/Node-Red/Docker-compose.yml @@ -0,0 +1,55 @@ +version: '3' +services: + node-red: + image: nodered/node-red + ports: + - "1880:1880" + volumes: + - myNodeREDdata:/data + depends_on: + - mqtt-broker + + mqtt-broker: + image: emqx/emqx:5.0.0 + ports: + - "1883:1883" + - "8083:8083" + - "8084:8084" + - "8883:8883" + - "18083:18083" + +volumes: + myNodeREDdata: + +#sudo docker-compose up -d + +#docker run -it -p 1880:1880 -v myNodeREDdata:/data --name mynodered nodered/node-red + +################################# +# version: '3' +# services: +# node-red: +# image: nodered/node-red +# ports: +# - "1880:1880" +# environment: +# - TZ=UTC # Optional: Set timezone if needed +# volumes: +# - ./data:/data # Optional: Mount a volume to persist data + +# version: '3' +# services: +# node-red: +# image: nodered/node-red +# ports: +# - "1880:1880" +# environment: +# - TZ=UTC # Optional: Set timezone if needed +# volumes: +# - ./data:/data # Optional: Mount a volume to persist data + + # mqtt-broker: + # image: emqx/emqx + # ports: + # - "1883:1883" + diff --git a/Z_MQTT/Python/Python_push_distribution.py b/Z_MQTT/Python/Python_push_distribution.py new file mode 100644 index 0000000..ae7e650 --- /dev/null +++ b/Z_MQTT/Python/Python_push_distribution.py @@ -0,0 +1,30 @@ +import time +import random +import math +import paho.mqtt.publish as publish + +# MQTT Broker (EMQX) details +#broker_address = "broker.emqx.io" #local broker +broker_address = "192.168.3.200" #local network broker +port = 1883 +topic = "python/mqtt" + +while True: + # Generate a random value based on normal distribution + mean = 25 # Mean of the distribution + std_dev = 10 # Standard deviation of the distribution + value = random.normalvariate(mean, std_dev) + value = max(0, min(50, value)) # Ensure value is within [0, 50] range + + # Message to publish + message = str(value) + + # Publish the message + publish.single(topic, message, hostname=broker_address, port=port) + + print(f"Message Published: {message}") + + # Wait for 1 second + time.sleep(0.1) + +#python3 Python_push_distribution.py \ No newline at end of file diff --git a/Z_MQTT/Readme.md b/Z_MQTT/Readme.md new file mode 100644 index 0000000..e69de29 diff --git a/_posts/2021-07-21-getting-started.md b/_posts/2021-07-21-getting-started.md index 3d9b2ba..7669d08 100644 --- a/_posts/2021-07-21-getting-started.md +++ b/_posts/2021-07-21-getting-started.md @@ -101,12 +101,10 @@ You can also use it with the Cloud: | **Apache Superset** | Yes | Open-source data visualization and data exploration platform. Supports SQL querying. Customizable and extensible | Requires technical knowledge for setup and customization. May have performance issues with very large datasets | | **Kibana** | Yes | Part of the Elastic Stack, excellent for visualizing Elasticsearch data. Great for log and time-series analytics. Real-time data visualization | Primarily tailored to Elasticsearch data. Can be complex to configure and optimize. Less versatile for non-Elasticsearch data | | **KNIME** | Yes | User-friendly, visual data pipeline design. Extensive plugin ecosystem. Good for non-programmers. Strong in data preprocessing and analysis | Can be less intuitive for complex, custom data analysis. Performance issues with very large datasets | -| **Tableau** | No | Exceptional data visualization capabilities. Intuitive and user-friendly. Strong in business intelligence | Expensive. Not open source. More focused on visualization than data modeling | | **Python Libraries** (e.g., pandas, scikit-learn) | Yes | Highly flexible and powerful. Huge ecosystem and community. Ideal for custom, complex analysis | Requires programming knowledge. Steeper learning curve for non-programmers | | **R Libraries** (e.g., ggplot2, dplyr) | Yes | Excellent for statistical analysis and data visualization. Large number of packages for various analyses. Strong academic and research community support | Requires programming knowledge. Less intuitive for those unfamiliar with R | -| **Alteryx** | No | Strong in data blending and preparation. Advanced analytics capabilities. Good integration with other tools | Expensive. Not open source. Steeper learning curve | -| **RapidMiner** | No | Comprehensive data science platform. Good for machine learning and predictive modeling. User-friendly with a visual approach | Free version is limited. Can be expensive for the full version. Steep learning curve for advanced features | -| **QlikView/Qlik Sense** | No | Powerful for interactive data discovery and BI. Flexible and customizable. Good data integration | Can be expensive. Steeper learning curve compared to some competitors. Not open source | + +* **Others**: Grafana, Redash, Node-Red