Skip to content

Commit

Permalink
improving mongodb dht
Browse files Browse the repository at this point in the history
  • Loading branch information
JAlcocerT committed Oct 20, 2023
1 parent 9723b0d commit 1c62e9f
Show file tree
Hide file tree
Showing 8 changed files with 670 additions and 8 deletions.
19 changes: 19 additions & 0 deletions Z_IoT/DHT-to-MongoDB/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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 and MongoDB
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 Python2MongoDB.py /app/

# Install additional dependencies
RUN pip install Adafruit_DHT==1.4.0 pip install pymongo==4.5.0

# Run the Python script
CMD ["python", "Python2MongoDB.py"]m
115 changes: 115 additions & 0 deletions Z_IoT/DHT-to-MongoDB/Python2MongoDB-Stack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
### Already Built ###

#docker build -t dht_sensor_mongo .

version: '3'
services:
mongodb:
image: mongo:latest
container_name: mongodb
environment:
MONGO_INITDB_ROOT_USERNAME: yourusername
MONGO_INITDB_ROOT_PASSWORD: yourpassword
MONGO_INITDB_DATABASE: sensor_data
volumes:
- mongodb_data:/data/db
ports:
- "27017:27017"
restart: always

dht_sensor_mongo:
image: dht_sensor_mongo:latest # Use the name of your custom image
container_name: dht_sensor_mongo
privileged: true
depends_on:
- mongodb
environment:
MONGODB_HOST: mongodb
MONGODB_PORT: 27017 # Specify the MongoDB port
MONGO_INITDB_ROOT_USERNAME: yourusername # Specify the MongoDB root username
MONGO_INITDB_ROOT_PASSWORD: yourpassword # Specify the MongoDB root password
MONGO_DB_NAME: sensor_data # Specify the MongoDB database name
MONGO_COLLECTION_NAME: dht_sensor # Specify the MongoDB collection name
DHT_SENSOR_TYPE: DHT22 # Set the DHT sensor type here (DHT11 or DHT22)
DHT_PIN: 4 # Set the DHT sensor pin here

volumes:
mongodb_data:



### Only MongoDB ###

version: '3'
services:
mongodb:
image: mongo:latest
container_name: mongodb
environment:
MONGO_INITDB_ROOT_USERNAME: yourusername
MONGO_INITDB_ROOT_PASSWORD: yourpassword
MONGO_INITDB_DATABASE: test
volumes:
- mongodb_data:/data/db
ports:
- "27017:27017"
restart: always

volumes:
mongodb_data:

### Only Python Script ###

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
environment:
MONGODB_HOST: 192.168.3.200
MONGODB_PORT: 27017 # Specify the MongoDB port
MONGO_INITDB_ROOT_USERNAME: yourusername # Specify the MongoDB root username
MONGO_INITDB_ROOT_PASSWORD: yourpassword # Specify the MongoDB root password
MONGO_DB_NAME: sensor_data # Specify the MongoDB database name
MONGO_COLLECTION_NAME: dht_sensor # Specify the MongoDB collection name
DHT_SENSOR_TYPE: DHT22 # Set the DHT sensor type here (DHT11 or DHT22)
DHT_PIN: 4 # Set the DHT sensor pin here


### Building dht_sensor_mongo ###

# version: '3'
# services:
# mongodb:
# image: mongo:latest
# container_name: mongodb
# environment:
# MONGO_INITDB_ROOT_USERNAME: yourusername
# MONGO_INITDB_ROOT_PASSWORD: yourpassword
# MONGO_INITDB_DATABASE: test
# volumes:
# - mongodb_data:/data/db
# ports:
# - "27017:27017"
# networks:
# - mynetwork

# dht_sensor_mongo:
# build:
# context: ./Python2MongoDB.py #./path/to/your/python/code
# dockerfile: Dockerfile
# container_name: dht_sensor_mongo
# depends_on:
# - mongodb
# networks:
# - mynetwork
# privileged: true


# networks:
# mynetwork:
# driver: bridge

# volumes:
# mongodb_data:
80 changes: 80 additions & 0 deletions Z_IoT/DHT-to-MongoDB/Python2MongoDB.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import Adafruit_DHT
import time
import os
from pymongo import MongoClient


dht_sensor_type = os.environ.get('DHT_SENSOR_TYPE', 'DHT11')

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 DHT11.")
DHT_SENSOR = Adafruit_DHT.DHT11


# Get the DHT sensor pin from environment variable (default to 4)
DHT_PIN = int(os.environ.get('DHT_PIN', '4'))


# Configure MongoDB connection parameters
mongo_host = os.environ.get('MONGODB_HOST', 'localhost') # Default to 'localhost' if not set
mongo_port = int(os.environ.get('MONGODB_PORT', '27017'))
mongo_username = os.environ.get('MONGO_INITDB_ROOT_USERNAME', 'yourusername')
mongo_password = os.environ.get('MONGO_INITDB_ROOT_PASSWORD', 'yourpassword')
mongo_db_name = os.environ.get('MONGO_DB_NAME', 'sensor_data')
mongo_collection_name = os.environ.get('MONGO_COLLECTION_NAME', 'dht_sensor')

