树莓派 Pi Pico W 自动启动并连接网络

huangapple go评论65阅读模式
英文:

Raspberry Pi Pico W Autostart with Network

问题

我正在尝试自动启动一个Pico W程序。

我可以让它闪烁LED,但当我添加关于温度传感器和网络的逻辑时,它不再自动启动。

我将这个程序保存为main.py。

当我在Thonny中点击播放时,程序会运行,但不会自动启动。

忽略混乱的代码,因为它仍然在进行中 树莓派 Pi Pico W 自动启动并连接网络

https://pastebin.com/x39b7PgK

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 树莓派 Pi Pico W 自动启动并连接网络

https://pastebin.com/x39b7PgK

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(&quot;LED&quot;, machine.Pin.OUT)
# Fill in your network name (SSID) and password here:
ssid = &#39;SuperHot&#39;
password = &#39;(pass)&#39;
while not wlan.isconnected():
wlan.connect(ssid, password)
utime.sleep(2)  # Wait for 2 seconds before retrying
url = &quot;http://192.168.4.23:8000/data&quot;
headers = {
&quot;Content-Type&quot;: &quot;text/plain&quot;
}
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 &lt;= 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 = &quot;Celcius=&quot; + str(T_c) + &quot;  Fahrenheit=&quot; + 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 = &#39;SuperHot&#39;
password = &#39;(pass)&#39;
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

huangapple
  • 本文由 发表于 2023年6月26日 03:55:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定