• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

MQTT publish Micropython unit 12.3 error 'BME280' object has no attribute 'temperature'

Q&A Forum › Category: Questions › MQTT publish Micropython unit 12.3 error 'BME280' object has no attribute 'temperature'
0 Vote Up Vote Down
Judson Belmont asked 6 months ago

I am using the code for the micropython MQTT Publish example and receive the error noted above.(does connect to wifi and then the error occurs)
My other  BME280 library  programs otherwise work fine so my i2c connection is good.
So I tried  changing  # Read sensor data
temperature, humidity, pressure = get_sensor_readings()    to 
temperature,humidity,pressure =bme.get_sensor_readings()    at this point the error message changed to ‘bytes out of range” after connecting to wifi.
 
below is exact copy of the GitHub with i2c pins/channel changed.
from machine import Pin, I2Ca
from time import sleep
import network
from umqtt.simple import MQTTClient
import config
import BME280
# Constants for MQTT Topics
MQTT_TOPIC_TEMPERATURE = ‘pico/temperature’
MQTT_TOPIC_PRESSURE = ‘pico/pressure’
MQTT_TOPIC_HUMIDITY = ‘pico/humidity’
# MQTT Parameters
MQTT_SERVER = config.mqtt_server
MQTT_PORT = 0
MQTT_USER = config.mqtt_username
MQTT_PASSWORD = config.mqtt_password
MQTT_CLIENT_ID = b”raspberrypi_picow”
MQTT_KEEPALIVE = 7200
MQTT_SSL = True # set to False if using local Mosquitto MQTT broker
MQTT_SSL_PARAMS = {‘server_hostname’: MQTT_SERVER}
# Initialize I2C communication
i2c = I2C(id=1, scl=Pin(3), sda=Pin(2), freq=10000)
# Initialize BME280 sensor
bme = BME280.BME280(i2c=i2c, addr=0x76)
def get_sensor_readings():
temp = bme.temperature[:-1]
hum = bme.humidity[:-1]
pres = bme.pressure[:-3]
return temp, hum, pres
def initialize_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# Connect to the network
wlan.connect(ssid, password)
# Wait for Wi-Fi connection
connection_timeout = 10
while connection_timeout > 0:
if wlan.status() >= 3:
break
connection_timeout -= 1
print(‘Waiting for Wi-Fi connection…’)
sleep(1)
# Check if connection is successful
if wlan.status() != 3:
return False
else:
print(‘Connection successful!’)
network_info = wlan.ifconfig()
print(‘IP address:’, network_info[0])
return True
def connect_mqtt():
try:
client = MQTTClient(client_id=MQTT_CLIENT_ID,
server=MQTT_SERVER,
port=MQTT_PORT,
user=MQTT_USER,
password=MQTT_PASSWORD,
keepalive=MQTT_KEEPALIVE,
ssl=MQTT_SSL,
ssl_params=MQTT_SSL_PARAMS)
client.connect()
return client
except Exception as e:
print(‘Error connecting to MQTT:’, e)
raise # Re-raise the exception to see the full traceback
def publish_mqtt(topic, value):
client.publish(topic, value)
print(topic)
print(value)
print(“Publish Done”)
try:
if not initialize_wifi(config.wifi_ssid, config.wifi_password):
print(‘Error connecting to the network… exiting program’)
else:
# Connect to MQTT broker, start MQTT client
client = connect_mqtt()
while True:
# Read sensor data
temperature, humidity, pressure = get_sensor_readings()
# Publish as MQTT payload
publish_mqtt(MQTT_TOPIC_TEMPERATURE, str(temperature))
publish_mqtt(MQTT_TOPIC_PRESSURE, str(pressure))
publish_mqtt(MQTT_TOPIC_HUMIDITY, str(humidity))
# Delay 10 seconds
sleep(10)
except Exception as e:
print(‘Error:’, e)

Question Tags: bme280, MicroPython, MQTT, MQTT Publish
1 Answers
0 Vote Up Vote Down
Judson Belmont answered 6 months ago

I solved this by using the bme.values method:
def get_sensor_readings():
temp,hum,pres=bme.values
temp=temp[:-1]
hum=hum[:-1]
pres=pres[:-3]
#temp = bme.temperature[:-1]
#hum = bme.humidity[:-1]
#pres = bme.pressure[:-3]
return temp, hum, pres

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.