-2

I am trying to plot the real time graph of temperature and humidity. I have used dht22(am2302) sensor and arduino for this.
This is my code:

import sys, serial, argparse
import numpy as np
from time import sleep
from collections import deque

import matplotlib.pyplot as plt 
import matplotlib.animation as animation


# plot class
class AnalogPlot:
  # constr
  def __init__(self, strPort, maxLen):
      # open serial port
      self.ser = serial.Serial(strPort, 9600)
      self.ser.isOpen()
      self.ax = deque([0.0]*maxLen)
      self.ay = deque([0.0]*maxLen)
      self.maxLen = maxLen

  # add to buffer
  def addToBuf(self, buf, val):
      if len(buf) < self.maxLen:
          buf.append(val)
      else:
          buf.pop()
          buf.appendleft(val)

  # add data
  def add(self, data):
      assert(len(data) == 2)
      self.addToBuf(self.ax, data[0])
      self.addToBuf(self.ay, data[1])

  # update plot
  def update(self, frameNum, a0, a1):
      try:
          line = self.ser.readline()
          data = [float(val) for val in line.split()]
          # print data
          if(len(data) == 2):
              self.add(data)
              a0.set_data(range(self.maxLen), self.ax)
              a1.set_data(range(self.maxLen), self.ay)
      except KeyboardInterrupt:
          print('exiting')

      return a0, 

  # clean up
  def close(self):
      # close serial
      self.ser.flush()
      self.ser.close()    

# main() function
def main():
  # create parser
  parser = argparse.ArgumentParser(description="LDR serial")
  # add expected arguments
  parser.add_argument('--port',dest='port')

  # parse args
  args = parser.parse_args()

  #strPort = '/dev/tty.usbserial-A7006Yqh'
  strPort = args.port
  print(args.port)
  print('reading from serial port %s...' % strPort)

  # plot parameters
  analogPlot = AnalogPlot(strPort, 100)

  print('plotting data...')

  # set up animation
  fig = plt.figure()
  ax = plt.axes(xlim=(0, 100), ylim=(0, 1023))
  a0, = ax.plot([], [])
  a1, = ax.plot([], [])
  anim = animation.FuncAnimation(fig, analogPlot.update, 
                                 fargs=(a0, a1), 
                                 interval=50)

 # show plot
  plt.show()
 # clean up
  analogPlot.close()

  print('exiting.')

# call main
if __name__ == '__main__':
  main()

Please help me to resolve this error and plot the real time graph accurately. This is the figure window:

This figure does not display any data. I want to plot real time data.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

Change the following from

parser.add_arguement('--port', dest='port', required=True)

to

parser.add_arguement('--port', dest='port')

or else you need to pass the port while running the script

python second.py --port <port name>
Jeril
  • 7,858
  • 3
  • 52
  • 69
  • thank you!!! but still my figure window does not display anything. It shows some errors like: File "C:\Users\cw\AppData\Local\Programs\Python\Python37-32\lib\site-packages\serial\serialwin32.py", line 445, in out_waiting raise SerialException("ClearCommError failed ({!r})".format(ctypes.WinError())) – shabnam deep Feb 05 '19 at 04:45
  • i have wriiten in the comment above. Plz check – shabnam deep Feb 05 '19 at 04:49
  • I guess the issue is with serialpy. check this [link](https://stackoverflow.com/q/14525977/2825570). – Jeril Feb 05 '19 at 04:52
  • this is my error: serial.serialutil.SerialException: Attempting to use a port that is not open – shabnam deep Feb 05 '19 at 05:17
  • Can you try [this](https://stackoverflow.com/q/37288278/2825570) or [this](https://github.com/adafruit/Adafruit_BLESniffer_Python/issues/7) – Jeril Feb 05 '19 at 05:20
  • this is not helpful. can you please suggest something else – shabnam deep Feb 05 '19 at 05:43
  • what is your actual issue, is it regarding the plot or serial data? – Jeril Feb 05 '19 at 05:57
  • my problem is that when i try to run my code then the figure window does not display any data. i want my figure window to display graph of temperature and humidity. but it is blank as you can see from the picture that i have attached above. – shabnam deep Feb 05 '19 at 06:01
  • the data that you are trying to plot, is it available? – Jeril Feb 05 '19 at 06:03
  • yes. the data is available on python shell window. but only figure window does not display the data. – shabnam deep Feb 05 '19 at 06:09
  • Then you need to check the`anim = animation.FuncAnimation` line, try doing it for a simple example – Jeril Feb 05 '19 at 06:13