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.
IoT-For-Beginners/2-farm/lessons/6-keep-your-plant-secure/code/pi/soil-moisture-sensor/app.py

40 lines
1.1 KiB

import time
from grove.adc import ADC
from grove.grove_relay import GroveRelay
import json
from azure.iot.device import IoTHubDeviceClient, Message, MethodResponse, X509
adc = ADC()
relay = GroveRelay(5)
host_name = "<host_name>.azure-devices.net"
device_id = "soil-moisture-sensor-x509"
x509 = X509("./soil-moisture-sensor-x509-cert.pem", "./soil-moisture-sensor-x509-key.pem")
device_client = IoTHubDeviceClient.create_from_x509_certificate(x509, host_name, device_id)
print("Connecting")
device_client.connect()
print("Connected")
def handle_method_request(request):
print("Direct method received - ", request.name)
if request.name == "relay_on":
relay.on()
elif request.name == "relay_off":
relay.off()
method_response = MethodResponse.create_from_method_request(request, 200)
device_client.send_method_response(method_response)
device_client.on_method_request_received = handle_method_request
while True:
soil_moisture = adc.read(0)
print("Soil moisture:", soil_moisture)
message = Message(json.dumps({ "soil_moisture": soil_moisture }))
device_client.send_message(message)
time.sleep(10)