Archived
1
0

First working code for HC-SR04 sensor.

This commit is contained in:
Shaun Setlock
2022-12-30 23:03:38 -05:00
parent 58a40379a9
commit 551e8f9e30
4 changed files with 141 additions and 108 deletions

View File

@@ -18,4 +18,15 @@ import upip
upip.install('umqtt.simple') upip.install('umqtt.simple')
upip.install('umqtt.robust') upip.install('umqtt.robust')
``` ```
for later versions of MicroPython, you should use `mip` instead...
```
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Wi-Fi AP", "PASSWORD")
import mip
mip.install('umqtt.simple')
mip.install('umqtt.robust')
```
- Upload files onto PICO, adjust params (such as Wi-Fi Credentials // pins // etc ), deploy to your PICO W - Upload files onto PICO, adjust params (such as Wi-Fi Credentials // pins // etc ), deploy to your PICO W

34
hcsr04_funcs.py Normal file
View File

@@ -0,0 +1,34 @@
import time
'''
hcsr04_funcs: Helper functions to interact with the HC-SR04 sensor.
'''
def read_hc_sr04(trig, echo):
"""
reads the HC-SR04 sensor.
returns tuple(temperature, humidity) if no errors
returns None if there was an error
"""
try:
trig.value(0)
time.sleep(0.1)
trig.value(1)
time.sleep_us(2)
trig.value(0)
while echo.value()==0:
pulse_start = time.ticks_us()
while echo.value()==1:
pulse_end = time.ticks_us()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17165 / 1000000
distance = round(distance, 2)
return distance
except:
return None

136
main.py
View File

@@ -1,107 +1,29 @@
# Built-in's
import network import network
import rp2 import rp2
import time import time
import json import json
import machine import machine
from machine import Pin
# From PyPI
from umqtt.robust import MQTTClient from umqtt.robust import MQTTClient
import dht
import urequests # From this project...
import wifi_config
import mqtt_config import mqtt_config
from pico_funcs import read_cpu_temp, wlan_up, led_error_code
from hcsr04_funcs import read_hc_sr04
# Change this GPIO PIN where your DHT22 sensor is connected # Change this GPIO PIN where your DHT22 sensor is connected
DHT_22_GPIO_PIN = 3 TRIG_PIN = 3
ECHO_PIN = 2
# Debug Mode # Debug Mode
DEBUG = False DEBUG = False
def read_cpu_temp():
"""
If you print the value of the temperature value you are going to get an integer number between 0 and 65535.
So, we have to convert this value either to the Celsius degree scales.
The temperature sensor works by delivering a voltage to the ADC4 pin that is proportional to the temperature.
From the datasheet, a temperature of 27 degrees Celsius delivers a voltage of 0.706 V.
With each additional degree the voltage reduces by 1.721 mV or 0.001721 V.
The first step in converting the 16-bit temperature is to convert it back to volts, which is done based on the 3.3 V maximum voltage used by the Pico board.
ref: https://how2electronics.com/read-temperature-sensor-value-from-raspberry-pi-pico/
"""
cpu_temp_conversion_factor = 3.3 / 65535
cpu_temp_sensor = machine.ADC(4)
reading = cpu_temp_sensor.read_u16() * cpu_temp_conversion_factor
temperature_c = 27 - (reading - 0.706) / 0.001721
temperature_f = temperature_c * 9/5. + 32.0
return temperature_f
def read_dht_22(sensor):
"""
reads the temperature and humidity from dht.DHT22 sensor.
returns tuple(temperature, humidity) if no errors
returns None if there was an error
"""
try:
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
return temperature, humidity
except:
time.sleep(2)
return None
def wlan_up(wlan):
wlan.active(True)
wlan.connect(wifi_config.HOME_WIFI_SSID, wifi_config.HOME_WIFI_PWD)
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
if DEBUG:
print('Waiting for WiFi connection...')
time.sleep(1)
if max_wait == 0:
return None
ifconfig = wlan.ifconfig()
if DEBUG:
print(ifconfig)
return ifconfig
def led_error_code(led, error_code: int):
"""Blink LED for a given error code (int). error code == number of times to blink"""
if DEBUG:
print("LED Error Status code: {}".format(error_code))
# Run a quick 'start error code sequence'
# So we know when LED error sequence starts
start_sequence_counter = 0
while start_sequence_counter < 3:
led.value(True)
time.sleep(0.1)
led.value(False)
time.sleep(0.1)
start_sequence_counter += 1
# Run real error code sequence
blink_counter = 0
while blink_counter < error_code:
time.sleep(1)
led.value(True)
time.sleep(1)
led.value(False)
blink_counter += 1
# Make sure to turn off LED when this subroutine finished
led.value(False)
if DEBUG:
print("LED Error Status code finished for: {}".format(error_code))
def main(): def main():
# Start Up Activities # Start Up Activities
if DEBUG: if DEBUG:
print("Start up") print("Start up")
@@ -110,7 +32,7 @@ def main():
led.value(False) led.value(False)
led_error_code(led, 1) led_error_code(led, 1)
# Set Wi-Fi Country # Set Wi-Fi Country and Create a Wireless Interface
rp2.country('US') rp2.country('US')
wlan = network.WLAN(network.STA_IF) wlan = network.WLAN(network.STA_IF)
@@ -120,8 +42,9 @@ def main():
server = mqtt_config.MQTT_HOST_NAME, server = mqtt_config.MQTT_HOST_NAME,
) )
# Create DHT22 # Create HC-SR04
sensor = dht.DHT22(Pin(DHT_22_GPIO_PIN)) trig = machine.Pin(TRIG_PIN, machine.Pin.OUT)
echo = machine.Pin(ECHO_PIN, machine.Pin.IN, machine.Pin.PULL_DOWN)
# Let's Go! # Let's Go!
if DEBUG: if DEBUG:
@@ -138,20 +61,18 @@ def main():
# Create Local Flags for Control # Create Local Flags for Control
wifi_ready = False wifi_ready = False
mqtt_ready = False mqtt_ready = False
dht22_ready = False hc_sr04_ready = False
# DHT22 and CPU Reading. # CPU Reading.
try: try:
# CPU Reading.
cpu_temp = read_cpu_temp() cpu_temp = read_cpu_temp()
except:
continue
# DHT22 Reading. # HC-SR04 Reading.
dht22_reading = read_dht_22(sensor) try:
#debug_str = "None" distance = read_hc_sr04(trig, echo)
if dht22_reading is not None: hc_sr04_ready = True
temp,hum = dht22_reading
temp = temp * 9/5. + 32.0
dht22_ready = True
except: except:
continue continue
@@ -196,18 +117,17 @@ def main():
# Ready to Publish? # Ready to Publish?
try: try:
if wifi_ready and mqtt_ready and dht22_ready: if wifi_ready and mqtt_ready and hc_sr04_ready:
# Timestamp # Timestamp
time_now = time.localtime() time_now = time.localtime()
timestamp = "{}/{}/{} {}:{}:{}".format(time_now[1],time_now[2],time_now[0],time_now[3],time_now[4],time_now[5]) timestamp = "{}/{}/{} {}:{}:{}".format(time_now[1],time_now[2],time_now[0],time_now[3],time_now[4],time_now[5])
# Build JSON Payloads # Build JSON Payloads
dht_data = { hc_sr04_data = {
'Location':mqtt_config.MQTT_ZONE_ID, 'Location':mqtt_config.MQTT_ZONE_ID,
'Temperature':temp, 'Distance':distance,
'Relative Humidity':hum,
} }
dht_payload = json.dumps(dht_data) hc_sr04_payload = json.dumps(hc_sr04_data)
hw_data = { hw_data = {
'Timestamp':timestamp, 'Timestamp':timestamp,
@@ -218,7 +138,7 @@ def main():
hw_payload = json.dumps(hw_data) hw_payload = json.dumps(hw_data)
# Publish # Publish
mqtt_client.publish("home/{}".format(mqtt_config.MQTT_ZONE_ID),dht_payload, retain=True) mqtt_client.publish("home/{}".format(mqtt_config.MQTT_ZONE_ID),hc_sr04_payload, retain=True)
mqtt_client.publish("hw/{}".format(mqtt_config.MQTT_HW_ID),hw_payload, retain=True) mqtt_client.publish("hw/{}".format(mqtt_config.MQTT_HW_ID),hw_payload, retain=True)
mqtt_client.disconnect() mqtt_client.disconnect()

68
pico_funcs.py Normal file
View File

@@ -0,0 +1,68 @@
import machine
import network
import wifi_config
import time
'''
pico_funcs: Helper functions for things the pico can do.
'''
def read_cpu_temp():
"""
If you print the value of the temperature value you are going to get an integer number between 0 and 65535.
So, we have to convert this value either to the Celsius degree scales.
The temperature sensor works by delivering a voltage to the ADC4 pin that is proportional to the temperature.
From the datasheet, a temperature of 27 degrees Celsius delivers a voltage of 0.706 V.
With each additional degree the voltage reduces by 1.721 mV or 0.001721 V.
The first step in converting the 16-bit temperature is to convert it back to volts, which is done based on the 3.3 V maximum voltage used by the Pico board.
ref: https://how2electronics.com/read-temperature-sensor-value-from-raspberry-pi-pico/
"""
cpu_temp_conversion_factor = 3.3 / 65535
cpu_temp_sensor = machine.ADC(4)
reading = cpu_temp_sensor.read_u16() * cpu_temp_conversion_factor
temperature_c = 27 - (reading - 0.706) / 0.001721
temperature_f = temperature_c * 9/5. + 32.0
return temperature_f
def wlan_up(wlan):
wlan.active(True)
wlan.connect(wifi_config.HOME_WIFI_SSID, wifi_config.HOME_WIFI_PWD)
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
time.sleep(1)
if max_wait == 0:
return None
ifconfig = wlan.ifconfig()
return ifconfig
def led_error_code(led, error_code: int):
"""Blink LED for a given error code (int). error code == number of times to blink"""
# Run a quick 'start error code sequence'
# So we know when LED error sequence starts
start_sequence_counter = 0
while start_sequence_counter < 3:
led.value(True)
time.sleep(0.1)
led.value(False)
time.sleep(0.1)
start_sequence_counter += 1
# Run real error code sequence
blink_counter = 0
while blink_counter < error_code:
time.sleep(1)
led.value(True)
time.sleep(1)
led.value(False)
blink_counter += 1
# Make sure to turn off LED when this subroutine finished
led.value(False)