Dec. 23, 2019, 9:41 p.m.

Tooting to Mastodon with Python3 (bot)

Tooting to Mastodon using Python with API

I recently registered for a bot account on botsin.space. The purpose of this bot is to inform me events on my home server, whether the temperature is too high, power failure, service interruption and other stuff. But every hour it will toot the current uptime, CPU temperature, barometric pressure and temperature as well as the current problem of my server. I used the Mastodon.py to toot automatically using python. Here are the steps that I did:

  1. Register for a Mastodon account (Bot friendly instances)
  2. Create a new application API key for your Mastodon account (Edit profile > Development > New Application) (Set your permissions and copy your generated access token)
  3. Read the example code on https://mastodonpy.readthedocs.io/en/stable/
  4. Install Mastodon.py python3 -m pip install Mastodon.py and write the test script myscript.py, do not forget to edit api_base_url and access_token.
  5. Execute the script with python3 ./myscript.py

Code for getting the barometric pressure from my ESP8266 and other server info is still a WIP, stay tuned!

myscript.py source code:

1
2
3
4
5
6
7
8
#!/usr/bin/python
from mastodon import Mastodon

mastodon = Mastodon(
    access_token = 'MY-ACCESS-TOKEN-FROM-DEVELOPMENT-NEW-APPLICATION',
    api_base_url = 'https://botsin.space/'
)
mastodon.toot('My first toot from python using #mastodonpy !')

Modifying ESP_WNATS to submit our custom MQTT data

UPDATE: 12/24/2019

I now added the data from my ESP8266 MQTT Client for getting the BMP180 sensor data (temperature and pressure) as well as the current UNIX EPOCH time. To make things easier, I just added a code in my esp_wnats project (near line 790 in user_main.c):

uint8_t baro[256];
int32_t pressure = BMP180_GetPressure(OSS_0);
int32_t temp = BMP180_GetTemperature();
uint32_t current_stamp = sntp_get_current_timestamp();

if (current_stamp>0){
os_sprintf(baro, "EPOCH: %d TMP: %d.%d C BMP: %d.%d hPa", current_stamp, temp/10, temp%10,(int)(pressure/100), (int)(pressure%100));
} else{
os_sprintf(baro, "TMP: %d.%d C BMP: %d.%d hPa", temp/10, temp%10,(int)(pressure/100), (int)(pressure%100));
}

//MQTT Publish to mqtt_prefix/bmp
os_sprintf(buf, "%s/bmp", config.mqtt_prefix);
MQTT_Publish(client, buf, baro, os_strlen(baro), 0, 1);

Getting MQTT Data from ESP8266 with PAHO-MQTT

To read the data that we published using MQTT, we need to install a python package named paho-mqttpython3 -m pip install paho-mqtt

An example code from https://pypi.org/project/paho-mqtt/

Be sure to replace localhost to your Pi’s IP and WiFi/ESPRouter_XXXXX/system/bmp to your ESP8266 MQTT Topic.

import paho.mqtt.subscribe as subscribe

msg = subscribe.simple("WiFi/ESPRouter_XXXXX/system/bmp", hostname="localhost")
print("%s %s" % (msg.topic, msg.payload))

The output will be:

WiFi/ESPRouter_XXXXX/system/bmp EPOCH: 1577181443 TMP: 32.4 C BMP: 1010.79 hPa

We are now ready to toot this to our Mastodon bot instance.

You also need to install the MQTT package for your disto.

$ sudo apt install mosquitto mosquitto-client