4

Question: What I'm trying to do is to get the REST API to be able to publish to my Mosquitto MQTT Broker (which is also hosted on my Pi on Port 1883) via IFTTT Webhook.

Context

I've built a webapp using ASP.NET Core and it is being hosted on my Raspberry Pi 3 B+ using NGINX. Likewise, I've also made a RESTFUL API using ASP.NET Core. As for the REST API, I can either publish it to Microsoft Azure / Host it on my Pi as well, whichever is better in the solution given.

What I've tried

I've had a successful attempt using Home Assistant's REST API /api/service/mqtt/publish but to no avail when setting up my own. I'm pretty sure I'm missing something in my ASP.NET Core REST API configuration. I did it just as how I would do any HttpPost thinking it would work.

Code

[Route("api/broker")]
[ApiController]
public class BrokerController : ControllerBase
{
    #region HTTPGET
    // HTTP GET: /api/broker
    [HttpGet]
    public IEnumerable<Broker> Get()
    {
        List<Broker> dbList = DBUtl.GetList<Broker>("SELECT * FROM Broker");
        return dbList;
    }
}

Things I've attempted:

  1. Butler-mqtt-master.
  • Got it working and it says "Rest Server Listening on http://[::]:8081" But I've no idea what exactly does that mean. My API is available on a azure api link.
  1. mqtt2rest-master
  • Outdated (i think?)

Successful Workaround (but not very reliable / fully functional):

  1. Node-RED
  • By using a inject node to constantly poll a http request node at a fixed interval of 4 seconds together with a function node and finally pushing the object obtained (with mqtt payload in JSON) to the mqtt node, I am able to get bridge from HTTP RESTapi to MQTT Broker. But this method has lots of loopholes and isn't efficient. If possible I want mine to be working in the same as as how home assistant works.

Link: https://developers.home-assistant.io/docs/en/external_api_rest.html

E.G: Sending a POST request to https:///api/services/mqtt/publish? will send my POST request to the MQTT Broker (which I have no idea how that works)

Bence Kaulics
  • 7,843
  • 8
  • 42
  • 90
Eric Lim
  • 161
  • 1
  • 8

1 Answers1

1

May be it is simpler to use MQTT Broker that supports REST API by its nature? Like flespi broker which contain secure and private MQTT namespace, free to use and actively used by some home automations systems. The REST API for messages publishing is described here.

shal
  • 806
  • 5
  • 4