Skip to content

Instantly share code, notes, and snippets.

@dybber
Last active June 15, 2022 12:41
Show Gist options
  • Save dybber/ee60086ae31b12e0a06f0cb082e11e7c to your computer and use it in GitHub Desktop.
Save dybber/ee60086ae31b12e0a06f0cb082e11e7c to your computer and use it in GitHub Desktop.
Microbit til Airtable link
from microbit import *
while True:
if button_a.was_pressed():
print("BUTTON_A_PRESSED")
display.show(Image.SURPRISED)
elif button_b.was_pressed():
print("BUTTON_B_PRESSED")
display.show(Image.ASLEEP)
import requests
import json
from serial import Serial
# APIkey findes under Profil -> Account -> API
apiKey = "AIRTABLE API KEY SKRIVES HER"
# baseID findes under din base -> Help -> API documentation
baseID = "SKRIV AIRTABLE BASE ID HER"
# Tabelnavn (urlencoded, dvs. %20 er mellemrum)
tableName = "Table%201"
apiURL = "https://api.airtable.com/v0/" + baseID + "/" + tableName
headers = {"Authorization": "Bearer " + apiKey,
"Content-Type": "application/json"}
# Send
def post_data(cmd, args=[]):
data = {
"fields": {
"Command": cmd,
"Arguments": str(args),
}
}
json_string = json.dumps(data)
print(apiURL)
# hvis I vil hente data, så erstat requests.post med requests.get og
# og drop data-argumentet
response = requests.post(apiURL, headers=headers, data=json_string)
print(response.text)
def main():
# Connect to microbit
# Først argument er COM-port / serial-port, fx COM1 på Windows
serial = Serial("/dev/cu.usbmodem14344402", baudrate=115200)
while True:
line = serial.readline().decode("utf8")
l = line.split() # Split at whitespace
if len(l) > 0:
command = l[0]
print(command)
post_data(command)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment