# Control Heater using node-red

**URL:** <https://discourse.nodered.org/t/control-heater-using-node-red/63604>\
**Category:** General\
**Created:** [8 June 2022 09:11 UTC](https://discourse.nodered.org/t/control-heater-using-node-red/63604 "2022-06-08T09:11:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lizzie](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@Lizzie](https://discourse.nodered.org/u/Lizzie)\
**Post date:** [8 June 2022 09:11 UTC](https://discourse.nodered.org/t/control-heater-using-node-red/63604/1 "2022-06-08T09:11:09Z")

</div>

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 🙂

```auto
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()

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [8 June 2022 14:18 UTC](https://discourse.nodered.org/t/control-heater-using-node-red/63604/2 "2022-06-08T14:18:56Z")

</div>

Taking a quick look at the [tinkerforge documentation](https://www.tinkerforge.com/en/doc/Technical_Data.html) 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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 August 2022 14:19 UTC](https://discourse.nodered.org/t/control-heater-using-node-red/63604/3 "2022-08-07T14:19:22Z")

</div>

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