You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
50 lines
1.2 KiB
import json
|
|
import time
|
|
|
|
import paho.mqtt.client as mqtt
|
|
import threading
|
|
|
|
id = '<ID>'
|
|
|
|
client_telemetry_topic = id + '/telemetry'
|
|
server_command_topic = id + '/commands'
|
|
client_name = id + 'soilmoisturesensor_server'
|
|
|
|
mqtt_client = mqtt.Client(client_name)
|
|
mqtt_client.connect('test.mosquitto.org')
|
|
|
|
mqtt_client.loop_start()
|
|
|
|
water_time = 5
|
|
wait_time = 20
|
|
|
|
def send_relay_command(client, state):
|
|
command = { 'relay_on' : state }
|
|
print("Sending message:", command)
|
|
client.publish(server_command_topic, json.dumps(command))
|
|
|
|
def control_relay(client):
|
|
print("Unsubscribing from telemetry")
|
|
mqtt_client.unsubscribe(client_telemetry_topic)
|
|
|
|
send_relay_command(client, True)
|
|
time.sleep(water_time)
|
|
send_relay_command(client, False)
|
|
|
|
time.sleep(wait_time)
|
|
|
|
print("Subscribing to telemetry")
|
|
mqtt_client.subscribe(client_telemetry_topic)
|
|
|
|
def handle_telemetry(client, userdata, message):
|
|
payload = json.loads(message.payload.decode())
|
|
print("Message received:", payload)
|
|
|
|
if payload['soil_moisture'] > 450:
|
|
threading.Thread(target=control_relay, args=(client,)).start()
|
|
|
|
mqtt_client.subscribe(client_telemetry_topic)
|
|
mqtt_client.on_message = handle_telemetry
|
|
|
|
while True:
|
|
time.sleep(2) |