#🔒 Raspberry / API Airtable

22 messages · Page 1 of 1 (latest)

quartz imp
#

I installed Raspberry OS to track planes using Dumper1090-FA (PiAware) and I need to connect the API to Airtable, but my Python code looks nothing like what other people are using. I honestly don’t know what’s causing it. I even asked ChatGPT already, but it was too dumb to help.

Please help me.

vague lichenBOT
#

@quartz imp

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

quartz imp
#
import requests
import json
import time

# -------------------------------
# Airtable Config
# -------------------------------
API_KEY = "Sensor"
BASE_ID = "Sencor"
TABLE_NAME = "Bangkok"
AIRTABLE_URL = f"https://api.airtable.com/v0/{BASE_ID}/{TABLE_NAME}"

# Headers
headers = {
    "Authorization": f"Bearer {API_KEY}",
    "Content-Type": "application/json"
}

# -------------------------------
# PiAware JSON URL
# -------------------------------
PIAWARE_URL = "http://127.0.0.1:8080/data/aircraft.json"

#

# -------------------------------
# Send to Airtable
# -------------------------------
def send_to_airtable(fields):
    payload = {
        "records": [
            {
                "fields": fields
            }
        ]
    }

    r = http://requests.post(AIRTABLE_URL, headers=headers, data=json.dumps(payload))
    if r.status_code in (200, 201):
        print("Uploaded:", fields.get("Hex"))
    else:
        print("Airtable ERROR:", r.status_code, r.text)

# -------------------------------
# Fetch ADS-B data
# -------------------------------
def fetch_data():
    try:
        r = requests.get(PIAWARE_URL, timeout=3)
        r.raise_for_status()
        data = r.json()
    except Exception as e:
        print("Error fetching:", e)
        return

    aircraft_list = data.get("aircraft", [])

    for ac in aircraft_list:
        hex_code = ac.get("hex")
        if not hex_code:
            continue

        callsign = (ac.get("flight") or "").strip() or "N/A"

        # -------------------
        # Fields Mapping
        # -------------------
        row = {
            "UniqueID": hex_code,
            "Callsign": callsign,
            "FlightNumberIATA": callsign,  # ถ้ามี API อื่น สามารถแยกได้
            "Hex": hex_code,

            # ค่าที่ PiAware ไม่มี → ให้ NONE ไว้ก่อน
            "Status": "In Flight" if ac.get("alt_baro") else "N/A",
            "Takeoff": None,
            "Landing": None,
            "FinalApproach": None,
            "Airport": "N/A",

            # คุณต้องกำหนด logic เองทีหลัง
            "Checking progress": False,
            "IsInbound": False,
            "IsOutbound": False,

            "Note": "Detected via PiAware"
        }

        send_to_airtable(row)

# -------------------------------
# Loop every 30s
# -------------------------------
def loop():
    while True:
        fetch_data()
        time.sleep(30)

loop()

#

Anyone help me fix this 😂

pallid lichen
#

what return code are you getting for the API?7

quartz imp
#

Tmr i can check

#

I hope you online 🥹

pallid lichen
#

cause in your code your not actually passing the errors to the log file

#

so you could just be getting api authentication errors and not know abuot it

#

use the loggin libary import logging as your code just currntly sends it to stdout 😉

#

it it just prints it but as its running on a process isntead of an interactive session you wont see anything

quartz imp
#

Tmr i can go office and I’m try 😂 thanks you I’m newbie

pallid lichen
#

try use thunderclient or curl to test your API keys. (you can use gpt to show you how to do that its a common question its asked)

#

you are also gonna commonly have to send it an agent ID as well

#

as most API servers demand an agent id, you can just take that and pretend your stuff is chrome/firefox

#

then you know your API key works,

#

id also include some rate limiting and support for http 304 (Not modifed)
thats a common way to not conusme more API calls, and not keep trying to recconect when there is no data updates

#

as for benig a noobie, thats cool buddy we were all there once and this is a great place to aks and improve yourself 😉

vague lichenBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.