**Tutorial on how to count warmwater consuption with temperature sensor**
Peter Cordova
pc222hf
In this project I try to count my monthly warmwater consuption with detailed costs. I use temperature sensor and Pycom device to send data to Pybytes plattform that gather information and visualize it in charts.
It takes about 5 hours to set-up everything and about 5-10 minutes a day for maintenance.
**Objective**
I chose this project because i was curious about finding out speific warmwater costs. Every month I get a invoice for warm water that is around 450sek. I just get amount without any specifications. Then I was intrested to find out how costs are devided and asigned. Thats why I decided to create a device that count my warmwater consuption during the day. I think this information is usefull to know for both invironment and better private economy.
**Material**
I used following material:
| Material | Purpose |
| -------- | -------- |
| Pycom ExtansionBoard | connection and power
Lopy4|network and programming
Lora Anthena|network
USB cable|power supply
sensor DHT11|measure temperature
I bought the material on Electrokit.se and I paid 995SEK for a set. I got all I needed plus other sensors that I didnt use.
 ExtansionBoard and Lopy4
Lora Anthena
 DHT11 Temperature and Humidity sensor
**Computer setup**
I installed and used Atom as IDE and Pymakr plugins.
I uploaded all the code through the Atom and used Pybytes plattform to get the data. I followed Pybytes tutorials for setting Wifi and Lora connections.
**Putting everything together**
I started with connection of my Lopy4 to the computer, checked the connection and network, sended some signals to Pybytes through Atom/Pymakr. Then I turned off the Pycom device and connected my DHT11 to extansionboard: ground(brown cable) to GND, VCC to 3VR on the board and finally data to P23.

**Platform**
I used Pybytes cloud plattform and I think its very easy to use, simple with step by step guides. I used free version. Pybytes had good mobile version, in case you want to check your device outside. And firware over the air worked quickly without any errors.
**The code**
The code is attached below:
```
# https://github.com/JurassicPork/DHT_PyCom
import time
import pycom
from machine import enable_irq, disable_irq, Pin
class DHTResult:
'DHT sensor result returned by DHT.read() method'
ERR_NO_ERROR = 0
ERR_MISSING_DATA = 1
ERR_CRC = 2
error_code = ERR_NO_ERROR
temperature = -1
humidity = -1
def __init__(self, error_code, temperature, humidity):
self.error_code = error_code
self.temperature = temperature
self.humidity = humidity
def is_valid(self):
return self.error_code == DHTResult.ERR_NO_ERROR
class DHT:
'DHT sensor (dht11, dht21,dht22) reader class for Pycom'
#__pin = Pin('P3', mode=Pin.OPEN_DRAIN)
__dhttype = 0
def __init__(self, pin, sensor=0):
self.__pin = Pin(pin, mode=Pin.OPEN_DRAIN)
self.__dhttype = sensor
self.__pin(1)
time.sleep(1.0)
def read(self):
# pull down to low
self.__send_and_sleep(0, 0.019)
data = pycom.pulses_get(self.__pin,100)
self.__pin.init(Pin.OPEN_DRAIN)
self.__pin(1)
#print(data)
bits = []
for a,b in data:
if a ==1 and 18 <= b <= 28:
bits.append(0)
if a ==1 and 65 <= b <= 75:
bits.append(1)
#print("longueur bits : %d " % len(bits))
if len(bits) != 40:
return DHTResult(DHTResult.ERR_MISSING_DATA, 0, 0)
#print(bits)
# we have the bits, calculate bytes
the_bytes = self.__bits_to_bytes(bits)
# calculate checksum and check
checksum = self.__calculate_checksum(the_bytes)
if the_bytes[4] != checksum:
return DHTResult(DHTResult.ERR_CRC, 0, 0)
# ok, we have valid data, return it
[int_rh, dec_rh, int_t, dec_t, csum] = the_bytes
if self.__dhttype==0: #dht11
rh = int_rh #dht11 20% ~ 90%
t = int_t #dht11 0..50°C
else: #dht21,dht22
rh = ((int_rh * 256) + dec_rh)/10
t = (((int_t & 0x7F) * 256) + dec_t)/10
if (int_t & 0x80) > 0:
t *= -1
return DHTResult(DHTResult.ERR_NO_ERROR, t, rh)
def __send_and_sleep(self, output, mysleep):
self.__pin(output)
time.sleep(mysleep)
def __bits_to_bytes(self, bits):
the_bytes = []
byte = 0
for i in range(0, len(bits)):
byte = byte << 1
if (bits[i]):
byte = byte | 1
else:
byte = byte | 0
if ((i + 1) % 8 == 0):
the_bytes.append(byte)
byte = 0
#print(the_bytes)
return the_bytes
def __calculate_checksum(self, the_bytes):
return the_bytes[0] + the_bytes[1] + the_bytes[2] + the_bytes[3] & 255
```
```
# Data is sent to Pybytes. Needs to flashed with Pybyte firmware
import time
from machine import Pin
from dht import DHT # https://github.com/JurassicPork/DHT_PyCom
# Type 0 = dht11
# Type 1 = dht22
th = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0)
time.sleep(2)
while True:
result = th.read()
while not result.is_valid():
time.sleep(.5)
result = th.read()
print('Temp:', result.temperature)
print('RH:', result.humidity)
pybytes.send_signal(1,result.temperature)
pybytes.send_signal(2,result.humidity)
time.sleep(5)
```
DHT and DHTResult classes are responsible for taking the valid information and main class kör några metoder som skickar data till både atom terminal och pybytes.
**Transmitting the data / connectivity**
Data is sent every 7 seconds, and I used Wifi as first choice for network, MQTT protocol.
**Presenting the data**
The data is sent every 7th second to pybytes and displays in the linechart diagram, the example bellow shows last 4 hours.

**Finalizing the design**
I am happy with my discoveries and I find out that it cost for me around 1,8SEK to shower, 2,5SEK for dishwashing and 0,09SEK each time I wash my hands. To use warm water for one minute cost 0,18SEK.
Here is the picture of my device in action
