Cov txheej txheem:

Ntse Kas Fes Tshuab Twj Tswj los ntawm Raspberry Pi & HC-SR04 Ultrasonic Sensor thiab Cloud4RPi: 6 Cov Kauj Ruam
Ntse Kas Fes Tshuab Twj Tswj los ntawm Raspberry Pi & HC-SR04 Ultrasonic Sensor thiab Cloud4RPi: 6 Cov Kauj Ruam

Video: Ntse Kas Fes Tshuab Twj Tswj los ntawm Raspberry Pi & HC-SR04 Ultrasonic Sensor thiab Cloud4RPi: 6 Cov Kauj Ruam

Video: Ntse Kas Fes Tshuab Twj Tswj los ntawm Raspberry Pi & HC-SR04 Ultrasonic Sensor thiab Cloud4RPi: 6 Cov Kauj Ruam
Video: Saib Mis paub Poj niam (hluas nkauj) tus yam ntxwv lub siab 2024, Hlis ntuj nqeg
Anonim
Ntse Kas Fes Tshuab Twj Tswj los ntawm Raspberry Pi & HC-SR04 Ultrasonic Sensor thiab Cloud4RPi
Ntse Kas Fes Tshuab Twj Tswj los ntawm Raspberry Pi & HC-SR04 Ultrasonic Sensor thiab Cloud4RPi

Hauv kev xav, txhua lub sijhawm koj mus rau lub tshuab kas fes rau koj lub khob thaum sawv ntxov, tsuas yog muaj ib-hauv-nees nkaum lub sijhawm koj yuav tau ntim cov dej tso rau hauv. Hauv kev xyaum, txawm li cas los xij, nws zoo li lub tshuab pom qee txoj hauv kev ib txwm ua txoj haujlwm no rau koj. Qhov koj xav tau kas fes ntau dua, qhov muaj feem yuav ua rau koj txaus ntshai "sau lub tank dej" cov lus. Kuv cov npoj yaig xav zoo ib yam txog qhov no. Ua nerds uas peb yog, peb txiav txim siab los siv thev naus laus zis uas yuav tso qhov kawg rau qhov no.

Khoom siv

Peb Cov Khoom Siv

Peb muaj SAECO Aulika Focus tshuab kas fes. Txog rau hnub no, peb siv lub twj tso kua mis kom puv lub tshuab lub tank dej los ntawm tus qauv 5 Gallon (19L) lub raj mis dej.

Peb Lub Hom Phiaj

  1. Siv lub tshuab hluav taws xob uas tau tsav los ntawm qee yam xws li tus tswj lossis lub tshuab microcomputer los ntawm kev xa tawm.
  2. Muaj ib txoj hauv kev los ntsuas cov qib dej hauv lub tshuab kas fes lub tank kom peb cov txheej txheem paub thaum twg yuav tsum rov ua dua.
  3. Muaj txhais tau tias los tswj cov kab ke, nyiam dua lub sijhawm tiag tiag los ntawm lub xov tooj ntawm tes.
  4. Tau txais cov ntawv ceeb toom (los ntawm Slack lossis cov kev pabcuam zoo sib xws) yog tias muaj dab tsi ua tsis raug nrog lub system.

Kauj Ruam 1: Xaiv Cov Khoom Siv

Xaiv Cov Khoom Siv
Xaiv Cov Khoom Siv
Xaiv Cov Khoom Siv
Xaiv Cov Khoom Siv
Xaiv Cov Khoom Siv
Xaiv Cov Khoom Siv
Xaiv Cov Khoom Siv
Xaiv Cov Khoom Siv

Lub Pump

Kev tshawb nrhiav lub vev xaib sai yuav qhia ntau lub tshuab fais fab tsim rau koj lub raj mis dej uas koj xaiv. Cov twj zoo li no feem ntau yog tswj los ntawm ON/OFF hloov (piv txwv li, Kub Frost A12 lossis SMixx ХL-D2). Nov yog lub twj tso kua mis peb xaiv rau peb tes num.

