英文:
Raspberry Pi Pico W Autostart with Network
问题
我正在尝试自动启动一个Pico W程序。
我可以让它闪烁LED,但当我添加关于温度传感器和网络的逻辑时,它不再自动启动。
我将这个程序保存为main.py。
当我在Thonny中点击播放时,程序会运行,但不会自动启动。
忽略混乱的代码,因为它仍然在进行中
import network
import urequests
import machine
import utime
import gc # 导入垃圾收集器
# 连接到网络
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
led = machine.Pin("LED", machine.Pin.OUT)
# 在这里填写你的网络名称(SSID)和密码:
ssid = 'SuperHot'
password = '(pass)'
while not wlan.isconnected():
wlan.connect(ssid, password)
utime.sleep(2) # 等待2秒后重试
url = "http://192.168.4.23:8000/data"
headers = {
"Content-Type": "text/plain"
}
LM35 = machine.ADC(0) # 设置ADC上的模拟读取
Cal_Offset = 5050 # 校准偏移值
# 通过实际测试确定
def Compute_Temp(Avg_A):
LM35_A = Avg_A + Cal_Offset # 添加校准调整
LM35_V = LM35_A * .00005 # 将模拟读数转换为伏特
Tmp_C = round((LM35_V * 100),1) # 将伏特转换为摄氏温度
Tmp_F = round((Tmp_C * 1.8 + 32),1) # 将Tmp_C转换为Tmp_F
return Tmp_C, Tmp_F # 返回温度
Samples = 0 # 变量保存所有样本
Num_Samples = 1 # 计数器,用于记录采集的样本数
while True:
led.on()
utime.sleep(.5)
led.off()
utime.sleep(.25)
if Num_Samples <= 10: # 存储总共10个样本
LM35_A = LM35.read_u16() # 读取ADC端口以获取传感器数据
Samples = Samples + LM35_A # 将当前读数添加到样本批次中
Num_Samples += 1 # 增加计数器
else:
Avg_A = Samples / 10 # 获取样本的平均值
Samples = 0 # 将样本变量重置为零
Num_Samples = 1 # 将计数器重置为一
T_c, T_f = Compute_Temp(Avg_A) # 从函数中获取温度
data = "Celcius=" + str(T_c) + " Fahrenheit=" + str(T_f)
response = urequests.put(url, data=data, headers=headers)
gc.collect() # 在这里调用垃圾收集器
utime.sleep(.1) # 减慢循环速度
英文:
I am trying to autostart a pico W program.
I can get it to flash LEDs, but when I add my logic about temp sensor and network, it doesn't autostart anymore.
I save this program to main.py.
The program runs when I hit play with Thonny, just does not autostart.
Ignore the messy code, as it's very much a WIP
import network
import urequests
import machine
import utime
import gc # Import the garbage collector
# Connect to network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
led = machine.Pin("LED", machine.Pin.OUT)
# Fill in your network name (SSID) and password here:
ssid = 'SuperHot'
password = '(pass)'
while not wlan.isconnected():
wlan.connect(ssid, password)
utime.sleep(2) # Wait for 2 seconds before retrying
url = "http://192.168.4.23:8000/data"
headers = {
"Content-Type": "text/plain"
}
LM35 = machine.ADC(0) #setup analog reading on ADC
Cal_Offset = 5050 #Calibration offset value
#Determined from practical testing
def Compute_Temp(Avg_A):
LM35_A = Avg_A + Cal_Offset #Add Calibration Adjustment
LM35_V = LM35_A * .00005 #Convert analog reading to volts
Tmp_C = round((LM35_V * 100),1) #Convert volts to temp celcius
Tmp_F = round((Tmp_C * 1.8 + 32),1) #Convert Tmp_C to Tmp_F
return Tmp_C, Tmp_F #Return Temps
Samples = 0 #Variable holds all samples
Num_Samples = 1 #Counter for num samples collected
while True:
led.on()
utime.sleep(.5)
led.off()
utime.sleep(.25)
if Num_Samples <= 10: #storing a total of 10 samples
LM35_A = LM35.read_u16() #Read the ADC port to get sensor data
Samples = Samples + LM35_A #Add current reading to sample batch
Num_Samples += 1 #Increment counter
else:
Avg_A = Samples / 10 #Get the average of samples
Samples = 0 #Reset Samples variable to zero
Num_Samples = 1 #Reset counter to one
T_c, T_f = Compute_Temp(Avg_A) #Fetch the temps from the function
data = "Celcius=" + str(T_c) + " Fahrenheit=" + str(T_f)
response = urequests.put(url, data=data, headers=headers)
gc.collect() # Invoke the garbage collector here
utime.sleep(.1) #slow the loop down```
</details>
# 答案1
**得分**: 1
连接到网络对我来说看起来不太对,这部分我会更改为以下内容:
```python
ssid = 'SuperHot'
password = '(pass)'
wlan.connect(ssid, password) # 尝试连接
while not wlan.isconnected(): # 然后等待板子连接
utime.sleep(2) # 重试前等待2秒
如果这不能解决你的问题,请在串行控制台中发布日志,也许你会得到一些错误。
英文:
Connecting to network does not look right for me, this part I would change to following:
ssid = 'SuperHot'
password = '(pass)'
wlan.connect(ssid, password) # try to connect
while not wlan.isconnected(): # and then wait till board connects
utime.sleep(2) # Wait for 2 seconds before retrying
If this does not solve you question- post log from serial console, maybe yu get some errors
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论