Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # main.py -- put your code here!
- import machine
- import utime
- import wipy
- import utime
- import ustruct
- import usocket
- import ujson
- import gc
- SYNC = bytes([0x24, 0x02, 0xa2, 0xe1, 0x5a, 0xd6, 0x19, 0x73])
- SYNC_LEN = 8
- FRAME_LEN = 1032
- SERVER = ('192.168.1.2', 5000)
- wipy.heartbeat(False)
- uart = machine.UART(0, 921600, timeout=5000)
- led = machine.Pin('GP25', mode=machine.Pin.OUT)
- def reader():
- while True:
- gc.collect()
- #utime.sleep_ms(10)
- #print('Seeking SYNC')
- buffer = uart.read(FRAME_LEN)
- idx = buffer.find(SYNC)
- if idx != -1:
- buffer = buffer[idx:]
- while len(buffer) != FRAME_LEN:
- rest = FRAME_LEN - len(buffer)
- buffer = b''.join([buffer, uart.read(rest)])
- else:
- #print('{}'.format(len(buffer)))
- yield buffer
- buffer = b''
- #utime.sleep_ms(5)
- def unpack(data):
- try:
- return ujson.dumps(ustruct.unpack('!8x512h', data))[SYNC_LEN:]
- except:
- return b''
- def test():
- for data in reader():
- print('Got Data')
- print('Free:', gc.mem_free())
- def worker(conn):
- for data in reader():
- try:
- conn.send(unpack(data))
- except OSError:
- break
- def server():
- sock = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
- while True:
- try:
- sock.connect(SERVER)
- except:
- utime.sleep(1)
- continue
- print('Connected to Server {}:{}'.format(*SERVER))
- worker(sock)
- sock.close()
- #server()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement