Skip to content

Add an sample that is publishing to MQTT broker. #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The valiable `YOU_WISH_TO_TRY_FILE` can be replaced with one of the following:
* _simplest_mrb.rb_ - Simply prints two strings
* _wifi_example_mrb.rb_ - An example of connecting to WiFi, you will need to
modify this file to include your SSID and password
* _mqtt_publish.rb_ - An sample of publishing to MQTT broker
* _system_mrb.rb_ - Examples of most of the system APIs

The clean command will clean both the ESP32 build and the mruby build:
Expand All @@ -50,5 +51,6 @@ configuration file found in

* _mruby-esp32-system_ - ESP32 system calls
* _mruby-esp32-wifi_ - ESP32 WiFi
* _mruby-esp32-mqtt_ - ESP32 MQTT library


4 changes: 2 additions & 2 deletions components/mruby_component/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(MRUBY_CONFIG ${COMPONENT_DIR}/esp32_build_config.rb)

idf_component_register(
INCLUDE_DIRS mruby/include
REQUIRES esp_wifi esp_hw_support esp_rom
REQUIRES esp_wifi esp_hw_support esp_rom mqtt
)

add_custom_command(
Expand All @@ -16,7 +16,7 @@ add_custom_command(

add_prebuilt_library(
libmruby ${LIBMRUBY_FILE}
PRIV_REQUIRES esp_wifi esp_hw_support esp_rom
PRIV_REQUIRES esp_wifi esp_hw_support esp_rom mqtt
)
target_link_libraries(${COMPONENT_LIB} INTERFACE libmruby)

Expand Down
2 changes: 2 additions & 0 deletions components/mruby_component/esp32_build_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@
conf.gem :core => "mruby-compiler"
conf.gem :github => "mruby-esp32/mruby-esp32-system"
conf.gem :github => "mruby-esp32/mruby-esp32-wifi"
conf.gem :github => "mruby-esp32/mruby-esp32-mqtt"
conf.gem :github => "mruby-esp32/mruby-io", :branch => 'esp32'
end
26 changes: 26 additions & 0 deletions main/examples/mqtt_publish.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
wifi = ESP32::WiFi.new

wifi.on_connected do |ip|
puts "Connected: #{ip}"

mqtt = ESP32::MQTT::Client.new('test.mosquitto.org', 1883)
mqtt.connect
mqtt.publish("mruby-esp32-mqtt", '{ "hello": "world." }')
mqtt.disconnect
end

wifi.on_disconnected do
puts 'Disconnected'
end

puts 'Connecting to wifi'
wifi.connect('SSID', 'password')

#
# Loop forever otherwise the script ends
#
while true do
mem = ESP32::System.available_memory() / 1000
puts "Free heap: #{mem}K"
ESP32::System.delay(10000)
end