Tus Tswj Cov Ntaus

Peb tau sim ntau yam khoom siv tab sis tseb ntawm Raspberry Pi vim qhov zoo hauv qab no:

  • Nws muaj GPIO uas tso cai rau peb txuas lub sensor sib thooj
  • Nws txhawb Python

Peb tau teeb tsa qhov tshiab ntawm Raspbian Buster Lite thiab txhua yam xav tau los khiav Python 3.

Yuav Ua Li Cas Peb Toggle Lub Pump

Txhawm rau tswj lub zog, peb tau xaiv lub zog nruab nrab (12V/2A) lub xeev hloov pauv kom haum rau kev hloov pauv tam sim no. Qhov txuas txuas lub twj tso kua mis mus rau lub qhov hluav taws xob thiab raug tswj los ntawm Raspberry Pi tus lej digital.

Yuav Ua Li Cas Peb Txheeb Xyuas Cov Dej

Nws yog qhov tseem ceeb rau peb kom tsis hloov kho lub tshuab kas fes kev tsim kho, yog li peb txiav txim siab siv HC-SR04 Ultrasonic sib thooj sensor los ntsuas qib dej.

Peb 3d-luam tawm cov txheej txheem dej tank npog nrog ob lub qhov rau lub ntsuas pa emitters. Peb pom yooj yim lub tsev qiv ntawv GitHub rau lub ntsuas pa. Lub sijhawm no txhua qhov kev npaj tau ua tiav.

Kauj Ruam 2: Tsim Qhov System

Tsim Qhov System
Tsim Qhov System
Tsim Qhov System
Tsim Qhov System

Qhov System Logic

Lub kaw lus tau tsim nrog cov laj thawj yooj yim hauv qab no hauv siab:

  • Lub kaw lus saib xyuas qhov kev ncua deb ntawm lub ntsuas thiab dej saum npoo av tas li.
  • Thaum twg los xij qhov kev hloov pauv mus deb dhau tus nqi pib, lub kaw lus xa cov ntaub ntawv hais txog nws lub xeev mus rau huab.
  • Yog tias qhov kev ncua deb dhau qhov tso cai siab tshaj plaws (lub tank tsis muaj dab tsi), lub kaw lus qhib lub twj tso kua mis thiab tig nws tawm thaum qhov kev ncua deb tsawg dua qhov tso cai tsawg kawg.
  • Thaum twg los xij ntawm lub xeev kev hloov pauv (piv txwv li, lub twj tso kua mis qhib), nws qhia txog huab.

Thaum muaj qhov yuam kev, ceeb toom raug xa mus rau Slack channel.

Thaum lub tshuab kas fes tsis ua haujlwm, lub kaw lus ua haujlwm pabcuam huab nrog cov ntaub ntawv kuaj mob ib zaug txhua feeb. Ib qho ntxiv, nws xa nws lub xeev mus rau huab txhua 5 feeb.

Thaum lub twj tso kua mis ua haujlwm, lub kaw lus xa cov ntaub ntawv ntau dua tab sis tsis ntau dua ib zaug ib nrab ib ob.

def xa (huab, qhov sib txawv, deb, error_code = 0, quab yuam = Tsis raug): pump_on = is_pump_on () feem pua = calc_water_level_percent (dist) sib txawv ['Nrug'] ['tus nqi'] = qhov sib txawv ['WaterLevel'] [' tus nqi '] = feem pua sib txawv [' PumpRelay '] [' tus nqi '] = pump_on hloov pauv [' xwm txheej '] [' tus nqi '] = calc_status (error_code, feem pua, pump_on)

tam sim no = sijhawm ()

ntiaj teb kawg_sending_time yog quab yuam lossis tam sim no - last_sending_time> MIN_SEND_INTERVAL: nyeem = cloud.read_data () cloud.publish_data (nyeem) last_sending_time = tam sim no

Ua haujlwm nrog Pump

