Tethering Codebug With Python

Introduction

Reach the full potential of your CodeBug with tethered mode. You can make programs that will be able to:

  • Respond and control Minecraft

  • Display Tweets on CodeBug

  • Control CodeBug from the web

  • Display alerts and the time on CodeBug

  • Create anything you can imagine in Python

  • Turn CodeBug into an Internet of Things device

To use CodeBug Tether you will need to load it with a special project file. Once this is done you can control CodeBug with Python over a USB connection.

You will need
CodeBug CodeBug
Micro USB cable
Computer

  • Ask a new question:

Set up Tethered mode

For tethered mode you need to load your CodeBug with a special project file. This is done in the same way as a regular CodeBug program (refer to the download guide for details).

Download the CodeBug tether project file and load it onto your CodeBug.

Next, you need to install some Python modules.


2 Questions
  • Link to github is to codebug_tether_v0.5.0.cbg witch has bugs . Correct please to codebug_tether_v0.6.0. Márton Kovács @ May 9, 2016, 1:17 p.m.


    Answer this question:

  • -1

    Link to github is broken. this works https://github.com/codebugtools/codebug_tether/tree/master/firmware Aino @ Dec. 7, 2015, 9:17 p.m.


    Thanks, we have updated the link Thomas Macpherson-Pope @ Dec. 8, 2015, 10:04 a.m.

    Answer this question:

  • Ask a new question:

Python libraries for Raspberry Pi & Linux

You must now install the Python libraries that will talk to your CodeBug. Python should already be installed but for good measure:

sudo apt-get install python3

To install pip, securely download get-pip.py, then run the following:

python get-pip.py

To install codebug_tether, open up a terminal and type:

pip install codebug_tether

