IoT Hottub Temperature Controlled Power Switch: Difference between revisions

From munkjensen.net/wiki
mNo edit summary
 
(41 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:Arduino]]
[[Category:Arduino]]<span style="float:right;">__TOC__</span>
# Køb en TH16
I needed an IoT thingy that would make it possible for me to show the current temperature of the water in my hottub on a webpage.
# + en temperatursensor der passer til TH16 og dine behov
I found that the Sonoff TH16 could do this task - but not using the lousy standard firmware. So a little hacking was needed.  
# + en 3.3 volt USB til seriel adapter
# + ledninger
# læs det her https://www.redelijkheid.com/blog/2017/12/31/flashing-the-sonoff-th16-wireless-switch


https://hos.munkjensen.net/vildmarksbad.php
During the process of making this fit my needs I made it so that if you push the button on the TH16 then the red LED light up and the relay close. If you push the button again then the red LED will switch off and the relay will open. My intented use for this is to control the lights over the hottub.
* If the TH16 button is not activated the the lights will turn on when the temperature of the water is above 37 degrees celcius. This threshold can be changeg in Rule Set 1 below.
* If the button is pushed then the relay will close if open, or open if closed unless the temperature is above 37 degrees celcious.
 
=== Shopping list ===
# Buy a [https://www.google.com/search?q=sonoff+th16 Sonoff TH16].
# Buy a temperature sensor that fits TH16 and your needs. I use the [https://www.google.com/search?q=Sonoff+DS18B20+waterproof+temperature+sensor Sonoff DS18B20 waterproof temperature sensor].
# Buy a 3.3 volt [https://www.google.com/search?q=usb+to+ttl+ch340 USB to seriel adaptor]
 
