wsq2
Posts: 1
Joined: Thu Sep 22, 2022 8:03 pm

First project, looking for help on optimizing the code

Thu Sep 22, 2022 10:13 pm

Hello everyone.

This is my first experience with microcontrollers and with Python.
I'm using Raspberry Pi Pico W to get data from a hall sensor that's attached to a door frame and sending it to a web server I set up.

I got my project working, but the controller crashes every now and then because of Oserror: [errno 12] cannot allocate memory.
Sometimes it works for 15 minutes, sometimes for 15 hours. I'm guessing it runs out of memory because of my code is not very elegantly written.

I was hoping someone more experienced could take a quick look and maybe give a tip on how to make it more efficient.

Code: Select all

import time
import network
import machine
import urequests as req
import json

ssid = ''
pw = ''
url = ''

op = {
    "status": "open"
}

cl = {
    "status": "closed"
}

h = {
    "Content-Type": "application/json"
}

last_sent = False
door_open = False
sensor = machine.Pin(1, machine.Pin.IN, machine.Pin.PULL_UP)

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, pw)
while not wlan.isconnected() and wlan.status() >= 0:
    print("Waiting for a successful WiFi connection")
    time.sleep(1)

while True:
    time.sleep(7)
    door_open = bool(sensor.value())
    if door_open != last_sent:
        if door_open == False:
            res = req.post(url, headers = h, data = json.dumps(cl))
            res.close()
            last_sent = False
        if door_open == True:
            res = req.post(url, headers = h, data = json.dumps(op))
            res.close()
            last_sent = True

cleverca22
Posts: 6600
Joined: Sat Aug 18, 2012 2:33 pm

Re: First project, looking for help on optimizing the code

Thu Sep 22, 2022 11:31 pm

Code: Select all

op = {
    "status": "open"
}

cl = {
    "status": "closed"
}
....
            res = req.post(url, headers = h, data = json.dumps(cl))
            res = req.post(url, headers = h, data = json.dumps(op))
first thing that catches my eye, is that you never change what cl or op is
so you would save some cpu time by just doing the json.dumps once, like this:

Code: Select all

op = json.dumps({"status":"open"})
cl = json.dumps({"status":"close"})
but that is unlikely to change the memory usage, and i cant see any reason why it would crash

MarkDH102
Posts: 571
Joined: Fri Feb 13, 2015 3:18 pm

Re: First project, looking for help on optimizing the code

Fri Sep 23, 2022 3:06 pm

How do you know it crashes? What are the symptoms.
In your main while loop, toggle the main board LED every 7 seconds to give you a visual indication that the code is running (or not).

User avatar
B.Goode
Posts: 14328
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: First project, looking for help on optimizing the code

Fri Sep 23, 2022 3:31 pm

wsq2 wrote:
Thu Sep 22, 2022 10:13 pm

I was hoping someone more experienced could take a quick look and maybe give a tip on how to make it more efficient.



My suggestion would be to make the code RELIABLE first and worry about efficiency/optimisation later.


Might it be that your wireless network connection is not 100% reliable? (That would not be unusual.) You don't seem to have any capability to detect or recover from a network connectivity failure.


How would a call to urequests.post() fail if the wireless network was no longer available? (Easy to test - just turn off the Access Point.) Similarly for the target web server. Take the web server offline - how does the Pico code respond?

Also I have a recollection that some implementations of urequests are 'fragile' and limited in how much data they can accept in response to a connection: maybe there are occasional situations where your web server returns an unexpected volume of data.

My approach to debugging would be to split the code into two test scripts. One that prints the status of the door sensor to your standard output at regular intervals. And another that sends a fixed message payload to your web log at regular intervals. See which fails. My guess is the second.

ghp
Posts: 2707
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany

Re: First project, looking for help on optimizing the code

Sat Sep 24, 2022 6:07 am

Ran the code here and got this error message after a few iterations. Then reset the pico and did not get the error again within a few hours. Used a local flask server which returns same amount of data for each request.
Experimented with large amount of return data also which did not change the game.

Optimization idea: the post method has a json-parameter which can read the dict directly.

Return to “Python”