8 Questions
  • How do I do this on Windows? Jim Evans @ Aug. 29, 2015, 8:22 p.m.

    • Not sure about this, but the code is not working on my Raspberry Pi - The bit that says pip3 needs to read pip-3.2 or it won't work — stevefoster @ Sept. 6, 2015, 8:37 a.m.

    • This bit is done on the Pi, not Windows. — Tony Goodhew @ Oct. 5, 2015, 3:02 p.m.

    • I cannot find the "bit that says pip3" anywhere. However here is what to do on ubuntu sudo apt-get install python3-pip sudo pip3 install codebug-tether — Roger James @ Oct. 15, 2015, 6:06 p.m.

    • You need to install python (I used 3.4 with pip included), then installed the codebug_tether module (pip install codebug_tether) and which depends on the serial (=pyserial) module (pip install pyserial). Then there is a small modification to the instructions as the codebug_tether.CodeBug() will not automatically detect the codebug in windows, you need to find the number of your virtual COM port (under device manager look under Ports (COM & LPT) and change initialisation line in your program to: codebug_tether.CodeBug('COM3') #in my case it was COM3 — cagedev @ Oct. 21, 2015, 8:45 p.m.

    • cagedev’s tips also work for Mac OS X – since you don’t have `apt-get` you can just `pip install` the two packages as he describes. There’s no need to run `sudo python` – plain `python` will do. And as with Windows, you will need to specify the path to the codebug device manually, by following the steps in the "Accessing your CodeBugs" section below – eg: codebug_tether.CodeBug('/dev/tty.usbmodemfd121') — Zarino Zappia @ Oct. 31, 2015, 5:32 p.m.


    Hello. This was posted on the 29/08. It would be useful if the people behind codebug would answer. Most schools do not use Linux. michaelpjones @ Oct. 28, 2015, 10:17 a.m.
    • I agree. I'm looking at the CodeBug for use in schools, but we run Windows. Most schools don't use Linux. Is it possible to tether CodeBug with Windows please? Thanks. — gadgetgurl42 @ Oct. 29, 2015, 6:50 a.m.


    Answer this question:

  • Are codebug libraries only available for Python3? Anthony Harrison @ Sept. 12, 2015, 9:55 p.m.


    Yes only Python 3. This makes it easier to manage the software development and documentation. Python 3 is always becoming more supported and Python 2 less. Thomas Macpherson-Pope @ Sept. 15, 2015, 10:04 a.m.

    E: unable to locate package python3-codebug-tether i get this error message Andreas Kopp @ Sept. 18, 2015, 1:07 p.m.
    • You might need to install via pip - I didn't have the right repos on my Ubuntu laptop either. — Jim Evans @ Sept. 19, 2015, 3:17 p.m.

    • I got this working on Mint (pretty much the same as Ubuntu) by just copying the codebug_tether folder into the folder I am running python in. See here: http://codebug-tether.readthedocs.org/en/latest/installation.html#using-codebug-tether-without-installing. This page also explains how to install via pip. — John Wilkes @ Sept. 26, 2015, 1:15 p.m.


    After running `pip install codebug-tether` on my Mac, I've got codebug-tether version 0.4.3 working fine under Python 2.7.9 so maybe it doesn't need Python3? Zarino Zappia @ Oct. 31, 2015, 5:35 p.m.

    My Mac (El Capitan) is running Python 2.7.10 and codebug_tether 0.4.3, pyserial 2.7 work fine (my serial port is /dev/tty.usbmodem1411). Simon Wright @ Nov. 7, 2015, 3:33 p.m.

    Answer this question:

  • Where is the best place to get support on this? I'm stuck with an error I don't understand as I'm a Python novice. Jim Evans @ Sept. 19, 2015, 3:18 p.m.

    • What is the error? — Tony Goodhew @ Oct. 5, 2015, 3:02 p.m.


    Answer this question:

  • Is it possible to tether CodeBug to Windows? Given Python is cross-platform, surely there must be a way? Where can we get the necessary Python libraries to install - I'd love to try this out and happy to contribute if successful. Thx Jenny @ Oct. 27, 2015, 10:08 a.m.

    • Hi Jenny - looks like some basic instructions in answer to the question here: http://www.codebug.org.uk/learn/step/399/python-libraries-for-raspberry-pi-linux/#answer29 — Jim Evans @ Oct. 28, 2015, 10:36 a.m.

    • Hi Jim, these instructions assume a Linux OS, but I'm using Windows. — Jenny @ Oct. 29, 2015, 7 a.m.


    Answer this question:

  • Are you planning to support Windows and Python? michaelpjones @ Oct. 28, 2015, 10:18 a.m.

    • Please, please, please! :) If Windows is supported, lots of schools could use CodeBug in classrooms. Thanks. — Jenny @ Oct. 29, 2015, 7:02 a.m.


    We plan to support Windows and Mac in tethered mode Thomas Macpherson-Pope @ April 14, 2016, 11:19 a.m.

    Answer this question:

  • Hi, I have the Codebug connected to a Pizero and I am using version 0.7 of the tether cbg file. I'm working though the commands at http://codebug-tether.readthedocs.org/en/latest/example.html The follwoing commands work:- codebug.set_row(0, 5) codebug.set_row(1, 0x1a) codebug.set_row(2, 0b10101) However, the following commands fail:- codebug.set_pixel(2, 1, 1) (TypreError: 'bytes' object cannot be interpreted as an integer) codebug.clear() (TypreError: 'list' object cannot be interpreted as an integer) Can you please advise? Thanks Andrew — mamvcivm @ April 10, 2016, 4:05 p.m.


    Answer this question:

  • Tonight I've tried to install python3-codebug-tether on an Ubuntu 15.10 laptop but I get the following error:- ~$ sudo apt-get install python3-codebug-tether Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package python3-codebug-tether This is after ensuring updates have been installed. Can you please advise... Thanks — mamvcivm @ April 11, 2016, 10:17 p.m.


    Currently only available in apt-get for Raspbian, please follow these instructions for installing on Ubuntu with pip: http://codebug-tether.readthedocs.org/en/latest/installation.html we will update this activity shortly. Thomas Macpherson-Pope @ April 14, 2016, 11:16 a.m.
    • Hi Thomas - this works :-) Thanks! — mamvcivm @ April 16, 2016, 9:25 p.m.


    Answer this question:

  • I'm running this on an Odroid XU-4 (arm cpu) with Ubuntu 16.04; I'm getting the following error: >>> import codebug_tether Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.5/dist-packages/codebug_tether/__init__.py", line 1, in <module> from codebug_tether.core import (CodeBug, File "/usr/local/lib/python3.5/dist-packages/codebug_tether/core.py", line 3, in <module> import serial ImportError: No module named 'serial' John Wiggins @ April 9, 2017, 7:27 a.m.

    • fixed by running "pip3 install pyserial" — John Wiggins @ April 9, 2017, 7:29 a.m.


    Answer this question:

  • Ask a new question:

