tempsend.py – Client side for temperature measurement

#! /usr/bin/python3
import os
import time
import sys
import socket

os.system('sudo modprobe w1-gpio')
os.system('sudo modprobe w1-therm')

#File where the sensors store the temp
temp_sensor='/sys/bus/w1/devices/<change to your device address>/w1_slave'

#Capture the lines from the files
def temp_raw():
    f1 = open(temp_sensor, 'r')
    lines1 = f1.readlines()
    f1.close()
    return lines1

#Get the temperature from the second line if status is YES on the first line
def read_temp():
    lines1 = temp_raw()
    while lines1[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines1 = temp_raw()
    temp_output1 = lines1[1].find('t=')
    if temp_output1 != -1:
        temp_string1 = lines1[1].strip()[temp_output1+2:]
        temp_c1 = float(temp_string1) / 1000.0
        return float(temp_c1)

# Get temperature and send to server
temp1 = str(read_temp())
print ("Temperature on sensor ", temp_sensor, "---", temp1)

HOST, PORT = "192.168.0.100", 3000

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

print ("Sending")
sock.sendto(bytes(temp1, "utf-8"), (HOST, PORT))
print("Sent:     {}".format(temp1))

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>