-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonHYDROPONIC.py
64 lines (52 loc) · 2.25 KB
/
MonHYDROPONIC.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import MCP3202, os
from time import sleep
import math
import Adafruit_DHT
import RPi.GPIO as gpio
import paho.mqtt.client as paho
import telepot
from telepot.loop import MessageLoop
Aman_id=******
bot = telepot.Bot('1023574477:AAFDMgn1TBpBG******qHya3b2IHPT1kg')
DHT_SENSOR = Adafruit_DHT.DHT11
gpio = 5
broker="10.42.0.1" #ip broker
port=1883
def translate(value,leftMin,leftMax,rightMin,rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
return rightMin + (valueScaled * rightSpan)
def on_publish(client,userdata,result): #create function for callback
print("data published \n")
pass
try:
while True:
os.system("clear")
value1 = MCP3202.readADC(0)
map = translate(value1, 0, 1023, 0, 100)
output = map * 3.3 / 100
output = (133.42*output*output*output - 255.86*output*output + 857.39*output)/2
tds = str(math.trunc(output))
print(tds +" ppm")
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, gpio)
if humidity is not None and temperature is not None:
print("Temp={0:0.1f}*C Humidity={1:0.1f}%".format(temperature, humidity))
else:
print("Failed to retrieve data from humidity sensor")
client1= paho.Client("suhu") #create client object
client1.on_publish = on_publish #assign function to callback
client1.connect(broker,port) #establish connection
ret= client1.publish("temp",temperature) #publish
ret= client1.publish("hum", humidity)
ret= client1.publish("tds", tds)
tdsnilai = output
if temperature > 30 :
bot.sendMessage(Aman_id, 'hey! tanaman hidroponik mu kepanasan')
elif tdsnilai > 1200 :
bot.sendMessage(Aman_id, 'hey! tds tanaman hidroponik mu telah melebihi jumlah maksimal, segera tambahkan air')
except KeyboardInterrupt:
print("bye")