Sending python file from STM32 to XBEE

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Sending python file from STM32 to XBEE

Post by Roberthh » Thu Feb 11, 2021 10:55 am

Slightly updated:

Code: Select all

static char *start_cmd[] = {
    "import uos",
    "try:",
    "    uos.remove('/flash/lib/umqtt/robust.py')",
    "except:",
    "    pass",
    "f = open('/flash/lib/umqtt/robust.py', 'w')"
};

static char *robust_py[] = {
    "/flash/lib/umqtt/robust.py",
    "import utime",
    "from . import simple",
    "",
    "class MQTTClient(simple.MQTTClient):",
    "",
    "    DELAY = 2",
    "    DEBUG = False",
    "",
    "    def delay(self, i):",
    "        utime.sleep(self.DELAY)",
    "",
    "    def log(self, in_reconnect, e):",
    "        if self.DEBUG:",
    "            if in_reconnect:",
    "                print(\"mqtt reconnect: %r\" % e)",
    "            else:",
    "                print(\"mqtt: %r\" % e)",
    "",
    "    def reconnect(self):",
    "        i = 0",
    "        while 1:",
    "            try:",
    "                return super().connect(False)",
    "            except OSError as e:",
    "                self.log(True, e)",
    "                i += 1",
    "                self.delay(i)",
    "",
    "    def publish(self, topic, msg, retain=False, qos=0):",
    "        while 1:",
    "            try:",
    "                return super().publish(topic, msg, retain, qos)",
    "            except OSError as e:",
    "                self.log(False, e)",
    "            self.reconnect()",
    "",
    "    def wait_msg(self):",
    "        while 1:",
    "            try:",
    "                return super().wait_msg()",
    "            except OSError as e:",
    "                self.log(False, e)",
    "            self.reconnect()",
};

void XbeeSendCommandWait(uint8_t *str)
{
    XbeeSendCommand((uint8_t *)str, strlen(str));
    osDelay(50000);
}
/*
* Function:         ProcessFileWrite
* arguments:        none
* return value:     none
* brief description:robust.py script transper to Xbee flash
*
*/
void ProcessFileWrite(char **data, int len)
{
    char str[100];
    printf("%d\n", len);
    for (int i = 1; i < len; i++) {
        printf("%d, %s\n", i, data[i]);
        sprintf(str, "f.write('%s\\r\\n')\r", data[i]);
        XbeeSendCommandWait(str);
    }
}
/*
 * Function:            XbeeWriteLibFiles
 * arguments:           none
 * return value:        none
 * brief description:   write AWS mqtt library files[simple.py & robust.py] to XBee "/flash/lib/umqtt"'
 *
 */
void  XbeeWriteLibFiles(void)
{
    char str[60];
    int sizeOfFile;
    // sizeOfFile = sizeof(ROBUST_PY);
    // TRACE_INFO("File size %d\n",sizeOfFile);

    char *pyFCloseStr = "f.close()\r";
    char *filename = robust_py[0];


    XbeeSendCommandWait("\x05");
    for (int i = 0; i < (sizeof(start_cmd)/sizeof(char *)); i++) {
        printf("%d, %s\n", i, start_cmd[i]);
        sprintf(str, "%s\r", start_cmd[i]);
        XbeeSendCommandWait(str);
    }    
    ProcessFileWrite(robust_py, sizeof(robust_py)/sizeof(char *));
    XbeeSendCommandWait(pyFCloseStr);
    XbeeSendCommandWait("\x04");
}


hiharsh
Posts: 13
Joined: Sun Jan 31, 2021 4:31 pm

Re: Sending python file from STM32 to XBEE

Post by hiharsh » Thu Feb 11, 2021 1:24 pm

Thank you very much. I will try this and update.

hiharsh
Posts: 13
Joined: Sun Jan 31, 2021 4:31 pm

Re: Sending python file from STM32 to XBEE

Post by hiharsh » Fri Feb 12, 2021 8:53 pm

@Roberthh The code works. Thank you very much.

hiharsh
Posts: 13
Joined: Sun Jan 31, 2021 4:31 pm

