Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Python help needed
Author | Message | ||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
I have this problem Indoor Humidity only flashes on the screen the goes off again I know the sensor is working as the CO2 data is displaying Here is the console output showing the result of print("scd4x.data_ready:", scd4x.data_ready) How do I get the data to display on the screen but ignore the False, unless there is over 60 seconds of False then it might be a sensor issue Basically I need a trigger to display the data when the data is true, start a timer ignore any False >= 60 seconds If any True then the timer restarts I have spent a total of maybe 18 hours now trying different ways of doing this and even ChatGpt can't help with this because it tried to get me to use a "While True" loop which locks the Pi up I can't use While True any suggestions? if mcp and tempF is not None: #Air Temp Sensor Indoor # Indoor Temperature ren = font.render("Indoor Temp: ", 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) screen.blit(ren, (5, 90)) ren_data = font.render("{:.1f}F {:.1f}C".format(tempF, tempC), 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) right_justified_x = 270 - ren_data.get_width() screen.blit(ren_data, (right_justified_x, 90)) print("scd4x.data_ready:", scd4x.data_ready) if scd4x.data_ready: HumidityScd = scd4x.relative_humidity # Left-justified text ren = font.render("Indoor Humidity: ", 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) screen.blit(ren, (5, 130)) # Right-justified data humidity_text = "{:.1f}%".format(HumidityScd) ren_data = font.render(humidity_text, 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) right_justified_x = 270 - ren_data.get_width() screen.blit(ren_data, (right_justified_x, 130)) else: ren = font.render("Indoor Humidity:", 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) screen.blit(ren, (5, 130)) # If forecastData is available if forecastData is not None: # Temperature ren = font.render("Temperature: ", 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) screen.blit(ren, (5, 50)) ren_data = font.render("{0:.1f}°C {1:.1f}°F".format(forecastData.tempnow, forecastData.tempnow * 9 / 5 + 32), 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) right_justified_x = 270 - ren_data.get_width() screen.blit(ren_data, (right_justified_x, 50)) # Humidity ren = font.render("Humidity: ", 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) screen.blit(ren, (5, 170)) ren_data = font.render("{}%".format(forecastData.relative_humidity), 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) right_justified_x = 270 - ren_data.get_width() screen.blit(ren_data, (right_justified_x, 170)) # Pressure ren = font.render("Pressure: ", 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) screen.blit(ren, (5, 210)) ren_data = font.render("{}mb".format(forecastData.station_pressure), 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) right_justified_x = 270 - ren_data.get_width() screen.blit(ren_data, (right_justified_x, 210)) # Pressure trend ren = font.render("Pressure trend: ", 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) screen.blit(ren, (5, 250)) ren_data = font.render("{}".format(forecastData.pressure_trend), 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) right_justified_x = 270 - ren_data.get_width() screen.blit(ren_data, (right_justified_x, 250)) # Feels like ren = font.render("Feels like: ", 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) screen.blit(ren, (5, 290)) ren_data = font.render("{0:.1f}°C".format(forecastData.feels_like), 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) right_justified_x = 270 - ren_data.get_width() screen.blit(ren_data, (right_justified_x, 290)) # Left-justified text ren = font.render("Lightning:", 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) screen.blit(ren, (5, 330)) # Right-justified data data_text = "{} {}".format(time.strftime("%d %b"), forecastData.lightning_strike_last_distance) ren_data = font.render(data_text, 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) right_justified_x = 270 - ren_data.get_width() screen.blit(ren_data, (right_justified_x, 330)) |
||||
Andy-g0poy Regular Member Joined: 07/03/2023 Location: United KingdomPosts: 59 |
Use the python timer function. My python is to rusty to give you exact code, but: from: https://www.learndatasci.com/solutions/python-timer/ import time start_time = time.time() # The thing to time. Using sleep as an example time.sleep(10) end_time = time.time() elapsed_time = end_time - start_time print(elapsed_time) does most of what you want. Your code: print("scd4x.data_ready:", scd4x.data_ready) # remember my python is rusty, so something like: if scd4x.data_ready: and time.time() - start_time == however long you want HumidityScd = scd4x.relative_humidity # Left-justified text ren = font.render("Indoor Humidity: ", 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) screen.blit(ren, (5, 130)) # Right-justified data humidity_text = "{:.1f}%".format(HumidityScd) ren_data = font.render(humidity_text, 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) right_justified_x = 270 - ren_data.get_width() screen.blit(ren_data, (right_justified_x, 130)) else: ren = font.render("Indoor Humidity:", 1, pg.Color(162, 160, 160), pg.Color(25, 25, 112)) screen.blit(ren, (5, 130)) # set the timer running here, i.e. on the first true value. If you want it to be from before, then you will need to set the timeer before you start the readings. start_time = time.time() -------------- so: start the timer / restart the timer every time you have a true reading that's the start_time = time.time() line. This will mean that things might not stabilise until you get the first true reading. If you want to be 100% then start the timer when you first run the program as well. At the top of your routing add and and clause that subtrascts the stored start time from the current time, and if it is greater than whatever limit you want you can then clear the value screen otherwise the previous true reading will be left there. Hope my Python memory is working ! Andy |
||||
thwill Guru Joined: 16/09/2019 Location: United KingdomPosts: 4043 |
EDIT: cross-posted with Andy, whose solution looks similar. Something like this Lewis - you'll need to change it into valid Python: if scd4x.data_ready: HumidityScd = scd4x.relative_humidity # What time will it be 1 minute from now ? HumidityTimer = datetime.now() + datetime.timedelta(minutes=1) else if datetime.now() > humidity_timer: # Only "clear" if we are 1 minute from the last time the data was ready. HumidityScd = -1 # Data not ready. Best wishes, Tom Edited 2023-08-03 03:33 by thwill Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Thanks Tom |
||||
Print this page |