Hello danjperron,danjperron wrote: ↑Fri Aug 26, 2022 12:32 pmI didn't have any problem with the BME280 sensor but I installed that version
https://github.com/SebastianRoll/mpy_bme280_esp8266
I just copy the bme280.py into the pico lib folder.
This is an example. I didn't test this exact code but it should work since I extracted it from my working solar garden station.
https://github.com/danjperron/PicoWSolar
Be aware that some BME280 aren't at 0x77 address. Check yours using i2c.scan()
Code: Select all
import machine import bme280 bme_PinSDA=4 bme_PinSCL=5 bme_i2c_address = 0x77 i2c=machine.I2C(0,sda=machine.Pin(bme_PinSDA), scl=machine.Pin(bme_PinSCL), freq=100000) #initializing the I2C method bme = bme280.BME280(i2c=i2c,address=bme_i2c_address) def readBme(): try: t, p, h = bme.read_compensated_data() t, p, h = bme.read_compensated_data() return (t/100.0 , p/25600.0, h/1024.0) except OSError as e: pass return None t, p , h = readBme() print("Temperature :", t) print("Humidity:", h) print("Pressure:", p)
I was looking at your code, and i went further and had a look at your code at your PicoWSolar github page. As long as it was ok for me to do so, I took a copy of your overall code, which included the wifi connection programming etc too. This looks most different to mine which I have running with an SHT30 sensor. So i was interested to try yours for my switch over from RP4B to PicoW for my TWO bme280 sensros in my loft.
After I took your code, I've attempted to edit it somewhat to account for the fact i have two bme280 sensors,not just one.
I'm confused at how to account for two sensors with the way your code is laid out. Honestly, I think i'd have trouble even with my own code at understanding how to read two sensors at once. I'm not even doing that within one script with my RP4B. I have two individual scripts running there, although they both look at the same serial link. Now that i'm writing this, I'm wondering if I have to do the same in a Pico W too?
I've included how I'm doing my serial link to my ONE SHT30 device in my first Pico W here:
Code: Select all
#Necessary library imports for this code to function
from secrets import SSID
from secrets import PASSWORD
from secrets import IP
from secrets import IP_mask
from secrets import IP_gateway
from secrets import IP_DNS
from secrets import MQTTSERVE
from umqtt.simple import MQTTClient
import time
import network
import ubinascii
import socket
from machine import SoftI2C, Pin
import sht30
import uasyncio as asyncio
#Define the interface of the SHT30 Temp/Humidity sensor with the Pico W
i2c=SoftI2C(sda=Pin(4), scl=Pin(5), freq=50000)
sht=sht30.SHT30(i2c=i2c, i2c_address=68)
sensor = sht.measure()
temperature, humidity = sensor
#Define the onboard Pico Temperature sensor and necessary conversion factor
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)
#Active the onboard Pico W Wi-Fi and connect to home network defined through "secrets.py"
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
#print(wlan.scan())
wlan.connect(SSID, PASSWORD)
#Configure this Pico W for a static IP address on router using the following
UseStaticIP = True
IP = IP
IP_mask = IP_mask
IP_gateway = IP_gateway
IP_DNS = IP_DNS
#Read the MAC address from the Pico W itself
mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
print("MAC address for this Pico W:", mac)
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection attempt failed')
else:
print("Pico W Wi-Fi is connected to: ",SSID)
status = wlan.ifconfig()
print( "ip address for this Pico W: " + status[0] )
print()
print("Channel: ", wlan.config('channel'))
print("SSID: ", wlan.config('essid'))
print("TXPOWER: ", wlan.config('txpower'))
wlan.config(pm = 0xa11140)
#Show some onboard LED flashing to show that attempting to connect to the Wi-Fi has been completed
time.sleep(5)
led = machine.Pin("LED", machine.Pin.OUT)
#led = Pin(15, Pin.OUT)
led.off()
time.sleep(2)
led.toggle()
time.sleep(0.2)
led.toggle()
time.sleep(1)
led.toggle()
time.sleep(0.2)
led.toggle()
time.sleep(1)
led.toggle()
time.sleep(0.2)
led.toggle()
time.sleep(1)
led.toggle()
time.sleep(2)
#Setup MQTT server, local client ID (not really required),
#and published data transmissions hierarchy
mqtt_server = MQTTSERVE
client_id = b'PICOW'
topic_pub1 = b'OS_Temp'
topic_pub2 = b'OS_Hum'
topic_pub3 = b'Pico_Temp'
#For the above topics, use "PicoW/+"
#at the subscriber side to receive all
#three messages at once or use the full
#titles individually i.e PicoW/OS_Temp for
#just the outside temperature variable
def mqtt_connect():
client = MQTTClient(client_id, mqtt_server, keepalive=3600)
client.connect()
print('Connected to %s MQTT Broker'%(mqtt_server))
return client
def reconnect():
print('Failed to connect to the MQTT Broker. Reconnecting...')
time.sleep(5)
machine.reset()
try:
client = mqtt_connect()
except OSError as e:
reconnect()
#The main script of repeativeness for reading in from the connected sensor
#and taking in onboard temperature readings
while True: #do stuff here
led.on()
time.sleep(0.5)
sensor = sht.measure()
temperature, humidity = sensor
reading = sensor_temp.read_u16() * conversion_factor
picotemp = 27 - (reading - 0.706)/0.001721
#make readings 1 decimal place
tpm1 = round(temperature,1)
tpm2 = round(humidity,1)
tpm3 = round(picotemp,1)
#convert float reading values into bytes ready for MQTT transmission
tpm4 = f"{tpm1}".encode()
tpm5 = f"{tpm2}".encode()
tpm6 = f"{tpm3}".encode()
topic_msg1 = tpm4
topic_msg2 = tpm5
topic_msg3 = tpm6
#Message displayed to the screen
print()
print('SHT Temperature: {:3.1f}ºC'.format(tpm1))
print('SHT Humidity: {:3.1f}%'.format(tpm2))
print('Pico W Temperature: {:3.1f}ºC'.format(tpm3))
#Equivalent transmission of displayed readings above
#(reverse order to match above order on subscriber receipt)
client.publish(topic_pub3, topic_msg3)
client.publish(topic_pub2, topic_msg2)
client.publish(topic_pub1, topic_msg1)
led.off()
time.sleep(10)
Code: Select all
#Necessary library imports for this code to function
from secrets2 import SSID
from secrets2 import PASSWORD
from secrets2 import IP
from secrets2 import IP_mask
from secrets2 import IP_gateway
from secrets2 import IP_DNS
from secrets2 import MQTTSERVE
#from umqtt.simple import MQTTClient
import time
import network
import ubinascii
#import socket
#from machine import SoftI2C, Pin
from machine import Pin
import machine
import rp2
import sys
import bme280
wifi_essid = SSID
wifi_password = PASSWORD
# define IP,mask and gateway if IP is static
UseStaticIP = True
IP = IP
IP_mask = IP_mask
IP_gateway = IP_gateway
IP_DNS = IP_DNS
IP_MQTT_BROKER = MQTTSERVE
UnitID = "LoftPicoW"
#do we want to use watchdog to reset in case
WatchDog_Enable = True
#sensors definition
#bme280 sensors
bme_Enable = False
bme_PinSDA=4
bme_PinSCL=5
bme0_i2c_address = 0x77
bme0_temperature_topic = "bme0T"
bme0_pressure_topic = "bme0P"
bme0_humidity_topic = "bme0H"
bme1_i2c_address = 0x76
bme1_temperature_topic = "bme1T"
bme1_pressure_topic = "bme1P"
bme1_humidity_topic = "bme1H"
# stop switch (Press to quit on reset if file is main.py)
#switchPin = 19
#ok let's set the Pins
#sw = machine.Pin(switchPin,machine.Pin.IN,machine.Pin.PULL_UP)
time.sleep_ms(10)
#On boot do we want to stop main() if yes exit
#if sw.value() == 0:
# sys.exit()
#### WATCH DOG
# set maximum wachtdog 8.3s ( 2**24 -1)/2
wdt = machine.WDT(timeout=8300000)
# define lightsleep with watch dog limits
# because we implemented wachtdog we need to
# maximize the light sleep to be lower than 8 second
# the watch dog is 24 bits at 1us clock by a count of 2
# then the maximum is (2 power 24 -1)/2 which is ~8.3 sec
# we will use 5 seconds step
def wd_lightsleep(value):
if WatchDog_Enable:
while value >0:
if value >= 5000:
machine.lightsleep(5000)
value = value - 5000
else:
machine.lightsleep(value)
value=0
wdt.feed()
else:
machine.lightsleep(value)
#ok let's set the Pins
#i2c
# in bme280 i2c address was 0x76 put adafruit need to be 0x77
if bme_Enable and (address = 0x77): # I don't know how to read from two sensors at once, so this is just a note so you can see what I'm trying to do
i2c=machine.I2C(0,sda=machine.Pin(bme_PinSDA), scl=machine.Pin(bme_PinSCL), freq=1000) #initializing the I2C method
bme0 = bme280.BME280(i2c=i2c,address=bme0_i2c_address)
if bme_Enable and (address = 0x76): # I don't know how to read from two sensors at once, so this is just a note so you can see what I'm trying to do
i2c=machine.I2C(0,sda=machine.Pin(bme_PinSDA), scl=machine.Pin(bme_PinSCL), freq=1000) #initializing the I2C method
bme1 = bme280.BME280(i2c=i2c,address=bme1_i2c_address)
#Led
led = machine.Pin('LED', machine.Pin.OUT)
#cpu temperature declaration
cpu_temp = machine.ADC(machine.ADC.CORE_TEMP)
conversion_factor = 3.3 / (65535)
def readCpuTemperature():
reading = cpu_temp.read_u16() * conversion_factor
return 27 - (reading - 0.706)/0.001721
def readBme():
try:
t, p, h = bme.read_compensated_data()
t, p, h = bme.read_compensated_data()
return (t/100.0 , p/25600.0, h/1024.0)
except OSError as e:
pass
return None
#network declaration
# Set country to avoid possible errors / https://randomnerdtutorials.com/micropython-mqtt-esp32-esp8266/
rp2.country('GB')
wlan=None
client=None
def stopWifi():
global client
global wlan
if not(client==None):
client.disconnect()
client=None
if not(wlan==None):
wlan.disconnect()
wlan.active(False)
wlan.deinit()
wlan=None
time.sleep_ms(100)
def setWifi():
global wlan
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# See the MAC address in the wireless chip OTP
mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
print("MAC address for this Pico W:", mac)
# Load logimin data from different file for safety reasons
#set static IP
# (IP,mask,gateway,dns)
if UseStaticIP:
wlan.ifconfig((IP, IP_mask,IP_gateway,IP_DNS))
wlan.connect(wifi_essid,wifi_password)
#set 10 seconds for time out delay
wlandelay = time.ticks_ms() + 12000
while time.ticks_ms() < wlandelay:
if wlan.isconnected():
if wlan.status() <0 or wlan.status() >=3 :
break
machine.idle()
if WatchDog_Enable:
wdt.feed()
# Handle connection error
# Error meanings
# 0 Link Down
# 1 Link Join
# 2 Link NoIp
# 3 Link Up
# -1 Link Fail
# -2 Link NoNet
# -3 Link BadAuth
if wlan.status() != 3:
return False
return True
def connectWifi():
for i in range(3):
print("connect Client",i)
if setWifi()==False:
stopWifi()
continue
else:
return True
#unable to connect after 3 tentatives
return False
###MQTT Topic Setup ###
def connectMQTT():
client = MQTTClient(UnitID,IP_MQTT_BROKER)
client.connect()
return client
def publish(topic,value,idx=None):
global UnitID
if WatchDog_Enable:
wdt.feed()
print(topic,value)
pub_msg = "%5.3f" % value
print(topic," ",pub_msg)
if idx==None:
client.publish(UnitID+"/"+topic, pub_msg)
else:
client.publish(UnitID+"/"+topic+"_"+str(idx), pub_msg)
print("publish Done")
def getSensorsAndPublish():
global vsys
if bme_Enable:
v = readBme()
publish(bme0_temperature_topic,v[0])
publish(bme0_pressure_topic,v[1])
publish(bme0_humidity_topic,v[2])
publish(bme1_temperature_topic,v[3])
publish(bme1_pressure_topic,v[4])
publish(bme1_humidity_topic,v[5])
publish('TempCPU',readCpuTemperature())
#ok main process
try:
while True:
PowerAnalogPin(True)
try:
if not connectWifi():
print("wifi connect Failed restart")
time.sleep_ms(500)
machine.reset()
client = connectMQTT()
except OSError as e:
print("WIFI OR MQTT Failed restart")
time.sleep_ms(500)
machine.reset()
led.value(True)
time.sleep_ms(100)
led.value(False)
getSensorsAndPublish()
stopWifi()
time.sleep_ms(50)
wd_lightsleep(9000) #9 sec cycle
except OSError as e:
print("OS ERROR restart")
time.sleep_ms(500)
machine.reset()