1

I am trying to build a simple notification application to detect when my letter box is opened and send a notification via pushbullet using Raspberry Pico W.

This is the application:

import os
import adafruit_requests
import board
import digitalio
import time
import adafruit_debouncer
import wifi
import ssl
import os
import ipaddress
import wifi
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()

It used to work fine but now I get:

ValueError: invalid syntax for integer with base 10

The first get request is just a test and works perfectly, the second one throws the error, here's a more complete log:

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

How can I solve the error?

3
  • 2
    What exactly do you mean by "first" and "second" get request? Commented Aug 10, 2023 at 18:21
  • @mkrieger1 Sorry I removed it, I just added it again. Commented Aug 10, 2023 at 20:30
  • It seems that the response of https://api.pushbullet.com/v2/pushes doesn't follow the http protocol. Commented Aug 11, 2023 at 4:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.