Re: Sending python file from STM32 to XBEE

Post by hiharsh » Sat Feb 13, 2021 6:19 pm

@Roberthh
I tried your code for robust.py and it works flawlessly.

However, when I attempted to write simple.py, encountered the following issues.

There are three issue in simply.py transfer
simple.py can be retrieved from https://github.com/micropython/micropyt ... /simple.py

1. line #17 has some issue. While writing this line to Xbee over Serial port, the file write function restarts every time and keeps on repeating like recursive function.
2. line #73 has "/x04" in file line. it means cntrl+D so it gets out from code copy command (i.e., cntrl+E).

3. Xbee has limited serial buffer memory so need to transfer simple.py by segmentation of
approx. 1 kb.

Below is the STM32 code snippet for the simple.py file transfer. I would appreciate your help on this.

Code: Select all

static char *simple_py_cmd[] = {
"import uos",
"try:",
"    uos.remove('/flash/lib/umqtt/simple.py')",
"except:",
"    pass",
"f = open('/flash/lib/umqtt/simple.py', 'w')"
};


static char *simple_py[] = {
	"\"\"\"",
	"Based on umqtt.simple 1.3.4 from micropython-lib:	",
	"https://github.com/micropython/micropython-lib/tree/master/umqtt.simple	",
	"",
	"Copyright (c) 2013, 2014 micropython-lib contributors",
	"MIT License",
	"\"\"\"",
	"",
	"import usocket as socket",
	"import ustruct as struct",
	"from ubinascii import hexlify",
	"",
	"class MQTTException(Exception):",
	"pass",
	"",
	"class MQTTClient:",
	"",
	"def __init__(self, client_id, server, port=0, user=None, password=None, keepalive=0,",
	"                 ssl=False, ssl_params={}):",
	"        if port == 0:",
	"            port = 8883 if ssl else 1883",
	"        self.client_id = client_id",
	"        self.sock = None",
	"        self.server = server",
	"        self.port = port",
	"        self.ssl = ssl",
	"        self.ssl_params = ssl_params",
	"        self.pid = 0",
	"        self.cb = None",
	"        self.user = user",
	"        self.pswd = password",
	"        self.keepalive = keepalive",
	"        self.lw_topic = None",
	"        self.lw_msg = None",
	"        self.lw_qos = 0",
	"        self.lw_retain = False",
	"",
	"    def _send_str(self, s):",
	"        self.sock.write(struct.pack(\"!H\", len(s)))",
	"        self.sock.write(s)",
	"",
	"    def _recv_len(self):",
	"        n = 0",
	"        sh = 0",
	"        while 1:",
	"            b = self.sock.read(1)[0]",
	"            n |= (b & 0x7f) << sh",
	"            if not b & 0x80:",
	"                return n",
	"            sh += 7",
	"",
	"    def set_callback(self, f):",
	"        self.cb = f",
	"",
	"    def set_last_will(self, topic, msg, retain=False, qos=0):",
	"        assert 0 <= qos <= 2",
	"        assert topic",
	"        self.lw_topic = topic",
	"        self.lw_msg = msg",
	"        self.lw_qos = qos",
	"        self.lw_retain = retain",
	"",
	"    def connect(self, clean_session=True):",
	"        if self.ssl:",
	"            proto = socket.IPPROTO_SEC",
	"        else:",
	"            proto = socket.IPPROTO_TCP",
	"        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, proto)",
	"        if self.ssl:",
	"            import ussl",
	"            self.sock = ussl.wrap_socket(self.sock, **self.ssl_params)",
	"        self.sock.connect((self.server, self.port))",
	"        premsg = bytearray(b\"\x10\0\0\0\0\0\")",
	"        msg = bytearray(b\"\x04MQTT\x04\x02\0\0\")",
	"",
	"        sz = 10 + 2 + len(self.client_id)",
	"        msg[6] = clean_session << 1",
	"        if self.user is not None:",
	"            sz += 2 + len(self.user) + 2 + len(self.pswd)",
	"            msg[6] |= 0xC0",
	"        if self.keepalive:",
	"            assert self.keepalive < 65536",
	"            msg[7] |= self.keepalive >> 8",
	"            msg[8] |= self.keepalive & 0x00FF",
	"        if self.lw_topic:",
	"            sz += 2 + len(self.lw_topic) + 2 + len(self.lw_msg)",
	"            msg[6] |= 0x4 | (self.lw_qos & 0x1) << 3 | (self.lw_qos & 0x2) << 3",
	"            msg[6] |= self.lw_retain << 5",
	"",
	"        i = 1",
	"        while sz > 0x7f:",
	"            premsg[i] = (sz & 0x7f) | 0x80",
	"            sz >>= 7",
	"            i += 1",
	"        premsg[i] = sz",
	"",
	"        self.sock.write(premsg, i + 2)",
	"        self.sock.write(msg)",
	"        #print(hex(len(msg)), hexlify(msg, \":\"))",
	"        self._send_str(self.client_id)",
	"        if self.lw_topic:",
	"            self._send_str(self.lw_topic)",
	"            self._send_str(self.lw_msg)",
	"        if self.user is not None:",
	"            self._send_str(self.user)",
	"            self._send_str(self.pswd)",
	"        resp = self.sock.read(4)",
	"        assert resp[0] == 0x20 and resp[1] == 0x02",
	"        if resp[3] != 0:",
	"            raise MQTTException(resp[3])",
	"        return resp[2] & 1",
	"",
	"    def disconnect(self):",
	"        self.sock.write(b\"\xe0\0\")",
	"        self.sock.close()",
	"",
	"    def ping(self):",
	"        self.sock.write(b\"\xc0\0\")",
	"",
	"    def publish(self, topic, msg, retain=False, qos=0):",
	"        pkt = bytearray(b\"\x30\0\0\0\")",
	"        pkt[0] |= qos << 1 | retain",
	"        sz = 2 + len(topic) + len(msg)",
	"        if qos > 0:",
	"            sz += 2",
	"        assert sz < 2097152",
	"        i = 1",
	"        while sz > 0x7f:",
	"            pkt[i] = (sz & 0x7f) | 0x80",
	"            sz >>= 7",
	"            i += 1",
	"        pkt[i] = sz",
	"        #print(hex(len(pkt)), hexlify(pkt, \":\"))",
	"        self.sock.write(pkt, i + 1)",
	"        self._send_str(topic)",
	"        if qos > 0:",
	"            self.pid += 1",
	"            pid = self.pid",
	"            struct.pack_into(\"!H\", pkt, 0, pid)",
	"            self.sock.write(pkt, 2)",
	"        self.sock.write(msg)",
	"        if qos == 1:",
	"            while 1:",
	"                op = self.wait_msg()",
	"                if op == 0x40:",
	"                    sz = self.sock.read(1)",
	"                    assert sz == b\"\x02\"",
	"                    rcv_pid = self.sock.read(2)",
	"                    rcv_pid = rcv_pid[0] << 8 | rcv_pid[1]",
	"                    if pid == rcv_pid:",
	"                        return",
	"        elif qos == 2:",
	"            assert 0",
	"",
	"    def subscribe(self, topic, qos=0):",
	"        assert self.cb is not None, \"Subscribe callback is not set\"",
	"        pkt = bytearray(b\"\x82\0\0\0\")",
	"        self.pid += 1",
	"        struct.pack_into(\"!BH\", pkt, 1, 2 + 2 + len(topic) + 1, self.pid)",
	"        #print(hex(len(pkt)), hexlify(pkt, \":\"))",
	"        self.sock.write(pkt)",
	"        self._send_str(topic)",
	"        self.sock.write(qos.to_bytes(1, \"little\"))",
	"        while 1:",
	"            op = self.wait_msg()",
	"            if op == 0x90:",
	"                resp = self.sock.read(4)",
	"                #print(resp)",
	"                assert resp[1] == pkt[2] and resp[2] == pkt[3]",
	"                if resp[3] == 0x80:",
	"                    raise MQTTException(resp[3])",
	"                return",
	"",
	"    # Wait for a single incoming MQTT message and process it.",
	"    # Subscribed messages are delivered to a callback previously",
	"    # set by .set_callback() method. Other (internal) MQTT",
	"    # messages processed internally.",
	"    def wait_msg(self):",
	"        res = self.sock.read(1)",
	"        self.sock.setblocking(True)",
	"        if res is None:",
	"            return None",
	"        if res == b"":",
	"            raise OSError(-1)",
	"        if res == b\"\xd0\":  # PINGRESP",
	"            sz = self.sock.read(1)[0]",
	"            assert sz == 0",
	"            return None",
	"        op = res[0]",
	"        if op & 0xf0 != 0x30:",
	"            return op",
	"        sz = self._recv_len()",
	"        topic_len = self.sock.read(2)",
	"        topic_len = (topic_len[0] << 8) | topic_len[1]",
	"        topic = self.sock.read(topic_len)",
	"        sz -= topic_len + 2",
	"        if op & 6:",
	"            pid = self.sock.read(2)",
	"            pid = pid[0] << 8 | pid[1]",
	"            sz -= 2",
	"        msg = self.sock.read(sz)",
	"        self.cb(topic, msg)",
	"        if op & 6 == 2:",
	"            pkt = bytearray(b\"\x40\x02\0\0\")",
	"            struct.pack_into(\"!H\", pkt, 2, pid)",
	"            self.sock.write(pkt)",
	"        elif op & 6 == 4:",
	"            assert 0",
	"",
	"    # Checks whether a pending message from server is available.",
	"    # If not, returns immediately with None. Otherwise, does",
	"    # the same processing as wait_msg.",
	"    def check_msg(self):",
	"        self.sock.setblocking(False)",
	"        return self.wait_msg()"
};

