Raspberry pico w ( climate controll greenhouse ) #11681
Replies: 8 comments 48 replies
-
a) Formatting: Add a line with three backticks ``` before and behind your code. Then it is properly formatted. |
Beta Was this translation helpful? Give feedback.
-
as robert-hh suggested, put code in bracket an put 'python; keyword ad beginning ( ```python ) the will be more readable. Your code looks like simple 'ladder logic' just in code but for any on/off control : for fan, or heater better to use ON-OFF controller based on relay( for example from my repo, or just implementation [relay with hysteresis: relay2h] (https://github.com/2dof/esp_control/blob/main/src/on_off_control.py) with small hysteresis. It will prevent any sensor noise influence on on-off ( 'glittering effect' ) when temperature will be close of threshold value. at least it will be safe for any mechanical relays and actuators. |
Beta Was this translation helpful? Give feedback.
-
About code format in a post: Please add you code into lines with three backticks ``` . You have something else there. The line at the start of code may be extended with the work python. import sys
print sys.path in the REPL prompt? Did you copy all files to the board? |
Beta Was this translation helpful? Give feedback.
-
@N2E5c8, Also, I think it is better practice to open the file, write or read and then close it ... every second if necessary. What happens if the program crashes with the files open ... I don't know. |
Beta Was this translation helpful? Give feedback.
-
Hello davefes, correct Moisture: 100.0 Temp: 22.46 Humidity: 76.35 Any ideas? from machine import Pin
from pmon import PlantMonitor
import bme280
import time
import sys
from RingBuffer import ring_buffer
import simple_models_esp
from on_off_control import OnOff_controller
error_log_file = open("error_log.txt", "a")
# Function to log exceptions to the error log file
def log_exception(exception):
error_log_file.write("Exception: {}\n".format(exception))
sys.print_exception(exception, error_log_file)
error_log_file.flush()
# Function to log data
def log_data(data):
data_log_file = open("data_log.txt", "a")
max_file_size = 512 * 512 # Maximum file size limit in bytes (e.g., 250 kB)
# Check if the file size exceeds the maximum
if data_log_file.tell() >= max_file_size:
# Reset the file position to the beginning
data_log_file.seek(0)
# Truncate the file to remove its contents
data_log_file.truncate()
# Write the data to the file
# data_log_file.write(data + "\n")
data_log_file.write(data)
# Close the file
data_log_file.close()
# Configure the onboard LED
led_pin = Pin("LED", Pin.OUT)
# Blink the LED
blink_duration = 1 # Blink duration in seconds
blink_interval = 5 # Interval between blinks in seconds
led_pin.on() # Turn on the LED
time.sleep(blink_duration)
led_pin.off() # Turn off the LED
time.sleep(blink_interval) # Wait for the interval
pm = PlantMonitor()
# Create an instance of the OnOff_controller class
temperature_controller = OnOff_controller(hystL=-1, hystH=1)
# Start the temperature control calculation
temperature_controller.start()
water_valve_pin = Pin(15, Pin.OUT)
nebulizer_pin = Pin(16, Pin.OUT)
fan_pin = Pin(17, Pin.OUT)
outer_fan_pin = Pin(14, Pin.OUT)
moisture_threshold = 25
humidity_threshold = 85
temperature_threshold = 24
sdaPIN = machine.Pin(20)
sclPIN = machine.Pin(21)
i2c = machine.I2C(0, sda=sdaPIN, scl=sclPIN, freq=400000)
bme = bme280.BME280(i2c=i2c)
# Get the current time
current_time = time.time()
while True:
try:
wetness = pm.get_wetness()
current_temp = pm.get_temp()
current_humidity = pm.get_humidity()
data = "Moisture: {0} Temp: {1} Humidity: {2}\n".format(wetness, current_temp, current_humidity)
# Get the current time
new_time = time.time()
# Check if an hour has passed since the last data log
if new_time - current_time >= 3600:
# Log the data
log_data(data)
# Update the current time
current_time = new_time
print(data)
t, p, h = bme.read_compensated_data()
temperature = t / 100
p = p // 256
pressure = p // 100
hi = h // 1024
hd = h * 100 // 1024 - hi * 100
print("Weather: {}C".format(temperature), "{}hPa".format(pressure), "{}.{:02d}%".format(hi, hd))
if wetness <= moisture_threshold:
water_valve_pin.on()
print("Watering plants ...")
else:
water_valve_pin.off()
if current_humidity <= humidity_threshold:
nebulizer_pin.on()
print("Nebulizer fogging ...")
else:
nebulizer_pin.off()
# Read the indoor and outdoor temperature values
indoor_temperature = pm.get_temp()
outdoor_temperature = bme.read_compensated_data()[0] / 100
# Update the control and get the control signal
control_signal = temperature_controller.updateControl(temperature_threshold, indoor_temperature)
if control_signal == 1: # Need heat to raise indoor temperature
if outdoor_temperature >= indoor_temperature: # Check if we can use temperature from outside
outer_fan_pin.on()
fan_pin.off()
print('Heating by outer fan: outer_fan_on')
print('Heater off')
else:
fan_pin.on()
outer_fan_pin.off()
print('Heating by heater: heater on')
print('Heating by outer fan: off')
else: # control_signal = 0
fan_pin.off()
outer_fan_pin.off()
print('Heater off')
print('Heating by outer fan: off')
led_pin.on() # Turn on the LED
time.sleep(blink_duration)
led_pin.off() # Turn off the LED
time.sleep(blink_interval) # Wait for the interval
except Exception as e:
log_exception(e)
continue
# Close the log file
error_log_file.close()
|
Beta Was this translation helpful? Give feedback.
-
Hello 2dof, any idea? except Exception as e:
print("Communication Problem with Plant Monitor or BME280. Check your wiring.")
# Optionally, you can log the error or take additional actions.
# Turn off the relays
water_valve_relay2h.off()
nebulizer_relay2h.off()
fan_relay2h.off()
outer_fan_relay2h.off()
# Wait for the shutdown pin to be activated
while not shutdown_pin.value():
pass
break
except Exception as e:
log_exception(e)
# continue
# Initiate the shutdown process
#machine.deepsleep()
# Close the log file
error_log_file.close()
# Initiate the shutdown process
deepsleep()
# Perform a soft reset
#reset() |
Beta Was this translation helpful? Give feedback.
-
My comments: -> this is just suggestion, since You log data every 60 sec ( -> please - next time instead of a bunch of screens, give only couple of them and give link to the files (not screen of file data - at least someone can use your file data to test/reproduce loading file, also then you plot data and You want share - not need do screen all plotted data - just some short plot period - at least it is more readable. I will try to reproduce Your procedure in weekend but You need confirm how do You transfer files to phone. -> Question: what is the purpose of plotting data in phone (py3 with matplot lib) - just check/visualise data? |
Beta Was this translation helpful? Give feedback.
-
Hey There ! there is a GitHub Repo that could help: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using MicroPython v1.20.0 on 2023-04-26; Raspberry Pi Pico W with RP2040
I have written some code but now I'm stuck. Everything works so far, now I'm trying to add
a now function to control the green house temperature either the use of electrical heat or when
conditions allow through the use of atmospheric heat. I use The Soil-Monitor, and the onboard
Temp-sensor as the outer-temp meter. Here are codes I tried out, they do not switch properly.
Can someone help?
My main.py is at the bottom.
first:
if current_temp <= temperature_threshold:
fan_pin.on()
outer_fan_pin.off()
print("Using electrical heat ...")
elif current_temp > temperature_threshold and outer_temp <= temperature_threshold:
fan_pin.off()
outer_fan_pin.on()
print("Using atmospheric heat ...")
else:
fan_pin.off()
outer_fan_pin.off()
second:
if temperature <= t:
outer_fan_pin.off()
inner_fan_pin.on()
print("Using electrical heat ...")
else:
outer_fan_pin.on()
inner_fan_pin.off()
print("Using atmospheric heat ...")
third:
if temperature_threshold >= t >= temperature:
outer_fan_pin.on()
inner_fan_pin.off()
print("Using atmospheric heat ...")
else:
outer_fan_pin.off()
inner_fan_pin.on()
print("Using electrical heat ...")
Here is my main.py
from machine import Pin
from pmon import PlantMonitor
from PicoSensor import Temperature
import time
import sys
Open the log files in append mode
data_log_file = open("data_log.txt", "a")
error_log_file = open("error_log.txt", "a")
Function to log exceptions to the error log file
def log_exception(exception):
error_log_file.write("Exception: {}\n".format(exception))
sys.print_exception(exception, error_log_file)
error_log_file.flush()
Maximum file size limit in bytes (e.g., 1 MB)
max_file_size = 1024 * 1024
Check if the data log file size exceeds the maximum limit
if data_log_file.tell() >= max_file_size:
# Truncate the data log file to the maximum limit
data_log_file.seek(0)
data_log_file.truncate()
Configure the onboard LED
led_pin = Pin("LED", Pin.OUT)
Blink the LED
blink_duration = 0.5 # Blink duration in seconds
blink_interval = 1 # Interval between blinks in seconds
led_pin.on() # Turn on the LED
time.sleep(blink_duration)
led_pin.off() # Turn off the LED
time.sleep(blink_interval) # Wait for the interval
pm = PlantMonitor()
outer_sensor = Temperature() # Create instance of the outer temperature sensor
outer_fan_pin = Pin(14, Pin.OUT)
water_valve_pin = Pin(15, Pin.OUT)
nebulizer_pin = Pin(16, Pin.OUT)
fan_pin = Pin(17, Pin.OUT)
moisture_threshold = 25
humidity_threshold = 65
temperature_threshold = 24
while True:
try:
wetness = pm.get_wetness()
current_temp = pm.get_temp()
current_humidity = pm.get_humidity()
Close the log files
data_log_file.close()
error_log_file.close()
Thank you
Beta Was this translation helpful? Give feedback.
All reactions