Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomvictor/iotcore
MQTT Broker and IoT Capabilities written in Rust for Python, Django and FastAPI
https://github.com/tomvictor/iotcore
asyncio django django-rest-framework fastapi iot mqtt mqtt-broker mqtt-client python rust rust-lang
Last synced: 10 days ago
JSON representation
MQTT Broker and IoT Capabilities written in Rust for Python, Django and FastAPI
- Host: GitHub
- URL: https://github.com/tomvictor/iotcore
- Owner: tomvictor
- License: mit
- Created: 2016-08-13T11:16:56.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2024-08-20T11:35:52.000Z (3 months ago)
- Last Synced: 2024-10-12T07:31:21.789Z (25 days ago)
- Topics: asyncio, django, django-rest-framework, fastapi, iot, mqtt, mqtt-broker, mqtt-client, python, rust, rust-lang
- Language: Rust
- Homepage: https://tomvictor.dev/iotcore/
- Size: 27 MB
- Stars: 48
- Watchers: 8
- Forks: 7
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Iotcore - Python MQTT Broker and IoT Features for Django and FastAPI
[![.github/workflows/CI.yml](https://github.com/tomvictor/iotcore/actions/workflows/CI.yml/badge.svg)](https://github.com/tomvictor/iotcore/actions/workflows/CI.yml)
[![](https://img.shields.io/pypi/v/iotcore?color=%2334D058&label=pypi%20package)](https://pypi.org/project/iotcore)
[![](https://img.shields.io/pypi/pyversions/fastapi.svg?color=%2334D058)](https://pypi.org/project/iotcore)A Python package, written in Rust, helps to run MQTT Broker and subscribe to MQTT topics in a multithreaded manner without any extra Python dependency. The internals of the MQTT server are written in Rust using the Tokio framework. The motive of the project is to overcome the GIL limitation, provide simple-to-use MQTT broker Python projects, and bring all the concurrent features offered by Rust to Python.
## Features
* Full-fledged configurable Tokio based MQTT broker
* No python GIL limitation
* All Standard MQTT broker features
* Zero extra setup required to run mqtt broker in you Django and Fastapi project
* MQTT client, with callback support for async or non-blocking applications
* and more## Planned Features
* Device support
* Sensor support
* Sensor data storage
* Django based admin pages
* Django rest framework based APIs for managing devices and sensors
* SSL certificates and policy management## Installation
```
pip install iotcore
```Create a new file called mqtt.toml in your root project directory and copy pase the sample mqtt.toml from
https://tomvictor.github.io/iotcore/config/## FastAPI setup
**Broker only**
```python
from fastapi import FastAPI
from iotcore.fastapi import iotcore_brokerapp = FastAPI(lifespan=iotcore_broker)
@app.get("/")
def read_root():
return {"Hello": "World"}```
**Broker plus Mqtt client**
```python
from fastapi import FastAPI
from contextlib import asynccontextmanager
from iotcore import IotCoreiot = IotCore()
@asynccontextmanager
async def lifespan(app: FastAPI):
iot.background_loop_forever()
yieldapp = FastAPI(lifespan=lifespan)
@iot.accept(topic="temperature")
def temperature_data(request):
print(f"Temperature data : {request}")def mqtt_callback(data):
print(f"iot >: {data}")@app.get("/sub")
def sub():
iot.subscribe("iot", mqtt_callback)
return {"response": "subscribed"}@app.get("/pub")
def pub():
iot.publish("temperature", "{'temp': 18}")
return {"response": "published"}@app.get("/")
def home():
return {"Hello": "World"}```
## Django Setup
```python
from django.http import JsonResponse
from iotcore import IotCoreiot = IotCore()
iot.background_loop_forever()def mqtt_callback(data):
print(f"Django >: {data}")def subscribe(request):
iot.subscribe("iot", mqtt_callback)
return JsonResponse({"response": "subscribed"})def publish(request):
iot.publish("iot", "demo")
return JsonResponse({"response": "published"})
```Now Connect to mqtt broker on localhost
MQTT Port : 1883## Run Example project
**Django**
```shell
pip install iotcore
pip install djangopython examples/django/manage.py runserver
```**FastAPI**
```shell
pip install iotcore
pip install fastapi
pip install uvicornuvicorn examples.fastapi.main:app
```Open you mqtt client and use below details to connect to the broker:
**_Host_**: **127.0.0.1** or **localhost**
**_Port_**: **1883**## Contribute
- Issue Tracker: github.com/tomvictor/iotcore/issues
- Source Code: github.com/tomvictor/iotcore## Support
Star the project on GitHub :)
## License
The project is licensed under the MIT license.