Skip to content

Instantly share code, notes, and snippets.

@IdrisCytron
Created August 11, 2020 06:13
Show Gist options
  • Save IdrisCytron/29776d181b0262264c2da55ef80464a5 to your computer and use it in GitHub Desktop.
Save IdrisCytron/29776d181b0262264c2da55ef80464a5 to your computer and use it in GitHub Desktop.
Control LED on EDU:BIT Using IoT Blynk App and Raspberry Pi
from microbit import *
import music
music.play(["C4:1", "G4:1"])
pot_value = 0
pot_prev = 0
data_in = ''
while True:
# Read serial uart data from Raspberry Pi and control LEDs
try:
data_in = uart.read()
#display.scroll(data_in)
pin16.write_digital(int(data_in) & 1)
pin15.write_digital((int(data_in) & 2) >> 1)
pin14.write_digital((int(data_in) & 4) >> 2)
except:
pass
from gpiozero import LED, Button, Buzzer
from time import sleep
import serial
import BlynkLib
# Replace with your microbit port number
PORT = "/dev/ttyACM0"
BAUD = 115200
s = serial.Serial(PORT)
s.baudrate = BAUD
s.parity = serial.PARITY_NONE
s.databits = serial.EIGHTBITS
s.stopbits = serial.STOPBITS_ONE
s.timeout = 1
s.reset_input_buffer()
BLYNK_AUTH = 'Your Auth Blynk Token'
blynk = BlynkLib.Blynk(BLYNK_AUTH)
# Register virtual pin handler
@blynk.VIRTUAL_WRITE(0)
def v0_write_handler(value):
global ledStatus
global led1State
led1State = int(value[0])
print("V0: {}".format(led1State))
if led1State == 1:
ledStatus = ledStatus + 1
else:
ledStatus = ledStatus - 1
@blynk.VIRTUAL_WRITE(1)
def v1_write_handler(value):
global ledStatus
global led2State
led2State = int(value[0])
print("V1: {}".format(led2State))
if led2State == 1:
ledStatus = ledStatus + 2
else:
ledStatus = ledStatus - 2
@blynk.VIRTUAL_WRITE(2)
def v2_write_handler(value):
global ledStatus
global led3State
led3State = int(value[0])
print("V2: {}".format(led3State))
if led3State == 1:
ledStatus = ledStatus + 4
else:
ledStatus = ledStatus - 4
led1State = 0
led2State = 0
led3State = 0
ledStatus = 0
prevLedStatus = 1
blynk.virtual_write(0, 0)
blynk.virtual_write(1, 0)
blynk.virtual_write(2, 0)
while True:
blynk.run()
if ledStatus != prevLedStatus:
print("LED Status: {0:03b}".format(ledStatus))
s.write(str(ledStatus).encode('UTF-8'))
prevLedStatus = ledStatus
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment