Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
612eb3d
cliente mqtt con docker
investigatronica Mar 1, 2023
e1feec1
uso de ENV
investigatronica Mar 1, 2023
d2fad21
orden ENV
investigatronica Mar 2, 2023
b1d71c6
env var TOPICO
investigatronica Mar 2, 2023
46b24bb
docker-compose
investigatronica Mar 2, 2023
8554f67
base de datos
investigatronica Mar 2, 2023
96c68b1
phpmyadmins depends_on
investigatronica Mar 3, 2023
675ee50
grafana
investigatronica Mar 3, 2023
ea08a45
agregar mosquitto
investigatronica Mar 4, 2023
a746d97
Actualizar 'docker-compose.yml'
investigatronica Mar 6, 2023
5fe81cf
portainer
investigatronica Mar 6, 2023
bcbed19
swag
investigatronica Mar 6, 2023
a717a5a
phpmyadmin + tls
investigatronica Mar 6, 2023
d091ec9
grafana + tls
investigatronica Mar 6, 2023
03df65b
mosquitto + tls + pass
investigatronica Mar 6, 2023
e43ab36
mi primer bot
investigatronica Mar 7, 2023
dd3a255
más compacto
investigatronica Mar 7, 2023
d74699f
sin ->
investigatronica Mar 7, 2023
99e1001
Actualizar 'clienteMqtt/requirements.txt'
investigatronica Mar 7, 2023
59e4d11
Actualizar 'clienteMqtt/clienteMqtt.py'
investigatronica Mar 7, 2023
d6da76d
Actualizar 'clienteMqtt/clienteMqtt.py'
investigatronica Mar 7, 2023
50f839e
Actualizar 'clienteMqtt/clienteMqtt.py'
investigatronica Mar 8, 2023
a867882
Actualizar 'telegrambot/telegrambot.py'
investigatronica Mar 8, 2023
43c5c0d
Actualizar 'telegrambot/telegrambot.py'
investigatronica Mar 9, 2023
b3f1967
Actualizar 'clienteMqtt/clienteMqtt.py'
investigatronica Mar 9, 2023
15c26d8
Actualizar 'clienteMqtt/clienteMqtt.py'
investigatronica Mar 24, 2023
c9d52ae
Actualizar '.gitignore'
investigatronica Apr 19, 2023
3367abd
Actualizar 'docker-compose.yml'
investigatronica Apr 19, 2023
d7e27a7
nueva versión de swag
investigatronica Apr 19, 2023
a84e4ca
ver2024
GermanXander May 20, 2024
709e5ef
ver2024
GermanXander May 20, 2024
40d7c67
ver2024b
GermanXander May 20, 2024
674bf91
ver2024
GermanXander May 23, 2024
0a2284b
ver2024
GermanXander May 23, 2024
78752be
ver2025. set sensor_id to sensor_1
GermanXander May 9, 2025
841a601
ver2025b
GermanXander May 12, 2025
e404045
ver2025b
GermanXander May 12, 2025
0e60983
prueba1
LAV419 May 20, 2025
8504923
Prueba2
LAV419 May 20, 2025
691100a
Prueba3
LAV419 May 20, 2025
054a34d
Final1
LAV419 May 21, 2025
30fd17b
Final
LAV419 May 21, 2025
41a2759
Update README.md
LAV419 May 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
bin/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
include/
lib/
lib64/
lib64
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
pyvenv.cfg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

.env

#swag related files: ignore all except NGINX conf
swag/
grafana/
mosquitto/
wireguard/
mariadb/
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# docker
# Ejercicio Telegram bot

Vellbach, Lucas Alejandro

13 changes: 13 additions & 0 deletions clienteMqtt/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.11-slim

WORKDIR /app

ENV TZ="America/Argentina/Buenos_Aires"

COPY ./requirements.txt /app/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt

COPY . /app

CMD ["python", "/app/clienteMqtt.py"]
47 changes: 47 additions & 0 deletions clienteMqtt/clienteMqtt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import asyncio, ssl, certifi, logging, os, aiomysql, json, traceback
import aiomqtt

logging.basicConfig(format='%(asctime)s - cliente mqtt - %(levelname)s:%(message)s', level=logging.INFO, datefmt='%d/%m/%Y %H:%M:%S %z')

async def main():

tls_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
tls_context.verify_mode = ssl.CERT_REQUIRED
tls_context.check_hostname = True
tls_context.load_default_certs()

async with aiomqtt.Client(
os.environ["SERVIDOR"],
username=os.environ["MQTT_USR"],
password=os.environ["MQTT_PASS"],
port=int(os.environ["PUERTO_MQTTS"]),
tls_context=tls_context,
) as client:
await client.subscribe(os.environ['TOPICO'])
async for message in client.messages:
logging.info(str(message.topic) + ": " + message.payload.decode("utf-8"))
dispositivo=str(message.topic).split('/')[-1]
datos=json.loads(message.payload.decode('utf8'))
sql = "INSERT INTO `mediciones` (`sensor_id`, `temperatura`, `humedad`) VALUES (%s, %s, %s)"
try:
conn = await aiomysql.connect(host=os.environ["MARIADB_SERVER"], port=3306,
user=os.environ["MARIADB_USER"],
password=os.environ["MARIADB_USER_PASS"],
db=os.environ["MARIADB_DB"])
except Exception as e:
logging.error(traceback.format_exc())

cur = await conn.cursor()