=== Documentation &amp; inspiration pages ===
* I found a lot of inspiration in the pages https://www.redelijkheid.com/blog/2017/12/31/flashing-the-sonoff-th16-wireless-switch and https://tinkerman.cat/post/sonoff-th10-th16-sensors-displays-actuators
* Be aware of [https://espeasy.readthedocs.io/en/latest/ dokumentation] for [https://github.com/letscontrolit/ESPEasy ESPeasy]
* https://espeasy.readthedocs.io/en/latest/Reference/URLs.html?highlight=csv is usefull for pulling data from the hacked TH16
 
=== Flashing ===
* DO NOT CONNECT THE TH16 TO MAINS POWER WHILE FLASHING!
* I [https://espeasy.readthedocs.io/en/latest/Reference/Flashing.html flashed] my TH16 Ver. 2.1 dated 2019-08-26 with the  file <code>ESP_Easy_mega_20211105_normal_ESP8285_1M.bin</code>.
 
The page [https://www.letscontrolit.com/wiki/index.php/Basics:_Connecting_and_flashing_the_ESP8266 Basics: Connecting and flashing the ESP8266] also got a lot of good advice.
 
=== Configuration after flashing ===
You need to do a bit of configureing after your TH16 have been succesfullly flashed.
I reccomend keeping the TH16 connected to you computer using the 3.3 volt USB to seriel adaptor untill you are certain it works as you intend. I reccomend this of two reasons.
# There is no need to connect it to the mains power before you actually want to use it. This way you are much safer from getting electrical shocks ;-)
# You can use [https://www.chiark.greenend.org.uk/~sgtatham/putty/ PuttY] to monitor the activities that your TH16 is performing including errors... and errors will most probably happen unless you are lucky like nobody else.
 
=== First boot ===
''NOTE:'' TH16 can only connect to 2.4Ghz WiFi
 
Reboot the TH16 after the flasing. Keep an eye on the Putty console. If nothing shows up you need to make sure PuttY is configured to listen on the correct COM port using the correct serial port settings.
 
After reboot is done the TH16 will start to spit out repeating lines.
 
Now fetch your smartphone or another wireless enabled device and connect to the open WiFi that the TH16 is broadcasting. Is will be named something like '''ESP_Easy_0''' and you will need to enter '''configesp''' as WiFi password. If you are not presented with a webpage automagically - look at Putty to figure out what IP address the TH16 have given it self... '''192.168.4.1''' could be it but you never know if it gets changed in the future.
 
Enter the IP in your browser and configure the TH16 to connect to your WiFi. BE AWARE that if you screw this up and enter wrong data/credentials you MIGHT need to re-flash to get the first-boot WiFi and webpage to launch again ;-)
 
=== Second boot ===
Find the NEW IP that the TH16 got on your own WiFi and open that IP in your webbrowser. You should land on the Main-page that shows som genereal information about the TH16.
 
'''Click the Config tab.'''
# Set Unit Number to 1. If you got more that one TH16 then you need to give them each a unique number here.
# Click Submit at the bottom.
# You don't need to change anything on the Controllers tab.
'''Click the Hardware tab.'''
# Set GPIO → LED to use GPIO--13 (D7)
# GPIO ⇄ SDA & SCL to none.
# Click Submit at the bottom.
'''Click the Devices tab.'''
'''Click the Add button left of task 1.'''
# Select the device that fits your temperature sensor. Environment - DS18b20 if you bought the same as me.
# Set the name to sensorTemp.
# Set GPIO ← TX to GPIO-14(D5)
# Set Number Output Values to Single
# Set Device Resolution to maximum (12 Bit)
# Scroll to bottom of the page.
# Set Interval to 5 seconds.
# Set the name of Value 1 to temperature
# Set the number of decimals to 2.
# Click Submit at the bottom.
'''Click the Add button left of task 2.'''
# Select Generic - Dummy Device
# Set the name to varTemp
# Click the Enable tick-box so it is enabled.
# Set Output Data Type to Single
# Set Interval to 0.
# Set the name of Value 1 to lastValue.
# Set the number of decimals to 2.
# Click Submit at the bottom.
'''Click the Add button left of task 3.'''
# Select Generic - Dummy Device
# Set the name to Switch1
# Click the Enable tick-box so it is enabled.
# Set Output Data Type to Switch
# Set Interval to 0.
# Set the name of Value 1 to State.
# Set the number of decimals to 0.
# Click Submit at the bottom.
'''Click the Add button left of task 4.'''
# Select Switch input - Switch
# Set the name to toggleSwitch
# Click the Enable tick-box so it is enabled.
# Set GPIO ⇄ to GPIO-0(D3)
# set Switch Type to Switch
# Set Switch Button Type to Push Button active Low
# Click the Send Boot state tick-box so it is enabled.
# Set De-bounce(ms) to 10
# Set the name of Value 1 to State
# Click Submit at the bottom.
'''Click the Tools tab.'''
# Click Advenced button to Open advanced settings.
# Click the Rules tick-box so it is enabled.
# Scroll to bottom of the page.
# Click the Submit button.
'''Click the :→Rules tab.'''
# Make sure to have Rule Set 1 selected. This is default.
# Copy the content of Rule Set 1 below.
# Paste it into the big text-box of Rule Set 1.
# Click Save.
 
=== Did it work? ===
If you were luck then you should see lines in Putty that looks something like this:
<pre>5248698 : Info  : DS  : Temperature: 22.44 (28-ff-d4-cd-a1-16-05-89 [DS18B20])
5248745 : Info  : EVENT: sensorTemp#temperature=22.44
5248753 : Info  : ACT  : TaskValueSet,varTemp,lastValue,22.44
5248773 : Info  : ACT  : TaskValueSet,Switch1,state,0
5248783 : Info  : ACT  : gpio,12,00
5248785 : Info  : GPIO : port#12: set to 0
5251653 : Info  : WD  : Uptime 88 ConnectFailures 0 FreeMem 21488 WiFiStatus 3 ESPeasy internal wifi status: Conn. IP Init
5253697 : Info  : DS  : Temperature: 22.50 (28-ff-d4-cd-a1-16-05-89 [DS18B20])
5253745 : Info  : EVENT: sensorTemp#temperature=22.50
5253753 : Info  : ACT  : TaskValueSet,varTemp,lastValue,22.50
5253773 : Info  : ACT  : TaskValueSet,Switch1,state,0
5253783 : Info  : ACT  : gpio,12,00
5253785 : Info  : GPIO : port#12: set to 0
5258696 : Info  : DS  : Temperature: 22.44 (28-ff-d4-cd-a1-16-05-89 [DS18B20])</pre>
And if you push the button on the TH16 then the red LED should light up and the realy sholud make an audible 'ClicK' sound indicating that it is closed. If you push the button again then the red LED should switch off and the relay should make a 'ClicK' sound again...
 
=== Rule Set 1 ===
<blockquote><pre>
on sensorTemp#temperature do
 
        // Store current sensor temperature in a dummy variable
    TaskValueSet,varTemp,lastValue,[sensorTemp#temperature]
 
        // If temperature is above 37 OR the toggleSwitch state is 1
    if [varTemp#lastValue]>=37 or [toggleSwitch#state]=1
            // then set dummy Switch1 state to 1
      TaskValueSet,Switch1,state,1
    else
          // else set dummy Switch1 state to 0
      TaskValueSet,Switch1,state,0
    endif
 
        // Set the relay and red LED state to the same state as dummy Switch1 state
    gpio,12,0[Switch1#state]
endon
</pre></blockquote>
 
=== The webpage ===
PHP code will be added here later <sup>(TM)</sup>
 
Webpage is avilable [https://hos.munkjensen.net/vildmarksbad.php here].

Latest revision as of 16:24, 17 December 2021

I needed an IoT thingy that would make it possible for me to show the current temperature of the water in my hottub on a webpage. I found that the Sonoff TH16 could do this task - but not using the lousy standard firmware. So a little hacking was needed.

During the process of making this fit my needs I made it so that if you push the button on the TH16 then the red LED light up and the relay close. If you push the button again then the red LED will switch off and the relay will open. My intented use for this is to control the lights over the hottub.

  • If the TH16 button is not activated the the lights will turn on when the temperature of the water is above 37 degrees celcius. This threshold can be changeg in Rule Set 1 below.
  • If the button is pushed then the relay will close if open, or open if closed unless the temperature is above 37 degrees celcious.

Shopping list

  1. Buy a Sonoff TH16.
  2. Buy a temperature sensor that fits TH16 and your needs. I use the Sonoff DS18B20 waterproof temperature sensor.
  3. Buy a 3.3 volt USB to seriel adaptor

Documentation & inspiration pages

Flashing

  • DO NOT CONNECT THE TH16 TO MAINS POWER WHILE FLASHING!
  • I flashed my TH16 Ver. 2.1 dated 2019-08-26 with the file ESP_Easy_mega_20211105_normal_ESP8285_1M.bin.

The page Basics: Connecting and flashing the ESP8266 also got a lot of good advice.

Configuration after flashing

You need to do a bit of configureing after your TH16 have been succesfullly flashed. I reccomend keeping the TH16 connected to you computer using the 3.3 volt USB to seriel adaptor untill you are certain it works as you intend. I reccomend this of two reasons.

  1. There is no need to connect it to the mains power before you actually want to use it. This way you are much safer from getting electrical shocks ;-)
  2. You can use PuttY to monitor the activities that your TH16 is performing including errors... and errors will most probably happen unless you are lucky like nobody else.

First boot

NOTE: TH16 can only connect to 2.4Ghz WiFi

Reboot the TH16 after the flasing. Keep an eye on the Putty console. If nothing shows up you need to make sure PuttY is configured to listen on the correct COM port using the correct serial port settings.

After reboot is done the TH16 will start to spit out repeating lines.

Now fetch your smartphone or another wireless enabled device and connect to the open WiFi that the TH16 is broadcasting. Is will be named something like ESP_Easy_0 and you will need to enter configesp as WiFi password. If you are not presented with a webpage automagically - look at Putty to figure out what IP address the TH16 have given it self... 192.168.4.1 could be it but you never know if it gets changed in the future.

Enter the IP in your browser and configure the TH16 to connect to your WiFi. BE AWARE that if you screw this up and enter wrong data/credentials you MIGHT need to re-flash to get the first-boot WiFi and webpage to launch again ;-)

Second boot

Find the NEW IP that the TH16 got on your own WiFi and open that IP in your webbrowser. You should land on the Main-page that shows som genereal information about the TH16.

Click the Config tab.

  1. Set Unit Number to 1. If you got more that one TH16 then you need to give them each a unique number here.
  2. Click Submit at the bottom.
  3. You don't need to change anything on the Controllers tab.

Click the Hardware tab.

  1. Set GPIO → LED to use GPIO--13 (D7)
  2. GPIO ⇄ SDA & SCL to none.
  3. Click Submit at the bottom.

Click the Devices tab. Click the Add button left of task 1.

  1. Select the device that fits your temperature sensor. Environment - DS18b20 if you bought the same as me.
  2. Set the name to sensorTemp.
  3. Set GPIO ← TX to GPIO-14(D5)
  4. Set Number Output Values to Single
  5. Set Device Resolution to maximum (12 Bit)
  6. Scroll to bottom of the page.
  7. Set Interval to 5 seconds.
  8. Set the name of Value 1 to temperature
  9. Set the number of decimals to 2.
  10. Click Submit at the bottom.

Click the Add button left of task 2.

  1. Select Generic - Dummy Device
  2. Set the name to varTemp
  3. Click the Enable tick-box so it is enabled.
  4. Set Output Data Type to Single
  5. Set Interval to 0.
  6. Set the name of Value 1 to lastValue.
  7. Set the number of decimals to 2.
  8. Click Submit at the bottom.

Click the Add button left of task 3.

  1. Select Generic - Dummy Device
  2. Set the name to Switch1
  3. Click the Enable tick-box so it is enabled.
  4. Set Output Data Type to Switch
  5. Set Interval to 0.
  6. Set the name of Value 1 to State.
  7. Set the number of decimals to 0.
  8. Click Submit at the bottom.

Click the Add button left of task 4.

  1. Select Switch input - Switch
  2. Set the name to toggleSwitch
  3. Click the Enable tick-box so it is enabled.
  4. Set GPIO ⇄ to GPIO-0(D3)
  5. set Switch Type to Switch
  6. Set Switch Button Type to Push Button active Low
  7. Click the Send Boot state tick-box so it is enabled.
  8. Set De-bounce(ms) to 10
  9. Set the name of Value 1 to State
  10. Click Submit at the bottom.

Click the Tools tab.

  1. Click Advenced button to Open advanced settings.
  2. Click the Rules tick-box so it is enabled.
  3. Scroll to bottom of the page.
  4. Click the Submit button.

Click the :→Rules tab.

  1. Make sure to have Rule Set 1 selected. This is default.
  2. Copy the content of Rule Set 1 below.
  3. Paste it into the big text-box of Rule Set 1.
  4. Click Save.

Did it work?

If you were luck then you should see lines in Putty that looks something like this:

5248698 : Info   : DS   : Temperature: 22.44 (28-ff-d4-cd-a1-16-05-89 [DS18B20])
5248745 : Info   : EVENT: sensorTemp#temperature=22.44
5248753 : Info   : ACT  : TaskValueSet,varTemp,lastValue,22.44
5248773 : Info   : ACT  : TaskValueSet,Switch1,state,0
5248783 : Info   : ACT  : gpio,12,00
5248785 : Info   : GPIO : port#12: set to 0
5251653 : Info   : WD   : Uptime 88 ConnectFailures 0 FreeMem 21488 WiFiStatus 3 ESPeasy internal wifi status: Conn. IP Init
5253697 : Info   : DS   : Temperature: 22.50 (28-ff-d4-cd-a1-16-05-89 [DS18B20])
5253745 : Info   : EVENT: sensorTemp#temperature=22.50
5253753 : Info   : ACT  : TaskValueSet,varTemp,lastValue,22.50
5253773 : Info   : ACT  : TaskValueSet,Switch1,state,0
5253783 : Info   : ACT  : gpio,12,00
5253785 : Info   : GPIO : port#12: set to 0
5258696 : Info   : DS   : Temperature: 22.44 (28-ff-d4-cd-a1-16-05-89 [DS18B20])

And if you push the button on the TH16 then the red LED should light up and the realy sholud make an audible 'ClicK' sound indicating that it is closed. If you push the button again then the red LED should switch off and the relay should make a 'ClicK' sound again...

Rule Set 1

on sensorTemp#temperature do

        // Store current sensor temperature in a dummy variable
    TaskValueSet,varTemp,lastValue,[sensorTemp#temperature]

        // If temperature is above 37 OR the toggleSwitch state is 1
    if [varTemp#lastValue]>=37 or [toggleSwitch#state]=1
            // then set dummy Switch1 state to 1
       TaskValueSet,Switch1,state,1
    else
           // else set dummy Switch1 state to 0
       TaskValueSet,Switch1,state,0
    endif

        // Set the relay and red LED state to the same state as dummy Switch1 state
    gpio,12,0[Switch1#state]
endon

The webpage

PHP code will be added here later (TM)

Webpage is avilable here.