Help needed: Raspberry Pi sensor data not updating in Telegram bot

I’m working on a Telegram bot for my Raspberry Pi. It’s supposed to send temperature and humidity readings from a sensor. But I’m stuck. The bot keeps sending the same values over and over. It’s like the loop isn’t working right.

Here’s what I’ve got so far:

import climate_sensor
import chatbot_api

bot = chatbot_api.ChatBot('SECRET_KEY')

device = climate_sensor.ClimateDevice()
device.configure()

def get_temperature():
    return device.read_temperature()

def get_humidity():
    return device.read_humidity()

while True:
    if device.data_available():
        @bot.on_command('start')
        def greet(msg):
            bot.reply(msg, 'Hey there! Want temperature or humidity?')
        
        @bot.on_message
        def handle_message(msg):
            if msg.text == 'Temperature':
                bot.reply(msg, f'Temperature: {get_temperature()}')
            elif msg.text == 'Humidity':
                bot.reply(msg, f'Humidity: {get_humidity()}')
        
        bot.run()

Any ideas on why it’s not updating? I’m pretty new to this stuff. Thanks!

I’ve encountered a similar issue before. The problem lies in your loop structure and event handler placement. Move your bot.run() outside the while loop, and define your command handlers before entering any loop. This way, your bot will continuously listen for updates without getting stuck.

Here’s a suggested structure:

# Initialize bot and device
bot = chatbot_api.ChatBot('SECRET_KEY')
device = climate_sensor.ClimateDevice()
device.configure()

# Define command handlers
@bot.on_command('start')
def greet(msg):
    bot.reply(msg, 'Hey there! Want temperature or humidity?')

@bot.on_message
def handle_message(msg):
    if msg.text == 'Temperature':
        bot.reply(msg, f'Temperature: {device.read_temperature()}')
    elif msg.text == 'Humidity':
        bot.reply(msg, f'Humidity: {device.read_humidity()}')

# Start the bot
bot.run()

This should resolve your updating issue. Let me know if you need further clarification.

I’ve worked with Raspberry Pi projects and Telegram bots before, and I can see where you’re running into trouble. The main issue is that your bot.run() method is inside the while loop, which prevents it from properly updating and responding to new messages.

Here’s what I’d suggest:

  1. Move your bot.run() outside of any loops. This should be the last line of your script.

  2. Define your command handlers (like @bot.on_command and @bot.on_message) at the start of your script, not inside any loops.

  3. If you want to continuously update sensor data, you could use a separate thread or a timer to periodically check the sensor and update a global variable.

  4. In your message handler, read from this global variable instead of directly from the sensor.

This approach should keep your bot responsive while still allowing for regular sensor updates. Let me know if you need more details on implementing any of these suggestions!

Hey alexr1990, looks like ur bot’s stuck in a loop! try moving bot.run() outside the while True loop. it’s blocking ur code from updating. also, define ur command handlers outside the loop. that should fix it. lemme know if u need more help!