BME280 app works connected to RPI but not on battery power

I have a Pico micropython app running well on the pico Explorer with pico 2W. This has been developed using Thonny and the Pimoroni Micopython download (2040).

However the app doesn’t work properly when run on battery power, it starts and displays a message but then seems to hang during the ‘from connect import wlan’ statement.

Can you describe more details about your setup? With the limited information, I can guess that you might have a power issue. Initializing the WLAN-chip draws far more power than the rest of the system uses. The power draw is only short, but it cause a brown out. But this is just speculation without details.

Posting your code will also help, use the preformatted text </> button to retain the code format.

I’m running a multitude of i2c breakouts, one of which is a BME280 on a Pico Plus 2w. It’s powered via a Lipo Amiga Pro and a 10050 Mah Lipo battery.
I do get the occasional connection error, but as far as i know, it’s not a result of being on battery power. It also happens when USB powered. I think it’s more to do with the RM2 being on the back side of my Plus 2W, and the metal mounting plate I’m using for all my bits and bytes. ;)

My wlan code is as follows.

import network
from secrets import WIFI_SSID, WIFI_PASSWORD
display.set_pen(0)
display.clear()
display.set_pen(15)

led.value(1)
time.sleep(5)
if button.value() == 1:

    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(WIFI_SSID, WIFI_PASSWORD)
    while wlan.isconnected() is False:
        print('Waiting for connection...')
        display.text("RP2350B", 30, 0, scale=2)
        display.text("RM2 WIFI", 25, 18, scale=2)
        display.text("PIFI24", 35, 36, scale=2)
        display.text("Connecting", 15, 54, scale=2)
        display.update()
        led.value(1)
        time.sleep(1)

    if wlan.isconnected() is True:
        display.set_pen(0)
        display.clear()
        display.set_pen(15)
        display.text("RP2350B", 30, 0, scale=2)
        display.text("RM2 WIFI", 25, 18, scale=2)
        display.text("PIFI24", 35, 36, scale=2)
        display.text("Connected", 18, 54, scale=2)
        display.text("WIFI OK", 30, 72, scale=2)
        display.update()
        led.value(0)
        time.sleep(5)
    
    else:
        display.set.pen(0)
        display.clear()
        display.set.pen(15)
        display.text("RP2350B", 30, 0, scale=2)
        display.text("RM2 WIFI", 25, 18, scale=2)
        display.text("PIFI24", 35, 36, scale=2)
        display.text("WIFI ??", 25, 54, scale=2)
        display.update()
        led.value(1)
        time.sleep(5)
        

I have a small OLED display setup so I can see what’s happening. And a bypass option if need be.

Thanks both for your help, I have it working now.

I was following the Raspberry Pi book on Micropython (2nd Edition) and as a result copied the idea of logging on to the wifi using a python file called connect. This didn’t work when using battery or usb connection, this only worked when Pico was connected to a RPi5 using Thonny.

Looking at your code alphanumeric (thanks for uploading) I decided to incorporate the login code into the main.py application. Here is the code for my app. The aim is to create en environment sensor which is stand-alone and uses a small display to indicate temperature etc. and at intervals transfer the data to a web server on the local network.

# This example lets you plug a BME280 breakout into your Pico Explorer and make a little indoor weather station, with barometer style descriptions.
from breakout_bme280 import BreakoutBME280
from pimoroni_i2c import PimoroniI2C
from pimoroni import PICO_EXPLORER_I2C_PINS
from picographics import PicoGraphics, DISPLAY_PICO_EXPLORER

# set up the hardware
display = PicoGraphics(display=DISPLAY_PICO_EXPLORER)
i2c = PimoroniI2C(**PICO_EXPLORER_I2C_PINS)
bme = BreakoutBME280(i2c, address=0x76)

# lets set up some pen colours to make drawing easier
TEMPCOLOUR = display.create_pen(255, 255, 255)
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
RED = display.create_pen(255, 0, 0)
GREY = display.create_pen(192, 192, 192)
GREEN = display.create_pen(0,255,0)
YELLOW = display.create_pen(255,255,0)
BLUE = display.create_pen(120,120,255)
LIGHTBLUE = display.create_pen(140,140,255)
colourlist = [WHITE,RED,GREY,GREEN,RED,WHITE,YELLOW,BLUE,YELLOW,LIGHTBLUE]
pos = [ 2,24,48,72,96,120,144,168,192,216]
display.set_pen(GREY)
display.clear()

