Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import serial
- import urllib.request
- import json
- from OpenSSL import SSL
- import ssl
- import sys
- # from memory_profiler import profile
- PORT = '/dev/ttyACM0'
- serialPort = serial.Serial(
- port=PORT,
- baudrate=9600,
- parity=serial.PARITY_NONE,
- stopbits=serial.STOPBITS_ONE,
- bytesize=serial.EIGHTBITS,
- timeout=0.5
- )
- #@profile
- def run():
- if serialPort.isOpen():
- print("Serial is opened")
- else:
- print("Serial cannot be opened")
- exit(1)
- time.sleep(0.1)
- serialPort.reset_input_buffer()
- # counter = 0
- # while (counter < 10):
- while(True):
- try:
- data = serialPort.read(100)
- if len(data) > 0:
- text = data.decode().strip()
- print("Received data: %s" % text)
- body = {'temperature' : [text]}
- myurl = "https://cog-dev.xyz:5000/postjson"
- myssl = ssl.create_default_context();
- myssl.check_hostname=False
- myssl.verify_mode=ssl.CERT_NONE
- jsondata = json.dumps(body)
- jsondataasbytes = jsondata.encode('utf-8')
- req = urllib.request.Request(myurl, jsondataasbytes)
- req.add_header('Content-Type', 'application/json; charset=utf-8')
- req.add_header('Content-Length', len(jsondataasbytes))
- print (jsondataasbytes)
- response = urllib.request.urlopen(req, context=myssl)
- # counter += 1
- except:
- print("Exception happened: ", sys.exc_info()[0])
- time.sleep(0.5)
- if __name__ == "__main__":
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement