51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
|
import json
|
||
|
import threading
|
||
|
import time
|
||
|
|
||
|
import serial
|
||
|
import paho.mqtt.client as mqtt
|
||
|
|
||
|
from api import add
|
||
|
from db.models.log_data_model import LOT_DATA
|
||
|
|
||
|
|
||
|
def t():
|
||
|
while True:
|
||
|
data = {'ambient_temperature': 2,
|
||
|
'ambient_humidity': 2,
|
||
|
'carbon_dioxide': 2,
|
||
|
'ambient_air_pressure': 12,
|
||
|
'ambient_lighting': 12,
|
||
|
'soil_moisture': 12,
|
||
|
'soil_temperature': 12,
|
||
|
'soil_conductivity': 12,
|
||
|
'soil_PH': 12,
|
||
|
'soil_potassium_phosphate_nitrogen': 12,
|
||
|
'soil_potassium_phosphate_phosphorus': 12,
|
||
|
'soil_potassium_phosphate_potassium': 12,
|
||
|
'rainfall': 2,
|
||
|
'wind_speed': 2,
|
||
|
'wind_direction': 2,
|
||
|
'create_time': int(time.time())
|
||
|
}
|
||
|
t = LOT_DATA(**data)
|
||
|
client.publish('demo', payload=json.dumps(data, ensure_ascii=False), qos=0)
|
||
|
# add(t)
|
||
|
print(t)
|
||
|
time.sleep(1)
|
||
|
|
||
|
|
||
|
def on_connect(client, userdata, flags, rc):
|
||
|
threading.Thread(target=t).start()
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
client = mqtt.Client(transport="websockets")
|
||
|
client.username_pw_set("demo", "123456")
|
||
|
# Specify callback function
|
||
|
client.on_connect = on_connect
|
||
|
# Establish a connection
|
||
|
client.connect('ceshi-mqtt.lihaink.cn', 8083)
|
||
|
# Publish a message
|
||
|
client.loop_forever()
|