Git Product home page Git Product logo

fake-ev3-bluetooth's Introduction

fake-ev3-bluetooth


Description

Implement the bluetooth messaging module of EV3 pybricks on normal python. Based on PyBluez

In the offical document of pybricks.messaging, it says:

An EV3 Brick can send information to another EV3 Brick using Bluetooth. This page shows you how to connect multiple bricks and how to write scripts to send messages between them.
...

Now you can also connect the EV3 brick and your computer, and let them send messages to each other.


Installation

This project is based on PyBluez. So you need to install it first.

pip install PyBluez

Then you just need to simply copy these files to your 'lib/' directory.


Usage

It's almost like the usage of the offical module for micropython. Here's a example from the offical document (comments modified).

Server:

# Before running this program, make sure the client and server EV3 bricks or 
# PC are paired using Bluetooth, but do NOT connect them. The program will
# take care of establishing the connection.

# The server must be started before the client!

from pybricks.messaging import BluetoothMailboxServer, TextMailbox

server = BluetoothMailboxServer()
mbox = TextMailbox('greeting', server)

# The server must be started before the client!
print('waiting for connection...')
server.wait_for_connection()
print('connected!')

# In this program, the server waits for the client to send the first message
# and then sends a reply.
mbox.wait()
print(mbox.read())
mbox.send('hello to you!')

Client:

from pybricks.messaging import BluetoothMailboxClient, TextMailbox

# This is the name of the remote EV3 or PC we are connecting to.
SERVER = 'ev3dev'

client = BluetoothMailboxClient()
mbox = TextMailbox('greeting', client)

print('establishing connection...')
client.connect(SERVER)
print('connected!')

mbox.send('hello!')
mbox.wait()
print(mbox.read())

Note: When you run it on your computer, it may fail with an OSError like this:

Traceback (most recent call last):
  File "<Your Program>", line 1, in <module>
    pybricks.messaging.BluetoothMailboxServer()
  File "C:\Projects\fake-ev3-bluetooth\pybricks\messaging.py", line 251, in __init__
    super(ThreadingRFCOMMServer, self).__init__(
  File "C:\Projects\fake-ev3-bluetooth\pybricks\bluetooth.py", line 97, in __init__
    self.socket.bind(server_address)
  File "C:\Program Files\Python38\lib\site-packages\pybluez_win10-0.22-py3.8-win-amd64.egg\bluetooth\msbt.py", line 72, in bind
    bt.bind (self._sockfd, addr, port)
OSError

The reason may be that you can't bind BluetoothSocket on port 1 (default). To solve this problem, add this to both the client and server code before instancing the BluetoothMailboxServer or BluetoothMailboxClient class:

import pybricks.messaging
pybricks.messaging.EV3_RFCOMM_CHANNEL = 20  # Modify the default port. Other port numbers should also work.

For more usage, you can read the offical document.

Sorry for my bad English.

fake-ev3-bluetooth's People

Contributors

kevinli-py avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.