我正在尝试在我的 ESP32 上连接一堆传感器。我想将它连接到 Blynk 应用程序,以便在我的手机上获取一些数据。
我目前正在使用 python 编码,但是,我有一个问题。
代码在读取光传感器后似乎总是停止。基本上,我只从光传感器获取数据,然后从那里循环。
任何线索代码有什么问题?
import network
from time import sleep
import machine
import onewire, ds18x20
import dht
from machine import Pin
import blynklib_mp as blynklib
"""--- Connect to WiFi ---"""
WIFI_SSID = 'SSID'
WIFI_PASS = 'PASS'
BLYNK_AUTH = 'AUTH'
print("Connecting to WiFi network '{}'".format(WIFI_SSID))
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASS)
while not wifi.isconnected():
sleep(0.1)
print('.', end="")
print('\nWiFi IP:', wifi.ifconfig()[0])
"""--- Connect to Blynk Server ---"""
print("Connecting to Blynk server...")
blynk = blynklib.Blynk(BLYNK_AUTH)
"""--- Settings ---"""
#LIGHT
light_PIN = 35
blynk_VPIN_Light = 0
light_RAW = machine.ADC(machine.Pin(light_PIN))
light_RAW.atten(machine.ADC.ATTN_11DB)
#MOISTURE
moisture_PIN = 34
blynk_VPIN_Moisture = 1
moisture_RAW = machine.ADC(machine.Pin(moisture_PIN))
moisture_RAW.atten(machine.ADC.ATTN_11DB)
moisture_PWM_PIN = 25
moisture_RAW_MIN = 20
moisture_RAW_MAX = 3600
#SOIL TEMPERATURE
soil_temperature_PIN = 4
blynk_VPIN_Soil_Temp = 2
ds_pin = machine.Pin(soil_temperature_PIN)
soil_temperature_RAW = ds18x20.DS18X20(onewire.OneWire(ds_pin))
roms = soil_temperature_RAW.scan()
#AIR TEMPERATURE HUMIDITY
blynk_VPIN_Air_Temp = 3
blynk_VPIN_Air_Hum = 4
sensor = dht.DHT22(Pin(33))
@blynk.handle_event('read V{}'.format(blynk_VPIN_Light))
def read_handler(vpin):
"""--- Light Sensor ---"""
#WHILE LOOP
print('Light RAW: ',light_RAW.read())
sleep(1)
blynk.virtual_write(blynk_VPIN_Light, light_RAW.read())
"""--- Moisture Sensor ---"""
#WHILE LOOP
moisture_PWM = machine.PWM(machine.Pin(moisture_PWM_PIN), freq=600, duty=512) #Send a PWM signal of 600 kHz to pin 25 with a dutycycle of 50%
sleep(0.2) #Allow the circuit to stabilize
moisture_Cumulative_RAW = 0
for _ in range(20):
moisture_Cumulative_RAW = moisture_Cumulative_RAW + moisture_RAW.read() #Read data from analog pin 34 and add it to MoistLevel variable
sleep(0.05)
if moisture_RAW.read() < moisture_RAW_MIN:
moisture_RAW_MIN = moisture_RAW.read()
if moisture_RAW.read() > moisture_RAW_MAX:
moisture_RAW_MAX = moisture_RAW.read()
moisture_Average_RAW = moisture_Cumulative_RAW / 20
moisture_PERC = (1 - (moisture_Average_RAW - moisture_RAW_MIN) / (moisture_RAW_MAX - moisture_RAW_MIN))*100
print('Soil Moisture: ' + str(moisture_PERC) + '%')
moisture_PWM.deinit()
blynk.virtual_write(blynk_VPIN_Moisture, moisture_PERC)
"""--- Soil Temperature Sensor ---"""
#WHILE LOOP
soil_temperature_RAW.convert_temp()
sleep(0.750)
for rom in roms:
#print(rom)
print("Soil Temperarture: ", soil_temperature_RAW.read_temp(rom))
sleep(1)
blynk.virtual_write(blynk_VPIN_Soil_Temp, soil_temperature_RAW.read_temp(rom))
"""--- Air Temperature/Humidity Sensor ---"""
try:
sleep(2)
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print('Air Temperature: %3.1f C' %temp)
print('Air Humidity: %3.1f %%' %hum)
except OSError as e:
print('Failed to read sensor.')
print(" ")
blynk.virtual_write(blynk_VPIN_Air_Temp, temp)
blynk.virtual_write(blynk_VPIN_Air_Hum, hum)
while True:
blynk.run()
sleep(5)