async with conn.cursor() as cur:
try:
await cur.execute(sql, (dispositivo, datos['temperatura'], datos['humedad']))
await conn.commit()
await cur.close()
await conn.ensure_closed()
except Exception as e:
logging.error(traceback.format_exc())
# Logs the error appropriately.

if __name__ == "__main__":
asyncio.run(main())
4 changes: 4 additions & 0 deletions clienteMqtt/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
aiomqtt
certifi
environs
aiomysql
120 changes: 120 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
services:
mariadb:
image: mariadb
container_name: mariadb
environment:
- PUID=1000
- PGID=1000
- MARIADB_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
- TZ=America/Argentina/Buenos_Aires
volumes:
- ./mariadb:/config
ports:
- 3306:3306
restart: unless-stopped
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
restart: always
environment:
- PMA_HOST=mariadb
- PMA_ABSOLUTE_URI=https://${DOMINIO}:${PUERTO}/phpmyadmin/
ports:
- 8080:80
depends_on:
- mariadb
clientemqtt:
image: clientemqtt
container_name: clientemqtt
environment:
- TZ=America/Argentina/Buenos_Aires
- SERVIDOR=${SERVIDOR} #Se reemplazará por la env var SERVIDOR defineda en .env
- TOPICO=${TOPICO}
- MARIADB_SERVER=${MARIADB_SERVER}
- MARIADB_USER=${MARIADB_USER}
- MARIADB_USER_PASS=${MARIADB_USER_PASS}
- MARIADB_DB=${MARIADB_DB}
- MQTT_USR=${MQTT_USR}
- MQTT_PASS=${MQTT_PASS}
- PUERTO_MQTTS=${PUERTO_MQTTS}
restart: unless-stopped
depends_on:
- mariadb
mosquitto:
image: eclipse-mosquitto
container_name: mosquitto
user: "1000:1000"
ports:
- 1883:1883
- ${PUERTO_MQTTS}:8883
restart: unless-stopped
volumes:
- ./mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
- ./mosquitto/config:/mosquitto/config
- ./swag/etc/letsencrypt:/var/tmp
- ./mosquitto/data:/mosquitto/data
- ./mosquitto/log:/mosquitto/log
grafana:
image: grafana/grafana-oss
container_name: grafana
user: "1000"
volumes:
- ./grafana:/var/lib/grafana
ports:
- 3000:3000
depends_on:
- mariadb
environment:
GF_SERVER_PROTOCOL: http
GF_SERVER_ROOT_URL: https://${DOMINIO}:${PUERTO}/grafana/
# GF_SERVER_SERVE_FROM_SUB_PATH: "true"
GF_SERVER_DOMAIN: https://${DOMINIO}
GF_ANALYTICS_REPORTING_ENABLED: "false"
restart: unless-stopped
portainer:
image: portainer/portainer-ce
container_name: portainer
ports:
- 9443:9443
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- portainer_data:/data
restart: unless-stopped
swag:
image: lscr.io/linuxserver/swag:latest
container_name: swag
cap_add:
- NET_ADMIN
environment:
- PUID=1000
- PGID=1000
- TZ=America/Argentina/Buenos_Aires
- URL=${DOMINIO}
- VALIDATION=dns
- DNSPLUGIN=duckdns
- SUBDOMAINS=
volumes:
- ./swag:/config
ports:
- ${PUERTO}:443/tcp
- 80:80
restart: unless-stopped
telegrambot:
image: telegrambot
container_name: telegrambot
environment:
- TZ=America/Argentina/Buenos_Aires
- TB_TOKEN=${TB_TOKEN}
- MARIADB_SERVER=${MARIADB_SERVER}
- MARIADB_USER=${MARIADB_USER}
- MARIADB_USER_PASS=${MARIADB_USER_PASS}
- MARIADB_DB=${MARIADB_DB}
- DOMINIO=${DOMINIO}
- MQTT_USR=${MQTT_USR}
- MQTT_PASS=${MQTT_PASS}
- PUERTO_MQTTS=${PUERTO_MQTTS}
restart: unless-stopped
depends_on:
- mariadb
volumes:
portainer_data:
36 changes: 36 additions & 0 deletions sensores_remotos.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--
-- Base de datos: `sensores_remotos`
--
CREATE DATABASE IF NOT EXISTS `sensores_remotos` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `sensores_remotos`;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `mediciones`
--

CREATE TABLE `mediciones` (
`id` int(11) NOT NULL,
`sensor_id` char(12) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT current_timestamp(),
`temperatura` decimal(3,1) NOT NULL,
`humedad` decimal(3,1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Indices de la tabla `mediciones`
--
ALTER TABLE `mediciones`
ADD PRIMARY KEY (`id`),
ADD KEY `timestamp` (`timestamp`);

--
-- AUTO_INCREMENT de la tabla `mediciones`
--
ALTER TABLE `mediciones`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;

CREATE USER 'mediciones'@'%' IDENTIFIED BY 'passworddeiot';GRANT USAGE ON *.* TO 'mediciones'@'%' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
GRANT SELECT, INSERT ON `sensores\_remotos`.* TO 'mediciones'@'%';
13 changes: 13 additions & 0 deletions telegrambot/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.11-slim

WORKDIR /app

ENV TZ="America/Argentina/Buenos_Aires"

COPY ./requirements.txt /app/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt

COPY . /app

CMD ["python", "/app/telegrambot.py"]
4 changes: 4 additions & 0 deletions telegrambot/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
python-telegram-bot
aiomysql
aiomqtt
matplotlib
Loading