Peb txiav txim siab cov hauv qab no raws li lub hauv paus rau kev ua haujlwm logic.

# GPIO Pins (BCM) GPIO_PUMP = 4 GPIO_TRIGGER = 17 GPIO_ECHO = 27

# Tso twj tso kua mis

START_PUMP = 1 STOP_PUMP = 0 PUMP_BOUNCE_TIME = 50 # milliseconds PUMP_STOP_TIMEOUT = 5 # secs

TSEEM CEEB: Yog tias koj yuav siv Pin 4, tsis txhob hnov qab kaw qhov 1-Wire raspi-teeb tsa xaiv kom tsis txhob muaj teeb meem.

Thaum txoj haujlwm pib, peb sau npe rov qab thiab teeb tsa lub xeev thawj zaug rau OFF.

Nov yog cov cai rau kev ua haujlwm uas hloov lub twj tso kua mis:

def toggle_pump (tus nqi): yog pump_disabled: rov qab yog is_pump_on ()! = tus nqi: log_debug ("[x] % s" % ('START' yog tus nqi ntxiv 'STOP')) GPIO.setup (GPIO_PUMP, GPIO. OUT) GPIO.output (GPIO_PUMP, tus nqi) # Pib/Nres pouring

Raws li tau hais tseg hauv txoj cai pib ua haujlwm saum toj no, thaum qhov kev sib txuas qhib ON, cov hauv qab no hu rov qab:

pump_on = def def tsis ncaj pump_relay_handle (tus pin): thoob ntiaj teb pump_on pump_on = GPIO.input (GPIO_PUMP) log_debug ("Pump relay hloov mus rau % d" % pump_on)

Hauv qhov kev hu rov qab, peb txuag lub twj tso kua mis lub xeev tam sim no rau qhov sib txawv.

def is_pump_on (): thoob ntiaj teb pump_on rov qab pump_on

yog GPIO.event_detected (GPIO_PUMP):

is_pouring = is_pump_on () #… log_debug ('[!] Lub twj tso kua mis pom: % s' % ('On' yog is_pouring lwm yam 'Tawm')) xa (huab, qhov txawv txav, nrug, quab yuam = Muaj tseeb)

Ntsuas Qhov Nrug

Nws yog qhov yooj yim heev los ntsuas qhov kev ncua deb ntawm cov dej siv lub ntsuas pa ze ze ultrasonic. Hauv peb lub chaw cia khoom, peb tau qhia ob peb phau ntawv nab nab uas tso cai rau koj ntsuas lub ntsuas.

Hauv cov ntawv thov tiag tiag, kev nyeem ntawv lub suab tuaj yeem hloov pauv vim yog lub teeb pom kev zoo cuam tshuam thiab dej oscillations. Hauv qee kis, nyeem tau tuaj yeem ploj tag. Peb tau siv BounceFilter chav kawm uas suav nrog N qhov txiaj ntsig tsis ntev los no, pov tseg qhov siab tshaj thiab suav qhov nruab nrab ntawm kev ntsuas ntxiv. Cov txheej txheem ntsuas yog ua los ntawm cov txheej txheem asynchronous hauv qab no.

# Khaws qhov ntsuas ntsuas kawg nyeem = BounceFilter (qhov loj = 6, pov tseg_count = 1)

nyeem_complete = threading. Event ()

def tos_ rau_distance ():

nyeem_complete.clear () xov = threading. Thread (phiaj = read_distance) thread.start ()

yog tias tsis nyeem_complete.wait (MAX_READING_TIMEOUT):

log_info ('Nyeem lub sijhawm ntsuas lub sijhawm') rov qab Tsis muaj rov nyeem dua.avg ()

def nyeem_distance ():

sim: tus nqi = hcsr04.raw_distance (sample_size = 5) sib npaug = tus nqi yog tias tus nqi tsis muaj ib qho ntxiv ib puag ncig (tus nqi, 1) readings.add (sib npaug) tshwj tsis yog kev zam ua yuam kev: log_error ('Sab hauv yuam kev: % s' % yuam kev) thaum kawg: nyeem_complete.set ()