void  XbeeWriteLibFiles(void)
{
    char str[100];
//    int sizeOfFile;
    // sizeOfFile = sizeof(ROBUST_PY);
    // TRACE_INFO("File size %d\n",sizeOfFile);

    char *pyFCloseStr = "f.close()\r";
//    char *filename = robust_py[0];

    XbeeSendCommandWait("\x05");
    for (int i = 0; i < (sizeof(simple_py_cmd)/sizeof(char *)); i++)
    {
        TRACE_INFO("%d, %s\n", i, simple_py_cmd[i]);
        sprintf(str, "%s\r", simple_py_cmd[i]);
        XbeeSendCommandWait(str);
    }
    ProcessFileWrite(simple_py, sizeof(simple_py)/sizeof(char *));
    XbeeSendCommandWait(pyFCloseStr);
    XbeeSendCommandWait("\x04");

    XbeeSendCommandWait("\x05");

    for (int i = 0; i < (sizeof(robust_py_cmd)/sizeof(char *)); i++)
    {
    	TRACE_INFO("%d, %s\n", i, robust_py_cmd[i]);
        sprintf(str, "%s\r", robust_py_cmd[i]);
        XbeeSendCommandWait(str);
    }
    ProcessFileWrite(robust_py, sizeof(robust_py)/sizeof(char *));
    XbeeSendCommandWait(pyFCloseStr);

    XbeeSendCommandWait("\x04");
};


