Advertisement
Guest User

Untitled

a guest
May 5th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. import time
  2. import serial
  3. import urllib.request
  4. import json  
  5. from OpenSSL import SSL
  6. import ssl
  7. import sys
  8. # from memory_profiler import profile
  9.  
  10. PORT = '/dev/ttyACM0'
  11. serialPort = serial.Serial(
  12.         port=PORT,
  13.         baudrate=9600,
  14.         parity=serial.PARITY_NONE,
  15.         stopbits=serial.STOPBITS_ONE,
  16.         bytesize=serial.EIGHTBITS,
  17.         timeout=0.5
  18.     )
  19.  
  20. #@profile
  21. def run():
  22.     if serialPort.isOpen():
  23.         print("Serial is opened")
  24.     else:
  25.         print("Serial cannot be opened")
  26.         exit(1)
  27.     time.sleep(0.1)
  28.     serialPort.reset_input_buffer()
  29. #    counter = 0
  30. #    while (counter < 10):
  31.     while(True):
  32.         try:
  33.             data = serialPort.read(100)    
  34.             if len(data) > 0:
  35.                 text = data.decode().strip()
  36.                 print("Received data: %s" % text)
  37.                 body = {'temperature' : [text]}
  38.                 myurl = "https://cog-dev.xyz:5000/postjson"
  39.                 myssl = ssl.create_default_context();
  40.                 myssl.check_hostname=False
  41.                 myssl.verify_mode=ssl.CERT_NONE
  42.                 jsondata = json.dumps(body)
  43.                 jsondataasbytes = jsondata.encode('utf-8')
  44.                 req = urllib.request.Request(myurl, jsondataasbytes)
  45.                 req.add_header('Content-Type', 'application/json; charset=utf-8')
  46.                 req.add_header('Content-Length', len(jsondataasbytes))
  47.                 print (jsondataasbytes)
  48.                 response = urllib.request.urlopen(req, context=myssl)
  49. #                counter += 1
  50.         except:
  51.             print("Exception happened: ", sys.exc_info()[0])
  52.         time.sleep(0.5)
  53.  
  54. if __name__ == "__main__":                                                                                    
  55.     run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement