tuxmartin

Arduino DHT22 - PC app

Nov 15th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # bez definovani utf-8 nejde tisknout znak stupnu: "°"
  5.  
  6. import serial, httplib, datetime, time, syslog
  7. from threading import Thread
  8.  
  9.  
  10. # # # # # # # # # # NASTAVENI # # # # # # # # # #
  11. webHeslo =          '123456789'
  12. frekvenceMereni =   60 # [s]
  13. urlWeb =            'example.net'
  14. portRS232 =         '/dev/ttyS0'
  15. # # # # # # # # # # NASTAVENI # # # # # # # # # #
  16.  
  17. syslog.syslog('Teplomer spusten')
  18. # ---------------------------------------
  19. def httpOdeslat(teplota, vlhkost):
  20.     httpServ = httplib.HTTPConnection(urlWeb, 80)
  21.     httpServ.connect()
  22.  
  23.     # http://localhost/martin/teplota/teplota.php?h=heslo123&t=20.90&v=50.50&d=23.02.2013_00.00.24
  24.     httpServ.request('GET', '/app/teplota.php' +
  25.                      '?h=' + webHeslo +
  26.                      '&t=' + teplota +
  27.                      '&v=' + vlhkost +
  28.                      '&d=' + datetime.datetime.now().strftime('%d.%m.%Y_%H.%M.%S')                    
  29.                      )
  30.  
  31.     response = httpServ.getresponse()
  32.     if response.status == httplib.OK:
  33.         #print "Output from HTTP request"
  34.         #print response.read()
  35.         pass
  36.  
  37.     httpServ.close()
  38.  # ---------------------------------------
  39.    
  40.  # ---------------------------------------  
  41. def zpracujDataZPortu(text):
  42.     text = text.replace('C', '')
  43.     text = text.replace('%', '')
  44.     text = text.replace('\r\n', '')  
  45.     text = text.replace('#015', '')
  46.     text = text.replace(chr(15), '') # http://mail.python.org/pipermail/python-win32/2005-April/003100.html
  47.    
  48.     textRozdeleno = text.split(' ')
  49.     teplota = textRozdeleno[0]
  50.     vlhkost = textRozdeleno[1]
  51.     if teplota != '' and vlhkost != '':
  52.     syslog.syslog("datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S') + " " + teplota + "C " + vlhkost + "%")
  53.        print datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S') + " " + teplota + "C " + vlhkost + "%"
  54.        httpOdeslat(teplota, vlhkost)
  55. # ---------------------------------------
  56. ser = serial.Serial(
  57.    port=portRS232,
  58.    baudrate=9600,
  59.    parity=serial.PARITY_NONE,
  60.    stopbits=serial.STOPBITS_ONE,
  61.    bytesize=serial.EIGHTBITS
  62. )
  63.  
  64. ser.timeout = None      #arduino posila data kazdych 5s
  65. #timeout = None: wait forever
  66. #timeout = 0: non-blocking mode (return immediately on read)
  67. #timeout = x: set timeout to x seconds (float allowed
  68.  
  69. ser.writeTimeout = 9
  70.  
  71. try:
  72.    ser.open()
  73.    ser.flushInput()
  74.    ser.flushOutput()
  75. except Exception, e:
  76.    syslog.syslog("error open serial port: " + str(e))
  77.    print "error open serial port: " + str(e)
  78.    exit()
  79.  
  80. if ser.isOpen():
  81.    try:
  82.        while True:
  83.            #http://stackoverflow.com/questions/7266558/pyserial-buffer-wont-flush :
  84.            ser.flushInput()    #flush input buffer, discarding all its contents            
  85.            ser.flushOutput()   #flush output buffer, aborting current output
  86.             time.sleep(0.1) #100ms
  87.  
  88.            response = ser.readline()
  89.            response.strip() # http://stackoverflow.com/questions/761804/trimming-a-string-in-python
  90.            print("read data: " + response)
  91.            syslog.syslog("RS-232 response: " + response )
  92.            if response != '':          
  93.                vlakno = Thread(target=zpracujDataZPortu, args=(response,))
  94.                vlakno.start()      
  95. #            ser.flushInput()
  96. #            ser.flushOutput()
  97.  
  98.            time.sleep(frekvenceMereni)
  99.    except Exception, e1:
  100.        syslog.syslog("error communicating...: " + str(e1))
  101.        print "error communicating...: " + str(e1)
  102.    finally:
  103.        ser.close()        
  104. else:
  105.    syslog.syslog("cannot open serial port ")
  106.    print "cannot open serial port "
Advertisement
Add Comment
Please, Sign In to add comment