Carlos ChacinSamreen IslamAlie Gonzalez
Published © GPL3+

MATRIX Voice and MATRIX Creator Running Chirp

Learn how to send data over sound with chirp on your MATRIX Creator and MATRIX Voice.

BeginnerProtip30 minutes3,443

Things used in this project

Story

Read more

Code

Receive.py

Python
Set the MATRIX LEDs to a valid color that's given through Chirp
from chirpsdk import CallbackSet, CHIRP_CONNECT_STATE, ChirpConnect
from matrix_lite import led
import time
import sys

# Create Chirp Instance
chirp = ChirpConnect()

# Print Chirp Status & Audio Transmission type
print(str(chirp))
print('Audio Transmission: {protocol} [v{version}]'.format(
protocol=chirp.protocol_name,
version=chirp.protocol_version))

# Select MATRIX Microphones
print(chirp.audio.query_devices()) # Show list of audio devices
chirp.audio.input_device = 3 # select MATRIX microphones (assuming index is 3)

## Set Chirp Event Listeners ##
class Callbacks(CallbackSet):
    # On Chirp Received
    def on_received(self, data, channel):
        # Read Chirp message
        if data is not None:
            # Change Color Of LEDs
            message = data.decode('utf-8')
            print('Changing LEDs to: ' + message)
            led.set(message)

        else:
            print('Decode failed')

    # On Chirp Being Received
    def on_receiving(self, channel):
        print('Receiving a Chirp! [ch{ch}]'.format(ch=channel))

chirp.set_callbacks(Callbacks())

# Start The Chirp Service
chirp.start(send=False, receive=True)

# Keep Listening For Chirps
try:
    while True:
        time.sleep(0.1)
        sys.stdout.write('.')
        sys.stdout.flush()

except KeyboardInterrupt:
    print('Exiting')

chirp.stop()

Send.py

Python
This script automatically toggles between sending Chirps with the message "red" and "green".
from chirpsdk import CallbackSet, CHIRP_CONNECT_STATE, ChirpConnect
from matrix_lite import led
import time

# Create chirp instance
chirp = ChirpConnect()

# Print Chirp status & audio transmission type
print(str(chirp))
print('Audio Transmission: {protocol} [v{version}]'.format(
protocol=chirp.protocol_name,
version=chirp.protocol_version))

## Set Chirp Event Listeners ##
class Callbacks(CallbackSet):
    # On Chirp Sent
    def on_sent(self, payload, channel):
        print('Chirp Sent: {data} [ch{ch}]'.format(data=list(payload), ch=channel))

    # On Chirp Being Sent
    def on_sending(self, payload, channel):
        print('Sending Chirp: {data} [ch{ch}]'.format(data=list(payload), ch=channel))

chirp.set_callbacks(Callbacks())

# Start The Chirp Service
chirp.start(send=True, receive=False)

# Continuously Send Chirp Messages
counter = 0
color = ""

try:
    while True:
        # Toggle Color
        if counter%2 == 0: color = "red"
        elif counter%2 == 1: color = "green"
        counter += 1

        # Send Chirp With Color
        message = chirp.new_payload(color.encode('utf-8'))
        chirp.send(message)
        time.sleep(2)

except KeyboardInterrupt:
    print('Exiting')

chirp.stop()

Credits

Carlos Chacin

Carlos Chacin

23 projects • 49 followers
Software Developer
Samreen Islam

Samreen Islam

21 projects • 76 followers
Alie Gonzalez

Alie Gonzalez

21 projects • 64 followers
✨ Creative Technologist 💡 Arm Innovator 💼 Formerly: SparkFun, MATRIX Labs, & AdMobilize

Comments