Skip to content

Instantly share code, notes, and snippets.

@nickcjohnston
Created January 8, 2020 05:51
Show Gist options
  • Save nickcjohnston/f4c4dbd93481a2e70193743984f5cd85 to your computer and use it in GitHub Desktop.
Save nickcjohnston/f4c4dbd93481a2e70193743984f5cd85 to your computer and use it in GitHub Desktop.
################
# ARDUINO PART #
################
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10 //SDA
#define SCK_PIN 12
#define MOSI_PIN 13
#define MISO_PIN 11
MFRC522 mfrc522(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
void setup() {
Serial.begin(9600);
while (!Serial);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println("Setup complete. Ready to scan cards.");
}
void loop() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
// Get the basic card UID number and send it over the Serial port
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
delay(1000); //wait one second before we scan again
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? "0" : "");
Serial.print(buffer[i], HEX);
}
}
###############
# Python Part #
###############
import serial #pip install pyserial
import sys
import boto3 #pip install boto3
# Create Ubuntu instance on aws ec2
def create():
ec2 = boto3.resource('ec2', aws_access_key_id = access_key, aws_secret_access_key = secret_key)
instance = ec2.create_instances(ImageId='ami-0eb3e12d3927c36ef',
InstanceType='t2.micro',
KeyName='nick-python-central',
MinCount=1,
MaxCount=1)
print("Instance Created:")
instanceid = instance[0].id
print(instanceid)
# terminate all your ec2 instances
def destroy():
ec2 = boto3.client('ec2', aws_access_key_id = access_key, aws_secret_access_key = secret_key)
response = ec2.describe_instances()
for reservation in response["Reservations"]:
for instance in reservation["Instances"]:
ec2.terminate_instances(InstanceIds=[instance["InstanceId"]], DryRun=False)
print("Terminating instance:")
instanceid = instance["InstanceId"]
print(instanceid)
print("Starting")
print("Reading kwargs")
serial_device = sys.argv[1]
keys = sys.argv[2]
print("Reading aws keys")
lines = open(keys, "r").readlines()
access_key = lines[0].strip()
secret_key = lines[1].strip()
print("Binding functions to RFID cards")
functions = {
"5261F10E":create,
"52F7180E":destroy
}
# Read the card UID's over the USB port from the arduino.
print("Attaching to serial port")
with serial.Serial(serial_device, 9600, timeout=1) as ser:
while True:
line = ser.readline().decode("utf-8").strip()
if len(line) == 8:
print(line)
functions[line]()
elif len(line) > 0:
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment