0

I am working on a project that requires pyqtgraph library of python on raspberry pi. It functions well until I added a system time axis on my graphs. What also weird is that the script I am using works flawlessly on windows.

import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph.exporters
import time


import numpy as np

time.sleep(120)  # This is to give Raspberry Pi enough time to obtain IP adress and actual time from the internet

app = QtGui.QApplication([])

pg.setConfigOption('background', 'w')

win = pg.GraphicsWindow(title="Live Graphs")  # creating a window for graphs
win.showMaximized()
win.setWindowTitle('Condition Monitoring')   # Setting up the title
win.ci.setBorder((50, 50, 100))

class CAxisTime(pg.AxisItem):

    def tickStrings(self, values, scale, spacing):
        strns = []
        for x in values:
            try:
                strns.append(time.strftime("%H:%M:%S", time.localtime(x)))    # time_t --> time.struct_time
            except ValueError:  # Windows can't handle dates before 1970
                strns.append('')

        print(values)
        return strns

axis1 = CAxisTime(orientation='bottom')

p1 = win.addPlot(axisItems={'bottom': axis1}, title = '<b>Humidity')  
curve1 = p1.plot(pen = '#00A3E0')  # setting up the curve and the color
p1.setLabel('left', "<b>Relative Humidity", units='<b>%RH') # Label the axes
p1.setLabel('bottom', "<b>Time", units= '<b>s')
p1.setDownsampling(mode='peak')  # downsampling data in order to get fast graphs
p1.setClipToView(True)  # set clip to view to true in order to get fast graphs
p1.showGrid(x=True, y=True) # show graph grids
humC1 = [] # an empty list for humidity values
times1 = [] # an empty time list 
indx1 = 0   # variable set to zero
hum = BrickletHumidity(UIDhum, ipcon) # Humidity Bricklet 

def updateSensorHum():
    global curve1,tempC1, indx1, times1, hum, t0

    humidity = hum.get_humidity() # get the value of the relative humidity from humidity bricklet 

    dataArray1=str(humidity/10).split(',')  # creating a data array and splitting the values  
    temp1 = float(dataArray1[0]) # creating a float object of data array
    humC1.append(temp1) # appending the humidity values
    times1.append(time.time())   # appending the time values

    if (indx1 > 250):  # if indx1 > 300
        humC1.pop(0)    # start eliminating the 1st value stored in the array
        times1.pop(0) # start eliminating the time from the x axis
    indx1 = indx1 + 1   # increase index by 1
    curve1.setData(times1, humC1) # drawing the curve humidity vs time
    app.processEvents() #process the events in the created app

def update():
    updateSensorHum() # update Humidity Sensor

    exporter = pg.exporters.ImageExporter(win.scene()) # export the image exporter library
    exporter.export('/home/pi/Script/ConditionMonitoring.png') # export the file as SVG

timer = pg.QtCore.QTimer() # import timer library
timer.timeout.connect(update) # set the connection with the function to be update
timer.start(999) # start the timer

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_'):
        QtGui.QApplication.instance().exec_()

I don't understand why does it work on windows and not on raspberry pi although the script is same.

I'd be grateful if someone can help me with this problem.

Thanks

  • I am facing the same issue described here. Could you find a solution? – Wowa White Aug 5 '18 at 21:17
0

I don't have a pi around but if the paste is correct, there is an indent error here : axis1 = CAxisTime(orientation='bottom') (extra whitespace in the beginning of the line)

If I comment the two lines to access BrickletHumidity, the script runs fine on Linux (Python 2.7 and Python 3.4)

What is the error you get ?

  • thanks for replying. It might have been a paste error. IN my script the indentation is fine. Actually the problem is with class CAxistime and the empty list strns[ ] i tried to print strns, it starts good but then it doesn't append resulting in no plot and freezed x axis. – Ajay Sep 29 '16 at 6:47
  • I tried removing the HumidityBricklet and I got everthing correct. The moment I add sensors it starts behaving the same way on rpi – Ajay Sep 29 '16 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.