Control Heater using node-red

Hello, I have a raspberry pi which is connected to a solid-state relay bricklet. the following code describes how I turn off and on the heater. Is it possible to implement this on node-red? I want to control the temperature and heater using node red. I am a newbie so any leads would be really appreciated. Thank you :slight_smile:

HOST = "localhost"
PORT = 4223
UID1 = "PTC"  # PTC Bricklet
UID3 = "HL"  # Solid State Relay -- Heater 


import time
starttime = time.time()
while True:

    import csv
    import math
    from time import *
    from time import sleep
    import logging as log
    log.basicConfig(level=log.INFO)


    # Import components
    from tinkerforge.ip_connection import IPConnection
    from tinkerforge.bricklet_ptc_v2 import BrickletPTCV2
    from tinkerforge.bricklet_solid_state_relay_v2 import BrickletSolidStateRelayV2
    from tinkerforge.bricklet_thermocouple_v2 import BrickletThermocoupleV2

    import datetime
    from datetime import timedelta
    import calendar



    if __name__ == "__main__":
        ipcon = IPConnection()  # create IP connection

        tptc_cdo = BrickletPTCV2(UID1, ipcon)  # Create device object
        ssrHL_cdo = BrickletSolidStateRelayV2(UID3, ipcon)  # Create device object
        
        ipcon.connect(HOST, PORT)  # Don't use device before ipcon is connected

        temperature_IN = tptc_cdo.get_temperature()
        log.info("Temperature_OUT: " + str(temperature_IN/100.0) + " °C")

        # Get current date and time
        Time_act = gmtime()
        Year = str(Time_act[0])
        Month = str(Time_act[1])
        Day = str(Time_act[2])
        Hour = str(Time_act[3] + 2)
        Minute = str(Time_act[4])
        Second = str(Time_act[5])

        timestamp = ((Year)+'.'+(Month)+'.'+(Day)+';'+(Hour)+':'+(Minute)+':'+(Second))
        log.info("Start-time: " + str(timestamp))

        if Time_act[3] > 3 and Time_act[3] < 17: # This is GMT so +2 hours (start heating at 6: set to >3)

            with open('Experiments_Heating_20210628.csv', 'a', newline='') as file:
                writer = csv.writer(file)
                writer.writerow([str(timestamp), temperature_IN/100.0, ssrHL_cdo.get_state(), temperature_OUT/100.0])

            
            if (temperature_IN / 100.0) < 26.20:
                ssrHL_cdo.set_state(True)
                log.info("Heat R\L ON")
                sleep(60)
            if (temperature_IN / 100.0) < 27.75:
                ssrHL_cdo.set_state(True)
                log.info("Heat L ON")
                sleep(60)
            if (temperature_IN / 100.0) > 27.85:
                ssrHL_cdo.set_state(False)
                log.info("Heat OFF")
                sleep(60)
        if Time_act[3] > 16:
            ssrHL_cdo.set_state(False)
            log.info("Heat OFF")
    ipcon.disconnect()

Taking a quick look at the tinkerforge documentation it looks like they support I2C so you could try adding node-red-contrib-i2c to your flow and seeing if you can get it to talk the device.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.