logging = False
# use logging when testing using Thonny

def show(text,line,colour):
    text = text[:14]
    display.set_pen(colour)
    display.text(text, 8, pos[line], 240, 3)
    display.update()

def log(message):
    if logging:
        print(message)
        
display.set_pen(GREY)
display.clear()

show('Starting',1,YELLOW)
import socket
import machine
import time
import network
import rp2
import requests
show('Imports OK',2,YELLOW)

rp2.country('GB')
ssid = '....'
psk =  '....'

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid,psk)
status =  0
max_wait = 30
status = wlan.status()
while status != 3:
    max_wait +=1
    show(f'Waiting {status}',3,RED)
    time.sleep(1)
    status = wlan.status()

if status == 3:
    show('Connected',4,GREEN)
else:    
    show(f'No Network {status}',4,RED)
    raise RuntimeError(f'Network failed {status}')
    
log(wlan.ifconfig())

# converts values into a pen colour
def describe_temperature(temperature):
    if temperature         < 10: return BLUE
    elif temperature       < 18: return GRAY
    elif temperature       < 24: return GREEN
    elif 20 <= temperature < 28: return YELLOW
    else:                        return RED

def describe_pressure(pressure):
    if pressure            < 980:  return RED
    elif pressure          < 1000: return BLUE
    elif pressure          < 1020: return GREEN
    else:                          return YELLOW

def describe_humidity(humidity):
    if humidity < 35 :     return RED
    elif humidity < 40 :     return YELLOW
    elif humidity < 60:   return GREEN
    else:                  return LIGHTBLUE

# rolling average of a range of values governed by the 
# length of list at set up

def average(past,new):
    past = past[1:] + [new]
    return past, sum(past)/len(past)
        
# allow sensor to adjust after start up
# loop x times at 10 seconds intervals
for i in range(0,1): 
    temperature, pressure, humidity = bme.read()
    pressure = pressure/100
    time.sleep(10)

templist = [temperature] * 5
log(templist)

# measurement loop and report
report = 0
while True:
    display.set_pen(BLACK)
    display.clear()

    # read the sensors
    temperature, pressure, humidity = bme.read()
    pressure = pressure/100
    # pressure comes in pascals which is a 
    # reight long number, lets convert it to the
    # more manageable hPa

    # drawing the temperature text
    display.set_pen(WHITE)
    display.text("temperature:", 10, 10, 240, 3)
    templist, tempa = average(templist, temperature)
    display.set_pen(describe_temperature(tempa))
    display.text("{:.1f}".format(tempa) + "C", 10, 40, 240, 10)
    # print(tempa, humidity, pressure)
    display.set_pen(WHITE)
    #    display.text(describe_temperature(temperature), 10, 60, 240, 3)

    # and the pressure text
    # display.text("pressure:", 10, 120, 240, 3)
    display.set_pen(describe_pressure(pressure))
    pressurea = f'{pressure:.0f}'
    display.text(f"PRESSURE {pressurea}", 10, 167, 240, 3)

    # and the humidity text
    display.set_pen(describe_humidity(humidity))
    humiditya = f'{humidity:.0f}'
    display.text(f"HUMIDITY {humiditya}%", 10, 191, 240, 3)

    # display current time
    now = time.localtime()
    nowtime = f'{now[3]:02}:{now[4]:02};{now[2]}-{now[1]}-{str(now[0])[2:4]}'
    display.set_pen(GREY)
    display.text(nowtime, 10, 220, 240, 3)

    # time to update the display
    display.update()

    time.sleep(10) # takes a reading roughly every 10 seconds

    if report > 6: # data is reported roughly  every minute
        # compose data
        message = f'Pico1,{nowtime},{tempa},{humiditya},{pressurea}'
        log(message)
        show('Data sent',6,GREEN)
        time.sleep(1)
        messageb = bytes(message,'utf8')
        # send to receiver
        response = requests.get(f'http://192.168.1.100:8080/Pico1?temp={messageb}')
        log(response.content)
        response.close()
        report = 1
    else:
        report +=1

1 Like