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.
39 lines
915 B
39 lines
915 B
import time
|
|
from grove.adc import ADC
|
|
from grove.grove_relay import GroveRelay
|
|
import json
|
|
import paho.mqtt.client as mqtt
|
|
|
|
adc = ADC()
|
|
relay = GroveRelay(5)
|
|
|
|
id = '<ID>'
|
|
|
|
client_telemetry_topic = id + '/telemetry'
|
|
server_command_topic = id + '/commands'
|
|
client_name = id + 'soilmoisturesensor_client'
|
|
|
|
mqtt_client = mqtt.Client(client_name)
|
|
mqtt_client.connect('test.mosquitto.org')
|
|
|
|
mqtt_client.loop_start()
|
|
|
|
def handle_command(client, userdata, message):
|
|
payload = json.loads(message.payload.decode())
|
|
print("Message received:", payload)
|
|
|
|
if payload['relay_on']:
|
|
relay.on()
|
|
else:
|
|
relay.off()
|
|
|
|
mqtt_client.subscribe(server_command_topic)
|
|
mqtt_client.on_message = handle_command
|
|
|
|
while True:
|
|
soil_moisture = adc.read(0)
|
|
print("Soil moisture:", soil_moisture)
|
|
|
|
mqtt_client.publish(client_telemetry_topic, json.dumps({'soil_moisture' : soil_moisture}))
|
|
|
|
time.sleep(10) |