最小MQTT確認用ソースUP
This commit is contained in:
parent
b319d0a913
commit
1cf14c5755
5
.env.sample
Normal file
5
.env.sample
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# MQTT CONFIGURATION
|
||||||
|
MQTT_HOST=localhost
|
||||||
|
MQTT_PORT=1883
|
||||||
|
MQTT_DEVICE_CODE=test0001
|
||||||
|
|
||||||
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
mqttmock
|
||||||
|
.env
|
||||||
28
README.md
28
README.md
@ -0,0 +1,28 @@
|
|||||||
|
# mosquitto-mock-server
|
||||||
|
|
||||||
|
* Mosquittoを構築して、MQTTの動作を検証するためのブランチ
|
||||||
|
* Broker
|
||||||
|
* ブローカにはmosquittoを使用する
|
||||||
|
* Client
|
||||||
|
* Paho-mqttライブラリを使用して接続する
|
||||||
|
|
||||||
|
|
||||||
|
## init
|
||||||
|
|
||||||
|
|
||||||
|
### 開発用仮想空間を作成する
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python -m venv mqttmock
|
||||||
|
# (windows)
|
||||||
|
mqttmock\Scripts\activate
|
||||||
|
# (linux)
|
||||||
|
source mqttmock/bin/activate
|
||||||
|
```
|
||||||
|
|
||||||
|
ライブラリをインストールする
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
34
client/mqtt_client.py
Normal file
34
client/mqtt_client.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
import time
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
load_dotenv(dotenv_path=".env.test")
|
||||||
|
|
||||||
|
MQTT_HOST = os.getenv("MQTT_HOST")
|
||||||
|
MQTT_PORT = int(os.getenv("MQTT_PORT"))
|
||||||
|
MQTT_DEVICE_CODE = int(os.getenv("MQTT_DEVICE_CODE"))
|
||||||
|
|
||||||
|
|
||||||
|
def on_connect_mock(client, userdata, flags, rc, properties):
|
||||||
|
print(f"Connected with result code {rc}")
|
||||||
|
|
||||||
|
|
||||||
|
def test_mqtt_mock():
|
||||||
|
|
||||||
|
# connect処理
|
||||||
|
client = mqtt.Client()
|
||||||
|
client.on_connect = on_connect_mock
|
||||||
|
client.connect(MQTT_HOST, MQTT_PORT, 60)
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print("Mqtt Mock Client Start.")
|
||||||
|
test_mqtt_mock()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
8
docker-compose.yaml
Normal file
8
docker-compose.yaml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
version: '3.8'
|
||||||
|
services:
|
||||||
|
mosquitto-mock:
|
||||||
|
image: eclipse-mosquitto:latest
|
||||||
|
ports:
|
||||||
|
- "1883:1883" # 標準のMQTTポート
|
||||||
|
volumes:
|
||||||
|
- ./mosquitto/config:/mosquitto/config # Mosquittoの設定ファイルが含まれるディレクトリ
|
||||||
4
mosquitto/config/mosquitto.conf
Normal file
4
mosquitto/config/mosquitto.conf
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
set_tcp_nodelay true
|
||||||
|
listener 1883
|
||||||
|
allow_anonymous true
|
||||||
|
max_queued_messages 0
|
||||||
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
paho-mqtt
|
||||||
|
requests
|
||||||
|
python-dotenv
|
||||||
|
requests
|
||||||
Loading…
x
Reference in New Issue
Block a user