SHARE
TWEET

Untitled

a guest Dec 5th, 2014 770 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2.  
  3. import socket
  4.  
  5. TCP_ADDR = '10.2.2.250'
  6. TCP_PORT = 8899
  7.  
  8. PACK_LEN = 11
  9.  
  10. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11.  
  12. s.settimeout(30)
  13.  
  14. s.connect((TCP_ADDR, TCP_PORT))
  15.  
  16. bytes_data = zeros(PACK_LEN,int)
  17.  
  18.  
  19. while True:
  20.     str_data = s.recv(PACK_LEN) #this should probably have a timeout
  21.     hex_data = str_data.encode('hex')
  22.    
  23.     for n in range(0,PACK_LEN): #convert to array of bytes
  24.         lower = 2*n
  25.         upper = lower + 2
  26.         bytes_data[n] = int(hex_data[lower:upper],16)
  27.    
  28.  
  29.     humid =  (((bytes_data[6])<<8)+(bytes_data[7]))/10.0
  30.     temp =  (((((bytes_data[8])&0x7F)<<8)+(bytes_data[9]))/10.0)
  31.    
  32.     if int(bytes_data[8]) & 0x80: #invert temp if sign bit is set
  33.         temp = -1.0* temp
  34.        
  35.     checksum = (uint(sum(bytes_data[0:10])) & 0xFF)+1
  36.    
  37.     if checksum == bytes_data[10]:
  38.         print "Valid!"
  39.         print "Temp: " + str(temp)
  40.         print "Hum: " + str(humid)
  41.     else:
  42.         print "Invalid. :("
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top