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