Skip to content

Commit

Permalink
better mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
JAlcocerT committed Feb 17, 2024
1 parent 5e3b242 commit 307943f
Show file tree
Hide file tree
Showing 16 changed files with 365 additions and 60 deletions.
19 changes: 19 additions & 0 deletions Z_MQTT/Emqx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM erlang:latest

# Set working directory
WORKDIR /app

# Copy your Erlang application code into the container
COPY . /app

# Install any additional dependencies or packages if needed
#RUN apt-get update && apt-get install -y <package-name>

# Run your Erlang application
CMD ["erl"]


#docker build -t my-erlang-app .

#docker run -it my-erlang-app
#erl -version
22 changes: 22 additions & 0 deletions Z_MQTT/Emqx/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@



```Dockerfile
FROM erlang:latest

# Set working directory
WORKDIR /app

# Copy your Erlang application code into the container
COPY . /app

# Install any additional dependencies or packages if needed
RUN apt-get update && apt-get install -y <package-name>

# Run your Erlang application
CMD ["erl"]
```

```sh
docker build -t my-erlang-app .
```
45 changes: 45 additions & 0 deletions Z_MQTT/Python/DHT_to_MQTT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import adafruit_dht
import time
import os
import paho.mqtt.client as mqtt

# Set default sensor type to DHT22
dht_sensor_type = os.environ.get('DHT_SENSOR_TYPE', 'DHT22')

if dht_sensor_type == 'DHT11':
DHT_SENSOR = Adafruit_DHT.DHT11
elif dht_sensor_type == 'DHT22':
DHT_SENSOR = Adafruit_DHT.DHT22
else:
print(f"Invalid DHT sensor type '{dht_sensor_type}'. Defaulting to DHT22.")
DHT_SENSOR = Adafruit_DHT.DHT22

DHT_PIN = int(os.environ.get('DHT_PIN', '4'))

# Configure MQTT connection parameters
mqtt_broker = os.environ.get('MQTT_BROKER', '192.168.3.200')
mqtt_port = int(os.environ.get('MQTT_PORT', '1883'))
mqtt_topic_temp = os.environ.get('MQTT_TOPIC_TEMP', 'sensor/temperature')
mqtt_topic_hum = os.environ.get('MQTT_TOPIC_HUM', 'sensor/humidity')
mqtt_username = os.environ.get('MQTT_USERNAME', '')
mqtt_password = os.environ.get('MQTT_PASSWORD', '')

# Initialize MQTT client and connect to the broker
client = mqtt.Client()
if mqtt_username and mqtt_password:
client.username_pw_set(mqtt_username, mqtt_password)
client.connect(mqtt_broker, mqtt_port, 60)

while True:
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
if humidity is not None and temperature is not None:
# Publish temperature and humidity to their respective topics
client.publish(mqtt_topic_temp, '{:.2f}'.format(temperature))
client.publish(mqtt_topic_hum, '{:.2f}'.format(humidity))
print("Temperature sent to MQTT topic: {}".format(mqtt_topic_temp))
print("Humidity sent to MQTT topic: {}".format(mqtt_topic_hum))
else:
print("Sensor failure. Check wiring.")
time.sleep(5)

#python3 DHT_to_MQTT.py
55 changes: 55 additions & 0 deletions Z_MQTT/Python/MQTT_to_Streamlit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import streamlit as st
import paho.mqtt.client as mqtt
import time

# Define the MQTT client
client = mqtt.Client()

# MQTT Settings
MQTT_BROKER = "192.168.3.200" # Example broker, replace with your broker's address
MQTT_PORT = 1883
MQTT_TOPIC = "python/mqtt"

# Callback when connecting to the MQTT broker
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)

# Callback when receiving a message from the MQTT broker
def on_message(client, userdata, msg):
message = msg.payload.decode()
st.session_state['last_message'] = message
print(f"Received `{message}` from `{msg.topic}` topic")

client.on_connect = on_connect
client.on_message = on_message

client.connect(MQTT_BROKER, MQTT_PORT, 60)

# Subscribe to the topic
client.subscribe(MQTT_TOPIC)

# Start the loop in another thread
client.loop_start()

# Streamlit app
st.title('MQTT Streamlit Real-time Data Viewer')

# Initialize session state
if 'last_message' not in st.session_state:
st.session_state['last_message'] = "Waiting for data..."

# Display the last message
st.write(f"Last message: {st.session_state['last_message']}")

# Use a button to update messages manually (for demonstration)
if st.button('Update'):
st.write(f"Last message: {st.session_state['last_message']}")

# Stop the loop before exiting
st.stop()
client.loop_stop()

#python3 MQTT_to_Streamlit.py
2 changes: 1 addition & 1 deletion Z_MQTT/Python/Python_push_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
print(f"Message Published: {message}")

# Wait for 1 second
time.sleep(0.1)
time.sleep(0.5)

#python3 Python_push_distribution.py
6 changes: 6 additions & 0 deletions Z_MQTT/Python/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

* <https://pypi.org/project/paho-mqtt/>

```sh
pip install paho-mqtt
```
Empty file added Z_SelfHosting/Grafana/readme.md
Empty file.
17 changes: 17 additions & 0 deletions Z_SelfHosting/Home_Assistant/Docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: "2.1"
services:
homeassistant:
image: lscr.io/linuxserver/homeassistant:latest
container_name: homeassistant
#network_mode: host
environment:
- PUID=1000
- PGID=1000
- TZ=Europe/Rome
volumes:
- ~/Docker/HomeAssistant:/config
ports:
- 8123:8123 #optional
#devices:
# - /path/to/device:/path/to/device #optional
restart: unless-stopped
3 changes: 3 additions & 0 deletions Z_SelfHosting/Home_Assistant/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

* <https://jalcocert.github.io/RPi/posts/rpi-iot-dht11-influxdb/#integrating-home-assistant-with-influxdb>
* <https://jalcocert.github.io/RPi/posts/rpi-mqtt/>
Empty file added Z_SelfHosting/Readme.md
Empty file.
27 changes: 27 additions & 0 deletions Z_SelfHosting/Redash/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

* <https://github.com/getredash/redash>
* <https://redash.io/help/data-sources/querying/supported-data-sources>

* <https://hub.docker.com/r/redash/redash/tags?page=1>


https://github.com/getredash/redash


```sh
# git clone https://github.com/getredash/redash
# cd redash
# cd setup

git clone https://github.com/getredash/setup
cd setup
chmod +x setup.sh
./setup.sh
```

* <https://raw.githubusercontent.com/getredash/setup/master/setup.sh>
* <https://github.com/getredash/setup/blob/master/data/docker-compose.yml>

`localhost:5000`

https://redash.io/help/user-guide/visualizations
2 changes: 2 additions & 0 deletions Z_SelfHosting/Superset/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Presto

2 changes: 1 addition & 1 deletion _posts/2021-07-21-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ You can also use it with the Cloud:
| **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 |

* **Others**: Grafana, Redash, Node-Red
* **Others**: Grafana, Redash, Node-Red, JS ([Epoch](https://epochjs.github.io/epoch/real-time/), [Plotly](https://plotly.com/javascript/streaming/), [chartjs](https://nagix.github.io/chartjs-plugin-streaming/1.9.0/))

<!--
![img-description](https://pbs.twimg.com/media/FJAFshwXoAEf9HV?format=jpg&name=large)
Expand Down
Loading

0 comments on commit 307943f

Please sign in to comment.