# Establish MongoDB connection
mongo_client = MongoClient(host=mongo_host, port=mongo_port, username=mongo_username, password=mongo_password)

# Get MongoDB database and collection names from environment variables
mongo_db_name = os.environ.get('MONGO_DB_NAME', 'sensor_data')
mongo_collection_name = os.environ.get('MONGO_COLLECTION_NAME', 'dht_sensor')
db = mongo_client[mongo_db_name]
collection = db[mongo_collection_name]
# 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)


# 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)
33 changes: 33 additions & 0 deletions Z_IoT/DHT-to-MongoDB/mongo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
### Testing MongoDB Connection ###

#pip install pymongo


import pymongo

# MongoDB connection parameters
mongo_host = "192.168.1.100" # Change to your MongoDB host if needed
mongo_port = 27017 # Change to the MongoDB port if needed
mongo_username = "yourusername" # Replace with your MongoDB username
mongo_password = "yourpassword" # Replace with your MongoDB password

try:
# Create a MongoDB client
client = pymongo.MongoClient(host=mongo_host, port=mongo_port, username=mongo_username, password=mongo_password)

# Check if the connection is successful by listing the database names
db_names = client.list_database_names()

# Print a success message and list of database names
print("Successfully connected to MongoDB!")
print("Available databases:")
for db_name in db_names:
print(f"- {db_name}")

except Exception as e:
# If there's an error, print an error message
print(f"Error connecting to MongoDB: {e}")

finally:
# Close the MongoDB client
client.close()
8 changes: 0 additions & 8 deletions _posts/2023-06-05-rpi-iot-dht11-mongo.md

This file was deleted.

127 changes: 127 additions & 0 deletions _posts/2023-06-05-rpi-iot-mongodatabricks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
---
title: RPi IoT Project - Analizing sensors Data with MongoDB & Databricks
author: JAlcocerT
date: 2023-06-05 14:10:00 +0800
categories: [IoT & Data Analytics]
tags: [Sensors,Python,MongoDB]
render_with_liquid: false
---

### ToDo list

- [ ] Job
+ [x] The Python Script
+ [] MongoDB Cloud Setup
+ [ ] Databricks


## MongoDB Cloud Setup

Get a Cloud VM running (up to you to choose the provider) and get [Docker and Docker-Compose installed](https://jalcocert.github.io/RPi/posts/selfhosting-with-docker/).


You can Install Portainer as well and use the Stack I prepared for Mongo, or just execute this from the CLI to spin a MongoDB container.

```sh
docker run -d \
--name mongodb \
-e MONGO_INITDB_ROOT_USERNAME=yourusername \
-e MONGO_INITDB_ROOT_PASSWORD=yourpassword \
-e MONGO_INITDB_DATABASE=test \
-v mongodb_data:/data/db \
-p 27017:27017 \
--restart always \
mongo:latest
```




Get the hostname with:
```sh
curl -sS https://ipinfo.io/json #the command to use
```

Make sure that the MongoDB port is ready to accept connections, by default is 27017 TCP.


## The Python Script


Make your Raspberry Pi run this container

```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
environment:
MONGODB_HOST: 192.168.3.200 #a GCP Compute Engine that is running MongoDB
MONGODB_PORT: 27017 # Specify the MongoDB port
MONGO_INITDB_ROOT_USERNAME: yourusername # Specify the MongoDB root username
MONGO_INITDB_ROOT_PASSWORD: yourpassword # Specify the MongoDB root password
MONGO_DB_NAME: sensor_data # Specify the MongoDB database name
MONGO_COLLECTION_NAME: dht_sensor # Specify the MongoDB collection name
DHT_SENSOR_TYPE: DHT22 # Set the DHT sensor type here (DHT11 or DHT22)
DHT_PIN: 4 # Set the DHT sensor pin here

```


## Checking that it works

Get an interactive terminal of your MongoDB container and login to the Mongo Shell:

```sh
docker exec -it mongodb sh
mongosh --username yourusername --password yourpassword --authenticationDatabase admin
```

```sh
show dbs

# Switch to the our DB
use sensor_data

# List all collections in the "sensor_data" database
show collections

# And to get the latest ones
db.dht_sensor.find().sort({ timestamp: -1 }).limit(10);


db.dht_sensor.findOne();

#db.dht_sensor.explain().find({});
```

- [ ] Job
+ [x] The Python Script
+ [x] MongoDB Cloud Setup
+ [ ] Databricks


## Databricks


<https://community.cloud.databricks.com/>


```py
#connectionString='mongodb+srv://CONNECTION_STRING_HERE/ #mongodb://username:password@host:port/database
mongodb://yourusername:yourpassword@host:27017/test
mongodb://yourusername:yourpassword@34.27.181.245:27017/sensor_data
database="sample_supplies"
collection="sales"
```


db.dht_sensor.findOne();
{
_id: ObjectId("65324697e3578aaa6690a953"),
timestamp: '2023-10-20T09:21:27Z',
temperature: 21.700000762939453,
humidity: 52.599998474121094
}
Loading

0 comments on commit 1c62e9f

Please sign in to comment.