Koj tuaj yeem pom cov lim kev ua tiav hauv cov peev txheej.

Kauj Ruam 3: Ua Haujlwm Thaum Muaj Xwm Ceev

Ua Haujlwm Thaum Muaj Xwm Ceev
Ua Haujlwm Thaum Muaj Xwm Ceev
Ua Haujlwm Thaum Muaj Xwm Ceev
Ua Haujlwm Thaum Muaj Xwm Ceev
Ua Haujlwm Thaum Muaj Xwm Ceev
Ua Haujlwm Thaum Muaj Xwm Ceev

Yuav ua li cas yog tias lub ntsuas hluav taws kub tawm, lossis poob tawm, lossis taw tes rau thaj chaw tsis raug? Peb xav tau txoj hauv kev los tshaj tawm cov xwm txheej zoo li no kom peb tuaj yeem nqis tes ua.

Yog tias lub ntsuas tsis ua kom nyeem tau nrug, lub kaw lus xa cov xwm txheej hloov pauv mus rau huab thiab tsim kev ceeb toom sib xws.

Lub logic yog qhia los ntawm cov cai hauv qab no.

nrug = tos_for_distance () # Nyeem cov dej tam sim no yog qhov deb yog Tsis muaj: log_error ('Qhov yuam kev nyob deb!') ceeb toom_in_background (calc_alert (SENSOR_ERROR)) xa (huab, hloov pauv, nrug, error_code = SENSOR_ERROR, quab yuam = Muaj tseeb)

Peb muaj cov dej theem ua haujlwm uas yuav tsum tau saib xyuas thaum lub ntsuas hluav taws xob nyob hauv nws qhov chaw. Peb ntsuas yog tias theem dej tam sim no poob hauv qhov no:

# Nyob deb ntawm lub ntsuas mus rau qib dej # raws li lub tshuab kas fes lub tank dej MIN_DISTANCE = 2 # cm MAX_DISTANCE = 8 # cm

# Qhov nrug deb ntawm qhov xav tau ntau: tsis txhob pib nchuav

yog tias nrug> MAX_DISTANCE * 2: log_error ('Qhov nrug deb ntawm qhov ntau: %.2f' % nrug) txuas ntxiv mus

Peb tig lub twj tso kua mis tawm yog tias nws tau ua haujlwm thaum muaj qhov yuam kev tshwm sim.

yog is_pump_on () thiab prev_distance <STOP_PUMP_DISTANCE + DISTANCE_DELTA: log_error ('[!] Thaum muaj xwm ceev nres ntawm lub twj tso kua mis. Tsis muaj teeb liab los ntawm qhov deb sensor')

toggle_pump (STOP_PUMP)

Peb kuj ua cov ntaub ntawv thaum lub raj mis tsis muaj dej. Peb txheeb xyuas yog tias qib dej tsis hloov pauv thaum lub twj tso kua mis khiav. Yog tias yog, lub kaw lus tos 5 vib nas this thiab tom qab ntawv kuaj xyuas yog tias lub twj tso kua mis tau kaw lawm. Yog tias nws tsis muaj, tom qab ntawd lub kaw lus siv kaw lub twj tso kua mis thaum muaj xwm ceev thiab xa ntawv ceeb toom yuam kev.

PUMP_STOP_TIMEOUT = 5 # secsemergency_stop_time = Tsis muaj

def set_emergency_stop_time (tam sim no, yog_pouring):

xwm txheej thoob ntiaj teb_stop_time thaum muaj xwm txheej_stop_time = tam sim no + PUMP_STOP_TIMEOUT yog tias yog tsis muaj dab tsi ntxiv

def check_water_source_empty (tam sim no):

xa rov qab xwm txheej_stop_time thiab tam sim no> xwm txheej_ xwm txheej_ sijhawm

# --------- lub ntsiab lus -----------

