Hi
I am having problems with memory allocation.
In my project I save images to the SD card and then I read these images one by one and send via Wi-Fi. The problem is that when I run the code in the IDE it seems that the memory space is freed automatically and I don’t have problem with space, however, when I disconnect from IDE and feed with a Power Bank the memory space runs out and openMV stops sending the images. I tried to solve using gc.collect (), but it doesn’t work either, I don’t know if gc.collect () is not freeing memory space or if it is breaking the connection. I tried to use gc.enable too and just tried to delete the variables, nothing worked.
Notes:
1 - In the IDE it works by using gc.collect and without gc.collect.
2- The problem is happening in the last while, everything else works. The gc.collect works on all code, it just doesn’t work when I put in the last while.
import sensor, image, lcd, pyb, time, network, usocket, sys, gc, machine
from pyb import Pin, LED, Timer
pupil = 0
snapshot_source = False
SSID =''
KEY =''
HOST =''
PORT = 8080
sensor.reset()
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.skip_frames(time = 2000)
clock = time.clock()
while(pupil==0):
data = 0
print("Trying to connect... (may take a while)...")
wlan = network.WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)
print(wlan.ifconfig())
s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
s.bind([HOST, PORT])
s.listen(1)
client, ende = s.accept()
print('Conectado com ', ende)
while (data == 0):
data = client.recv(16)
if (data == b'1'):
record_time = 30000
Nflashes = 25
if (data == b'25'):
record_time = 45000
Nflashes = 25
client.close()
wlan.disconnect()
pin4 = Pin('P4', Pin.OUT)
pin4.value(1)
pin5 = Pin('P5', Pin.OUT)
pin5.value(0)
lcd.init()
pin1 = Pin('P1', Pin.IN, Pin.PULL_DOWN)
print(pin1.value())
while(pin1==0):
lcd.display(sensor.snapshot())
lcd.deinit()
cont=0
cont1 = 1
def tick(timer):
global cont1
global cont
cont = cont+1
if (cont==1):
if (cont1 <= Nflashes):
pin5.value(1)
cont1 = cont1+1
else:
pin5.value(0)
elif(cont!=1):
pin5.value(0)
if (cont==99):
cont=0
img_writer = image.ImageWriter("/stream.bin")
gc.collect()
start = pyb.millis()
tim = Timer(4, freq=100)
tim.callback(tick)
FPS = []
while pyb.elapsed_millis(start) < record_time:
clock.tick()
inicio = pyb.millis()
img = sensor.snapshot()
img_writer.add_frame(img)
fim = pyb.millis()
if ((fim - inicio)<32):
pyb.delay(32-(fim-inicio))
if (pyb.elapsed_millis(start))>5:
gc.collect()
FPS.append(clock.fps())
print(clock.fps())
img_writer.close()
tim.deinit()
img_reader = image.ImageReader("/stream.bin")
img_writer1 = image.ImageWriter("/stream.bin1")
while (True):
ima = img_reader.next_frame(copy_to_fb=True, loop=False)
if not ima: break
img = ima.compressed(95)
print(img.size())
img_writer1.add_frame(img)
gc.collect()
img_writer1.close()
gc.collect()
print("Trying to connect... (may take a while)...")
wlan = network.WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)
print(wlan.ifconfig())
s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
s.bind([HOST, PORT])
s.listen(1)
c, ende = s.accept()
print('Conectado com ', ende)
img_reader1 = image.ImageReader("/stream.bin1")
while (True):
print(gc.mem_free())
ima = img_reader1.next_frame(copy_to_fb=True, loop=False)
if not ima: break
c.send(ima)
c.send('1')
c.send(str(gc.mem_free()))
for i in FPS:
c.send(str(i))
c.send('1')
c.close()
wlan.disconnect()
gc.collect()