Write arbitrary POST data to socket with fixed size buffer

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
neurino
Posts: 14
Joined: Mon Aug 06, 2018 8:23 am

Write arbitrary POST data to socket with fixed size buffer

Post by neurino » Wed Nov 14, 2018 5:29 pm

In an application I send data from i2c eeprom to a server using a socket.

Code: Select all

import usocket

buf = bytearray(64)
#...
socket = usocket.socket(*addrinfo[:3])
socket.connect(addrinfo[-1])
#...
socket.write('{0} /{1} HTTP/1.0\r\n'.format(method, path))
socket.write('Host: {0}\r\n'.format(host))
socket.write('\r\n')
#... fill buf with eeprom data...
socket.write(buf)
This works for small amounts of data.

Problem is data can exceed 64 bytes -- the eeprom is 8kbytes and teoretically I could have the need to send thm all -- and I would like to write all data to the same socket reusing the same buffer over and over.

I tried with an iterator, only the first buffer is sent, following socket.write are lost

Code: Select all

def ipayload(self, buf):
    size = len(buf)
    for memaddr in range(self.__head_pointer, self.__tail_pointer, size):
        self.read_into(memaddr, buf)
        yield min(self.__tail_pointer - memaddr, size)
        
#...
for c in eeprom.ipayload(buf):
    socket.write(buf)
How can I do?

Post Reply