yog GPIO.event_detected (GPIO_PUMP): is_pouring = is_pump_on () set_emergency_stop_time (tam sim no, yog_pouring) #…

thoob ntiaj teb pump_disabled

yog check_water_source_empty (tam sim no): log_error ('[!] Thaum muaj xwm ceev nres ntawm lub twj tso kua mis. / Cov dej tsis muaj dab tsi') toggle_pump (STOP_PUMP) pump_disabled = Muaj tseeb

Saum toj no yog ib qho piv txwv ntawm cov lus xa tawm thaum lub sijhawm muaj xwm ceev nres.

Kauj Ruam 4: Khiav Qhov System 24/7

Khiav Qhov System 24/7
Khiav Qhov System 24/7

Cov cai ntawm lub cuab yeej raug debugged thiab ua haujlwm yam tsis muaj teeb meem. Peb tau pib nws ua qhov kev pabcuam, yog li nws rov pib dua yog tias Raspberry Pi tau rov pib dua. Txhawm rau kom yooj yim, peb tsim Makefile uas pab nrog kev xa tawm, khiav cov kev pabcuam thiab saib cov cav.

. PHONY: teeb tsa pib pib nres xwm txheej teeb tsa MAIN_FILE: = kas fes-twj tso kua mis/main.py SERVICE_INSTALL_SCRIPT: = service_install.sh SERVICE_NAME: = kas fes-twj.service

nruab:

chmod +x $ (SERVICE_INSTALL_SCRIPT) sudo./ $(SERVICE_INSTALL_SCRIPT) $ (MAIN_FILE)

khiav:

sudo python3 $ (MAIN_FILE)

pib:

sudo systemctl pib $ (SERVICE_NAME)

xwm txheej:

sudo systemctl xwm txheej $ (SERVICE_NAME)

nres:

sudo systemctl nres $ (SERVICE_NAME)

cav:

sudo journalctl -u kas fes -twj -txij hnub no

xa tawm:

rsync -av kas fes-twj sensor-teeb Makefile *.sh pi@XX. XX. XXX. XXX: ~/

Koj tuaj yeem pom cov ntawv no thiab txhua daim ntawv xav tau hauv peb lub chaw cia khoom.

Kauj Ruam 5: Saib Xyuas Huab Cua

Huab Saib Xyuas
Huab Saib Xyuas
Saib Xyuas Huab Cua
Saib Xyuas Huab Cua
Huab Saib Xyuas
Huab Saib Xyuas
Huab Saib Xyuas
Huab Saib Xyuas

Peb tau siv Cloud4RPi los siv lub tswj vaj huam sib luag. Peb xub ntxiv cov cuab yeej los qhia cov txheej txheem tsis tseem ceeb.

Los ntawm txoj kev, tus widget rau STATUS sib txawv tuaj yeem siv cov xim sib txawv raws li nws tus nqi (saib cov duab saum toj no).

Peb tau ntxiv daim duab qhia kom pom cov ntaub ntawv muaj zog. Hauv daim duab hauv qab no koj tuaj yeem pom lub sijhawm uas lub twj tso kua mis tig rau thiab OFF thiab cov qib dej.

Yog tias koj txheeb xyuas lub sijhawm ntev dua, koj tuaj yeem pom qhov siab tshaj - qhov ntawd yog thaum lub twj tso kua mis tau khiav.

Cloud4RPi tseem tso cai rau koj teeb tsa qib sib txawv sib luag.

Kauj Ruam 6: Nws Ua Haujlwm

Image
Image

Nws ua haujlwm! Kev tswj hwm vaj huam sib luag zoo li qhov qhia hauv qab no.

Tam sim no, peb lub twj tso kua mis tsis siv neeg tau ua haujlwm tau ob peb lub lis piam thiab txhua yam peb xav tau ua yog hloov cov fwj dej. Tag nrho cov cai rau peb txoj haujlwm muaj nyob hauv peb GitHub chaw cia khoom.

Pom zoo: