skip to Main Content

Currently writing a telegram bot to fetch live data from a Envrio+ phat on a Raspberry Pi…

When you run the built in example it will always print the below:

The current tempurature is 21.70818430223153 *C 

The current pressure is  700.2708792437015hPa

The Current Humidty is 84.54408663293306 %

On the next print of the while loop it will give you the correct data of the environment. To get around this I need to only fetch the 2nd set of data not the first.

I’ve tried indenting the variables in a while loop but that does not work through the bot and hit a brick wall.

Below is what works in the bot but fetches the same values back as posted above, every time the weather states are fetched.

def weather(bot, update):
    chat_id = update.message.chat_id
    temp = bme280.get_temperature()
    pressure = bme280.get_pressure()
    humidity = bme280.get_humidity()
    current_temp = "The current tempurature is " + str(temp)  + " *C " + "nThe current pressure is  " +  str(pressure) + "hPa" + "nThe Current Humidty is " + str(humidity) + " %"
    bot.send_message(chat_id=chat_id, text=current_temp)

The while loop stills brings the same values, not the next value in the example loop.

Any help would be appreciated

2

Answers


  1. Chosen as BEST ANSWER

    I was able to work through this over the last few days, it took a little longer as im still learning python as I go but before is the working code:

    while True: 
        if counter < 2:
            temperature = bme280.get_temperature()
            pressure = bme280.get_pressure()
            humidity = bme280.get_humidity()
            counter = counter + 1
            time.sleep(1)
        else:
            print("""Temperature: {:05.2f} *C
        Pressure: {:05.2f} hPa
        Relative humidity: {:05.2f} %
        """.format(temperature, pressure, humidity))
            break
    

  2. It sounds like the first request or the first loop initialises the bot and give back default values, thereafter you get real values.

    Maybe one of these two strategies might work:

    1.) Send a single request first and ignore the result, thereafter do a while loop
    2.) Initialise a counter before the while loop and if counter = 1 then ignore results and loop
    

    Some more code might help narrow down answers.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search