I've tried many variations of this project. The end goal being, a Raspberry Pi Pico W running Micropython, and a common cathode 4 digit, 7 segment display (NOT a TM1637 type, that will be the last resort, but there has to be a way to do it without, and I'm trying to figure it out), which will gather temperature data from an API, and then multiplex that data to display properly, while continuing to monitor the temp value.
The issue comes in that when I call back to urequests.get(temp_url).ujson() to gather the data again, that command causes the display temporarily go black until that block of code passes, but there's no other way I can imagine to keep getting the temp values repeatedly that either doesn't also cause a halt in the multiplexing loop, or doesn't update past the first get of the API data.
So here's the section of code that I'm struggling with. The main function just calls to connect and has some exception code, and the other exceptions have been removed for the purpose of diagnosing for now. This is definitely still a work in progress, ignore any indenting issues that were caused by copying and pasting, and remember this is file that has been changed and altered and tweaked to try to get results different ways, but all of my attempts seem to result in the same issue.
refresh = 0.0025 and dig1-4, SegDisplay, are all referencing part of the code for setting the digits and the outputting to the acceptable pins to produce the number. PL1-4 are just being used to split up the temp value into it's up to 4 individual numbers, to then be used to loop through at the refresh speed. This part of the code works fine, if I set a static number, it works well, but once the number needs to be checked at requests (urequests and ujson as requests and json), the display goes black for half a second or so and then comes back on and loops everytime it checks back to r=requests.get.....
def display():
dig1.value(1)
dig2.value(1)
dig3.value(1)
dig4.value(1)
while True:
value = get_temp()
value = round(value)
value = str(value)
if value == None:
clear()
break
elif len(value)==1:
pl1 = "0"
pl2 = "0"
pl3 = "0"
pl4 = value[0]
dig1.value(1)
dig2.value(1)
dig3.value(1)
dig4.value(1)
SegDisplay(pl4)
dig4.value(0)
sleep(refresh)
dig4.value(1)
elif len(value)==2:
value = get_temp()
print(value)
value = round(value)
value = str(value)
pl1 = "0"
pl2 = "0"
pl3 = value[0]
pl4 = value[1]
dig1.value(1)
dig2.value(1)
dig3.value(1)
dig4.value(1)
SegDisplay(pl3)
dig3.value(0)
sleep(refresh)
dig3.value(1)
SegDisplay(pl4)
dig4.value(0)
sleep(refresh)
dig4.value(1)
elif len(value)==3:
pl1 = "0"
pl2 = value[0]
pl3 = value[1]
pl4 = value[2]
dig1.value(1)
dig2.value(1)
dig3.value(1)
dig4.value(1)
SegDisplay(pl2)
dig2.value(0)
sleep(refresh)
dig2.value(1)
SegDisplay(pl3)
dig3.value(0)
sleep(refresh)
dig3.value(1)
SegDisplay(pl4)
dig4.value(0)
sleep(refresh)
dig4.value(1)
elif len(value)==4:
pl1 = value[0]
pl2 = value[1]
pl3 = value[2]
pl4 = value[3]
SegDisplay(pl1)
dig1.value(0)
sleep(refresh)
dig1.value(1)
SegDisplay(pl2)
dig2.value(0)
sleep(refresh)
dig2.value(1)
SegDisplay(pl3)
dig3.value(0)
sleep(refresh)
dig3.value(1)
SegDisplay(pl4)
dig4.value(0)
sleep(refresh)
dig4.value(1)
def get_temp():
r = requests.get(temp_url).json
temp = r.get('ispoint')
return temp
def connect():
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
print('Waiting to connect...')
sleep(1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
led.value(1)
display() #CALL DISPLAY
return ip
Things to remember: This all is done on Micropython. I couldn't find any prebuilt libraries for 4 digit 7 segment displays that aren't the TM1637 types. And again, that part seems to work, it's just when I try to call to r=requests.get(temp_url).json() that it stops the multiplexing loops for a split second which obviously makes the display visibly flash dark.
Sorry this is a bit confusing. Just looking to see if anyone has any ideas that I'm missing to try to get a smooth display multiplexing loop while also checking the temp from the API in the same loop.