User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Sending python file from STM32 to XBEE

Post by Roberthh » Sat Feb 13, 2021 8:09 pm

So I have a version that I can transfer. The changes:
a) I had to increase the delays dramatically from 50ms/line to 300 ms/line. Maybe my XBEE is tired.
b) I have sent the file in blocks of 32 lines. More may be possible, less is more safe.
c) For a backslash to end in a file it has to be escaped twice as \\\\

Code: Select all


static char *simple_py_cmd[] = {
"import uos",
"try:",
"    uos.remove('/flash/lib/umqtt/simple.py')",
"except:",
"    pass",
"f = open('/flash/lib/umqtt/simple.py', 'w')"
};


static char *simple_py[] = {
	"\"\"\"",
	"Based on umqtt.simple 1.3.4 from micropython-lib:",
	"https://github.com/micropython/micropython-lib/tree/master/umqtt.simple",
	"",
	"Copyright (c) 2013, 2014 micropython-lib contributors",
	"MIT License",
	"\"\"\"",
	"",
	"import usocket as socket",
	"import ustruct as struct",
	"from ubinascii import hexlify",
	"",
	"class MQTTException(Exception):",
	"pass",
	"",
	"class MQTTClient:",
	"",
	"def __init__(self, client_id, server, port=0, user=None, password=None, keepalive=0,",
	"                 ssl=False, ssl_params={}):",
	"        if port == 0:",
	"            port = 8883 if ssl else 1883",
	"        self.client_id = client_id",
	"        self.sock = None",
	"        self.server = server",
	"        self.port = port",
	"        self.ssl = ssl",
	"        self.ssl_params = ssl_params",
	"        self.pid = 0",
	"        self.cb = None",
	"        self.user = user",
	"        self.pswd = password",
	"        self.keepalive = keepalive",
	"        self.lw_topic = None",
	"        self.lw_msg = None",
	"        self.lw_qos = 0",
	"        self.lw_retain = False",
	"",
	"    def _send_str(self, s):",
	"        self.sock.write(struct.pack(\"!H\", len(s)))",
	"        self.sock.write(s)",
	"",
	"    def _recv_len(self):",
	"        n = 0",
	"        sh = 0",
	"        while 1:",
	"            b = self.sock.read(1)[0]",
	"            n |= (b & 0x7f) << sh",
	"            if not b & 0x80:",
	"                return n",
	"            sh += 7",
	"",
	"    def set_callback(self, f):",
	"        self.cb = f",
	"",
	"    def set_last_will(self, topic, msg, retain=False, qos=0):",
	"        assert 0 <= qos <= 2",
	"        assert topic",
	"        self.lw_topic = topic",
	"        self.lw_msg = msg",
	"        self.lw_qos = qos",
	"        self.lw_retain = retain",
	"",
	"    def connect(self, clean_session=True):",
	"        if self.ssl:",
	"            proto = socket.IPPROTO_SEC",
	"        else:",
	"            proto = socket.IPPROTO_TCP",
	"        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, proto)",
	"        if self.ssl:",
	"            import ussl",
	"            self.sock = ussl.wrap_socket(self.sock, **self.ssl_params)",
	"        self.sock.connect((self.server, self.port))",
	"        premsg = bytearray(b\"\\\\x10\\\\0\\\\0\\\\0\\\\0\\\\0\")",
	"        msg = bytearray(b\"\\\\x04MQTT\\\\x04\\\\x02\\\\0\\\\0\")",
	"",
	"        sz = 10 + 2 + len(self.client_id)",
	"        msg[6] = clean_session << 1",
	"        if self.user is not None:",
	"            sz += 2 + len(self.user) + 2 + len(self.pswd)",
	"            msg[6] |= 0xC0",
	"        if self.keepalive:",
	"            assert self.keepalive < 65536",
	"            msg[7] |= self.keepalive >> 8",
	"            msg[8] |= self.keepalive & 0x00FF",
	"        if self.lw_topic:",
	"            sz += 2 + len(self.lw_topic) + 2 + len(self.lw_msg)",
	"            msg[6] |= 0x4 | (self.lw_qos & 0x1) << 3 | (self.lw_qos & 0x2) << 3",
	"            msg[6] |= self.lw_retain << 5",
	"",
	"        i = 1",
	"        while sz > 0x7f:",
	"            premsg[i] = (sz & 0x7f) | 0x80",
	"            sz >>= 7",
	"            i += 1",
	"        premsg[i] = sz",
	"",
	"        self.sock.write(premsg, i + 2)",
	"        self.sock.write(msg)",
	"        #print(hex(len(msg)), hexlify(msg, \":\"))",
	"        self._send_str(self.client_id)",
	"        if self.lw_topic:",
	"            self._send_str(self.lw_topic)",
	"            self._send_str(self.lw_msg)",
	"        if self.user is not None:",
	"            self._send_str(self.user)",
	"            self._send_str(self.pswd)",
	"        resp = self.sock.read(4)",
	"        assert resp[0] == 0x20 and resp[1] == 0x02",
	"        if resp[3] != 0:",
	"            raise MQTTException(resp[3])",
	"        return resp[2] & 1",
	"",
	"    def disconnect(self):",
	"        self.sock.write(b\"\\\\xe0\\\\0\")",
	"        self.sock.close()",
	"",
	"    def ping(self):",
	"        self.sock.write(b\"\\\\xc0\\\\0\")",
	"",
	"    def publish(self, topic, msg, retain=False, qos=0):",
	"        pkt = bytearray(b\"\\\\x30\\\\0\\\\0\\\\0\")",
	"        pkt[0] |= qos << 1 | retain",
	"        sz = 2 + len(topic) + len(msg)",
	"        if qos > 0:",
	"            sz += 2",
	"        assert sz < 2097152",
	"        i = 1",
	"        while sz > 0x7f:",
	"            pkt[i] = (sz & 0x7f) | 0x80",
	"            sz >>= 7",
	"            i += 1",
	"        pkt[i] = sz",
	"        #print(hex(len(pkt)), hexlify(pkt, \":\"))",
	"        self.sock.write(pkt, i + 1)",
	"        self._send_str(topic)",
	"        if qos > 0:",
	"            self.pid += 1",
	"            pid = self.pid",
	"            struct.pack_into(\"!H\", pkt, 0, pid)",
	"            self.sock.write(pkt, 2)",
	"        self.sock.write(msg)",
	"        if qos == 1:",
	"            while 1:",
	"                op = self.wait_msg()",
	"                if op == 0x40:",
	"                    sz = self.sock.read(1)",
	"                    assert sz == b\"\\\\x02\"",
	"                    rcv_pid = self.sock.read(2)",
	"                    rcv_pid = rcv_pid[0] << 8 | rcv_pid[1]",
	"                    if pid == rcv_pid:",
	"                        return",
	"        elif qos == 2:",
	"            assert 0",
	"",
	"    def subscribe(self, topic, qos=0):",
	"        assert self.cb is not None, \"Subscribe callback is not set\"",
	"        pkt = bytearray(b\"\\\\x82\\\\0\\\\0\\\\0\")",
	"        self.pid += 1",
	"        struct.pack_into(\"!BH\", pkt, 1, 2 + 2 + len(topic) + 1, self.pid)",
	"        #print(hex(len(pkt)), hexlify(pkt, \":\"))",
	"        self.sock.write(pkt)",
	"        self._send_str(topic)",
	"        self.sock.write(qos.to_bytes(1, \"little\"))",
	"        while 1:",
	"            op = self.wait_msg()",
	"            if op == 0x90:",
	"                resp = self.sock.read(4)",
	"                #print(resp)",
	"                assert resp[1] == pkt[2] and resp[2] == pkt[3]",
	"                if resp[3] == 0x80:",
	"                    raise MQTTException(resp[3])",
	"                return",
	"",
	"    # Wait for a single incoming MQTT message and process it.",
	"    # Subscribed messages are delivered to a callback previously",
	"    # set by .set_callback() method. Other (internal) MQTT",
	"    # messages processed internally.",
	"    def wait_msg(self):",
	"        res = self.sock.read(1)",
	"        self.sock.setblocking(True)",
	"        if res is None:",
	"            return None",
	"        if res == b"":",
	"            raise OSError(-1)",
	"        if res == b\"\\\\xd0\":  # PINGRESP",
	"            sz = self.sock.read(1)[0]",
	"            assert sz == 0",
	"            return None",
	"        op = res[0]",
	"        if op & 0xf0 != 0x30:",
	"            return op",
	"        sz = self._recv_len()",
	"        topic_len = self.sock.read(2)",
	"        topic_len = (topic_len[0] << 8) | topic_len[1]",
	"        topic = self.sock.read(topic_len)",
	"        sz -= topic_len + 2",
	"        if op & 6:",
	"            pid = self.sock.read(2)",
	"            pid = pid[0] << 8 | pid[1]",
	"            sz -= 2",
	"        msg = self.sock.read(sz)",
	"        self.cb(topic, msg)",
	"        if op & 6 == 2:",
	"            pkt = bytearray(b\"\\\\x40\\\\x02\\\\0\\\\0\")",
	"            struct.pack_into(\"!H\", pkt, 2, pid)",
	"            self.sock.write(pkt)",
	"        elif op & 6 == 4:",
	"            assert 0",
	"",
	"    # Checks whether a pending message from server is available.",
	"    # If not, returns immediately with None. Otherwise, does",
	"    # the same processing as wait_msg.",
	"    def check_msg(self):",
	"        self.sock.setblocking(False)",
	"        return self.wait_msg()"
};

