import serial
import time
import json
import io

ser = serial.Serial('COM5', 115200, timeout=0)
s = 255
color = "red"
response = "Are you even shaking my hand right now?"
count = 0

while count < 1000:
    if ser.in_waiting:
        try:
            s = int(ser.readline().decode('utf-8'))
        except:
            s = s

    if s < 80:
        color = "red"
        response = "Are you even shaking my hand right now?"
    elif s < 190:
        color = "yellow"
        response = "Could be a little firmer"
    elif s < 215:
        color = "green"
        response = "Nice handshake!"
    elif s < 230:
        color = "yellow"
        response = "Whoa, a little hard there"
    else:
        color = "red"
        response = "Are you trying to break my hand or something?"
    
    data = [s, color, response]
    
    with io.open('C:\\Users\\lleyt\\OneDrive\\Desktop\\Schoolwork\\4.140\\Final Project\\Code\\data.json', 'w', encoding='utf8') as outfile:
        str_ = json.dumps(data,
                      indent=4, sort_keys=True,
                      separators=(',', ': '), ensure_ascii=False)
        outfile.write((str_))
    time.sleep(0.1)
    count += 1