Skip to content

Instantly share code, notes, and snippets.

@beothorn
Last active December 26, 2020 22:16
Show Gist options
  • Save beothorn/f2fdbe3a3121ffff66275fedc4bd0d0b to your computer and use it in GitHub Desktop.
Save beothorn/f2fdbe3a3121ffff66275fedc4bd0d0b to your computer and use it in GitHub Desktop.
Reads the temperature from android and writes a json file
import serial
import time
import json
from datetime import datetime
def read_from_serial(serial_device_name):
ser = serial.Serial(serial_device_name, 9600)
time.sleep(2)
b = ser.readline() # read a byte string
string_n = b.decode() # decode byte string into Unicode
string = string_n.rstrip() # remove \n and \r
obj = json.loads(string)
now = datetime.now()
current_time = now.strftime("%H:%M")
print("Umidade eh "+str(obj["h"])+" e temperatura eh "+str(obj["t"])+" as " +current_time )
ser.close()
for i in range(9):
try:
read_from_serial('/dev/ttyUSB'+ str(i))
break
except:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment