Skip to content

Instantly share code, notes, and snippets.

@HectorTorres
Created February 4, 2021 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HectorTorres/d3df9f4616a26da53f7b01070db312e1 to your computer and use it in GitHub Desktop.
Save HectorTorres/d3df9f4616a26da53f7b01070db312e1 to your computer and use it in GitHub Desktop.
#String json_data = "{\"Sesor_id\":\"3E24R\",\"Value\":" + (String)randNumber + "}";
import serial, time, json
import matplotlib.pyplot as plt
import numpy as np
hw_sensor = serial.Serial(port='COM4', baudrate=115200, timeout=1, write_timeout=1)
fig, ax = plt.subplots()
t = np.linspace(0,20,21)
s = np.zeros(21)
if __name__ == '__main__':
while True:
hw_sensor.write('getValue'.encode('utf-8'))
time.sleep(0.2)
try:
raw_string_b = hw_sensor.readline()
raw_string_s = raw_string_b.decode('utf-8')
if(raw_string_s.index("}")>=0 and raw_string_s.index("{")==0):
raw_string_s = raw_string_s[0:raw_string_s.index("}")+1]
raw_string_j = json.loads(raw_string_s)
print(raw_string_j)
print(raw_string_j["Sensor_id"])
print(raw_string_j["Value"])
else:
print("error/ no } found.")
except:
print("Exception occurred, somthing wrong...")
t=np.delete(t,0)
t=np.append(t,t[-1]+1)
s=np.delete(s,0)
try:
s=np.append(s,raw_string_j["Value"])
except:
s=np.append(s,-1)
print("Error 4534")
print(s)
plt.cla()
ax.plot(t,s)
plt.pause(0.02)
plt.show()
hw_sensor.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment