Information delivered via MQTT

Hello everyone,

I’m trying to figure out, how to access the bots name which can be set in the Device tab.
I’ve searched in the output of bot/device_xx/status and RPC command list (https://github.com/FarmBot/farmbot-js). All I found was the node_name, but this does not match.

@RickCarlino probably knows best :slight_smile:

1 Like

Hi @Ascend- that’s a great question.

We use MQTT for sending commands to the bot, but we use the REST API for storing information.

Since the bot’s name is information rather than a command, the API would be the place to look.

In the case that you have an API token and need to figure out your device’s name, you would perform an HTTP GET request to /api/device.

Don’t forget to add the encoded value of your auth token to the Authorization header in the form of:

Authorization: Bearer 7f60771ae450a1211809a734961fe4c7

otherwise the API will return a 401 (unauthorized) response. Your HTTP client probably has a mechanism to support this.

Although there are a few exceptions, we generally follow REST conventions of:

  • GET /api/RESOURCE_NAME - Fetch all resources of a particular type (Ex: download all sequences)
  • GET /api/RESOURCE_NAME/RESOURCE_ID - Download one single resource, using the ID as a lookup.
  • POST /api/RESOURCE_NAME - Create a new resource (Ex: add a new pin_binding).
  • PUT /api/RESOURCE_NAME/RESOURCE_ID - Update a resource that already exists (Eg: change your bot’s name)
  • DELETE /api/RESOURCE_NAME/RESOURCE_ID - Destroy a resource.

Resources:

Please let me know if you have any other questions about the API. I’m excited to see what you will build! :tada:

1 Like

Thanks for your reply Rick, that is exactly what I was searching for. I completely frogot about this REST API because I didn’t even know what it actually does :roll_eyes:

@Ascend A few people have brought this up already and we’re investigating more intuitive solutions. One idea we’ve considered is replacing the REST API completely with an RPC based system (eg: everything travels through MQTT), but that would be a pretty substantial departure from the app’s current architecture. That’s not to say it’s the wrong way to go, but rather it would require a substantial time investment on our part and would not be available for use in the near future.

If you have any input on how that system would look we would love to hear it.

Please let me know if you have any input on the matter or need additional assistance.

Right now I’m trying to get deeper into the code and learn how it works. I can’t say too much about the current architecture and alternatives at this time, but I will watch out for anything that could be helpful for improvements.

I’m trying to get HTTP requests to work, but for any reason I can’t get a valid response, because my authorization seems to be wrong.
Where do i get an auth token for the REST API? Is it simply a base64 encoded string as described in your link?
Where is the difference to the usual MQTT token?

Edit: Got it, I used the standard token that I’ve generated via Token generation example (Github).
I’ve been searching for hours until I realized, that ' are not allowed in the authorization header.
What I’ve tried first:
{‘Authorization’ : ’ Bearer 7f60771ae450a1211809a734961fe4c7’}
What worked:
Authorization : Bearer 7f60771ae450a1211809a734961fe4c7

@RickCarlino GET API seems to work fine for me, POST or PUT don’t seem to cause any reaction at the webapp. I can’t tell if its my HTTP binding what is not working, or if I’m sending the message to the wrong link. Could you please make an example for the RECOURCE_ID that you mentioned here:
PUT /api/RESOURCE_NAME/RESOURCE_ID?

My HTTP binding provides this syntax to POST/PUT requests:
http:">[<command>:<httpmethod>:<url>:<postcontent>]"

I always tried URLs like https://my.farmbot.io:443/api/firmware_config and { "pin_guard_5_time_out": 23 } as postcontent.

I’ll keep trying, maybe you’ve got an idea what could be (obviously) wrong :slight_smile:

@Ascend

A few things:

  • What was the HTTP response code? If you did not get a response code, what error did your HTTP client throw?
  • Did you check the response body? In the case of common errors (such as 422 errors) there will often be a JSON object expaning the issue.
  • Did you set the Content-Type header to Content-Type: application/json; ? All /api endpoints expect JSON and I’ve worked with HTTP clients in the past that don’t infer this sort of thing by deafult.

@RickCarlino
I wasn’t able to get it work so I did some research online. It seems like my inbound HTTP client can’t submit data (request) via PUT. GET and POSTis working perfectly, but PUT is intended to be used with the command integrated in the URL.

There is an alternative binding which also allows to GET/POST/PUT over HTTP. Sadly it doesn’t allow using headers for authentication.

Now I’ll try to run a python script to get the request on its way.
Right now I’m getting a response code [400] which would mean that the message is malformed. I can’t see any mistakes yet, header and request data are encoded in JSON, URL should fit aswell. Maybe someone can find an error in here (Note that I’ve replaced my actual token with “token”):

response = requests.put('https://my.farmbot.io:443/api/firmware_config', headers={'Authorization': 'Bearer token','content-type': 'application/json'}, data={'pin_guard_5_time_out': '23'})

1 Like

The data object needs to be serialized into a JSON formatted string. You can use json.dumps for this:

import json
import requests

token = ''
headers = {'Authorization': 'Bearer ' + token,
           'content-type': 'application/json'}
data = json.dumps({'pin_guard_5_time_out': '23'})
response = requests.put('https://my.farmbot.io/api/firmware_config',
                         headers=headers, data=data)
2 Likes

To keep record for the future, it took me several hours to find out that the username ist device_xxx and the password is the generated token to log into the farmbot MQTT server :wink:

Cheers!

Thanks, yes for MQTT, the username is the device ID and the password is the encoded token. I have updated the MQTT documentation. If you are using Python, some examples can be found here.

Also, although this topic has MQTT in the title, it primarily involves using the App API. This topic includes more about using MQTT.