La librairie Farmbot-py

Développée par l'équipe Farmbot avec la contribution d'Incaya dans le cadre du projet Farmbot Normandie, cette librairie permet de piloter un Farmbot avec du code Python.

Le langage Python#

Python est un langage de programmation interprété, multi-paradigme et multiplateformes. Créé en 1991 par Guido van Rossum, il fonctionne sur la plupart des plates-formes informatiques, des smartphones aux ordinateurs centraux, de Windows à Unix avec notamment GNU/Linux en passant par macOS, ou encore Android, iOS, et peut aussi être traduit en Java ou .NET. Il est conçu pour optimiser la productivité des programmeurs en offrant des outils de haut niveau et une syntaxe simple à utiliser.

C'est de façon générale un langage très utilisé pour l'apprentissage du code et de la programmation.

=> Pour en savoir plus

A ce jour (en 2021), il est plus que conseillé d'installer la version 3 : https://www.python.org/downloads/

Une documentation complète est disponible en français : https://docs.python.org/fr/3/

Dialoguer avec le Farmbot#

Commençons par installer la librairie farmbot-py :

pip install farmbot

Pour disposer des informations les plus à jour pour cette librairie, n'hésitez pas à consulter [le projet sur Github] (https://github.com/FarmBot-Labs/farmbot-py/).

Exemple de script de pilotage#

Cet exemple s'appuie sur wxpython pour proposer une interface graphique minimaliste mais suffisante pour envoyer des commandes au Farmbot.

farmbot-python-commander

from farmbot import Farmbot, FarmbotToken
import threading
import time
import wx
class MyHandler:
def on_connect(self, bot, mqtt_client):
bot.send_message("Hello, farmbot!")
def on_change(self, bot, state):
print("Current position: (%.2f, %.2f, %.2f)" % bot.position())
def on_log(self, bot, log):
print("New message from FarmBot: " + log['message'])
def on_response(self, bot, response):
print("ID of successful request: " + response.id)
def on_error(self, bot, response):
print("ID of failed request: " + response.id)
print("Reason(s) for failure: " + response.errors)
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title='Farmbot Python commander')
panel = wx.Panel(self)
main_sizer = wx.BoxSizer(wx.VERTICAL)
label_command = wx.StaticText(panel,label = "Enter a command: [move, home, log]",style = wx.ALIGN_CENTRE )
main_sizer.Add(label_command,0, wx.ALL|wx.EXPAND, 20)
self.text_ctrl = wx.TextCtrl(panel)
main_sizer.Add(self.text_ctrl, 0, wx.ALL | wx.EXPAND, 5)
button_command = wx.Button(panel, label='Send command')
main_sizer.Add(button_command, 0, wx.ALL | wx.CENTER, 5)
button_command.Bind(wx.EVT_BUTTON, self.onCommandClick)
panel.SetSizer(main_sizer)
self.Show()
def set_farmbot_commander(self, commander):
self.commander = commander
def onCommandClick(self, event):
print("Sending command...")
myCommand = self.text_ctrl.GetValue()
if myCommand == "home":
self.commander.go_home()
elif myCommand == "move":
self.commander.move_relative(100, 0, 0) # of course this should be made configurable
elif myCommand == "log":
self.commander.send_message("New log: " + myCommand)
class MyFarmbotThread (threading.Thread):
def __init__(self, handler, farmbot):
threading.Thread.__init__(self)
self.handler = handler
self.farmbot = farmbot
def run(self):
self.farmbot.connect(self.handler)
# custom rpc commands (to be extended and made configurable)
def send_message(self, message):
self.farmbot.send_message(message)
def go_home(self):
self.farmbot.go_to_home("all", 100)
def move_relative(self, x, y, z):
self.farmbot.move_relative(x, y, z)
if __name__ == '__main__':
raw_token = FarmbotToken.download_token("farmbot_username",
"farmbot_password",
"https://my.farm.bot") # it is best to create an access token and then store it securely
fb = Farmbot(raw_token)
handler = MyHandler()
f = MyFarmbotThread(handler, fb)
f.daemon= True # Farmbot thread will terminate when closing GUI
f.start()
app = wx.App()
frame = MyFrame()
frame.set_farmbot_commander(f)
app.MainLoop()