All posts
Robotics7 min read

Drive a robot from your phone.

If a Python script is already publishing controls to your cognira account, the Android app can drive them. The Robotics tab appears when a robot is online. Each row is one function, with the control you gave it.

Get it on Google Play

§ 01

The script does not change

Install the SDK with pip install cognira-robotics, decorate the functions, and run the script. The first run prints a code and asks you to approve the robot on the website. After that, any signed-in session on the account can see it: the browser at cognira.dev/robotics, and the app.

desk_lamp.py

from cognira_robotics import Robot

robot = Robot("Desk lamp")

@robot.button("On")
def on():
    robot.log("lamp on")

@robot.hold("Nudge")
def nudge(pressed):
    robot.show("Nudging", pressed)

@robot.slider("Brightness", min=0, max=100, default=40, unit="%")
def brightness(value):
    robot.show("Level", value)

@robot.joystick("Aim")
def aim(x, y):
    robot.log(f"{x:.2f}, {y:.2f}")

@robot.on_stop
def stop():
    robot.log("stopped")

robot.run()

The phone does not run this file. The computer or the Pi running robot.run() keeps the outbound connection. The app sends a command, the SDK calls your function, and anything you robot.log() or robot.show() streams back.

§ 02

What you see for each decorator

Open the app, sign in, and start the script. Robotics joins the bar along the bottom. The header names the robot and says Online. Under “Controls from Python”, each control shows its label, a type, and the function, written the way it is called.

  • @robot.button is a Run button. One press, one call, no arguments.
  • @robot.hold stays down while your finger is on it. The app tells the script True at once and again every quarter of a second, then False when you lift.
  • @robot.toggle is a switch. It sends the new on or off.
  • @robot.slider is a track with minus and plus. Dragging sends the value as you move. The number under it includes the unit you set.
  • @robot.joystick is a pad. X and Y run from −1 to 1, with Y up. Letting go sends (0, 0). While you hold it, the position keeps being sent, so a dropped connection is a release.
  • @robot.text is a field and a send button. The string is the argument.

Controls you grouped in the script stay in those groups. Keyboard shortcuts from the website stay on the website. On the phone you use the control itself.

§ 03

Stop is the red button

STOP sits in the header, next to the robot’s name. It runs your @robot.on_stop function at once, including in the middle of another command, and the queue behind it is dropped. The same rule as the browser: a late command is thrown away rather than played into a robot that just came back.

If the live stream drops, the header says it is reconnecting and the app tries again on its own. Until that stream is live, the controls sit disabled. An offline robot can still be named, with when it was last seen.

§ 04

Readouts, the log, and a second robot

Values from robot.show() land under Readouts, label and value. Lines from robot.log() land in the log, newest first, with the time. That is how you check a slider did what the board says it did, which is the same check we used on the ESP32 servo.

Two scripts online means two chips under the title. Pick one. The stream follows the robot you are looking at, and it closes when you leave the tab. If every robot goes offline, Robotics drops out of the bar the next time you are on another tab. The empty state points back at the Robotics page for the setup.

The app is free on Google Play. The account that approved the robot is the account that drives it.