User Tools

Site Tools


arduino:pms5003

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
arduino:pms5003 [2020/11/30 21:52] – created aistisarduino:pms5003 [2020/12/14 22:29] (current) – aistis
Line 7: Line 7:
 {{:arduino:pasted:20201130-215212.png}} {{:arduino:pasted:20201130-215212.png}}
  
 +<code python>
 +import serial
 +import matplotlib.pyplot as plt
 +import numpy as np
 +from struct import unpack
  
 +s = serial.Serial(port='COM6',baudrate=9600)
 +plt.ion()
 +fig = plt.figure()
 +plt.grid()
 +plt.title('particale concentration in my room');
 +
 +zz = np.array(0)
 +
 +y1 = np.array(np.nan)
 +y2 = np.array(np.nan)
 +y3 = np.array(np.nan)
 +
 +ax = fig.add_subplot(111)
 +line1, = ax.plot(zz, y1, 'r-', label='PM 1')
 +line2, = ax.plot(zz, y2, 'k-', label='PM 2.5')
 +line3, = ax.plot(zz, y3, 'g-', label='PM 10')
 +fig.show()
 +fig.legend()
 +
 +
 +while True:
 +    res = s.read(size=32)
 +    all_data = unpack('>BBHHHHHHHHHHHHHHH', res)
 +    # byte, byte,
 +    print(all_data)
 +
 +    y1 = np.append(y1, all_data[2])
 +    y2 = np.append(y2, all_data[3])
 +    y3 = np.append(y3, all_data[4])
 +
 +    x = np.arange(y1.shape[0])
 +    line1.set_xdata(x)
 +    line2.set_xdata(x)
 +    line3.set_xdata(x)
 +    line1.set_ydata(y1)
 +    line2.set_ydata(y2)
 +    line3.set_ydata(y3)
 +    fig.canvas.draw()
 +    fig.canvas.flush_events()
 +    plt.pause(0.05)
 +    v1 = np.min([np.nanmin(y1),np.nanmin(y2),np.nanmin(y3)])
 +    v2 = np.max([np.nanmax(y1),np.nanmax(y2),np.nanmax(y3)])
 +
 +    ax.set(xlim=(x.min()-1, x.max()+1), ylim=(v1-1, v2+1))
 +
 +s.close()
 +</code>
 +
 +{{:arduino:pasted:20201214-222902.png}}
arduino/pms5003.1606765940.txt.gz · Last modified: by aistis