MQTT library for mruby-esp32.
You need esp-idf v5.0 to use this mrbgems.
Add the line below to your build_config.rb
:
conf.gem :github => 'mruby-esp32/mruby-esp32-mqtt'
In addition, you may need to add mqtt
to the component linking mruby.
idf_component_register(
# ...
REQUIRES esp_wifi esp_hw_support esp_rom mqtt # <- add
)
add_prebuilt_library(
# ...
PRIV_REQUIRES esp_wifi esp_hw_support esp_rom mqtt # <- add
)
Connect MQTT.
mqtt = ESP32::MQTT::Client.new('test.mosquitto.org', 1883)
mqtt.connect
Connect MQTT + TLS.
mqtt = ESP32::MQTT::Client.new('test.mosquitto.org', 8883)
mqtt.ssl = true
mqtt.ca = IO.read('root-ca.pem')
mqtt.cert = IO.read('certificate.pem.crt')
mqtt.key = IO.read('private.pem.key')
mqtt.connect
Publish message to topic.
mqtt.publish("topic", 'message')
Subscribe to topic and get message.
mqtt.subscribe("topic")
topic, message = mqtt.get
Disconnect.
mqtt.disconnect
MIT License.