블로그 이미지
규미

Rss feed Tistory
카테고리 없음 2011. 7. 26. 21:02

온도측정 프로그램

리눅스의 sensors 명령어를 통하여 온도를 측정하여 udp로 서버에 전송하는 프로그램

기본적으로 sensors-detect를 실행하여 온도측정의 환경변수를 설정해줘야 한다. 그리고 사설ip를 사용시에 공유기에서 포트를 열어줘야 하며, 서버측은 정보를 보는쪽이 되며 클라이언트는 정보를 보내는 쪽이 된다.

클라이언트 (client.py)

from socket import *
import os
import time

HOST='59.22.142.234' #서버측 ip
sport=7000 #서버포트
cport=5000 #클라이언트포트
csock = socket(AF_INET,SOCK_DGRAM)
csock.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
csock.bind(('',cport))

while 1:
        str1=os.popen('sensors') #sensors를 실행
        str1=str1.read()
        str1=str1[str1.find('Sys Temp'):] #정보추출 ~
        systemp=str1[str1.find('Sys Temp'):str1.find('(')-2]
        str1=str1[str1.find('CPU Temp'):]
        cputemp=str1[str1.find('CPU Temp'):str1.find('(')-2]
        str1=str1[str1.find('AUX Temp'):]
        auxtemp=str1[str1.find('AUX Temp'):str1.find('(')-2]
        str1=str1[str1.find('Sys Temp'):]
        str2='nyx'+'\n'+systemp+'\n'+cputemp+'\n'+auxtemp
        csock.sendto(str2,(HOST,sport))#전송
        msg,addr=csock.recvfrom(1024)
        time.sleep(2)#2초간 sleep


 

서버측 <tkinter가 설치되어있어야함>
from Tkinter import *
from socket import *
import select
PORT = 7000 #서버 포트

svrsock = socket(AF_INET,SOCK_DGRAM)
svrsock.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
svrsock.bind(('',PORT))
args=[svrsock],[],[]
root=Tk()
label=Label(root,text='')
label.pack()
while 1:
         reti,retw,rete=select.select(*args)
         for sock in reti:
                  msg,addr=sock.recvfrom(1024)
                  label.config(text=msg)
                  label.update_idletasks()
                  sock.sendto(msg,addr)
                  label.pack()

 

,
TOTAL TODAY