Real-time Water Thermostat with Azure IoT pt.1

The reason behind this is that these two guys were having health problems.

One had shell disease and another caught a cold(or more professionally, respiratory infection). It was fall and the room temp was changing rapidly (I also caught a cold one morning). So I would have to separate them and keep the environment at a steady temperature. The cheap heater is not so reliable and I wanted to monitor the water temperature.

I used a DS18B20 sensor to detect the temperature.

Since the three-pinned sensor is not waterproof, I also purchased the water-resistant wire and solder the pins so that they could be used underwater (super ugly though).

Test the functionality of the sensor
I registered the Microsoft Azure(using Brandeis student account) and created the device(the pi)
Install AZURE IoT on pi and run the python code

(Always remember to change the mirror source if something needs to be downloaded or installed…)

AZURE IoT doc reference:

https://docs.microsoft.com/en-us/azure/iot-develop/quickstart-send-telemetry-iot-hub?pivots=programming-language-python

If you want to use your own device, remember to adjust the code:

#replace with your own
CONNECTION_STRING = "xxxxxxxxxxxxxxxxxxxx"

# Define the JSON message to send to IoT Hub.
MSG_TXT = '{{"temperature": {temperature}}}'

#replace with your own device(sensor)
deviceNum = "xxxxxxxxxx"

deviceFile = '/sys/bus/w1/devices/' + deviceNum + '/w1_slave'

def readTempFile():
    f = open(deviceFile,'r')
    lines = f.readlines()
    f.close()
    return lines

def readTemp():
    lines = readTempFile()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = readTempFiles()
    tempInx= lines[1].find('t=')
    if tempInx != -1:
        temp = lines[1][tempInx + 2:]
        tempC = float(temp)/1000.0
    return tempC

An error occurred: after sending the first message successfully, it stopped.(I had not finished the code yet so the humidity is the default random number)

Leave a Reply

Your email address will not be published. Required fields are marked *