-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirrigation.py
50 lines (39 loc) · 1.28 KB
/
irrigation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
from time import sleep
# MQTT settings
mqtt_broker = "127.0.0.1"
mqtt_topic_irrigation = "irrigation"
# Set up GPIO for analog input
analog_pin = 23 # GPIO 23 (pin 16) for analog input
# MQTT on_connect callback
def on_connect(client, userdata, flags, rc):
print(f"Connected with result code {rc}")
# Read and publish analog values
def read_and_publish_analog_values():
GPIO.setmode(GPIO.BCM)
GPIO.setup(analog_pin, GPIO.IN)
client = mqtt.Client()
client.on_connect = on_connect
# Connect to the MQTT broker
client.connect(mqtt_broker, 1883, 60)
client.loop_start()
try:
while True:
# Read the analog value from GPIO
analog_value = GPIO.input(analog_pin)
# Publish the analog value to the "irrigation" topic
client.publish(mqtt_topic_irrigation, str(analog_value))
sleep(1)
except KeyboardInterrupt:
client.disconnect()
if __name__ == "__main__":
# Create a new thread for reading and publishing analog values
import threading
analog_thread = threading.Thread(target=read_and_publish_analog_values)
analog_thread.start()
try:
while True:
sleep(1)
except KeyboardInterrupt:
pass