You can send a toot to Mastodon using CircuitPython and your Raspberry Pi Pico W! This example provides a prompt through the serial console where you can type in your message, and then works with the Mastodon API to send your content as a toot.

This example will also run on any CircuitPython-compatible microcontroller board with native WiFi. No changes necessary!

Load the Example and Library

This example requires one external library. Luckily you can load the code and the necessary library together. Click the blue Download Project Bundle button above the code below to download the necessary library and the code.py file in a zip file.

Connect the Raspberry Pi Pico W to your computer via a known good data+power USB A to micro B cable. The Pico W should show up in your File Explorer or Finder (depending on Operating System) as a thumb drive named CIRCUITPY. Extract the contents of the zip file, and copy the entire lib folder, and the code.py file to your CIRCUITPY drive.

Your CIRCUITPY/lib folder should contain the following file:

  • adafruit_requests.mpy

Your contents of your CIRCUITPY drive should resemble the following:

CIRCUITPY

Example Code

# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
# SPDX-License-Identifier: MIT

import os
import ssl
import wifi
import socketpool
import adafruit_requests

# add your mastodon token as 'mastodon_token' to your settings.toml file
headers = {'Authorization': 'Bearer ' + os.getenv('mastodon_token')}

# add your mastodon instance to your settings.toml file as mastodon_host
url = 'https://' + os.getenv('mastodon_host') + '/api/v1/statuses'

# connect to SSID
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))

pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

# you'll be prompted in the REPL to enter your post text
post = input("Please enter your Mastodon post text: ")
# pack the post for sending with the API
post_text = {"status": post}

# confirm in the REPL that you want to post by entering y for yes or n for no
send_check = input("Send post to Mastodon (y/n)?")

# if you type y
if send_check == "y":
    # send to mastodon with a POST request
    r = requests.post(url, data=post_text, headers=headers)
    print()
    print("You posted '%s' to Mastodon. Goodbye." % post)
# if you type n
else:
    print("You did not post to Mastodon. Goodbye.")

Add Your settings.toml File

Remember to add your settings.toml file as described in the Create Your settings.toml File page earlier in the guide. You'll need to include your CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD, mastodon_host and mastodon_token in the file.

The mastodon_host variable should be assigned only the text part of the URL to your Mastodon instance. For example, 'mastodon.social' or 'fosstodon.org' without the https:// or a trailing /. 

CIRCUITPY_WIFI_SSID = "your-wifi-ssid-here"
CIRCUITPY_WIFI_PASSWORD = "your-wifi-password-here"

# Your host should be only the text part of the URL to your Mastodon instance.
# For example, 'mastodon.social' or 'fosstodon.org' without the https://. 
mastodon_host = "your-mastodon-instance-here"
mastodon_token = "your-mastodon-token-here"

Sending a Toot

Once everything is saved to CIRCUITPY, you'll want to connect to the serial console. You should see the following prompt:

Type in the content of your message.

When you're happy with your message, press enter. You'll be greeted with the following question:

This is included as another step between writing up your message and posting it. It gives you a chance to make sure your message is what you want it to be.

If you're happy with your message, and wish to post it, type y.

If you're not happy with your message, and you do not wish to post it, type n.

If you choose y, the code follows through with interacting with the API and  sends your message. Take a look at your Mastodon profile to see your new post! You'll also see the following in the serial console, where your message content will be the content of the message you sent.

If you choose n, it skips sending anything, and simply prints the following to the serial console:

That's all there is to sending a toot to Mastodon using CircuitPython and the Pico W!

Code Walkthrough

Here are some details of what's happening behind the scenes in this example.

Headers and URL

At the top of the code, your authentication token is packed into headers via your settings.toml file. This is included in your API POST request to gain access to your Mastodon account. url is your API request URL. It passes your Mastodon instance via your settings.toml file.

# add your mastodon token as 'mastodon_token' to your settings.toml file
headers = {'Authorization': 'Bearer ' + os.getenv('mastodon_token')}

# add your mastodon instance to your settings.toml file as mastodon_host
url = 'https://' + os.getenv('mastodon_host') + '/api/v1/statuses'

input() Your Post

The input() function is used to prompt you to enter your post text in the REPL. input() is blocking, so the code is paused while it awaits your text. After entering your post text, it is packed into the variable post_text which will be sent via the API.

Before sending the toot though, an additional input() is requested to confirm. If you type y, then the post will be tooted. If you type n, then the post will not be tooted. An if statement checks this logic. With y, a requests.post() request is sent to Mastodon, which includes the API URL, your post text and your authentication headers.

# you'll be prompted in the REPL to enter your post text
post = input("Please enter your Mastodon post text: ")
# pack the post for sending with the API
post_text = {"status": post}

# confirm in the REPL that you want to post by entering y for yes or n for no
send_check = input("Send post to Mastodon (y/n)?")

# if you type y
if send_check == "y":
    # send to mastodon with a POST request
    r = requests.post(url, data=post_text, headers=headers)
    print()
    print("You posted '%s' to Mastodon. Goodbye." % post)
# if you type n
else:
    print("You did not post to Mastodon. Goodbye.")

This guide was first published on Dec 06, 2022. It was last updated on Jul 20, 2024.

This page (Post to Mastodon) was last updated on Jul 20, 2024.

Text editor powered by tinymce.