NameError: name 'timers' isn't defined #14106
Unanswered
mdibit
asked this question in
RP2040 / Pico
Replies: 1 comment 1 reply
-
I'm sorry for the code not well indented, I'll try to get it better (maybe with mark down) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
To whom is reading this post : this program is just a simplified exercise to better understand the strange behavior of my original program where the thread Semaforo should be substituted by a robot code which should receive command from web app.
So :
The error generated by the code:
Unhandled exception in thread started by <function thread_Semaforo at 0x20015d80>
Traceback (most recent call last):
File "", line 91, in thread_Semaforo
File "", line 44, in timerWait
File "", line 34, in timerAttivo
NameError: name 'timers' isn't defined
I'd like to understand why, in the following code, I get the error only if the function generating the error is called from the thread Semaforo but it's perfectly working if called from the main program (or main thread).
What is even more strange is that, in the same function, the error is generated after the same name has been used before.
Thanks
Michele
import network
import socket
import time
import random
from machine import Pin
import _thread
TEMPO_STOP = 2**31
global timers
timers = [TEMPO_STOP,TEMPO_STOP,TEMPO_STOP,TEMPO_STOP,TEMPO_STOP,TEMPO_STOP]
global T_1
T_1 = 1 # timer 1
Create an LED object on pin 'LED'
led = Pin('LED', Pin.OUT)
Wi-Fi credentials
ssid = 'YYYYYYY'
password = 'XXXXXXXX'
pulsantePremuto = False
def timerStart(timer, durata):
global timers
timers[timer] = time.ticks_add(time.ticks_ms(),durata)
def timerStop(timer):
global timers, TEMPO_STOP
timers[timer] = TEMPO_STOP
def timerAttivo(timer):
global timers, TEMPO_STOP
return (timers[timer] != TEMPO_STOP)
def timerScaduto(timer):
global timers
return ((time.ticks_ms() > timers[timer]) and timerAttivo(timer))
def timerWait(durata):
global T_1
timerStart(T_1, durata)
while ( timerAttivo(T_1)):
pass
timerStop(T_1)
HTML template for the webpage
def webpage(random_value, state):
html = f"""
<title>Pico Web Server</title>
Raspberry Pi Pico Web Server
Led Control
LED state: {state}
Fetch New Value
Fetched value: {random_value}
"""
return str(html)
def thread_Semaforo():
global pulsantePremuto
global T_1
while (True):
print("Rosso")
if pulsantePremuto == True:
for i in range(10):
led.value(1)
print("ON",i)
timerWait(1000)
led.value(0)
print("OFF",i)
timerWait(1000)
#pulsantePremuto = False
timerWait(5000)
print("Verde")
timerWait(5000)
print("Giallo")
timerWait(2000)
#-------------------------------------------------------------------------------------
_thread.start_new_thread(thread_Semaforo, ())
print("Connect to WLAN")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
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...')
time.sleep(1)
Check if connection is successful
if wlan.status() != 3:
raise RuntimeError('Failed to establish a network connection')
else:
print('Connection successful!')
network_info = wlan.ifconfig()
print('IP address:', network_info[0])
Set up socket and start listening
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen()
print('Listening on', addr)
Initialize variables
state = "OFF"
random_value = 0
Main loop to listen for connections
while True:
try:
print("qui")
conn, addr = s.accept()
print('Got a connection from', addr)
Beta Was this translation helpful? Give feedback.
All reactions