Last active
June 15, 2022 12:41
-
-
Save dybber/ee60086ae31b12e0a06f0cb082e11e7c to your computer and use it in GitHub Desktop.
Microbit til Airtable link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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