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```
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