Raspberry Pico W发送Pushbullet通知时遇adafruit_requests十进制整数语法错误
问题:Raspberry Pico W Pushbullet通知应用报错
ValueError: invalid syntax for integer with base 10 我用Raspberry Pico W开发了一款简易信箱开箱状态检测应用,通过Pushbullet发送通知。应用曾正常运行,但现在POST请求触发错误,GET请求可正常执行。
应用代码
import os import adafruit_requests import board import digitalio import time import adafruit_debouncer import wifi import ssl import ipaddress import socketpool quotes_url = "https://www.adafruit.com/api/quotes.php" def connect_to_wifi(): print("Connecting to WiFi") wifi.radio.connect(os.getenv('SSID'), os.getenv('PASSWORD')) print("Connected to WiFi") time.sleep(10) def send_notification(title, message): api_key = os.getenv('PUSHBULLET_API_KEY') url = "https://api.pushbullet.com/v2/pushes" headers = { "Authorization": "Bearer " + api_key, "Content-Type": "application/json" } data = { "type": "note", "title": title, "body": message } print(headers) pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) response = requests.get(quotes_url) print(response.content) response = requests.post(url, headers=headers, json=data) if response.status_code == 200: print("Notification sent successfully!") else: print("Failed to send notification.") def blink_led(): led = digitalio.DigitalInOut(board.LED) led.direction = digitalio.Direction.OUTPUT switch_pin = digitalio.DigitalInOut(board.GP21) switch_pin.direction = digitalio.Direction.INPUT switch_pin.pull = digitalio.Pull.UP switch_db = adafruit_debouncer.Debouncer(switch_pin) last_switch_state = False BLINK_ON_DURATION = 1000000000 # 1 second in nanoseconds BLINK_OFF_DURATION = 1000000000 # 1 second in nanoseconds LAST_BLINK_TIME = -1 while True: switch_db.update() if switch_db.fell: LAST_BLINK_TIME = -1 current_switch_state = switch_db.value if current_switch_state != last_switch_state: current_time = time.localtime() notification_time = "{}/{}/{} {}:{}:{}".format(current_time.tm_mday, current_time.tm_mon, current_time.tm_year, current_time.tm_hour, current_time.tm_min, current_time.tm_sec) #send_notification("Mailbox notification", notification_time + " - You (probably) got mail :)") send_notification("Mailbox notification", "You (probably) got mail :)") last_switch_state = current_switch_state now = time.monotonic_ns() if not current_switch_state and now >= LAST_BLINK_TIME + BLINK_OFF_DURATION: led.value = True LAST_BLINK_TIME = now if led.value and now >= LAST_BLINK_TIME + BLINK_ON_DURATION: led.value = False LAST_BLINK_TIME = now print("LED status:", led.value) print("Reed switch status:", current_switch_state) time.sleep(0.01) connect_to_wifi() blink_led()
错误日志
b'[{"text":"The most worth-while thing is to try to put happiness into the lives of others","author":"Robert Baden-Powell"}]' Traceback (most recent call last): File "code.py", line 94, in <module> File "code.py", line 76, in blink_led File "code.py", line 41, in send_notification File "adafruit_requests.py", line 715, in post File "adafruit_requests.py", line 677, in request File "adafruit_requests.py", line 183, in __init__ ValueError: invalid syntax for integer with base 10
解决方案
这个错误是adafruit_requests库在解析HTTP响应头时,尝试将非整数字符串转换为整数导致的。结合你的情况,可按以下步骤排查修复:
验证Pushbullet API密钥:检查
PUSHBULLET_API_KEY环境变量是否正确,确保没有多余空格、引号或换行符。可以直接替换代码中的os.getenv('PUSHBULLET_API_KEY')为实际密钥,临时测试是否是环境变量读取问题。测试Pushbullet API可用性:用curl手动发送POST请求,确认API服务正常:
curl -X POST https://api.pushbullet.com/v2/pushes \ -H "Authorization: Bearer 你的API密钥" \ -H "Content-Type: application/json" \ -d '{"type": "note", "title": "测试通知", "body": "测试内容"}'如果请求失败,说明是API本身或密钥问题;如果成功,则问题出在代码或库的兼容性上。
复用Session对象:不要每次调用
send_notification都创建新的Session,重复初始化可能引发异常。修改代码,将Session创建移到全局范围:# 在connect_to_wifi函数后添加全局Session初始化 pool = None requests = None def connect_to_wifi(): global pool, requests print("Connecting to WiFi") wifi.radio.connect(os.getenv('SSID'), os.getenv('PASSWORD')) print("Connected to WiFi") pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) time.sleep(10) def send_notification(title, message): global requests api_key = os.getenv('PUSHBULLET_API_KEY') url = "https://api.pushbullet.com/v2/pushes" headers = { "Authorization": "Bearer " + api_key, "Content-Type": "application/json" } data = { "type": "note", "title": title, "body": message } print(headers) # 移除原有的pool和requests创建代码 response = requests.post(url, headers=headers, json=data) if response.status_code == 200: print("Notification sent successfully!") else: print("Failed to send notification.")检查adafruit_requests版本:如果是库版本兼容问题,尝试更新到最新稳定版,或者回退到之前能正常运行的版本。
添加错误捕获:在
send_notification中捕获异常,打印更多细节,帮助定位问题:def send_notification(title, message): try: # 原函数内容 except ValueError as e: print(f"ValueError详情: {e}") # 若能获取到响应对象,可打印响应头进一步排查 if 'response' in locals(): print(f"响应头: {response.headers}")
内容的提问来源于stack exchange,提问作者Pitto
相关产品推荐
相关产品推荐

