makeuseofcode / soil-monitor

soil monitor and simple web server for remote viewing within your LAN
3 stars 1 forks source link

Sensor_data recording problem #2

Open N2E5c8 opened 9 months ago

N2E5c8 commented 9 months ago

Hello github support,

I have probably a small issue with my own programming can you help? I have written with the help of others a main.py to use (soil-monitor) in another way. I have achieved to use new main.py on a raspberry pico (usb) and to communicate with it through a cheap android smart phone (android 11) with the app Serial USB Terminal, through the use of this app I can download my data log file.txt to the phone, once it's there I use the app py3 (+matplotlib...) to make a python file (sd.py). The data flows in as a python format: ´´´ sensor_data = [ 'Moisture: 100.0 Temp_in: 19.56 Humidity: 64.14 Temp_out: 18.62 Air_pressure: 1026', 'Moisture: 100.0 Temp_in: 19.53 Humidity: 63.68 Temp_out: 18.59 Air_pressure: 1025',]

Though I still have to add a closing bracket ( ] ) that isn't a big problem, when everything is python confirm I open the file with py3 to view
the data file with matplotlib, it works wonderful.

So, my main issue is that when data file gets large the pico stalls (file size >=128bytes) and sets my alarm system ON.

Any help would be appreciated 

Thank you

Here is my main.py:

´´´
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, relay2h 

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):
    max_file_lines = 330  # Maximum number of lines in the log file

    with open("sd.py", "a+") as data_log_file:
        # Read the existing content
        data_log_file.seek(0)
        lines = data_log_file.readlines()

        # Check if the number of lines exceeds the maximum allowed
        if len(lines) >= max_file_lines:
            # Remove the oldest lines to maintain the maximum limit
            lines = lines[len(lines) - max_file_lines + 1:]

            # Clear the file content and write the remaining lines
            data_log_file.seek(0)
            data_log_file.truncate()
            data_log_file.writelines(lines)

        # Write the new data to the file
        data_log_file.write(data)

# Configure the onboard LED
led_pin = Pin("LED", Pin.OUT)

# Configure the onboard LED
led_pin = Pin("LED", Pin.OUT)

# Blink the LED
blink_duration = 1  # Blink duration in seconds
blink_interval = 10  # 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_relay2h = Pin(15, Pin.OUT)
nebulizer_relay2h = Pin(16, Pin.OUT)
fan_relay2h = Pin(17, Pin.OUT)
outer_fan_relay2h = Pin(14, Pin.OUT)
warn_light_relay = Pin(18, Pin.OUT)
warn_light_relay(1)
shutdown_pin = Pin(19, Pin.IN)

moisture_threshold = 25
humidity_threshold = 78
temperature_threshold = 23

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()
        indoor_temperature = pm.get_temp() # indoor_temperature = pm.get_temp()
        current_humidity = pm.get_humidity()

        bme = bme280.BME280(i2c=i2c)  # BME280 object created
        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

        data = "    'Moisture: {0} Temp_in: {1} Humidity: {2} Temp_out: {3}',\n".format(wetness, indoor_temperature, current_humidity, temperature)

        print("Weather: Temp_out: {}C".format(temperature), "Air_pressure: {}hPa".format(pressure), "Humidity: {}.{:02d}%".format(hi, hd)) 

        # Get the current time
        new_time = time.time()

        # Check if a quarter of an hour has passed since the last data log
        if new_time - current_time >= 60:
            # Log the data
            log_data(data)
            # Update the current time
            current_time = new_time

        print(data)

        if wetness <= moisture_threshold:
            water_valve_relay2h.on()
            print("Watering ON")
        else:
            water_valve_relay2h.off()
            print("Watering OFF")

        if current_humidity <= humidity_threshold:
            nebulizer_relay2h.on()
            print("Nebulizer ON")
        else:
            nebulizer_relay2h.off()
            print("Nebulizer 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_relay2h.on()
                fan_relay2h.off()
                print('Heating by outer fan: outer_fan_on')
                print('Heater off')
            else:
                fan_relay2h.on()
                outer_fan_relay2h.off()
                print('Heating by heater: heater ON')
                print('Heating by outer fan: OFF')

        else:  # control_signal = 0
            fan_relay2h.off()
            outer_fan_relay2h.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:
        print("Communication Problem with Plant Monitor or BME280. Check your wiring, initialized Warn System!")

        # Turn off the relays
        water_valve_relay2h.off()
        state = water_valve_relay2h.value()
        print(state)

        nebulizer_relay2h.off()
        state = nebulizer_relay2h.value()
        print(state)

        fan_relay2h.off()
        state = fan_relay2h.value()
        print(state)

        outer_fan_relay2h.off()
        state = outer_fan_relay2h.value()
        print(state)

        # Turn on the Warn System
        warn_light_relay.off() # Warn System, warn_light ON
        state = warn_light_relay.value()
        print(state)

        break

    except Exception as e:
        log_exception(e)
        continue

# Close the error_log_file file
error_log_file.close()