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.
  2. 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)

  • 2
    Without seeing the code you've tried we can fix it – hardillb Jan 6 at 17:17
  • 2
    Hi @hardillb , i've added the code. for the downvotes - would be great if you gave comments on additional details I could provide to better help the question - Thank you! – Eric Lim Jan 6 at 17:43
  • 3
    This is is a generic programming question and as such should probably be on Stack Overflow (but you are going to need to show more effort to actually implement some MQTT logic in your code before anybody will help) – hardillb Jan 7 at 10:21
  • 2
    @hardillb sorry i was deciding the two and decided here since it involves mqtt/iot stuff. The problem i'm having is i do not know how i can implement MQTT logic into my ASP.NET RESTapi. What i've tried though is using some mqtt-rest bridge available on git but so far none seemed to work... – Eric Lim Jan 8 at 5:34
  • 2
    @Mawg thank you Mawg! Will try to keep my questions as detailed as i can!. – Eric Lim Jan 8 at 5:34
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.