Skip to content

Commit d5a244e

Browse files
authored
Merge pull request #32 from yuuu/add_sample_mqtt_publish
Add an sample that is publishing to MQTT broker.
2 parents aa47398 + 74f7ce1 commit d5a244e

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The valiable `YOU_WISH_TO_TRY_FILE` can be replaced with one of the following:
3636
* _simplest_mrb.rb_ - Simply prints two strings
3737
* _wifi_example_mrb.rb_ - An example of connecting to WiFi, you will need to
3838
modify this file to include your SSID and password
39+
* _mqtt_publish.rb_ - An sample of publishing to MQTT broker
3940
* _system_mrb.rb_ - Examples of most of the system APIs
4041

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

5152
* _mruby-esp32-system_ - ESP32 system calls
5253
* _mruby-esp32-wifi_ - ESP32 WiFi
54+
* _mruby-esp32-mqtt_ - ESP32 MQTT library
5355

5456

components/mruby_component/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set(MRUBY_CONFIG ${COMPONENT_DIR}/esp32_build_config.rb)
44

55
idf_component_register(
66
INCLUDE_DIRS mruby/include
7-
REQUIRES esp_wifi esp_hw_support esp_rom
7+
REQUIRES esp_wifi esp_hw_support esp_rom mqtt
88
)
99

1010
add_custom_command(
@@ -17,7 +17,7 @@ add_custom_command(
1717

1818
add_prebuilt_library(
1919
libmruby ${LIBMRUBY_FILE}
20-
PRIV_REQUIRES esp_wifi esp_hw_support esp_rom
20+
PRIV_REQUIRES esp_wifi esp_hw_support esp_rom mqtt
2121
)
2222
target_link_libraries(${COMPONENT_LIB} INTERFACE libmruby)
2323

components/mruby_component/esp32_build_config.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@
6363
conf.gem :core => "mruby-compiler"
6464
conf.gem :github => "mruby-esp32/mruby-esp32-system"
6565
conf.gem :github => "mruby-esp32/mruby-esp32-wifi"
66+
conf.gem :github => "mruby-esp32/mruby-esp32-mqtt"
67+
conf.gem :github => "mruby-esp32/mruby-io", :branch => 'esp32'
6668
end

main/examples/mqtt_publish.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
wifi = ESP32::WiFi.new
2+
3+
wifi.on_connected do |ip|
4+
puts "Connected: #{ip}"
5+
6+
mqtt = ESP32::MQTT::Client.new('test.mosquitto.org', 1883)
7+
mqtt.connect
8+
mqtt.publish("mruby-esp32-mqtt", '{ "hello": "world." }')
9+
mqtt.disconnect
10+
end
11+
12+
wifi.on_disconnected do
13+
puts 'Disconnected'
14+
end
15+
16+
puts 'Connecting to wifi'
17+
wifi.connect('SSID', 'password')
18+
19+
#
20+
# Loop forever otherwise the script ends
21+
#
22+
while true do
23+
mem = ESP32::System.available_memory() / 1000
24+
puts "Free heap: #{mem}K"
25+
ESP32::System.delay(10000)
26+
end

0 commit comments

Comments
 (0)