Feeds:
Posts
Comments

Archive for the ‘flask’ Category

This is my first python program ever, so bare with me if it’s not perfect.
This allows you to execute commands to mirobo so you can interact with the Xiaomi vacuum robot.

I use this small program to start/stop the robot, get status from it, etc from my (old) wall mounted tablet that runs Imperihome.

Here is the code:

# app.py file:

from flask import Flask, request
import subprocess
import logging

app = Flask (__name__)

log = logging.getLogger(‘werkzeug’)
log.setLevel(logging.ERROR)

@app.route(‘/mirobo’)
@app.route(‘/mirobo/status’)
def status():
command = “status”
return executeMiroboCommand(command)

@app.route(‘/mirobo/consumables’)
def consumables():
command = “consumables”
return executeMiroboCommand(command)

@app.route(‘/mirobo/start’)
def start():
command = “start”
return executeMiroboCommand(command)

@app.route(‘/mirobo/stop’)
def stop():
command = “stop”
return executeMiroboCommand(command)

@app.route(‘/mirobo/spot’)
def spot():
command = “spot”
return executeMiroboCommand(command)

@app.route(‘/mirobo/find’)
def find():
command = “find”
return executeMiroboCommand(command)

@app.route(‘/mirobo/home’)
def home():
command = “home”
return executeMiroboCommand(command)

# 38 quiet, 60 balanced , 77 turbo , 90 max. Different value than app may cause app button to stop working
@app.route(‘/mirobo/fanspeed/<speed>’)
def fanspeed(speed):
command = “fanspeed %s” % speed
return executeMiroboCommand(command)

@app.route(‘/mirobo/cleaning_history’)
def cleaning_history():
command = “cleaning_history”
return executeMiroboCommand(command)

# todo dnd, volume?

 

def executeMiroboCommand(command):
try:
result_success = subprocess.check_output(“mirobo ” + command, shell=True)

except subprocess.CalledProcessError as e:
return “An error occurred”
#Read more at https://www.pluralsight.com/guides/python/running-shell-commands-with-flask#iaQlrC0rPigBRpcK.99

decodedResult = result_success.decode().replace(‘\n’, ‘<br>’)
return decodedResult
if __name__ == ‘__main__’:
app.run(debug=True, host=’0.0.0.0′)

 

How to use
Start it by running python3 app.py

then connect to it by using the different commands like
http://<ip adress>:5000/mirobo/<command name>

<command name> could be any in the last part routes above, for instance status.

For example:

http://127.0.0.1:5000/mirobo/status
http://127.0.0.1:5000/mirobo/start

etc…

 

Advertisements

Read Full Post »