void XbeeSendCommandWait(uint8_t *str)
{
    XbeeSendCommand((uint8_t *)str, strlen(str));
    osDelay(300000);
}

void ProcessFileWrite(char **data, int len)
{
    char str[100];
    printf("%d\n", len);

    XbeeSendCommandWait("\x05");
    for (int i = 0; i < len; i++) {
        printf("%d, %s\n", i, data[i]);
        sprintf(str, "f.write('%s\\r\\n')\r", data[i]);
        XbeeSendCommandWait(str);
        if ((i % 32) == 31) {
            XbeeSendCommandWait("\x04");
            osDelay(100000);
            XbeeSendCommandWait("\x05");
        }
    }
    XbeeSendCommandWait("\x04");
}

void  XbeeWriteLibFiles(void)
{
    char str[100];
    // int sizeOfFile;
    // sizeOfFile = sizeof(ROBUST_PY);
    // TRACE_INFO("File size %d\n",sizeOfFile);

    char *pyFCloseStr = "f.close()\r";

    XbeeSendCommandWait("\x05");
    for (int i = 0; i < (sizeof(simple_py_cmd)/sizeof(char *)); i++)
    {
        TRACE_INFO("%d, %s\n", i, simple_py_cmd[i]);
        sprintf(str, "%s\r", simple_py_cmd[i]);
        XbeeSendCommandWait(str);
    }
    XbeeSendCommandWait("\x04");
    osDelay(100000);
    ProcessFileWrite(simple_py, sizeof(simple_py)/sizeof(char *));
    
    XbeeSendCommandWait(pyFCloseStr);
    
    osDelay(1000000);

    XbeeSendCommandWait("\x05");
    for (int i = 0; i < (sizeof(robust_py_cmd)/sizeof(char *)); i++)
    {
    	TRACE_INFO("%d, %s\n", i, robust_py_cmd[i]);
        sprintf(str, "%s\r", robust_py_cmd[i]);
        XbeeSendCommandWait(str);
    }
    XbeeSendCommandWait("\x04");
    ProcessFileWrite(robust_py, sizeof(robust_py)/sizeof(char *));

    XbeeSendCommandWait(pyFCloseStr);

};

hiharsh
Posts: 13
Joined: Sun Jan 31, 2021 4:31 pm

Re: Sending python file from STM32 to XBEE

Post by hiharsh » Mon Mar 01, 2021 6:23 pm

I am transferring library files to XBEE flash with your code and It is working well without any issue. But it alters the file checksum[SHA256]. I am using following script to find the checksum of the file. Which I verified via XCTU hash generate command.

Code: Select all

import uhashlib
import ubinascii
hash = uhashlib.sha256()
a_file = open("/flash/lib/umqtt/simple.py", "rb")
content = a_file.read()
hash.update(content)
digest = hash.digest()
ubinascii.hexlify(digest)
I think, the files stored at code memory as const char array adds up some CR/LF to file lines. That result into checksum alteration.
could you suggest any modification while transferring file that can prevent checksum alteration.
I always appreciate you suggestions.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Sending python file from STM32 to XBEE

Post by Roberthh » Mon Mar 01, 2021 7:38 pm

In this fwrite instruction in ProcessFile Write() :

Code: Select all

        sprintf(str, "f.write('%s\\r\\n')\r", data[i]);
a CR is added. You could try to use instead:

Code: Select all

        sprintf(str, "f.write('%s\\n')\r", data[i]);

Post Reply