Basically I created an object from a class and in the constructor of that object I spawned a thread that reads analog data from a sencsor in a tight while-loop. The main thread has also a while-loop that supposed to read the data from the spawned thread every 5 seconds...
The problem is that I see that the spawned thread is running but the main thread is not...not sure if it has to do with 'acquire' and 'release' of a flag that supposed to sync the data generation and reading (my main thread).
I posted this question on pycom forum but got no relevant responses...
Is anybody can tell me why the main thread that is not running?
In the main.py I call the constructor that starts the thread:
Code: Select all
ECG = DFRobotECG('P18', 460)
idx = 0;
while(idx < 3):
print("Main.py: Started loop iteration at local time: {}".format(utime.localtime()))
utime.sleep(2)
try:
message = ECG.GetData()
print("message: {} json string: {}".format(i+1, message))
data = '{"k": "%s", "d": "%s", "t": "%s"}' % (DEVICE_KEY, message, TOPIC)
lteSocket.send(bytes(data, 'ascii'))
utime.sleep(0.5)
print("==== LTE Socket data sent")
result = lteSocket.recv(8).decode()
print("==== LTE Socket result received: {}".format(result))
idx = idx + 1
print("i={}".format(i))
#time.sleep(5) # let the ECG thread read for 10 sec
except Exception as e:
print("Exception from while loop: {}".format(e.args[0]))
except Exception as e2:
print("Exception from main code: %s" % e2.args[0])
#pass # do nothing on error
BUT:
1. the main thread is not running. It is not even getting to the 1st print statement in a while loop: "print("Main.py: Started loop...". Any clues why?
2. After a few minutes the thread throws exception about memory allocation...Why?
Code: Select all
class DFRobotECG:
history = [(0,0)]
def __init__(self, analogInputPin, voltref):
print("### Entered DFRobot constructor")
self.exitFlag = False
self.adc = ADC(bits=12)
print("adc: {}".format(self.adc))
# Output Vref of P22
self.adc.vref_to_pin('P22')
print("set vref to pin 22")
# Set calibration - see note above
self.adc.vref(voltref)#1100)
print("set adc ref value to 460")
# Analogog Input Pin on Expansion Board 3.1 should be P13, P14, P17, P18 (G4, G5, G30, G31)
self.adc_c = self.adc.channel(pin=analogInputPin, attn=ADC.ATTN_11DB) # • Output Voltage: 0 - 3.3V
print("got adc channel on P18")
# allocate locks - initially unlocked
self.exitFlagLock = _thread.allocate_lock()
self.ReadLock = _thread.allocate_lock()
_thread.start_new_thread(self.ReadInput, ())
print("### Exited DFRobotECG constructor")
def SetExitFlag(self, exit_flag):
self.exitFlagLock.acquire()
self.exitFlag = exit_flag
self.exitFlagLock.release()
def GetExitFlag(self):
with self.exitFlagLock:
return self.exitFlag
def GetData(self):
self.ReadLock.acquire()
self.jsonMsg = ujson.dumps(self.history)
del self.history[:] # empty the array
self.ReadLock.release()
return self.jsonMsg
def ReadInput(self):
self.start = time.ticks_ms()
while (True):
if (self.GetExitFlag() == False):
try:
self.ReadLock.acquire()
# get the voltage value
self.v = self.adc_c.voltage()
# get the current ticks
self.diff = utime.ticks_diff(time.ticks_ms(), self.start)
# make json payload
tup = (self.diff, self.v)
print("Voltage: {}".format(tup))
self.history.append(tup)
self.ReadLock.release()
utime.sleep_ms(1)
except Exception as e:
print("ReadInput loop exception: {}".format(e.args[0]))
print('Exiting ReadInput...')