3

(coco) #1

Smarter hot water


TUTORIAL


as required. Sensor 1 should be the one
at the top of the tank, and sensor 8 at
the bottom.

The box ‘Addressing the Temperature Sensors’
will show how you can find the code number for
each sensor.

PART 3
Opening the sensor drivers
Starts the 1-Wire interface.

PART 4
A subroutine to upload the data
to Google
This is the code that will write the data to your
Google Sheet. This is just a case of using the
update_sheet method from the pygsheets module.

The data is added into a new row near the top of
the sheet and the last row of data is deleted. Based
on repeating the script every five minutes, this will
capture 24 hours of data before being overwritten.
The first few rows are deliberately left blank to
leave space for a chart.

PART 5
A loop that runs eight times


  • once per sensor
    This looping code is repeated eight times,
    once for each sensor. On each loop, it does
    the following:

    • Reads the sensor text file. This is made up of
      two lines of text.

    • Checks if the first line ends in ‘YES’ – if it
      does, then it knows the file has valid data.

    • In the second line, it looks for an equals sign
      and then saves the numbers that come after.
      This is the temperature data in 1000ths of a
      degree C.

    • The data is converted to degrees C.

    • If the temperature is above the threshold
      value, then it’s considered ‘hot’, and the
      variable hw_left is incremented by one. At
      the end of all eight loops, the total number of
      sensors above the hot water threshold value
      is converted to a percentage.

    • Based on the temperature, the colour of
      the corresponding LED is set: to fully red if
      above temp_max, fully blue if below temp_min,
      or an intermediate shade of purple for
      temperatures in-between.

    • Finally, the sensor address, temperature, and
      LED colours are printed to the screen. This is
      useful for troubleshooting.




PART 6
Formatting the data into a tidy state
The percentage of hot water remaining is calculated
and the time is converted to a useful format.

PART 7
Calling the data upload subroutine
The final step is a call-back to the subroutine
defined in Part 4. This writes the data to the
Google Sheet.

ADDRESSING THE


Every sensor has a 64-bit serial number that identifies it.
The digital data can be accessed through the Linux file
system at /sys/bus/w1/devices/<sensor address>. In this
folder is a text file called “w1_slave”. The temperature is
the last few digits, in 1000ths of a degree C.

TEMPERATURE SENSORS


Above
With the data
uploaded to a
spreadsheet, it’s
possible to make
charts of the
temperature over time
Free download pdf