From 4932f4a68adf2597c484563fa88ceaca9a760698 Mon Sep 17 00:00:00 2001 From: JAlcocerT Date: Fri, 19 Jan 2024 18:55:19 +0100 Subject: [PATCH] adding proy ideas --- _posts/2021-07-21-getting-started.md | 8 + _posts/2024-12-12-future-projects.md | 81 ++++ _posts/2024-12-12-microcontrollers.md | 476 ++++++++++++++++++++- _posts/2024-12-31-bme680-ES.md | 153 +++++++ _posts/2024-12-31-cassandra.md | 162 +++++++ _posts/2024-12-31-dht22.md | 589 ++++++++++++++++++++++++++ 6 files changed, 1468 insertions(+), 1 deletion(-) create mode 100644 _posts/2024-12-31-bme680-ES.md create mode 100644 _posts/2024-12-31-cassandra.md create mode 100644 _posts/2024-12-31-dht22.md diff --git a/_posts/2021-07-21-getting-started.md b/_posts/2021-07-21-getting-started.md index 274689b..3d9b2ba 100644 --- a/_posts/2021-07-21-getting-started.md +++ b/_posts/2021-07-21-getting-started.md @@ -84,6 +84,14 @@ ha --help Homebridge, OpenHAB... (you can also install them with Rpi Imager). +You can also use it with the Cloud: + +* Google Pub/Sub: + + + +* AWS IoT: + ## Analytical Software for IoT Projects diff --git a/_posts/2024-12-12-future-projects.md b/_posts/2024-12-12-future-projects.md index 0544c17..c238b9d 100644 --- a/_posts/2024-12-12-future-projects.md +++ b/_posts/2024-12-12-future-projects.md @@ -25,6 +25,87 @@ https://github.com/traccar/traccar-docker +### ESP32 Web + + + +### Pico DHT22 + + + +https://www.youtube.com/watch?v=eNF3X3D0cH4 + +https://github.com/neeraj95575/Temperature-sensor-connect-to-raspberry-pi-pico + +### ESP DHT22 + + + + + +GND +VIN (3v3 also works) +D23 + + + ---> + + +in platformio.ini + +adafruit/DHT sensor library@^1.4.4 + + + +lib_deps= +https://github.com/blynkkk/blynk-library.git +https://github.com/adafruit/Adafruit_Sensor +https://github.com/adafruit/DHT-sensor-library + + + +in the main.cpp + +#include + +https://github.com/adafruit/DHT-sensor-library + +not this one: adafruit/Adafruit Unified Sensor@^1.1.13 + + + +lib_deps = + https://github.com/adafruit/DHT-sensor-library.git + +OR + +lib_deps = + adafruit/DHT sensor library@^1.4.4 + +### MPU acelerometer + + +There are many 3-axis accelerometers that you can use with the Raspberry Pi Pico. Some of the most popular options include: + +MPU-6050: This is a popular and versatile accelerometer that is also compatible with the Raspberry Pi Pico. It has a wide range of features, including a built-in gyroscope. + + +**biblioman09** + + + +### DSB18B20 + +-55 to 125C + + + + +data to D13 + ### Pi off grid - Solar panels diff --git a/_posts/2024-12-12-microcontrollers.md b/_posts/2024-12-12-microcontrollers.md index 72be1e8..307a6f4 100644 --- a/_posts/2024-12-12-microcontrollers.md +++ b/_posts/2024-12-12-microcontrollers.md @@ -11,6 +11,480 @@ render_with_liquid: false --- + +A basics on how to connect the parts: + + + ## Connecting ESP32 to Linux -https://github.com/tio/tio \ No newline at end of file +https://github.com/tio/tio + +--- +title: "Raspberry Pi Pico W:" +date: 2023-08-30T23:20:21+01:00 +draft: true +tags: ["Self-Hosting"] +--- + +IDE - Thonny + + + +Ideas for Readme's - https://github.com/STJRush/handycode/tree/master/Raspi%20Pico + + + + +The chip: RP2040 + +The schema: + +W version (wifi): + +## Pico and MicroPython + +Thanks to [core-electronics](https://core-electronics.com.au/guides/raspberry-pi-pico-w-connect-to-the-internet/) + +Hold the BOOTSEL button on the Pico W +Connect the Pico W to your computer via the USB cable +Release the BOOTSEL button -> you will see a new device in the PC. + +Download this and move it to the Pico folder. + +* Mip: + * installing from fork: + +```py +import mip +mip.install(PACKAGE_NAME, index="https://USERNAME.github.io/micropython-lib/mip/BRANCH_NAME") +``` + + + +unplug usb and plug + +To install libraries, i have observed that recently **upip has been depricated in favour of mip** + +### Pico en vscode + + + + +### Pico in Arduino IDE + +Tools -> Board -> Boards Manager -> Install Arduino MBed OS RP2040 Boards + + + +### Pico with Thony + + + +#### using the built in led + +The led is the pin 25 as per the schema + + + +Run -> Configure Interpreter -> Interpreter -> MicroPython (Raspberry Pi Pico) + +View -> files + +The Pico will look for a **main.py** to execute in loop +View -> plotter + +CTRL+D for soft reboot and load the program + + +```py +from machine import Pin +from time import sleep + +#led = Pin(25, Pin.OUT) +led = Pin("LED", Pin.OUT) #For Pico W: Thanks to Easy Learning Video https://www.youtube.com/watch?v=PvH_yKwtoEA + +n=0 + +while True: + led.toggle() + print("13 times {} is {}".format(n,13)) + n = n+1 + sleep(0.5) + +``` + + +#### Reading internal temp sensor: + + + + +```py +import machine +import utime +sensor_temp = machine.ADC(4) +conversion_factor = 3.3 / (65535) #pico's datasheet +while True: + reading = sensor_temp.read_u16() * conversion_factor + temperature = 27 - (reading - 0.706)/0.001721 + print(temperature) + utime.sleep(2) +``` + +#### Connecting the Pico to Wifi + + + +Name the file different than main.py to avoid the automatic execution. + +```py +# A simple example that: +# - Connects to a WiFi Network defined by "ssid" and "password" +# - Performs a GET request (loads a webpage) +# - Queries the current time from a server + +import network # handles connecting to WiFi +import urequests # handles making and servicing network requests + +# Connect to network +wlan = network.WLAN(network.STA_IF) +wlan.active(True) + +# Fill in your network name (ssid) and password here: +ssid = 'HUAWEI P30' +password = 'mokradupa68' +wlan.connect(ssid, password) + + +# Example 1. Make a GET request for google.com and print HTML +# Print the html content from google.com +print("1. Querying the Web.com:") +r = urequests.get("https://fossengineer.com") +print(r.content) + +r = urequests.get("http://date.jsontest.com/") +print(r.json()) +print(r.json()['time']) +``` + +## Pico and MQTT + +Message Queue Telemetry Transport + +### with **HiveMQ** + +### with RPI and mqttx/mosquitto + + + + + +sudo apt install -y mosquitto +sudo apt install -y mosquitto-clients + +#sudo apt install python3-pip +sudo pip3 install paho-mqtt + +sudo systemctl status mosquitto.service + +In the absence of a direct configuration entry for the port, the port used by Mosquitto could be the default port (1883 for MQTT or 8883 for MQTT over TLS/SSL). + +You can check which one is in used with: netstat -tuln + + + #also hivemq + + +#without hivemq GREAT VIDEO!!!!!!!! **To be used mqttx and emqx** + + + + +REQUIRED: + +```py + +#connect to wifi +import network # handles connecting to WiFi +import urequests # handles making and servicing network requests + +# Connect to network +wlan = network.WLAN(network.STA_IF) +wlan.active(True) + +# Fill in your network name (ssid) and password here: +ssid = 'HUAWEI P30' +password = 'mokradupa68' +wlan.connect(ssid, password) + + +#install libraries for mqtt + +##with upip +#upip.install("micropython-umqtt-robust") +#upip.install("micropython-umqtt-simple") + +##with mip +#https://github.com/micropython/micropython-lib +#https://github.com/micropython/micropython-lib/tree/master/micropython/umqtt.robust +mip.install("umqtt.robust") + +#https://github.com/micropython/micropython-lib/tree/master/micropython/umqtt.simple +mip.install("umqtt.simple") + +``` + +then the code + + +boot.py + +```py +# boot.py -- run on boot-up +import network, utime, machine + +# Replace the following with your WIFI Credentials +SSID = "HUAWEI P30" +SSID_PASSWORD = "mokradupa68" + + +def do_connect(): + sta_if = network.WLAN(network.STA_IF) + if not sta_if.isconnected(): + print('connecting to network...') + sta_if.active(True) + sta_if.connect(SSID, SSID_PASSWORD) + while not sta_if.isconnected(): + print("Attempting to connect....") + utime.sleep(1) + print('Connected! Network config:', sta_if.ifconfig()) + +print("Connecting to your wifi...") +do_connect() +``` + + +main.py + + +```py +import time +import ubinascii +from umqtt.simple import MQTTClient +import machine +import random + +# Default MQTT_BROKER to connect to +MQTT_BROKER = "192.168.3.200" +CLIENT_ID = ubinascii.hexlify(machine.unique_id()) +SUBSCRIBE_TOPIC = b"led" +PUBLISH_TOPIC = b"temperature" + +# Setup built in PICO LED as Output +led = machine.Pin("LED",machine.Pin.OUT) + +# Publish MQTT messages after every set timeout +last_publish = time.time() +publish_interval = 5 + +# Received messages from subscriptions will be delivered to this callback +def sub_cb(topic, msg): + print((topic, msg)) + if msg.decode() == "ON": + led.value(1) + else: + led.value(0) + + +def reset(): + print("Resetting...") + time.sleep(5) + machine.reset() + +# Generate dummy random temperature readings +def get_temperature_reading(): + return random.randint(20, 50) + + +def get_chip_temperature_reading(): #added this function to read the Pico's Temp Sensor + sensor_temp = machine.ADC(4) + conversion_factor = 3.3 / (65535) #pico's datasheet + #while True: + reading = sensor_temp.read_u16() * conversion_factor + temperature = 27 - (reading - 0.706)/0.001721 + #print(temperature) + return(temperature) + +def main(): + print(f"Begin connection with MQTT Broker :: {MQTT_BROKER}") + mqttClient = MQTTClient(CLIENT_ID, MQTT_BROKER, keepalive=60) + mqttClient.set_callback(sub_cb) + mqttClient.connect() + mqttClient.subscribe(SUBSCRIBE_TOPIC) + print(f"Connected to MQTT Broker :: {MQTT_BROKER}, and waiting for callback function to be called!") + while True: + # Non-blocking wait for message + mqttClient.check_msg() + global last_publish + if (time.time() - last_publish) >= publish_interval: + #temp = get_temperature_reading() + temp=get_chip_temperature_reading() + mqttClient.publish(PUBLISH_TOPIC, str(temp).encode()) + last_publish = time.time() + time.sleep(1) + + +if __name__ == "__main__": + while True: + try: + main() + except OSError as e: + print("Error: " + str(e)) + reset() + +``` + +#### MQTT to NodeRed + +NodeRed will subscribe to the topic + + +```yml +version: '3' + +services: + emqx: + image: emqx/emqx + container_name: emqx + ports: + - "1883:1883" + - "8083:8083" + - "8883:8883" + - "8084:8084" + - "18083:18083" + restart: always + + nodered: + image: nodered/node-red + container_name: nodered + ports: + - "1880:1880" + restart: always + +``` + + + + + + +### + +https://www.youtube.com/watch?v=ybCMXqsQyDw&t=19s + + +## Pico to Pico Wifi comm + + + +## Pico W web server + + + +and also + +## Pico and Solar Panel + Battery + + + + + + + + + +## PicoW with DHT11 + + + + + +## Pico W with MLX90614 + +GND is pin38 +3v3 out is pin 36 + +11 (GP8) is I2C0 SDA +12 (GP9) is I2C0 SCL + + and they are giving their own library: + +we need to install: +it is not available in mip, the new package manager + +so cloned the repo and copied into /lib/mlx/mlx90614.py (i did not compiled it into .mpy) the .py file of the repo + +in this way, we can import with from mlx.mlx90614 import MLX90614 (we import the class) + + +```py +from machine import Pin, Timer, I2C, SoftI2C +#from aphanum import ALPHANUM_I2C +from mlx90614 import MLX90614_I2C + + +i2c2 = SoftI2C(scl=Pin(9),sda=Pin(8),freq=100000) + +print("I2C Comm Success") + +# d = i2c2.scan() +# print(hex(d[0]) +# print(hex(d[1]) +# alph = ALPHANUM_I2C(i2c2,0x70,000,15) +# print("Alpha display init") + +irtemp = MLX90614_I2C(i2c2,0x5A) + +led1 = Pin(25, Pin.OUT) + +timer1 = Timer() + +def tick_timer(timer): + global led1 + led1.toggle() + t1 = irtemp.get_temperature(0) + t2 = irtemp.get_temperature(1) + print("T1 = %f", t1) + print("T2 = %f", t2) + alph.set_digit(int(t2*100),2); +timer1.init(freq=2,mode=Timer.PERIODIC,callback=tick_timer) + +``` + + + +## Languages + +C/C++ +MicroPython +TinyGo (?) +CircuitPython (?) + +O.S FreeRTS ??? + +## Sensors + +### BME280 + +Temp Hum and Preassure + +I2C + + + +### DHT22 Pico + +https://www.youtube.com/watch?v=eNF3X3D0cH4 + +https://github.com/neeraj95575/Temperature-sensor-connect-to-raspberry-pi-pico \ No newline at end of file diff --git a/_posts/2024-12-31-bme680-ES.md b/_posts/2024-12-31-bme680-ES.md new file mode 100644 index 0000000..33f0789 --- /dev/null +++ b/_posts/2024-12-31-bme680-ES.md @@ -0,0 +1,153 @@ +--- +title: "Raspberry Pi: Airquality & Kibana" +date: 2024-12-30T23:20:21+01:00 +draft: true +tags: ["Self-Hosting"] +--- + + +ES supports RPi 64 by default, but thanks to + + + + ---> + + + + +```dockerfile +FROM arm32v7/openjdk:11-jdk + +# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=863199#23 +RUN apt update && apt install wget -y +RUN wget -O /tmp/elasticsearch.tar.gz https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.12.1-no-jdk-linux-x86_64.tar.gz +RUN mkdir /usr/share/elasticsearch +RUN tar -xf /tmp/elasticsearch.tar.gz -C /usr/share/elasticsearch --strip-components=1 +RUN adduser --disabled-password --gecos '' elastic +RUN chown -R elastic:root /usr/share/elasticsearch && chmod -R 777 /usr/share/elasticsearch +USER elastic +ENV ES_JAVA_HOME=${JAVA_HOME} +EXPOSE 9200 9300 +CMD [ "bash", "/usr/share/elasticsearch/bin/elasticsearch" ] + +``` +docker build -t elasticsearch_local . + + + +```python +import Adafruit_DHT +import time +from elasticsearch import Elasticsearch + +DHT_SENSOR = Adafruit_DHT.DHT11 +DHT_PIN = 4 + +# Configure Elasticsearch connection +es = Elasticsearch(hosts=['http://localhost:9200']) # Replace with your Elasticsearch host + +# Define the index mapping (optional but recommended) +index_name = "sensor_data" # Replace with your desired index name + +mapping = { + "mappings": { + "properties": { + "temperature": {"type": "float"}, + "humidity": {"type": "float"}, + "timestamp": {"type": "date"} + } + } +} + +# Create the index with the specified mapping +es.indices.create(index=index_name, ignore=400, body=mapping) + +print(f"Index '{index_name}' created.") + + +while True: + humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN) + if humidity is not None and temperature is not None: + data = { + "temperature": temperature, + "humidity": humidity, + "timestamp": int(time.time()) * 1000 # Elasticsearch expects timestamp in milliseconds + } + + index_name = "sensor_data" # Replace with your desired index name + es.index(index=index_name, body=data) + print("Data sent to Elasticsearch") + else: + print("Sensor failure. Check wiring.") + time.sleep(3) + +``` + + +```dockerfile +# Use an official Python runtime as the base image +FROM python:3.8-slim + +# Set the working directory in the container +WORKDIR /app + +# Install system-level dependencies +RUN apt-get update && \ + apt-get install -y python3-dev python3-pip libpq-dev gcc && \ + python3 -m pip install --upgrade pip setuptools wheel + +# Copy the local code to the container +COPY your_modified_python_script.py /app/ + +# Install additional dependencies +RUN pip install Adafruit_DHT elasticsearch + +# Run the Python script +CMD ["python", "your_modified_python_script.py"] + +``` + + +docker build -t dht_sensor_es . + + +The stack: + +```yml +version: '3' +services: + elasticsearch: + image: docker.elastic.co/elasticsearch/elasticsearch:7.15.0 + container_name: elasticsearch_container + environment: + - "discovery.type=single-node" + ports: + - "9200:9200" + networks: + - app_network + + kibana: + image: docker.elastic.co/kibana/kibana:7.15.0 + container_name: kibana_container + environment: + - "ELASTICSEARCH_HOSTS=http://elasticsearch:9200" # Connect Kibana to Elasticsearch + ports: + - "5601:5601" + depends_on: + - elasticsearch + networks: + - app_network + + dht_sensor_timescale: + image: dht_sensor_es # Use your pre-built image name + container_name: dht_sensor_es_container + privileged: true # Run the container in privileged mode (GPIO access) + depends_on: + - elasticsearch + networks: + - app_network + +networks: + app_network: + +``` \ No newline at end of file diff --git a/_posts/2024-12-31-cassandra.md b/_posts/2024-12-31-cassandra.md new file mode 100644 index 0000000..42bd8cf --- /dev/null +++ b/_posts/2024-12-31-cassandra.md @@ -0,0 +1,162 @@ +--- +title: Cassandra +author: JAlcocerT +date: 2024-12-01 00:10:00 +0800 +categories: [IoT & Data Analytics] +tags: [Sensors,Python,MongoDB] +draft: true +image: + path: /img/metabase.png + alt: IoT Project with Python, MongoDB, DHT11/22 sensors and Metabase. +render_with_liquid: false +--- + + + +## Cassandra + +Yes, you can push data from Python to Apache Cassandra, **a distributed NoSQL database**. To interact with Cassandra from Python, you'll need to use a Python driver for Cassandra. One popular driver for Cassandra is `cassandra-driver`. + +Here's how you can use `cassandra-driver` to push data to a Cassandra database: + +1. Install the `cassandra-driver` using pip: + + ```bash + pip install cassandra-driver + ``` + +2. Import the necessary modules and create a connection to your Cassandra cluster: + + ```python + from cassandra.cluster import Cluster + + # Connect to your Cassandra cluster (replace with your cluster's contact points) + cluster = Cluster(['127.0.0.1']) # Use the IP addresses or hostnames of your Cassandra nodes + session = cluster.connect() + ``` + +3. Create a keyspace (database) and switch to it: + + ```python + keyspace_name = 'your_keyspace_name' + + # Create a keyspace (if it doesn't exist) + session.execute(f"CREATE KEYSPACE IF NOT EXISTS {keyspace_name} WITH replication = {{'class': 'SimpleStrategy', 'replication_factor': 1}}") + + # Switch to the keyspace + session.set_keyspace(keyspace_name) + ``` + +4. Define your Cassandra table schema (CQL) and create the table: + + ```python + table_name = 'your_table_name' + + # Define your table schema (CQL) + create_table_query = f""" + CREATE TABLE IF NOT EXISTS {table_name} ( + id UUID PRIMARY KEY, + column1 text, + column2 int + ) + """ + + # Create the table + session.execute(create_table_query) + ``` + +5. Insert data into the Cassandra table: + + ```python + from cassandra.query import SimpleStatement + + # Define the insert query + insert_query = f"INSERT INTO {table_name} (id, column1, column2) VALUES (?, ?, ?)" + + # Prepare the insert statement + insert_statement = SimpleStatement(insert_query, consistency_level=1) + + # Insert data into the table + session.execute(insert_statement, (uuid.uuid4(), 'value1', 42)) + ``` + +6. Close the Cassandra session and cluster when you're done: + + ```python + session.shutdown() + cluster.shutdown() + ``` + +Make sure to replace `'your_keyspace_name'` and `'your_table_name'` with your desired keyspace and table names, and customize the table schema and data as needed. + +With these steps, you can push data from Python to Cassandra using the `cassandra-driver` library. Be sure to have a running Cassandra cluster with the appropriate configuration and keyspace set up before running the code. + + +## KAFKA + +Yes, Python can push data to Apache Kafka. Kafka is a distributed streaming platform that allows you to publish and consume streams of records, and there are Python libraries and clients available to work with Kafka. + +One popular Python library for interacting with Kafka is `confluent-kafka-python`, which is a Python wrapper for the Confluent Kafka client. You can use this library to produce (push) data to Kafka topics. + +Here's an example of how to use `confluent-kafka-python` to produce data to a Kafka topic: + +First, you need to install the library using pip: + +```bash +pip install confluent-kafka +``` + +Now, you can use the following Python code to produce data to a Kafka topic: + +```python +from confluent_kafka import Producer + +# Kafka broker address and topic name +bootstrap_servers = 'localhost:9092' # Change this to your Kafka broker's address +topic = 'your_topic_name' # Change this to your Kafka topic name + +# Create a Kafka producer configuration +producer_config = { + 'bootstrap.servers': bootstrap_servers, + # Other producer configuration options can be added here +} + +# Create a Kafka producer instance +producer = Producer(producer_config) + +try: + # Produce a message to the Kafka topic + message_key = 'key' # Change this to your desired message key + message_value = 'Hello, Kafka!' # Change this to your message content + + producer.produce(topic, key=message_key, value=message_value) + producer.flush() # Flush the producer buffer to send the message + + print(f"Produced message: key={message_key}, value={message_value} to topic: {topic}") + +except Exception as e: + print(f"Error producing message: {str(e)}") + +finally: + producer.close() +``` + +In this code: + +1. You import the `Producer` class from `confluent_kafka`. + +2. You define the Kafka broker address (`bootstrap_servers`) and the Kafka topic name you want to produce data to. + +3. You create a Kafka producer configuration dictionary, specifying the bootstrap servers. + +4. You create a Kafka producer instance using the provided configuration. + +5. Inside a try-except block, you produce a message to the Kafka topic, specifying a message key and message value. You can customize the key and value as needed. + +6. You flush the producer buffer to ensure that the message is sent to Kafka. + +7. Finally, you close the producer. + +Make sure to replace `'localhost:9092'` with the address of your Kafka broker and `'your_topic_name'` with the name of the Kafka topic you want to use. + +With this code, you can push data to Kafka from your Python application. \ No newline at end of file diff --git a/_posts/2024-12-31-dht22.md b/_posts/2024-12-31-dht22.md new file mode 100644 index 0000000..ea7d2a0 --- /dev/null +++ b/_posts/2024-12-31-dht22.md @@ -0,0 +1,589 @@ +--- +title: "Raspberry Pi: Temp and Humidity with DHT11 and MongoDB" +date: 2023-08-29T23:20:21+01:00 +draft: true +tags: ["Self-Hosting"] +--- + +# Raspberry Pi together with: Dht11, Python and Docker + +With MongoDB -> requires ARM64/x86 version + +https://www.dcddcc.com/blog/2018-06-09-building-mongodb-for-32-bit-ARM-on-debian-ubuntu.html + + + + +https://hub.docker.com/r/apcheamitru/arm32v7-mongo + +docker build -t mongo_db . + +GND +5v (or 3v3) +GPIO4 + +## Python + +### Simple print + +```py +import Adafruit_DHT +import time + +# Set the DHT sensor type (DHT22 or DHT11) +sensor_type = Adafruit_DHT.DHT22 + +# Set the GPIO pin where the sensor is connected +gpio_pin = 4 # Change this to the actual GPIO pin number + +try: + while True: + # Read temperature and humidity from the sensor + humidity, temperature = Adafruit_DHT.read_retry(sensor_type, gpio_pin) + + if humidity is not None and temperature is not None: + # Print the values + print(f'Temperature: {temperature:.2f}°C') + print(f'Humidity: {humidity:.2f}%') + else: + print('Failed to retrieve data from the sensor') + + # Delay for a while (in seconds) before the next reading + time.sleep(2) + +except KeyboardInterrupt: + print('Program terminated by user') + +``` + +### With MariaDB + + +```yml + +version: '3' +services: + mariadb: + image: linuxserver/mariadb:arm32v7-10.6.13 #arm32v7-latest but wrong arch + container_name: my-mariadb + environment: + MYSQL_ROOT_PASSWORD: your_root_password_here + MYSQL_DATABASE: mydatabase + MYSQL_USER: your_username_here + MYSQL_PASSWORD: your_password_here + ports: + - "3306:3306" + volumes: + - mariadb_data:/var/lib/mysql + restart: always + +volumes: + mariadb_data: + + + +# CREATE TABLE sensor_data ( +# id INT AUTO_INCREMENT PRIMARY KEY, +# timestamp DATETIME NOT NULL, +# temperature DECIMAL(5, 2) NOT NULL, +# humidity DECIMAL(5, 2) NOT NULL +# ); + + +#mysql -u root -p +#use mydatabase +#SHOW TABLES; +#DESCRIBE sensor_data; +#SELECT * from sensor_data; +#SELECT * FROM sensor_data ORDER BY timestamp DESC; + +``` + +pip install mysql-connector-python + + +**THIS BELOW WORKS AND PUSHES FROM RPI TO DOCKER CONTAINER MARIADB WITHOUT ISSUES< BUT NOT WORKING INSIDE THE CONTAINER** + +```py +import Adafruit_DHT +import time +import mysql.connector + +# Set the DHT sensor type (DHT22 or DHT11) +sensor_type = Adafruit_DHT.DHT22 + +# Set the GPIO pin where the sensor is connected +gpio_pin = 4 # Change this to the actual GPIO pin number + +# Database connection configuration +db_config = { + "user": "your_username_here", + "password": "your_password_here", + "host": "localhost", # Change if your MariaDB is on a different host + "database": "mydatabase", # Change to your database name +} + +try: + # Connect to the MariaDB database + connection = mysql.connector.connect(**db_config) + cursor = connection.cursor() + + while True: + # Read temperature and humidity from the sensor + humidity, temperature = Adafruit_DHT.read_retry(sensor_type, gpio_pin) + + if humidity is not None and temperature is not None: + # Print the values + print(f'Temperature: {temperature:.2f}°C') + print(f'Humidity: {humidity:.2f}%') + + # Get the current timestamp + current_time = time.strftime('%Y-%m-%d %H:%M:%S') + + # Insert sensor data into the database with timestamp + insert_query = "INSERT INTO sensor_data (timestamp, temperature, humidity) VALUES (%s, %s, %s)" + data = (current_time, temperature, humidity) + cursor.execute(insert_query, data) + connection.commit() + else: + print('Failed to retrieve data from the sensor') + + # Delay for a while (in seconds) before the next reading + time.sleep(2) + +except KeyboardInterrupt: + print('Program terminated by user') +finally: + # Close the database connection when done + if connection.is_connected(): + cursor.close() + connection.close() + +``` + + +```py + +import os +import Adafruit_DHT +import time +import mysql.connector + +# Set the DHT sensor type (DHT22 or DHT11) +sensor_type = Adafruit_DHT.DHT22 + +# Set the GPIO pin where the sensor is connected +gpio_pin = 4 # Change this to the actual GPIO pin number + +# Read database connection details and table name from environment variables +db_config = { + "user": os.getenv("DB_USER", "default_username"), + "password": os.getenv("DB_PASSWORD", "default_password"), + "host": os.getenv("DB_HOST", "localhost"), + "database": os.getenv("DB_NAME", "mydatabase"), +} +table_name = os.getenv("TABLE_NAME", "sensor_data") + +try: + # Connect to the MariaDB database + connection = mysql.connector.connect(**db_config) + cursor = connection.cursor() + + # Create the table if it doesn't exist + create_table_query = f""" + CREATE TABLE IF NOT EXISTS {table_name} ( + id INT AUTO_INCREMENT PRIMARY KEY, + timestamp DATETIME NOT NULL, + temperature DECIMAL(5, 2) NOT NULL, + humidity DECIMAL(5, 2) NOT NULL + ) + """ + cursor.execute(create_table_query) + + while True: + # Read temperature and humidity from the sensor + humidity, temperature = Adafruit_DHT.read_retry(sensor_type, gpio_pin) + + if humidity is not None and temperature is not None: + # Print the values + print(f'Temperature: {temperature:.2f}°C') + print(f'Humidity: {humidity:.2f}%') + + # Get the current timestamp + current_time = time.strftime('%Y-%m-%d %H:%M:%S') + + # Insert sensor data into the database with timestamp + insert_query = f"INSERT INTO {table_name} (timestamp, temperature, humidity) VALUES (%s, %s, %s)" + data = (current_time, temperature, humidity) + cursor.execute(insert_query, data) + connection.commit() + else: + print('Failed to retrieve data from the sensor') + + # Delay for a while (in seconds) before the next reading + time.sleep(2) + +except KeyboardInterrupt: + print('Program terminated by user') +finally: + # Close the database connection when done + if connection.is_connected(): + cursor.close() + connection.close() +``` + + +```py +import Adafruit_DHT +import time +import mysql.connector +import os # Import the os module + +# Set the DHT sensor type (DHT22 or DHT11) +sensor_type = Adafruit_DHT.DHT22 + +# Set the GPIO pin where the sensor is connected +gpio_pin = 4 # Change this to the actual GPIO pin number + +# Read database connection details and table name from environment variables +db_config = { + "user": os.getenv("DB_USER", "default_username"), + "password": os.getenv("DB_PASSWORD", "default_password"), + "host": os.getenv("DB_HOST", "localhost"), + "database": os.getenv("DB_NAME", "mydatabase"), +} +table_name = os.getenv("TABLE_NAME", "sensor_data") + +try: + # Connect to the MariaDB database + connection = mysql.connector.connect(**db_config) + cursor = connection.cursor() + + # Create the table if it doesn't exist + create_table_query = f""" + CREATE TABLE IF NOT EXISTS {table_name} ( + id INT AUTO_INCREMENT PRIMARY KEY, + timestamp DATETIME NOT NULL, + temperature DECIMAL(5, 2) NOT NULL, + humidity DECIMAL(5, 2) NOT NULL + ) + """ + cursor.execute(create_table_query) + + while True: + # Read temperature and humidity from the sensor + humidity, temperature = Adafruit_DHT.read_retry(sensor_type, gpio_pin) + + if humidity is not None and temperature is not None: + # Print the values + print(f'Temperature: {temperature:.2f}°C') + print(f'Humidity: {humidity:.2f}%') + + # Get the current timestamp + current_time = time.strftime('%Y-%m-%d %H:%M:%S') + + # Insert sensor data into the database with timestamp + insert_query = f"INSERT INTO {table_name} (timestamp, temperature, humidity) VALUES (%s, %s, %s)" + data = (current_time, temperature, humidity) + cursor.execute(insert_query, data) + connection.commit() + else: + print('Failed to retrieve data from the sensor') + + # Delay for a while (in seconds) before the next reading + time.sleep(2) + +except KeyboardInterrupt: + print('Program terminated by user') +except mysql.connector.Error as e: + print(f"Database error: {e}") +finally: + # Close the database connection when done (if it's defined) + if 'connection' in locals() and connection.is_connected(): + cursor.close() + connection.close() +``` + + +**connects to orange** + +```py + +import os +import Adafruit_DHT +import time +import mysql.connector + +# Set the DHT sensor type (DHT22 or DHT11) +sensor_type = Adafruit_DHT.DHT22 + +# Set the GPIO pin where the sensor is connected +gpio_pin = 4 # Change this to the actual GPIO pin number + +# Read database connection details and table name from environment variables +# Database connection configuration +db_config = { + "user": "your_username_here", + "password": "your_password_here", + "host": "192.168.3.200", # Updated host IP + "port": 3306, # Specified port number + "database": "mydatabase", # Change to your database name +} + +table_name = os.getenv("TABLE_NAME", "sensor_data") + +try: + # Connect to the MariaDB database + connection = mysql.connector.connect(**db_config) + cursor = connection.cursor() + + # Create the table if it doesn't exist + create_table_query = f""" + CREATE TABLE IF NOT EXISTS {table_name} ( + id INT AUTO_INCREMENT PRIMARY KEY, + timestamp DATETIME NOT NULL, + temperature DECIMAL(5, 2) NOT NULL, + humidity DECIMAL(5, 2) NOT NULL + ) + """ + cursor.execute(create_table_query) + + while True: + # Read temperature and humidity from the sensor + humidity, temperature = Adafruit_DHT.read_retry(sensor_type, gpio_pin) + + if humidity is not None and temperature is not None: + # Print the values + print(f'Temperature: {temperature:.2f}°C') + print(f'Humidity: {humidity:.2f}%') + + # Get the current timestamp + current_time = time.strftime('%Y-%m-%d %H:%M:%S') + + # Insert sensor data into the database with timestamp + insert_query = f"INSERT INTO {table_name} (timestamp, temperature, humidity) VALUES (%s, %s, %s)" + data = (current_time, temperature, humidity) + cursor.execute(insert_query, data) + connection.commit() + else: + print('Failed to retrieve data from the sensor') + + # Delay for a while (in seconds) before the next reading + time.sleep(2) + +except KeyboardInterrupt: + print('Program terminated by user') +finally: + # Close the database connection when done + if connection.is_connected(): + cursor.close() + connection.close() + +``` + + +```dockerfile + +# Use an official Python runtime as the base image +FROM python:3.8-slim + +# Set the working directory in the container +WORKDIR /app + +# Install system-level dependencies +RUN apt-get update && \ + apt-get install -y python3-dev python3-pip && \ + python3 -m pip install --upgrade pip setuptools wheel + +# Copy the local code to the container +COPY sensor_data.py /app/ + +# Install additional dependencies +RUN pip install Adafruit_DHT mysql-connector-python + +# Run the Python script +CMD ["python", "sensor_data.py"] + + +``` + + +docker build -t dht22_sensor_to_mysql . + + +```yml + +version: '3' +services: + mariadb: + image: linuxserver/mariadb:arm32v7-10.6.13 + container_name: my-mariadb + environment: + MYSQL_ROOT_PASSWORD: your_root_password_here + ports: + - "3306:3306" + volumes: + - mariadb_data:/var/lib/mysql + restart: always + + python-app: + build: + context: ./ # Specify the path to your Dockerfile and Python script + container_name: my-python-app + environment: + - DB_USER=mydbuser + - DB_PASSWORD=mydbpassword + - DB_HOST=mariadb # Use the service name defined above + - DB_NAME=mydatabase + - TABLE_NAME=sensor_data + depends_on: + - mariadb + restart: always + command: tail -f /dev/null #keep it running + + +volumes: + mariadb_data: + + +``` + + +```yml +version: '3' +services: + mariadb: + image: mariadb:latest + container_name: my-mariadb + environment: + MYSQL_ROOT_PASSWORD: your_root_password_here + ports: + - "3306:3306" + volumes: + - mariadb_data:/var/lib/mysql + restart: always + + python-app: + image: dht22_sensor_to_mysql # Use the name of your existing Docker image + container_name: my-python-app + environment: + - DB_USER=mydbuser + - DB_PASSWORD=mydbpassword + - DB_HOST=mariadb # Use the service name defined above + - DB_NAME=mydatabase + - TABLE_NAME=sensor_data + depends_on: + - mariadb + restart: always + command: tail -f /dev/null #keep it running + + +volumes: + mariadb_data: + +``` + + + +only th py script + + +```yml + +version: '3' +services: + + + python-app: + image: dht22_sensor_to_mysql # Use the name of your existing Docker image + container_name: py-dht22-mariadb + privileged: true + environment: + - DB_USER=mydbuser + - DB_PASSWORD=mydbpassword + - DB_HOST=mariadb # Use the service name defined above + - DB_NAME=mydatabase + - TABLE_NAME=sensor_data + restart: always + command: tail -f /dev/null #keep it running + +``` + + + +### With Mongo + +```py +import Adafruit_DHT +import time +from pymongo import MongoClient + +DHT_SENSOR = Adafruit_DHT.DHT11 +DHT_PIN = 4 + +# Configure MongoDB connection +mongo_client = MongoClient('mongodb://localhost:27017/') +db = mongo_client['sensor_data'] +collection = db['dht_sensor'] + +while True: + humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN) + if humidity is not None and temperature is not None: + data = { + "timestamp": time.strftime('%Y-%m-%dT%H:%M:%SZ'), + "temperature": temperature, + "humidity": humidity + } + collection.insert_one(data) + print("Data sent to MongoDB") + else: + print("Sensor failure. Check wiring.") + time.sleep(3) +``` + + + + + + + +### Split ARM32 and ARM64/x86 with Cloudflare Tunnels + + +I am a big fan of [Cloudflare Tunnels](https://fossengineer.com/selfhosting-cloudflared-tunnel-docker/) to expose services securely and thought of a solution for the users of RPi 32bits (ARMv7) - Run the python script with the RPi 32 bits and run Mongo DB with a ARM86/x86 device and expose the DB with Cloudflare and a Domain with SSL. + +```yml +version: '3' +services: + + dht_sensor_mongo: + image: dht_sensor_mongo:latest # Use the name of your custom image + container_name: dht_sensor_mongo + privileged: true + +``` + + +```yml +version: '3' +services: + mongodb: + image: mongo:7 + container_name: mongodb + environment: + MONGO_INITDB_ROOT_USERNAME: yourusername + MONGO_INITDB_ROOT_PASSWORD: yourpassword + volumes: + - mongodb_data:/data/db + ports: + - "27017:27017" + restart: always + command: ["mongod"] + +networks: + cloudflare_tunnel: + external: true +volumes: + mongodb_data: + +``` \ No newline at end of file