getting a really good answer at my previous question on this forum makes me to ask another question that i’m researching latelly. So my plan is to have a Raspberry PI with MQTT Mosquitto installed that will grab sensors data from the house. Can you point me to a library or code snippet or what’s the standard way of grabbing the mqqt messages and post them to a Rest API in cloud after please?
Hello Teodor, I’m gad you found the other answer helpful!
When you say “Rest API”, you basically want to make an HTTP POST or GET request to a web service, right?
yes exactly .. i’m just starting with MQTT and i have a custom API on the cloud that accept GET, POST,PUT, DELETE so i’m not sure after the PI gets MQTT message what would be the best way to post it for example to an endpoint. Right now my sensors send post requests directly to cloud but i think is better if i use the PI that accepts MQTT and from there the PI sends the posts. thanks
I don’t have any good tutorials on that subject and I should definitely write something about that… I also don’t know any library that could be an abstraction layer to make HTTP requests to APIs easily.
Basically, if you receive the messages via MQTT, you can make an HTTP GET or POST request using the ESP32.
As I said, I don’t have any good examples, but as I show in that project, you make an HTTP request to IFTTT: https://randomnerdtutorials.com/esp8266-wi-fi-button-diy-amazon-dash-button-clone/
You could merge one of the MQTT examples with that HTTP request example to do what you’re looking for. Something like this makes an HTTP GET request:
client.print(String("GET ") + resource + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: close\r\n\r\n");
I also recommend that if you’re using Node-RED, you could handle the HTTP request directly on Node-RED.