0

I am getting values from three different sensors connected to Raspberry Pi. I am using a ADC grove to get those data's. I am updating these sensor data with date and time in a text file. The problem is i created three different threads for processing each sensor data and each thread has to update its respective sensor data in the same text file without overwriting it. Currently i can able to update the file with sensor data from a single thread only.

import sys
import time
from PyQt5.QtWidgets import*
from PyQt5.QtGui import*
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QThread, QObject
from PyQt5.QtCore import pyqtSignal
from grove.i2c import Bus

ADC_DEFAULT_IIC_ADDR = 0X04

ADC_CHAN_NUM = 8

REG_RAW_DATA_START = 0X10
REG_VOL_START = 0X20
REG_RTO_START = 0X30

REG_SET_ADDR = 0XC0


class Pi_hat_adc():
    def __init__(self,bus_num=1,addr=ADC_DEFAULT_IIC_ADDR,parent=None):
        self.parent = parent
        self.bus=Bus(bus_num)
        self.addr=addr
             
    def get_nchan_adc_raw_data(self,n):
        data=self.bus.read_i2c_block_data(self.addr,REG_RAW_DATA_START+n,2)
        val =data[1]<<8|data[0]
        return val
   
ADC = Pi_hat_adc()
class Window(QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
       
        self.pi_hat_adc = Pi_hat_adc(parent=self)
       
      
        self.setGeometry(200,200,400,300)
    
        self.line_edit = QLineEdit("",self)
        self.line_edit.setGeometry(5,80,100,40)
        self.line_edit.setStyleSheet("background-color:White")
        self.line_edit1 = QLineEdit("",self)
        self.line_edit1.setGeometry(5,140,100,40)
        self.line_edit1.setStyleSheet("background-color:White")
        self.line_edit2 = QLineEdit("",self)
        self.line_edit2.setGeometry(5,200,100,40)
        self.line_edit2.setStyleSheet("background-color:White")
        self.myLongTask = TaskThread()
        self.myLongTask1 = TaskThread1()
        self.myLongTask2 = TaskThread2()
        self.myLongTask.notifyProgress.connect(self.on_progress)
        self.myLongTask1.notifyProgress.connect(self.on_progress1)
        self.myLongTask2.notifyProgress.connect(self.on_progress2)
        self.on_start()
       
    def on_start(self):
        self.myLongTask.start()
        self.myLongTask1.start()
        self.myLongTask2.start()
       
    def on_progress(self,y):
        self.line_edit.setText(str(y))
        return
   
    def on_progress1(self,y):
        self.line_edit1.setText(str(y))
        getdatetime = datetime.datetime.now() .strftime("%Y-%m-%d %H:%M:%S")
        fb = open('/home/pi/sensor.txt','a+')
        fb.write('\n')
        fb.write(getdatetime)
        fb.write(" ADC = ")
        fb.write(str(y))
        fb.write('\n')
        fb.close
        return
   
    def on_progress2(self,y):
        self.line_edit2.setText(str(y))
        return

class TaskThread(QtCore.QThread):
    notifyProgress = pyqtSignal(int)
   
    def run(self):
        while True:
            rawdata0 = ADC.get_nchan_adc_raw_data(0)
            self.notifyProgress.emit(rawdata0)                   
            time.sleep(0.3)

class TaskThread1(QtCore.QThread):
    notifyProgress = pyqtSignal(int)
   
    def run(self):
        while True:
                      
            rawdata1 = ADC.get_nchan_adc_raw_data(1)
            self.notifyProgress.emit(rawdata1)
            time.sleep(0.3)

class TaskThread2(QtCore.QThread):
    notifyProgress = pyqtSignal(int)
   
    def run(self):
        while True:
            rawdata2 = ADC.get_nchan_adc_raw_data(2)
            self.notifyProgress.emit(rawdata2)
            time.sleep(0.3)
2
  • 2
    This is not Pi specific, it is a programming question. It is also an XY problem You have decided on a solution and want someone to fix it.
    – Milliways
    Jan 7, 2021 at 11:28
  • Take this to SO, please. It doesn't belong here. Why? Because this is a programming problem that is not related to Pi - you would have the same issue on Windows, Mac, Linux, etc.
    – Seamus
    Jan 7, 2021 at 18:52

1 Answer 1

2

Maybe a Queue could work for you? Link. Queues are FirstInFirstOut so you could have your threads putting stuff in it and then have, I don't know, a fourth thread that will be grabbing stuff from the queue as they come and write them to the file.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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