2

I'm writing a surveillance camera app with ESP32-cam and MicroPython.

tl;dr at the end

I'm using the Threaded code below:

import camera
import machine
from config import app_config
from webserver import webcam
from _thread import start_new_thread
import time
import uos , os



start_time = time.time()


server = webcam()
server.run(app_config)

start_time = time.time()



def reb():
    print('reb')
    uos.umount('/sd')
    machine.reset()


sd = machine.SDCard(slot=3, width=1, 
                        sck=machine.Pin(app_config['sd']['sck']),
                        mosi=machine.Pin(app_config['sd']['mosi']),
                        miso=machine.Pin(app_config['sd']['miso']),
                        cs=machine.Pin(app_config['sd']['ss']))


time.sleep(5)

os.mount(sd, "/sd")

print(os.listdir('/sd')[:10])


def capture_image():
    while True:
        try:

            if time.time() - start_time > 3600:
                reb()
                break
            else:
                print(time.time() - start_time)




            buf = camera.capture()
            timestamp = time.time()
            time_str =  ''.join([str(i) for i in time.localtime()]) #str(timestamp) #'%4d%02d%02d%02d%02d%02d' %(timestamp[0], timestamp[1], timestamp[2], timestamp[4], timestamp[5], timestamp[6])
            with open('sd/'+time_str+'.jpg', 'w') as f:
                f.write(buf)
                time.sleep(1)
                f.close()
            
            print('Captured image!')
            if len(uos.listdir('/sd')) > 5000:
                print('lottt')
                uos.remove('sd/'+sorted(uos.listdir('/sd'))[0])

            time.sleep(60)


        except Exception as e:
            print('we',e)
            pass
            time.sleep(5)

def reb():
    print('staeted sleep')
    time.sleep(3600)
    print('resetting ......')
    machine.reset()



utime.sleep(5)

start_new_thread(capture_image,())
time.sleep(2)
start_new_thread(reb,())

#from ftp import ftpserver
#u = ftpserver()
#u.start_thread()
utime.sleep(5)

do_connect()


camera.flip(app_config['vflip'])
camera.mirror(app_config['hflip'])

which works not bad , but its performance is not good , because of threading...

I tried using asyncio and changed the code to this, but it doesn't work healthy...
It just runs one time and one loop, then it stops...

import camera
import machine
from config import app_config
from webserver import webcam
from _thread import start_new_thread
import time , utime
import uos , os
import uasyncio



start_time = time.time()


server = webcam()
#server.run(app_config)



start_time = time.time()


sd = machine.SDCard(slot=3, width=1, 
                        sck=machine.Pin(app_config['sd']['sck']),
                        mosi=machine.Pin(app_config['sd']['mosi']),
                        miso=machine.Pin(app_config['sd']['miso']),
                        cs=machine.Pin(app_config['sd']['ss']))


time.sleep(5)

os.mount(sd, "/sd")

print(os.listdir('/sd')[:10])


async def capture_image():
    while True:
        try:

            if time.time() - start_time > 3600:
                reb()
                break
            else:
                print(time.time() - start_time)

            buf = camera.capture()
            timestamp = time.time()
            time_str =  ''.join([str(i) for i in time.localtime()]) #str(timestamp) #'%4d%02d%02d%02d%02d%02d' %(timestamp[0], timestamp[1], timestamp[2], timestamp[4], timestamp[5], timestamp[6])
            with open('sd/'+time_str+'.jpg', 'w') as f:
                f.write(buf)
                await uasyncio.sleep(1)
                f.close()
            
            print('Captured image!')
            if len(uos.listdir('/sd')) > 5000:
                print('lottt')
                uos.remove('sd/'+sorted(uos.listdir('/sd'))[0])

            await uasyncio.sleep(60)


        except Exception as e:
            print('we',e)
            pass
            await uasyncio.sleep(60)


async def reb():
    print('staeted sleep')
    await uasyncio.sleep(3600)
    print('resetting ......')
    machine.reset()

async def main():
    uasyncio.create_task(capture_image())
    uasyncio.create_task(reb())
    #await uasyncio.sleep_ms(10_000)


#camera.flip(app_config['vflip'])
#camera.mirror(app_config['hflip'])

server.run(app_config)
uasyncio.run(main())

and if I use run_until_complete , I loose REPL input and output...

tl;dr how to change this code to uasyncio compatible code?

def capture_image():
    while True:
        try:

            if time.time() - start_time > 3600:
                reb()
                break
            else:
                print(time.time() - start_time)




            buf = camera.capture()
            timestamp = time.time()
            time_str =  ''.join([str(i) for i in time.localtime()]) #str(timestamp) #'%4d%02d%02d%02d%02d%02d' %(timestamp[0], timestamp[1], timestamp[2], timestamp[4], timestamp[5], timestamp[6])
            with open('sd/'+time_str+'.jpg', 'w') as f:
                f.write(buf)
                time.sleep(1)
                f.close()
            
            print('Captured image!')
            if len(uos.listdir('/sd')) > 5000:
                print('lottt')
                uos.remove('sd/'+sorted(uos.listdir('/sd'))[0])

            time.sleep(60)


        except Exception as e:
            print('we',e)
            pass
            time.sleep(5)
S.B
  • 13,077
  • 10
  • 22
  • 49
  • Have you tried working through [Peter Hinch's uasyncio tutorial](https://github.com/peterhinch/micropython-async/blob/master/v3/docs/TUTORIAL.md)? – nekomatic Apr 14 '22 at 08:23

0 Answers0