Skip to content

Instantly share code, notes, and snippets.

@kikeelectronico
Last active August 11, 2022 12:17
Show Gist options
  • Save kikeelectronico/67cdacd4fd42068a429ddf833876496f to your computer and use it in GitHub Desktop.
Save kikeelectronico/67cdacd4fd42068a429ddf833876496f to your computer and use it in GitHub Desktop.
Control a Zowi Army
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Un ejército de Zowi.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyPAvY2UggNaWlf78F81OKtn",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/kikeelectronico/67cdacd4fd42068a429ddf833876496f/un-ej-rcito-de-zowi.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Control a Zowi army\n",
"\n",
"This notebook allows you to control an army of Zowi robots. It should be run on a machine with Bluetooth and all the robots should be pair with the machine manually before trying to connect."
],
"metadata": {
"id": "n8daWIsGHszD"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "iOBtvJLscDb6"
},
"outputs": [],
"source": [
"import bluetooth\n",
"import time\n",
"import os\n",
"\n",
"target_name = \"Zowi\"\n",
"target_addresses = []\n",
"sockets = []"
]
},
{
"cell_type": "markdown",
"source": [
"## Find all the Zowis\n",
"\n",
"Find and connect to every single Zowi within your reach"
],
"metadata": {
"id": "XMaP2OV5H6GJ"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "kDSQPLLqLb7n"
},
"outputs": [],
"source": [
"print(\"Scanning\")\n",
"\n",
"nearby_devices = bluetooth.discover_devices(duration=10)\n",
"\n",
"for address in nearby_devices:\n",
" name = bluetooth.lookup_name( address )\n",
" print(address, name)\n",
" if name == target_name and not address in target_addresses:\n",
" target_addresses.append(address)\n",
"\n",
"print(\"\\n\")\n",
"print(\"Found\", len(target_addresses), \"Zowis\")\n",
"print(target_addresses)\n"
]
},
{
"cell_type": "code",
"source": [
"sockets = []\n",
"for address in target_addresses:\n",
" try:\n",
" sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)\n",
" sock.connect((address, 1))\n",
" sockets.append(sock)\n",
" time.sleep(1)\n",
" except:\n",
" print(\"Fail to connect to\", address)"
],
"metadata": {
"id": "njS42WE312Gy"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Control"
],
"metadata": {
"id": "axGRFfbIJST8"
}
},
{
"cell_type": "markdown",
"source": [
"### Go forward"
],
"metadata": {
"id": "3_eFCQAGLRVv"
}
},
{
"cell_type": "code",
"source": [
"trama = [77,32,49,32,49,48,48,48,32,13,10]\n",
"for sock in sockets:\n",
" for b in trama:\n",
" sock.send(chr(b))\n",
"\n",
"for sock in sockets:\n",
" response = sock.recv(100)\n",
" print(response)"
],
"metadata": {
"id": "mM3odrnRIMqC"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Turn LED matrix off"
],
"metadata": {
"id": "eKmaleA6Jh9A"
}
},
{
"cell_type": "code",
"source": [
"trama = \"L 000000000000000000000000000000 \\r\\n\"\n",
"for sock in sockets:\n",
" for b in trama:\n",
" sock.send(b)\n",
"\n",
"for sock in sockets:\n",
" response = sock.recv(1024)\n",
" print(response)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "NqkNLfLtPDGA",
"outputId": "67fab6f2-1c32-42d4-a92f-a4e113a1729c"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"b'&&F%%\\r\\n'\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"### Lean left"
],
"metadata": {
"id": "_nVqhlMiPBz3"
}
},
{
"cell_type": "code",
"source": [
"trama = \"M 15 1000 \\r\\n\"\n",
"for sock in sockets:\n",
" for b in trama:\n",
" sock.send(b)\n",
"\n",
"for sock in sockets:\n",
" response = sock.recv(100)\n",
" print(response)"
],
"metadata": {
"id": "iev8o1sdsjOa"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Make sounds"
],
"metadata": {
"id": "9_e8AyI8PiQW"
}
},
{
"cell_type": "code",
"source": [
"trama = \"H 7 \\r\\n\"\n",
"for sock in sockets:\n",
" for b in trama:\n",
" sock.send(b)\n",
"\n",
"for sock in sockets:\n",
" response = sock.recv(100)\n",
" print(response)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "-caTbcVqQ5S9",
"outputId": "2771164b-a0c3-44b7-81a5-eb8cc077fc32"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"b'&&A%%\\r\\n'\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"### Check battery"
],
"metadata": {
"id": "KJt327QQQ4E1"
}
},
{
"cell_type": "code",
"source": [
"trama = \"B \\r\\n\"\n",
"for sock in sockets:\n",
" for b in trama:\n",
" sock.send(b)\n",
"\n",
"time.sleep(1)\n",
"for sock in sockets:\n",
" response = sock.recv(100)\n",
" print(response)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Xc8ZAYvXPhU-",
"outputId": "7941ea7b-cc4a-4e2f-d352-f7d158f18cc5"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"b'&&B 93.54%%\\r\\n'\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"### Stop"
],
"metadata": {
"id": "MIg4VqlKLTcS"
}
},
{
"cell_type": "code",
"source": [
"trama = [53,13,10]\n",
"for sock in sockets:\n",
" for b in trama:\n",
" sock.send(chr(b))"
],
"metadata": {
"id": "GMk_gHynLGPz"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Close the connections"
],
"metadata": {
"id": "mll-yDFChIHV"
}
},
{
"cell_type": "code",
"source": [
"for sock in sockets:\n",
" sock.close()"
],
"metadata": {
"id": "hwHfvjC2hGdx"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment