03-15-2025, 11:26 PM
(This post was last modified: 03-15-2025, 11:50 PM by Shadowboxer.)
This script is a temperature controller using a DHT22 to switch a cooling device (fan, AC etc.) and DS18B20 to control a heating device (Heater, incubator, heat mat etc) at the same time. They could both be used to control a heating device or a cooling device, the logic in the code would just need to be adjusted/inverted and the serial messages altered to suit.
The DHT22 is connected to IO-2 (switches relay 3)
The DSB1820 is connected to IO-1 (switches relay 2)
That code doesn't work on the boards I have, I bought three boards and they're all the same. All I get is garbage in the serial monitor and no time is displayed. The KCS firmware is useless to me too because the RTC cannot be set. That's why I'm using Micropython, it's the only way I can get any sense out of the clock and salvage some use out of the boards.
If there's a way to configure the KCS firmware to use another RTC on the I2C port then I might be able to use that but without that all three boards may as well go in the rubbish. Without a functional clock they are useless to me. They might be OK with home assistant but I don't need all that extra overhead, I just want a standalone system with a working clock.
The DHT22 is connected to IO-2 (switches relay 3)
The DSB1820 is connected to IO-1 (switches relay 2)
Code:
from machine import Pin, SoftI2C
import onewire
import ds18x20
import dht
import uasyncio as asyncio # Use uasyncio for MicroPython
# PCF8574 I2C addresses
PCF8574_OUTPUT_ADDRESS = 0x24 # Address for controlling relays
PCF8574_INPUT_ADDRESS = 0x22 # Address for reading inputs
# Initialize SoftI2C
scl_pin = Pin(15)
sda_pin = Pin(4)
i2c = SoftI2C(scl=scl_pin, sda=sda_pin, freq=100000)
# Initialize OneWire and DS18B20
ow_pin = Pin(32) # Adjust according to your wiring for the DS18B20
ow = onewire.OneWire(ow_pin)
ds = ds18x20.DS18X20(ow)
# Initialize DHT22
dht_pin = Pin(33) # Adjust according to your wiring for the DHT22
dht_sensor = dht.DHT22(dht_pin)
# Temperature thresholds for DS18B20
TEMP_THRESHOLD_ON_DS18B20 = 26.0 # Temperature to turn on relay 2 (in °C)
TEMP_THRESHOLD_OFF_DS18B20 = 27.5 # Temperature to turn off relay 2 (in °C)
# Temperature thresholds for DHT22
TEMP_THRESHOLD_ON_DHT22 = 29 # Temperature to turn on relay 3 (in °C)
TEMP_THRESHOLD_OFF_DHT22 = 27.5 # Temperature to turn off relay 3 (in °C)
# Humidity thresholds for DHT22
HUMIDITY_THRESHOLD_ON_DHT22 = 85.0 # Humidity to turn on relay 3 (in %)
HUMIDITY_THRESHOLD_OFF_DHT22 = 80.0 # Humidity to turn off relay 3 (in %)
# Function to write to a pin
async def digital_write(pin, value):
# Read current state
current_state = i2c.readfrom(PCF8574_OUTPUT_ADDRESS, 1)[0]
# Set or clear the specific pin
if value == 1:
current_state |= (1 << pin) # Set the pin HIGH
else:
current_state &= ~(1 << pin) # Set the pin LOW
# Write back the new state
i2c.writeto(PCF8574_OUTPUT_ADDRESS, bytes([current_state]))
# Function to turn off all relays
async def turn_off_all_relays():
# Set all pins to HIGH (0xFF) to turn OFF all relays
for pin in range(6): # Assuming PCF8574 has 6 pins
await digital_write(pin, 1) # Set each pin HIGH to turn off the relay
print("All relays are turned OFF.")
# Function to read temperature from DS18B20 and control relay 1
async def read_ds18b20_temperature():
roms = ds.scan() # Scan for devices on the OneWire bus
if roms:
ds.convert_temp() # Start temperature conversion
await asyncio.sleep(1) # Wait for conversion to complete
for rom in roms:
temperature = ds.read_temp(rom) # Read temperature
print("DS18B20 Temperature: {:.2f} °C".format(temperature))
# Control relay 2 based on temperature
if temperature <= TEMP_THRESHOLD_ON_DS18B20:
await digital_write(1, 0) # Turn on relay 2 (active LOW)
print("Relay 2 is ON due to low temperature.")
elif temperature >= TEMP_THRESHOLD_OFF_DS18B20:
await digital_write(1, 1) # Turn off relay 2 (active LOW)
print("Relay 2 is OFF due to high temperature.")
else:
print("No DS18B20 sensor found.")
# Function to read temperature and humidity from DHT22 and control relays
async def read_dht22():
try:
dht_sensor.measure() # Trigger a measurement
temperature = dht_sensor.temperature() # Get temperature in °C
humidity = dht_sensor.humidity() # Get humidity in %
print("DHT22 Temperature: {:.2f} °C, Humidity: {:.2f} %".format(temperature, humidity))
# Determine if the relay should be on or off
relay_on = False
# Check temperature conditions
if temperature >= TEMP_THRESHOLD_ON_DHT22:
relay_on = True
print("Relay 3 is ON due to high temp.")
elif temperature <= TEMP_THRESHOLD_OFF_DHT22:
print("Temperature is below threshold, but checking humidity.")
# Check humidity conditions
if humidity >= HUMIDITY_THRESHOLD_ON_DHT22:
relay_on = True
print("Relay 3 is ON due to high humidity.")
elif humidity <= HUMIDITY_THRESHOLD_OFF_DHT22:
print("Humidity is below threshold.")
# Control relay based on the combined conditions
if relay_on:
await digital_write(2, 0) # Turn on relay 3 (active LOW)
print("Relay 3 is ON.")
else:
await digital_write(2, 1) # Turn off relay 3 (active LOW)
print("Relay 3 is OFF.")
except Exception as e:
print("Error reading DHT22 sensor:", e)
async def main():
await turn_off_all_relays()
while True: # Continuously check the temperature and humidity
await read_ds18b20_temperature() # Read from DS18B20
await read_dht22() # Read from DHT22
await asyncio.sleep(5) # Check every 5 seconds
# Run the main loop
asyncio.run(main())
(03-15-2025, 11:20 PM)admin Wrote: here is DS1307 RTC arduino demo code for KC868-A6: https://www.kincony.com/forum/showthread.php?tid=1863
That code doesn't work on the boards I have, I bought three boards and they're all the same. All I get is garbage in the serial monitor and no time is displayed. The KCS firmware is useless to me too because the RTC cannot be set. That's why I'm using Micropython, it's the only way I can get any sense out of the clock and salvage some use out of the boards.
If there's a way to configure the KCS firmware to use another RTC on the I2C port then I might be able to use that but without that all three boards may as well go in the rubbish. Without a functional clock they are useless to me. They might be OK with home assistant but I don't need all that extra overhead, I just want a standalone system with a working clock.