import paho.mqtt.client as paho
import ftrobopy
from time import sleep
import statistics
import bme680
import json
import threading
#TXT---------------------------------------------------------------------------------------------------------
txt = ftrobopy.ftrobopy('192.168.7.2', 65000)
#MQTT--------------------------------------------------------------------------------------------------------
broker="your-broker-host"
port=your-broker-port
topic="your-broker-topics"
username = 'your-broker-username'
password = 'your-broker-password'
#IO1---------------------------------------------------------------------------------------------------------
ioPin01 = txt.resistor(1)
#IO4---------------------------------------------------------------------------------------------------------
ioPin04 = txt.voltage(4)
#IO5---------------------------------------------------------------------------------------------------------
ioPin05 = txt.voltage(5)
io05VoltListSamples=[]
#IO6---------------------------------------------------------------------------------------------------------
ioPin06 = txt.voltage(6)
cal1Volt=1000
cal1Temp=27
#TableODEquivalence https://wiki.dfrobot.com/Gravity__Analog_Dissolved_Oxygen_Sensor_SKU_SEN0237
odEquivList=[14460,14220,13820,13440,13090,12760,12440,12110,11830,11560,11290,11040,10760,10540,10310,10060,9860,9640,9470,9270,9090,8910,8740,8570,8410,8250,8110,7960,7830,7680,7560,7430,7300,7170,7060,6940,6940,6840,6600,6520,6400,6330,6230,6130,6060,5970,5880,5790]
#M3----------------------------------------------------------------------------------------------------------
M3 = txt.motor(3)
M3Status = 'stop'
#------------------------------------------------------------------------------------------------------------
samples=20
offSetTempAmbiente= 0.0
offSetTempAgua= 0.0
offSetPh= 0.0
txtTimeAcum=0
pumpTime=0
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
def get_io01_samples(samples):
io01SamplesSum=0
io01VoltSamples=[]
for i in range(samples):
txt.updateWait(0.05)
ioVoltPin01Value = ioPin01.ntcTemperature()
ioVoltPin01ValueSample = '{:.2f}'.format(ioVoltPin01Value)
io01VoltSamples.append(float(ioVoltPin01ValueSample))
if i < samples:
continue
for j, val in enumerate(io01VoltSamples):
io01SamplesSum = val + io01SamplesSum
return io01SamplesSum/20
def get_io04_samples(samples):
io04SamplesSum=0
io04VoltSamples=[]
for i in range(samples):
txt.updateWait(0.05)
ioVoltPin04Value = ioPin04.voltage()/1000
ioVoltPin04ValueSample = '{:.2f}'.format(ioVoltPin04Value)
io04VoltSamples.append(float(ioVoltPin04ValueSample))
if i < samples:
continue
for j, val in enumerate(io04VoltSamples):
io04SamplesSum = val + io04SamplesSum
return io04SamplesSum/20
def get_io05_samples(samples):
io05SamplesSum=0
io05VoltSamples=[]
for i in range(samples):
txt.updateWait(0.1)
ioVoltPin05Value = ioPin05.voltage()/1000
ioVoltPin05ValueSample = '{:.2f}'.format(ioVoltPin05Value)
io05VoltSamples.append(float(ioVoltPin05ValueSample))
if i < samples:
continue
for j, val in enumerate(io05VoltSamples):
io05SamplesSum = val + io05SamplesSum
return io05SamplesSum/20
def get_io06_samples(samples):
io06SamplesSum=0
io06VoltSamples=[]
for i in range(samples):
txt.updateWait(0.05)
ioVoltPin06Value = ioPin06.voltage()/1000
ioVoltPin06ValueSample = '{:.2f}'.format(ioVoltPin06Value)
io06VoltSamples.append(float(ioVoltPin06ValueSample))
if i < samples:
continue
for j, val in enumerate(io06VoltSamples):
io06SamplesSum = val + io06SamplesSum
return io06SamplesSum/20
def json_mqtt(your-broker-payload):
j={
"payload" : { "your-broker-payload"
}
}
p = json.dumps(j)
return p
client = paho.Client()
client.username_pw_set("your-broker-username","your-broker-password")
client.on_connect = on_connect
client.connect("your-broker-host", your-broker-port)
client.loop_start()
while 1:
#temp---------------------------------------------
tempAgua = get_io01_samples(samples)+offSetTempAgua
#bme680--------------------------------------------
try:
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except (RuntimeError, IOError):
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_filter(bme680.FILTER_SIZE_3)
temp= '{:.1f}'.format(sensor.data.temperature)
hum= '{:.0f}'.format(sensor.data.humidity)
tempAmbiente= float(temp)-offSetTempAmbiente
tempComp=1+0.02*(tempAgua-25)
#od------------------------------------------------
ioPin06Volt = get_io06_samples(samples)
voltSat = cal1Volt + 35 * int(tempAgua) - cal1Temp * 35
od = (((ioPin06Volt*1000) * odEquivList[int(tempAgua)]) / voltSat)/1000
odFormat="{:.2f}".format(od)
#ph------------------------------------------------
ioPin05Volt=get_io05_samples(samples)
phValue=ioPin05Volt*3.5+offSetPh
phFormat=("{:.1f}".format(phValue))
#tds-----------------------------------------------
tdsCompVolt = get_io04_samples(samples)/tempComp
tdsValue=(133.42*tdsCompVolt**3 - 255.86*tdsCompVolt**2 + 857.39*tdsCompVolt)*0.55
tdsFormat="{:.0f}".format(tdsValue)
#print("{:.0f}".format(tdsValue))
#print("{:.1f}".format(phValue))
#print("{:.2f}".format(odFormat))
#print("{:.1f}".format(tempAmbiente))
#print("{:.1f}".format(tempAgua))
txt.updateWait(10)
pumpTime=pumpTime+10
print(pumpTime)
if pumpTime >= 1800:
txtTimeAcum=txtTimeAcum+10
if M3Status == 'stop':
M3Status='run'
M3.setSpeed(512)
#t1.start()
print(txtTimeAcum)
else:
if txtTimeAcum >= 900:
M3Status='stop'
M3.stop()
txtTimeAcum=0
pumpTime=0
print(txtTimeAcum)
print("PUMP STOP")
else:
pumpTimeMin=30-pumpTime/60
client.publish("your-broker-topics","your-broker-payload"),qos=1)
Comments