Python libraries for Windows

Download and install the latest version of Python 3 from here. Make sure you tick the Add Python 3 to environment variables checkbox.

To install codebug_tether, open up a command prompt and type:

pip install codebug_tether

9 Questions
  • why wont it work sonic_iron @ Aug. 1, 2016, 3:27 p.m.


    Answer this question:

  • Please clean up the Linux documentation to reflect reality -or- delete it all and point to the github page. This worked for me on a Debian-type Linux: sudo apt-get update sudo apt-get upgrade sudo apt-get install python3 ( Download https://bootstrap.pypa.io/get-pip.py to get pip3 source code ) sudo python3 get-pip.py sudo pip3 install codebug_tether sudo apt-get install python3-serial ( Verify ) python3 >>> import codebug_tether >>> codebug = codebug_tether.CodeBug() >>> codebug.set_pixel(2, 2, 1) Richard Elkins @ Oct. 26, 2016, 7:49 p.m.

    • It isn't linux it is windows — OrlaaniHUN @ Jan. 8, 2018, 6:44 p.m.

    • It isn't linux it is windows — OrlaaniHUN @ Jan. 8, 2018, 6:44 p.m.

    • It isn't linux it is windows — OrlaaniHUN @ Jan. 8, 2018, 6:44 p.m.

    • It isn't linux it is windows — OrlaaniHUN @ Jan. 8, 2018, 6:44 p.m.

    • It isn't linux it is windows — OrlaaniHUN @ Jan. 8, 2018, 6:44 p.m.


    Answer this question:

  • Why it not working? OrlaaniHUN @ Jan. 8, 2018, 6:43 p.m.


    Answer this question:

  • Why it not working? OrlaaniHUN @ Jan. 8, 2018, 6:43 p.m.


    Answer this question:

  • Why it not working? OrlaaniHUN @ Jan. 8, 2018, 6:43 p.m.


    Answer this question:

  • Why it not working? OrlaaniHUN @ Jan. 8, 2018, 6:43 p.m.


    Answer this question:

  • Why it not working? OrlaaniHUN @ Jan. 8, 2018, 6:43 p.m.


    Answer this question:

  • Why it not working? OrlaaniHUN @ Jan. 8, 2018, 6:43 p.m.


    Answer this question:

  • Why it not working? OrlaaniHUN @ Jan. 8, 2018, 6:43 p.m.


    Answer this question:

  • deleted


    Answer this question:

  • deleted


    Answer this question:

  • deleted


    Answer this question:

  • Ask a new question:

Python libraries for MaxOSX

Download and install Xcode (if you haven’t already) and then enable the command line tools by running (in a terminal):

xcode-select --install

Now install Homebrew (a package manager for OSX):

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

The script will explain what changes it will make and prompt you before the installation begins. Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file:

export PATH=/usr/local/bin:/usr/local/sbin:$PATH

Now, we can install Python 3:

brew install python3

This will take a minute or two.

To install codebug_tether, open up a terminal and type:

pip install codebug_tether

1 Question
  • For "pip install codebug_tether", Terminal says "pip install codebug_tether -bash: pip: command not found" How can I fix this? Charlie McMahon @ June 5, 2017, 6:08 a.m.


    Please Help Charlie McMahon @ June 5, 2017, 6:14 a.m.

    Answer this question:

  • Ask a new question:

Python tethered programs from Blockly emulator

If you are not confident with programming in Python or you have a CodeBug Blockly project you want to use in tethered mode, you can generate tethered Python from Blockly code.

On the create page, drag blocks to create a repeating scrolling message program in Blockly and then test it with the emulator. Now click the Python tab just above the Blockly workspace.

Left-click and drag the highlighter box around all of the Python code and then right-click and select Copy.

Now open a text editor (such as Notepad, Leafpad, TextEdit or IDLE) and Right-click and paste the code into the blank document. Save this as "codebug-tether-test.py" in your home directory.

Open a Terminal and type:

python3 codebug-tether-test.py

You will see your message scrolling on your tethered CodeBug. In your Terminal window, hold Ctrl C to stop the program.


  • Ask a new question:

Your own tethered CodeBug programs

You can write your own tethered CodeBug programs using Python and a few simple commands to control your CodeBug. In the next steps you will start an interactive Python session and enter commands to interact with your tethered CodeBug. Don’t forget you need the tethered mode project installed on your CodeBug for them to work.

Open a Terminal and type:

sudo python3

You will see the python prompt appear >>> Now type:

import codebug_tether
import codebug_tether.sprites

cb = codebug_tether.CodeBug()

cb.set_pixel(2, 2, 1)

You will see the center LED - at position (2, 2) - light up on CodeBug.

NB: On some computers, or if you have multiple CodeBugs you will have to put the address of your CodeBug in brackets. More details are in the section Addressing Your CodeBug.

Now try setting a row of CodeBug’s LEDs at the same time:

cb.set_row(3, 0b10100)

You will see the third row of LEDs light up in the sequence you gave. 0b10101 is a binary value. The 0b shows that it is binary and the 10100 determines which bits (or LEDs in our case) are on. 1 represents on and 0 represents off. This means that the LED to the far left is on (column 0), the next LED is off, the next on (column 2), and the final two LEDs are off.

Write text on the CodeBug’s LEDs, using the commands:

cbmessage = cb.sprites.StringSprite(("A")
cb.draw_sprite(0, 0, cbmessage)

An A will appear on the CodeBug LEDs.

To write scrolling text on CodeBug’s LEDs, first import the time module:

import time

Now you can scroll text using the commands:

cbmessage = cb.sprites.StringSprite('Hello')
for i in range(0,-30,-1):

    codebug.draw_sprite(i, 0, cbmessage)

    time.sleep(.1)

The text "Hello" will be scrolled on CodeBug’s display.

Check whether a button is pressed, by giving get_input either an 'A' or a 'B':

cb.get_input('A')

This will return True if the button is pressed, otherwise it will return False.

Get a full list of the commands available by typing:

help(cb)

Press Crtl D to exit Python. You can write longer programs for tethered mode CodeBug by writing commands in your text editor and then saving and running the file in the way you did with the examples earlier.

Tethered mode gives your CodeBug access to the full computing power, functionality and network connectivity of your computer! You can use variety of powerful yet easy to use Python modules allow your CodeBug to generate random numbers, react to emails or even respond to Twitter.


6 Questions
  • Really good activity - thanks! Can we write C code to download and run on the codebug? John Wilkes @ Sept. 26, 2015, 1:20 p.m.


    Thanks for the feedback :) You can't program CodeBug in C, and do to the nature of CodeBug we haven't created any C tethered libraries, however if you really wanted to control CodeBug with C tethered, you could inspect the Python libraries to create a C library. Thomas Macpherson-Pope @ Sept. 28, 2015, 1:24 p.m.

    Answer this question:

  • Where is the source to the ic2_tether program, please? Steve Holden @ Nov. 24, 2015, 10:41 a.m.

    • Do you mean the i2c program that is loaded onto the bug or that is run in python? — Thomas Macpherson-Pope @ Nov. 30, 2015, 2:36 p.m.



    Answer this question:

  • cb.write_text(0, 0, "A") produces AttributeError: 'CodeBug' object has no attribute 'write_text'. Why. Also write_text is not listed in the help file. Jeremy Flooks @ Dec. 26, 2015, 9:01 p.m.

    • Same here, can not find write_text function in the source. — kushaldas @ Jan. 8, 2016, 8:06 p.m.

    • code should read: cbmessage = codebug_tether.sprites.StringSprite("A") cb.draw_sprite(0,0,cbmessage) — crash.over.ride @ Feb. 17, 2016, 3:34 p.m.


    code should read: cbmessage = codebug_tether.sprites.StringSprite("A") cb.draw_sprite(0,0,cbmessage) — crash.over.ride @ Feb. 17, 2016, 3:38 p.m.
    • I get the error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3/dist-packages/codebug_tether/core.py", line 319, in draw_sprite self.set_bulk(0, bytes(cb_rows)) File "/usr/lib/python3/dist-packages/codebug_tether/serial_channel_device.py", line 114, in set_bulk len(values)) + bytes((values,))) TypeError: 'bytes' object cannot be interpreted as an integer — John Wiggins @ July 14, 2017, 3:07 p.m.


    Answer this question:

  • Hi there - making progress here - I've used pip3.exe install pyserial codebug_tether to install the APIs on my Win10 PC, however when I go through the exercise above I get the following:- Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import codebug_tether >>> import codebug_tether.sprites >>> cb = codebug_tether.CodeBug() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Program Files (x86)\Python 3.5\lib\site-packages\codebug_tether\core.py", line 81, in __init__ super(CodeBug, self).__init__(serial.Serial(serial_port, timeout=2)) File "C:\Program Files (x86)\Python 3.5\lib\site-packages\serial\serialwin32.py", line 31, in __init__ SerialBase.__init__(self, *args, **kwargs) File "C:\Program Files (x86)\Python 3.5\lib\site-packages\serial\serialutil.py", line 180, in __init__ self.open() File "C:\Program Files (x86)\Python 3.5\lib\site-packages\serial\serialwin32.py", line 62, in open raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError())) serial.serialutil.SerialException: could not open port '/dev/ttyACM0': FileNotFoundError(2, 'The system cannot find the path specified.', None, 3) By the way, the Codebug is connected to COM6 according to Control Panel/Devices and Printers Can you please advise? Thanks — mamvcivm @ April 16, 2016, 9:41 p.m.


    Answer this question:

  • >>> cb=codebug_tether.CodeBug() Traceback (most recent call last): File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 275, in open self.fd = os.open(self.portstr, os.O_RDWR|os.O_NOCTTY|os.O_NONBLOCK) FileNotFoundError: [Errno 2] No such file or directory: '/dev/ttyACM0' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.5/dist-packages/codebug_tether/core.py", line 84, in __init__ super(CodeBug, self).__init__(serial.Serial(serial_port, timeout=2)) File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 261, in __init__ self.open() File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 278, in open raise SerialException("could not open port %s: %s" % (self._port, msg)) serial.serialutil.SerialException: could not open port /dev/ttyACM0: [Errno 2] No such file or directory: '/dev/ttyACM0' hyperobjects @ Dec. 26, 2016, 8:53 p.m.


    Answer this question:

  • I tried to paste code from the Python screen from the CodeBug website but when I try to run I get: Traceback (most recent call last): File "example.py", line 2, in <module> from codebug_tether import (IO_DIGITAL_OUTPUT, ImportError: cannot import name 'IO_ANALOG_INPUT' John Wiggins @ July 14, 2017, 5:43 p.m.


    Answer this question:

  • Ask a new question:

Addressing Your CodeBugs

The CodeBug tether library has a default address for CodeBug. In most cases this will work, however if you have multiple CodeBugs connected you may need to change the address you’re using.

To do this, first open a Terminal and type the commands:

ls /dev/tty*

Plug the CodeBug back into your computer’s USB port and wait a couple of seconds. Then type the command:

ls /dev/tty*

See what address has been added to the list (you should see something like ttyACM0 for Raspberry Pi or tty.usbmodemfd141 for Mac). You may find it easier if you unplug your other USB devices first.

When you initiliase CodeBug in your Python programs, you must pass it the address you have found.

e.g.

cb = codebug_tether.CodeBug('/dev/tty.usbmodemfa141')

1 Question
  • Is there a way to get this address from the terminal in Raspian? It's just that once this is plugged onto the Pi and find you can't address it, it is then a faff to shutdown the Pi, plug it into a PC/Mac and then put it back on the Pi again. — stevefoster @ Sept. 6, 2015, 8:46 a.m.


    I think I may have answered my own question....I guess I can try shutting down the Pi, plugging the codebug into a USB port on the Pi and then re-booting. Having done the above, I can then shut down the Pi and put the CB back on the GPIO pins. I'll give it a go... — stevefoster @ Sept. 6, 2015, 9:06 a.m.

    Answer this question:

  • Ask a new question:

Back to top