Skip to content

CubeServer Snippets

Joseph Freeston edited this page May 3, 2023 · 5 revisions

CubeServer Snippets

These code snippets show examples for connecting to/communicating with the main server.

Import the servercom library

This is necessary before using features associated with server communications. (put this at the top of every program)

from servercom import *

Connection to the server

This demonstrates connecting and disconnecting from the server-

Creating the server connection:

connection = Connection()

This connects to the server over WiFi and immediately starts using a lot of power. To close this connection when unnecessary, use the following snippet:

connection.close()

Once the WiFi connection is closed, we will have to re-open it before doing anything, just as we opened it in the first place:

connection = Connection()

If you need the time for anything (you do)- run this occasionally

connection.sync_time()

Code Updates

You'll want to occasionally poll the server in case you wish to update the code on your cube remotely.

If there is no code update to download, execution of your program will continue after a brief pause. Otherwise, the new program will be downloaded, your old code will stop running, and the new code.py will start running from the beginning.

connection.code_update()

Posting "Data Points"

You can post a number of different things that will be publicly visible from the leaderboard.

Text Strings (no points associated)

connection.post(Text("Hello World!"))

Temperature values (points only awarded as per gameplay rules)

temp = 32.0
connection.post(Temperature(temp))

Barometric pressure values (points only awarded as per gameplay rules)

press = 32.0
connection.post(Pressure(press))

Beacon Challenge values- to be sent directly to the server for points as a response to something from a beacon message

connection.post(BeaconChallenge("Some text received from the beacon"))

Battery Level

current_voltage = 2.9
full_voltage = 3.1

fraction = current_voltage / full_voltage
connection.post(BatteryLevel(fraction * 100)) # Post percentage

Emails

Your cube can also send private emails home to all members of your team. There is a limited number of these messages that can be sent per day.

A basic example

# Form the email message:
my_msg = Email(
    "The Subject Line",
    (
        "Dear team,\n"
        "I am afraid I have failed you.\n"
        "My battery level is critically low, and by sending this message I am squandering what little remains.\n"
        "Thank you,\n"
        "Your Cube."
    )
)

# Send the email:
connection.email(my_msg)

Note the use of parentheses, quotes, and \ns to form a multi-line message body.

A more practical example:

# Here are some variables we want to make known to the team:
a = 42.5
b = "Hello!"

# Form the email message:
my_msg = Email(
    "The Values of Some Variables",
    f"Some values: variable a is {a}, variable b is {b}"
)

# Send the email:
connection.email(my_msg)

Such a snippet would use an f-string to send the following message:

Some Values: variable a is 42.5, variable b is Hello!

Getting Status

You can request the status from the server. This gives your code access to the current time (as the epoch time or unix timestamp) and your current score.

status = connection.get_status()
print(f"Current time: {status.time}")
print(f"Current Score: {status.score}")