Connect Microbit to Computer with Bluetooth or Wifi

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
vmcduarte
Posts: 5
Joined: Sat Dec 18, 2021 4:33 pm

Connect Microbit to Computer with Bluetooth or Wifi

Post by vmcduarte » Mon Jan 03, 2022 3:58 pm

Hello

Is it possible to connect the microbit to the computer with bluetooth or Wifi and then use the computer keyboard to control the microbit?

I mean, using the computer keyboard we have a big range of possible comands to send to the microbit.

If yes, can anyone explain how to do it please?

I searched only for any tutorial about this but found nothing about using the computer keyboard to control the microbit...

Thanks!

lujo
Posts: 21
Joined: Sat May 11, 2019 2:30 pm

Re: Connect Microbit to Computer with Bluetooth or Wifi

Post by lujo » Mon Jan 03, 2022 5:36 pm

Hi,

Microsoft MakeCode supports Bluetooth BLE on the micro:bit. MicroPython does not.
See: https://microbit-micropython.readthedoc ... cs/ble.htm

If I want to control a micro:bit from my laptop, I use a second micro:bit connected to the laptop.
What follows works with both V1 and V2 micro:bits.

On the laptop run this program (Linux laptop with Python installed):

Code: Select all

#!/usr/bin/python3

import serial
import time
from serial.tools.list_ports import comports as serial_ports
import sys

def find_microbit():

  ports = serial_ports()

  for port in ports:
    if "VID:PID=0D28:0204" in port.hwid:
      return port.device

  return None

port = find_microbit()

if port:
  print('\nMicro:bit found at port %s.\n' % port)
else:
  print('\nError: No micro:bit found.\n')
  sys.exit(1)

ser = serial.Serial(port=port, baudrate=115200, timeout=1)

while True:
    msg = input("Send message: ")
    if not msg:
        break
    else:
        msg = msg.encode("ascii")
        print(msg)
        ser.write(msg)
On the micro:bit connected to the laptop:

Code: Select all

from microbit import *
import radio

radio.config()
radio.on()
uart.init(115200)

while True:
    msg = uart.read()
    if msg:
        msg = str(msg, 'UTF-8')
        display.scroll(msg, delay=80, wait=False)
        radio.send(msg)
And on the microbit (with battery) you want to control remote:

Code: Select all

from microbit import *
import radio

radio.config()
radio.on()

while True:
    msg = radio.receive()
    if msg:
        display.scroll(msg, delay=80)
Now if you type something after the prompt on the laptop followed by Enter, the
text scrolls on both micro:bits. Use the messages to get commands from your laptop
to the remote micro:bit.

This is actually a project we did at our local CoderDojo sessions.

lujo

vmcduarte
Posts: 5
Joined: Sat Dec 18, 2021 4:33 pm

Re: Connect Microbit to Computer with Bluetooth or Wifi

Post by vmcduarte » Tue Jan 04, 2022 1:49 pm

Thanks a lot!

Post Reply