Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5103

MicroPython • Why does this code only work over USB connected to Thonny?

$
0
0
This is really perplexing me. I'm trying to use a DS3231 RTC, get NTP time, go to sleep. Eventually I'd like to use the RTC to wake up the Pico, but I'm not there yet. Instead, I can't even get this basic code to run on a battery.

Running over USB while connected to Thonny: Success
Running with a CR123A plugged into VSYS/Ground: Fail
Running with USB cable plugged into a wall charger: Fail

I don't understand why it only works while running through Thonny.

I've tried switching boards, doing a flash nuke and starting from scratch, but no luck.

By "fail", I mean that it gets to the spot in the code section here before blinking the LED for an OSError:

Code:

try:        addr = socket.getaddrinfo(host, 123)[0][-1]        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)        s.settimeout(1)        res = s.sendto(NTP_QUERY, addr)        msg = s.recv(48)except OSError as e:        LED.off()        time.sleep(0.1)        LED.on()        time.sleep(0.1)        LED.off()        time.sleep(0.1)

Code:

from machine import Pinimport time#GPIO for LED and Power###########################################powerPin = Pin(2, Pin.OUT)powerPin.on()time.sleep(0.5)LED = Pin("LED", Pin.OUT)LED.on()###########################################from machine import I2C, SPI#from vl53l0x_nb import VL53L0Ximport networkimport urequests as requestsfrom ds3231 import DS3231import socketimport struct# check RTC for hard reset############################################RTCpowerPin.on()i2c = I2C(0,sda=Pin(16), scl=Pin(17))ds = DS3231(i2c)if ds.OSF():    hard_reset = True    print("Hard reset detected..")# else:    hard_reset = False    ds.check_alarm(1)    # WiFi config and connection###########################################SSID = "SSID"password = "password"wlan = network.WLAN(network.STA_IF)wlan.active(True)wlan.connect(SSID,password)time.sleep(2)max_wait = 10while max_wait > 0:    if wlan.status() < 0 or wlan.status() >= 3:        break    max_wait -= 1    # Internet time setup and set RTC#############################################NTP_DELTA = 2208988800 + 21_600host = "pool.ntp.org"   def set_time():    # Get the external time reference    NTP_QUERY = bytearray(48)    NTP_QUERY[0] = 0x1B    try:        addr = socket.getaddrinfo(host, 123)[0][-1]        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)        s.settimeout(1)        res = s.sendto(NTP_QUERY, addr)        msg = s.recv(48)    except OSError as e:        LED.off()        time.sleep(0.1)        LED.on()        time.sleep(0.1)        LED.off()        time.sleep(0.1)            finally:        s.close()    #Set our internal time    val = struct.unpack("!I", msg[40:44])[0]    tm = val - NTP_DELTA        t = time.gmtime(tm)    #print(t)    #rtc.datetime((t[0],t[1],t[2],t[6]+1,t[3],t[4],t[5],0))    datetime = ((t[0], t[1], t[2], t[3], t[4], t[5], 0))    ds.datetime(datetime)    print(ds.datetime())    # Example: Set alarm 1 for 16:10:15 every day ds.alarm1((15, 10, 16), match=ds.AL1_MATCH_HMS)    ds.alarm1((00, 00, 00), match=ds.AL1_MATCH_S)    ds.alarm_int(enable=True, alarm=1)    if hard_reset:    set_time()    else:    pass#     ds.check_alarm(1)#     ds.alarm_int(enable=True, alarm=1)#############################################postto = "https://ntfy.sh/alert123"   while True:        LED.on()    time.sleep(1)    try:         r = requests.post(postto, data = "job",headers={"Title": "ALERT: GARAGE DOOR OPEN","Priority": "5", "Tags": "rotating_light",})        r.close()        LED.off()        powerPin.off()        time.sleep(5)            except OSError as e:        pass                

Statistics: Posted by Emotion8490 — Thu Apr 25, 2024 12:05 am



Viewing all articles
Browse latest Browse all 5103

Trending Articles