-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/JAlcocerT/RPi
- Loading branch information
Showing
32 changed files
with
754 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.5) | ||
|
||
#python3 Python_push_distribution.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 . | ||
``` |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
## Presto | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.