def __init__(self): super().__init__() subscribe('localhost', "iot/#", self.on_message, forever=False) self.sensors = { 'temp': [], 'humi': [], 'illu': [], 'dust': [], }
from mqtt_sub import subscribe import time def on_message(client, userdata, msg): print(msg.topic + " " + str(msg.payload)) subscribe('localhost', 'iot/#', on_message, forever=False) time.sleep(5) print('End')
def sub(): subscribe('localhost', 'iot/#', on_message)
from mqtt_sub import subscribe from datetime import datetime FILE_NAME = 'sesorvalues.csv' def on_message(client, userdata, msg): with open(FILE_NAME, 'at') as f: f.write(f'{datetime.now()},{msg.topic},{float(msg.payload)}\n') subscribe('localhost', 'iot/#', on_message)
from pymongo import MongoClient from datetime import datetime from mqtt_sub import subscribe db_client = MongoClient("mongodb://localhost:27017/") iot_db = db_client['iot_service'] sensors_col = iot_db['sensors'] def on_message(client, userdata, msg): msg.payload = msg.payload.decode("utf-8") # byte 데이터를 utf-8 문자열로 변환 print(msg.topic + " " + str(msg.payload)) sensor_value = { "topic": msg.topic, "value": float(str(msg.payload)), "reg_date": datetime.now() } sensors_col.insert_one(sensor_value) # C:\Users\hongj\TIL\arduino\NODEMCU\ex07\app.ino(온,습도 publish 하는 코드) # 실행하면 여기서 subscribe subscribe("localhost", "user1/home/#", on_message)
from pymongo import MongoClient from datetime import datetime from mqtt_sub import subscribe db_client = MongoClient("mongodb://localhost:27017/") iot_db = db_client['iot_service'] # 데이터베이스 선택, 없으면 자동 생성 sensors_col = iot_db['sensors'] # 컬렉션 선택, 없으면 자동 생성 def on_message(client, userdata, msg): msg.payload = msg.payload.decode("utf-8") # byte 데이터를 utf-8 문자열로 변환 print(msg.topic+" "+msg.payload) sensor_value = { "topic": msg.topic, "value": float(msg.payload), "reg_date": datetime.now() # 현재 시간 } sensors_col.insert_one(sensor_value) subscribe('localhost', 'user1/home/#', on_message)
def sub(): global door subscribe('172.30.1.87', 'iot/magnetic', on_message) door = ""