From 5106a7e6f1a9e20d5f60e48e8d846cebf45348c6 Mon Sep 17 00:00:00 2001 From: vickash Date: Sat, 8 Jul 2023 17:39:40 -0400 Subject: [PATCH 01/14] Use all of the 2MB flash --- partitions.csv | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/partitions.csv b/partitions.csv index a31cca5..cdac626 100644 --- a/partitions.csv +++ b/partitions.csv @@ -2,5 +2,5 @@ # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap nvs, data, nvs, 0x9000, 0x6000, phy_init, data, phy, 0xf000, 0x1000, -factory, app, factory, 0x10000, 1500K, -storage, data, spiffs, 0x190000,200K, \ No newline at end of file +factory, app, factory, 0x10000, 1728K, +storage, data, spiffs, , 256K, \ No newline at end of file From 26013fd987cd99b7c280cb437f0ab5d350105eec Mon Sep 17 00:00:00 2001 From: vickash Date: Sat, 8 Jul 2023 18:31:37 -0400 Subject: [PATCH 02/14] Change gems to their versioned branches --- components/mruby_component/esp32_build_config.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/components/mruby_component/esp32_build_config.rb b/components/mruby_component/esp32_build_config.rb index fd56fd7..64627eb 100644 --- a/components/mruby_component/esp32_build_config.rb +++ b/components/mruby_component/esp32_build_config.rb @@ -61,9 +61,14 @@ conf.gem :core => "mruby-print" 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' - conf.gem :github => "mruby-esp32/mruby-esp32-gpio" + conf.gem :github => "mruby-esp32/mruby-io", :branch => '0.5' + conf.gem :github => "mruby-esp32/mruby-fileio", :branch => '0.5' + conf.gem :github => "mruby-esp32/mruby-socket", :branch => '0.5' + + conf.gem :github => "mruby-esp32/mruby-esp32-system", :branch => '0.5' + conf.gem :github => "mruby-esp32/mruby-esp32-wifi", :branch => '0.5' + conf.gem :github => "mruby-esp32/mruby-esp32-mqtt", :branch => '0.5' + + conf.gem :github => "mruby-esp32/mruby-esp32-gpio", :branch => '0.5' + conf.gem :github => "mruby-esp32/mruby-esp32-ledc", :branch => '0.5' end From 1a6256e9cbcf712770a4c3da7e9d65e79f0554e4 Mon Sep 17 00:00:00 2001 From: vickash Date: Sat, 8 Jul 2023 18:51:38 -0400 Subject: [PATCH 03/14] Improve examples and readme --- README.md | 12 +++--- main/examples/filesystem.rb | 12 ++++++ .../examples/{simplest_mrb.rb => simplest.rb} | 0 main/examples/{system_mrb.rb => system.rb} | 0 .../{wifi_example_mrb.rb => wifi_connect.rb} | 0 main/examples/wifi_socket.rb | 38 +++++++++++++++++++ 6 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 main/examples/filesystem.rb rename main/examples/{simplest_mrb.rb => simplest.rb} (100%) rename main/examples/{system_mrb.rb => system.rb} (100%) rename main/examples/{wifi_example_mrb.rb => wifi_connect.rb} (100%) create mode 100644 main/examples/wifi_socket.rb diff --git a/README.md b/README.md index 5a966c6..ce0d898 100644 --- a/README.md +++ b/README.md @@ -33,12 +33,12 @@ idf.py -p $(YOUR_SERIAL_PORT) flash monitor The valiable `YOU_WISH_TO_TRY_FILE` can be replaced with one of the following: - * _simplest_mrb.rb_ - Simply prints two strings - * _gpio.rb_ - An example of using GPIO - * _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 + * _simplest.rb_ - Prints two strings + * _system.rb_ - Demonstrates most system APIs + * _gpio.rb_ - GPIO blink example + * _wifi_connect.rb_ - Connects to WiFi. You need to replace your SSID and password in this file. + * _mqtt_publish.rb_ - Publishes to MQTT broker + * _filesystem.rb_ - Write/Append/Read example on the virtual filesystem The clean command will clean both the ESP32 build and the mruby build: diff --git a/main/examples/filesystem.rb b/main/examples/filesystem.rb new file mode 100644 index 0000000..e3c971e --- /dev/null +++ b/main/examples/filesystem.rb @@ -0,0 +1,12 @@ +# Wait a bit for startup to complete. +ESP32::System.delay(1000) + +string1 = "testing " +string2 = "1,2,3..." + +puts "Writing to /spiffs/test.txt: \"#{string1}#{string2}\"" +File.open('/spiffs/test.txt', 'w') { |f| f.write(string1) } +File.open('/spiffs/test.txt', 'a') { |f| f.write(string2) } + +print "Read from /spiffs/text.txt: " +File.open('/spiffs/test.txt', 'r') { |f| f.each_line { |l| puts l } } diff --git a/main/examples/simplest_mrb.rb b/main/examples/simplest.rb similarity index 100% rename from main/examples/simplest_mrb.rb rename to main/examples/simplest.rb diff --git a/main/examples/system_mrb.rb b/main/examples/system.rb similarity index 100% rename from main/examples/system_mrb.rb rename to main/examples/system.rb diff --git a/main/examples/wifi_example_mrb.rb b/main/examples/wifi_connect.rb similarity index 100% rename from main/examples/wifi_example_mrb.rb rename to main/examples/wifi_connect.rb diff --git a/main/examples/wifi_socket.rb b/main/examples/wifi_socket.rb new file mode 100644 index 0000000..6160676 --- /dev/null +++ b/main/examples/wifi_socket.rb @@ -0,0 +1,38 @@ +# Stack sizes may need to be increased. +# See: https://github.com/mruby-esp32/mruby-socket/blob/0.4/README.md +# +puts "Getting ready to start Wi-Fi" + +wifi = ESP32::WiFi.new + +wifi.on_connected do |ip| + puts "Wi-Fi Connected: #{ip} (#{Socket.gethostname})" + soc = TCPSocket.open("www.kame.net", 80) + msg = "HEAD / HTTP/1.1\r\nHost: www.kame.net\r\nConnection: close\r\n\r\n" + msg.split("\r\n").each do |e| + puts ">>> #{e}" + end + soc.send(msg, 0) + puts "--------------------------------------------------------------------------------" + loop do + buf = soc.recv(128, 0) + break if buf.length == 0 + print buf + end + puts "" + puts "--------------------------------------------------------------------------------" +end + +wifi.on_disconnected do + puts "Wi-Fi Disconnected" +end + +puts "Connecting to Wi-Fi" +wifi.connect('SSID', 'PASSWORD') + +# +# Loop forever otherwise the script ends +# +while true do + ESP32::System.delay(1000) +end From 3d4f8ba5750530402b2b61c134c092938185f586 Mon Sep 17 00:00:00 2001 From: vickash Date: Sat, 8 Jul 2023 18:58:11 -0400 Subject: [PATCH 04/14] Add LEDC breathe example --- main/examples/ledc_breathe.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 main/examples/ledc_breathe.rb diff --git a/main/examples/ledc_breathe.rb b/main/examples/ledc_breathe.rb new file mode 100644 index 0000000..b82ef57 --- /dev/null +++ b/main/examples/ledc_breathe.rb @@ -0,0 +1,26 @@ +group = ESP32::LEDC_LOW_SPEED_MODE +channel = ESP32::LEDC_CHANNEL_0 +timer = ESP32::LEDC_TIMER_0 +resolution = ESP32::LEDC_TIMER_8_BIT +frequency = 1000 +pin = ESP32::GPIO::GPIO_NUM_2 # Built in LED on original ESP32 devkit. + +ESP32::LEDC.timer_config(group, timer, resolution, frequency) +ESP32::LEDC.channel_config(pin, group, timer, channel) + +# Fade the LED up and down. +loop do + i = 0 + while (i < 256) do + ESP32::LEDC.set_duty(group, channel, i) + i += 1 + ESP32::System.delay(10) + end + + i=255 + while (i > -1) do + ESP32::LEDC.set_duty(group, channel, i) + i -= 1 + ESP32::System.delay(10) + end +end From 0a6c514b35cb0b8ff16ab28a2e2ad26d78a29e41 Mon Sep 17 00:00:00 2001 From: vickash Date: Sat, 8 Jul 2023 18:59:46 -0400 Subject: [PATCH 05/14] Match require order in CmakeLists.txt to other branch --- components/mruby_component/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/mruby_component/CMakeLists.txt b/components/mruby_component/CMakeLists.txt index e9a08d0..004ab68 100644 --- a/components/mruby_component/CMakeLists.txt +++ b/components/mruby_component/CMakeLists.txt @@ -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 mqtt driver esp_timer + REQUIRES esp_hw_support esp_rom esp_timer esp_wifi driver mqtt ) add_custom_command( @@ -17,7 +17,7 @@ add_custom_command( add_prebuilt_library( libmruby ${LIBMRUBY_FILE} - PRIV_REQUIRES esp_wifi esp_hw_support esp_rom mqtt driver + PRIV_REQUIRES esp_hw_support esp_rom esp_timer esp_wifi driver mqtt ) target_link_libraries(${COMPONENT_LIB} INTERFACE libmruby) From 445ee304e4a8c4f65c7da4d322f89acd39cbd15b Mon Sep 17 00:00:00 2001 From: vickash Date: Sat, 8 Jul 2023 19:23:46 -0400 Subject: [PATCH 06/14] LEDC buzzer example --- main/examples/ledc_buzzer.rb | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 main/examples/ledc_buzzer.rb diff --git a/main/examples/ledc_buzzer.rb b/main/examples/ledc_buzzer.rb new file mode 100644 index 0000000..f709beb --- /dev/null +++ b/main/examples/ledc_buzzer.rb @@ -0,0 +1,39 @@ +group = ESP32::LEDC_LOW_SPEED_MODE +channel = ESP32::LEDC_CHANNEL_0 +timer = ESP32::LEDC_TIMER_0 +resolution = ESP32::LEDC_TIMER_8_BIT +pin = ESP32::GPIO::GPIO_NUM_4 + +# Configure the channel once. +ESP32::LEDC.channel_config(pin, group, timer, channel) + +# Note frequencies. +C4 = 262 +D4 = 294 +E4 = 330 + +# Melody to play. +notes = [ + [E4, 1], [D4, 1], [C4, 1], [D4, 1], [E4, 1], [E4, 1], [E4, 2], + [D4, 1], [D4, 1], [D4, 2], [E4, 1], [E4, 1], [E4, 2], + [E4, 1], [D4, 1], [C4, 1], [D4, 1], [E4, 1], [E4, 1], [E4, 1], [E4, 1], + [D4, 1], [D4, 1], [E4, 1], [D4, 1], [C4, 4], + ] + +# Calculate length of one beat in milliseconds. +bpm = 180 +beat_time = 60000.to_f / bpm + +# Play the melody. +notes.each do |note| + # Set timer frequency to 0th element of the note array. + ESP32::LEDC.timer_config(group, timer, resolution, note[0]) + # Duty cycle to 50% for square wave. + ESP32::LEDC.set_duty(group, channel, 128) + + # Wait for length of the note, 1st element. + ESP32::System.delay(note[1] * beat_time) + + # Duty cycle to 0 to stop note. + ESP32::LEDC.set_duty(group, channel, 0) +end From 7ca1b6d353a9ab808caeaa47de03a27595c08e98 Mon Sep 17 00:00:00 2001 From: vickash Date: Sat, 8 Jul 2023 20:09:18 -0400 Subject: [PATCH 07/14] Add LEDC servo example and add LEDC examples to readme --- README.md | 3 +++ main/examples/ledc_servo.rb | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 main/examples/ledc_servo.rb diff --git a/README.md b/README.md index ce0d898..a771b21 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,9 @@ The valiable `YOU_WISH_TO_TRY_FILE` can be replaced with one of the following: * _wifi_connect.rb_ - Connects to WiFi. You need to replace your SSID and password in this file. * _mqtt_publish.rb_ - Publishes to MQTT broker * _filesystem.rb_ - Write/Append/Read example on the virtual filesystem + * _ledc_breathe.rb_ - Gradually fades the brightness of an LED up and down + * _ledc_buzzer.rb_ - Plays a melody on a pizeo-electric buzzer + * _ledc_servo.rb_ - Controls position of a 180 degree hobby servo motor The clean command will clean both the ESP32 build and the mruby build: diff --git a/main/examples/ledc_servo.rb b/main/examples/ledc_servo.rb new file mode 100644 index 0000000..fe339ba --- /dev/null +++ b/main/examples/ledc_servo.rb @@ -0,0 +1,37 @@ +group = ESP32::LEDC_LOW_SPEED_MODE +channel = ESP32::LEDC_CHANNEL_0 +timer = ESP32::LEDC_TIMER_0 +resolution = ESP32::LEDC_TIMER_14_BIT +pin = ESP32::GPIO::GPIO_NUM_4 +frequency = 50 + +# Configure the channel and timer. +ESP32::LEDC.channel_config(pin, group, timer, channel) +ESP32::LEDC.timer_config(group, timer, resolution, frequency) + +# Using 14-bit PWM @ 50Hz, 0-16383 maps from 0 to 20 milliseconds. +# Calculate how many microseconds each LSB of duty cycle represents. +US_PER_BIT = (1000000.0 / frequency) / (2 ** resolution) + +# Convert from microseconds to duty cycle. +def microseconds_to_duty(us) + (us / US_PER_BIT).round +end + +# This is for a 180 degree MG995 motor. Values will differ for other sweep angles, +# or continuous rotation servos / ESCs. Values may vary between individual motors. +# +# Make sure to connect your servo motor to a separate power source! +# +5.times do + # Send 500us pulses for 2 seconds. Should map to 0 degrees. + ESP32::LEDC.set_duty(group, channel, microseconds_to_duty(500)) + ESP32::System.delay(2000) + + # Send 2500us pulses for 2 seconds. Should map to 180 degrees. + ESP32::LEDC.set_duty(group, channel, microseconds_to_duty(2500)) + ESP32::System.delay(2000) +end + +# Turn it off. +ESP32::LEDC.set_duty(group, channel, 0) From b29f80ae88700aa7403bdf13ba3988540ed99826 Mon Sep 17 00:00:00 2001 From: vickash Date: Sat, 8 Jul 2023 23:09:23 -0400 Subject: [PATCH 08/14] Switch from SPIFFS to littlefs. Adds about 5KB to app image size Default mount path is "/storage" now --- .gitignore | 1 + main/CMakeLists.txt | 4 ++-- main/examples/filesystem.rb | 12 ++++++------ main/idf_component.yml | 17 +++++++++++++++++ main/mruby_main.c | 17 ++++++++--------- main/{spiffs => storage}/main.rb | 0 6 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 main/idf_component.yml rename main/{spiffs => storage}/main.rb (100%) diff --git a/.gitignore b/.gitignore index 803584d..5bc1f15 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,6 @@ main/spiffs/*.pem.key main/spiffs/*.pem.crt components/mruby_component/esp32_build_config.rb.lock +managed_components .vscode diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index e1476fe..55b6e14 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -2,7 +2,7 @@ idf_component_register( SRCS mruby_main.c INCLUDE_DIRS . REQUIRES mruby_component - PRIV_REQUIRES nvs_flash spiffs + PRIV_REQUIRES nvs_flash ) -spiffs_create_partition_image(storage ./spiffs FLASH_IN_PROJECT) +littlefs_create_partition_image(storage ./storage FLASH_IN_PROJECT) diff --git a/main/examples/filesystem.rb b/main/examples/filesystem.rb index e3c971e..ff38ab2 100644 --- a/main/examples/filesystem.rb +++ b/main/examples/filesystem.rb @@ -1,12 +1,12 @@ # Wait a bit for startup to complete. -ESP32::System.delay(1000) +ESP32::System.delay(500) string1 = "testing " string2 = "1,2,3..." -puts "Writing to /spiffs/test.txt: \"#{string1}#{string2}\"" -File.open('/spiffs/test.txt', 'w') { |f| f.write(string1) } -File.open('/spiffs/test.txt', 'a') { |f| f.write(string2) } +puts "Writing to /storage/test.txt: \"#{string1}#{string2}\"" +File.open('/storage/test.txt', 'w') { |f| f.write(string1) } +File.open('/storage/test.txt', 'a') { |f| f.write(string2) } -print "Read from /spiffs/text.txt: " -File.open('/spiffs/test.txt', 'r') { |f| f.each_line { |l| puts l } } +print "Read from /storage/text.txt: " +File.open('/storage/test.txt', 'r') { |f| f.each_line { |l| puts l } } diff --git a/main/idf_component.yml b/main/idf_component.yml new file mode 100644 index 0000000..7f2872c --- /dev/null +++ b/main/idf_component.yml @@ -0,0 +1,17 @@ +## IDF Component Manager Manifest File +dependencies: + joltwallet/littlefs: "==1.5.5" + ## Required IDF version + idf: + version: ">=4.1.0" + # # Put list of dependencies here + # # For components maintained by Espressif: + # component: "~1.0.0" + # # For 3rd party components: + # username/component: ">=1.0.0,<2.0.0" + # username2/component2: + # version: "~1.0.0" + # # For transient dependencies `public` flag can be set. + # # `public` flag doesn't have an effect dependencies of the `main` component. + # # All dependencies of `main` are public by default. + # public: true diff --git a/main/mruby_main.c b/main/mruby_main.c index e129faa..ee97ba3 100644 --- a/main/mruby_main.c +++ b/main/mruby_main.c @@ -4,8 +4,8 @@ #include "freertos/task.h" #include "esp_system.h" #include "esp_log.h" -#include "esp_spiffs.h" #include "nvs_flash.h" +#include "esp_littlefs.h" #include "mruby.h" #include "mruby/irep.h" @@ -26,10 +26,10 @@ void mruby_task(void *pvParameter) ESP_LOGI(TAG, "%s", "Loading..."); mrb_load_func load = mrb_load_detect_file_cxt; - FILE *fp = fopen("/spiffs/main.rb", "r"); + FILE *fp = fopen("/storage/main.rb", "r"); if (fp == NULL) { load = mrb_load_irep_file_cxt; - fp = fopen("/spiffs/main.mrb", "r"); + fp = fopen("/storage/main.mrb", "r"); if (fp == NULL) { ESP_LOGI(TAG, "File is none."); goto exit; @@ -60,13 +60,12 @@ void app_main() { nvs_flash_init(); - esp_vfs_spiffs_conf_t conf = { - .base_path = "/spiffs", - .partition_label = NULL, - .max_files = 10, - .format_if_mount_failed = false + esp_vfs_littlefs_conf_t conf = { + .base_path = "/storage", + .partition_label = "storage", + .format_if_mount_failed = false, }; - ESP_ERROR_CHECK(esp_vfs_spiffs_register(&conf)); + ESP_ERROR_CHECK(esp_vfs_littlefs_register(&conf)); xTaskCreate(&mruby_task, "mruby_task", 16384, NULL, 5, NULL); } diff --git a/main/spiffs/main.rb b/main/storage/main.rb similarity index 100% rename from main/spiffs/main.rb rename to main/storage/main.rb From d7e863ed695016957619a0aa9eab58b7b93c9a92 Mon Sep 17 00:00:00 2001 From: vickash Date: Sun, 9 Jul 2023 00:20:48 -0400 Subject: [PATCH 09/14] Readme improvements --- README.md | 98 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index a771b21..1a7ca15 100644 --- a/README.md +++ b/README.md @@ -1,60 +1,90 @@ -# Example of mruby on the ESP32 +# mruby on the ESP32 -Before you get started you will need to follow the setup documentation from -the [esp-idf](https://github.com/espressif/esp-idf/tree/master/docs) project -for your specific operating system. +This is an ESP-IDF project template running mruby on the ESP32 microcontroller. -I have only tested this on macOS and using a certain version of -[esp-idf](https://github.com/espressif/esp-idf/tree/release/v5.0). -You should try to use [more recent version](https://github.com/espressif/esp-idf#setting-up-esp-idf) if you have failed. +To get started, you need to install the ESP-IDF, by following the instructions +[here](https://docs.espressif.com/projects/esp-idf/en/release-v5.1/esp32/get-started/index.html), +for your operating system. -You will need to recursively clone this project with the recursive flag -because it includes mruby as a submodule: +This has been tested on macOS and Ubuntu Linux, using +[ESP-IDF 5.1](https://github.com/espressif/esp-idf/tree/release/v5.1). + +## Usage + +Recursively clone this repo to ensure the mruby (3.2.0) submodule gets downloaded: ``` git clone --recursive https://github.com/mruby-esp32/mruby-esp32.git ``` -The main ruby program can be found in the `main/simplest_mrb.rb` file. The -makefile configuration in `main/component.mk` and the main entry point source -file `mruby_main.c` will also be of interest if you want to change the name of -the ruby script. The examples included are very simple scripts that only print -to the ESP32's debug console. +The makefile configuration is in `main/component.mk`. The entry point source +file is `mruby_main.c`. Once that starts, it looks for `storage/main.rb` and runs it in mruby. +You can change the expected filename `mruby_main.c`, or simply save your scripts as `main.rb` +inside the `storage` subfolder. -I'm assuming you have followed all the steps in the install documentation and -are at least somewhat familiar with the building steps. With that in mind you -can do something like the following and see the example running: +### First Build ``` -cp main/examples/$(YOU_WISH_TO_TRY_FILE) main/spiffs/main.rb idf.py build idf.py -p $(YOUR_SERIAL_PORT) flash monitor ``` +Your ESP32 should write 2 lines of numbers to the console. -The valiable `YOU_WISH_TO_TRY_FILE` can be replaced with one of the following: +### Examples - * _simplest.rb_ - Prints two strings - * _system.rb_ - Demonstrates most system APIs - * _gpio.rb_ - GPIO blink example - * _wifi_connect.rb_ - Connects to WiFi. You need to replace your SSID and password in this file. - * _mqtt_publish.rb_ - Publishes to MQTT broker - * _filesystem.rb_ - Write/Append/Read example on the virtual filesystem - * _ledc_breathe.rb_ - Gradually fades the brightness of an LED up and down - * _ledc_buzzer.rb_ - Plays a melody on a pizeo-electric buzzer - * _ledc_servo.rb_ - Controls position of a 180 degree hobby servo motor +The folder `main/examples` includes simple scripts demonstrating functionality. -The clean command will clean both the ESP32 build and the mruby build: +Once you are familiar with the build process, try them with: ``` -idf.py fullclean +cp main/examples/$(EXAMPLE_FILENAME) main/storage/main.rb +idf.py build +idf.py -p $(YOUR_SERIAL_PORT) flash monitor ``` -There are multiple GEMS that can be turned on and off via the mruby +Replace `EXAMPLE_FILENAME` with one of the following: + + * `simplest.rb` - Prints two strings + * `system.rb` - Demonstrates most system APIs + * `gpio.rb` - GPIO blink example + * `wifi_connect.rb` - Connects to WiFi + * `wifi_socket.rb` - Connects to WiFi and makes a TCPSocket connection + * `mqtt_publish.rb` - Connects to WiFi and publishes to MQTT broker + * `filesystem.rb` - Write/append/read a file on the virtual filesystem + * `ledc_breathe.rb` - Gradually fades the brightness of an LED up and down + * `ledc_buzzer.rb` - Plays a melody on a piezo-electric buzzer + * `ledc_servo.rb` - Controls position of a 180 degree hobby servo motor + +**Note**: Edit GPIO numbers to match ones you are connected to, insert your WiFI credentials, customize MQTT settings etc. + +### Build Customization + +There are multiple gems that can be turned on and off via the mruby configuration file found in `components/mruby_component/esp32_build_config.rb`: -* _mruby-esp32-system_ - ESP32 system calls -* _mruby-esp32-wifi_ - ESP32 WiFi -* _mruby-esp32-mqtt_ - ESP32 MQTT library +* [_mruby-socket_](https://github.com/mruby-esp32/mruby-socket/tree/0.5) - ESP32 Socket library (modified from mruby) +* [_mruby-esp32-system_](https://github.com/mruby-esp32/mruby-esp32-system/tree/0.5) - ESP32 system calls +* [_mruby-esp32-wifi_](https://github.com/mruby-esp32/mruby-esp32-wifi/tree/0.5) - ESP32 WiFi +* [_mruby-esp32-mqtt_](https://github.com/mruby-esp32/mruby-esp32-mqtt/tree/0.5) - ESP32 MQTT library +* [_mruby-esp32-gpio_](https://github.com/mruby-esp32/mruby-esp32-gpio/tree/0.5) - ESP32 GPIO library +* [_mruby-esp32-ledc_](https://github.com/mruby-esp32/mruby-esp32-ledc/tree/0.5) - ESP32 LEDC (PWM) library + +To get gem changes to update update, you need to `fullclean` the previous build, then build again: +``` +idf.py fullclean +idf.py build +``` + +All gems are enabled by default, so you can try out the examples, but it's a good idea to disable ones you don't need. + +## Hardware +This has been tested on: +- Original ESP32: `idf.py set-target esp32` +- ESP32-S2: `idf.py set-target esp32s2` +- ESP32-S3: `idf.py set-target esp32s3` +If you followed the IDF installation instructions correctly for your chip, +you can switch the project to target it with the corresponding command above. +Make sure to `fullclean` and `build` after switching. From 3afb04fc15c836236eaede9d2531c7b45e7007fc Mon Sep 17 00:00:00 2001 From: vickash Date: Sun, 9 Jul 2023 00:39:29 -0400 Subject: [PATCH 10/14] More readme fixes --- README.md | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1a7ca15..ed55349 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ git clone --recursive https://github.com/mruby-esp32/mruby-esp32.git The makefile configuration is in `main/component.mk`. The entry point source file is `mruby_main.c`. Once that starts, it looks for `storage/main.rb` and runs it in mruby. -You can change the expected filename `mruby_main.c`, or simply save your scripts as `main.rb` +You can change the expected filename in `mruby_main.c`, or simply save your scripts as `main.rb` inside the `storage` subfolder. ### First Build @@ -80,11 +80,33 @@ All gems are enabled by default, so you can try out the examples, but it's a goo ## Hardware -This has been tested on: +Everything works on: - Original ESP32: `idf.py set-target esp32` + +Everything except gpio gem works on: - ESP32-S2: `idf.py set-target esp32s2` - ESP32-S3: `idf.py set-target esp32s3` If you followed the IDF installation instructions correctly for your chip, -you can switch the project to target it with the corresponding command above. -Make sure to `fullclean` and `build` after switching. +you can switch the project target with the corresponding command above. + +You will probably not be able to build again, until the project partition table is reset to `partitions.csv`. To do this: + +``` +idf.py menuconfig +# Partition Table -> Partition Table (1st option) -> Custom partition Table CSV (Last options) +# Enter to select. Q to exit. Y to save +``` + +## Troubleshooting + +The following files and folders are safe to delete when trying to solve build issues: +- `build` +- `components/mruby_component/build` +- `components/mruby_component/esp32_build_config.rb.lock` +- `managed_components` +- `dependencies.lock` + +This project uses [littlefs](https://github.com/littlefs-project/littlefs) through IDF component +manager. If you see errors about files on disk changing, try deleting the last 2 items on this list. + From a8c8fa3cbc0cb1f8a54bc6899804e37b6f98dbb0 Mon Sep 17 00:00:00 2001 From: vickash Date: Sun, 9 Jul 2023 00:42:19 -0400 Subject: [PATCH 11/14] Readme typo fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ed55349..e05e9cd 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ configuration file found in * [_mruby-esp32-gpio_](https://github.com/mruby-esp32/mruby-esp32-gpio/tree/0.5) - ESP32 GPIO library * [_mruby-esp32-ledc_](https://github.com/mruby-esp32/mruby-esp32-ledc/tree/0.5) - ESP32 LEDC (PWM) library -To get gem changes to update update, you need to `fullclean` the previous build, then build again: +To get gem changes to reflect in the build, `fullclean` the previous build, then build again: ``` idf.py fullclean idf.py build From 896d5b4ffd23c37cd5c536e4b75591736fe6ce5e Mon Sep 17 00:00:00 2001 From: vickash Date: Sun, 9 Jul 2023 16:45:38 -0400 Subject: [PATCH 12/14] Use littlefs from github instead of component registry Fewer annoying build errors --- .gitignore | 1 + .gitmodules | 3 +++ components/esp_littlefs | 1 + main/CMakeLists.txt | 2 +- main/idf_component.yml | 17 ----------------- 5 files changed, 6 insertions(+), 18 deletions(-) create mode 160000 components/esp_littlefs delete mode 100644 main/idf_component.yml diff --git a/.gitignore b/.gitignore index 5bc1f15..de9a008 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +dependencies.lock build/ sdkconfig.old diff --git a/.gitmodules b/.gitmodules index 394fc6b..0de3cab 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "mruby"] path = components/mruby_component/mruby url = https://github.com/mruby/mruby.git +[submodule "components/esp_littlefs"] + path = components/esp_littlefs + url = https://github.com/joltwallet/esp_littlefs.git diff --git a/components/esp_littlefs b/components/esp_littlefs new file mode 160000 index 0000000..8fb0290 --- /dev/null +++ b/components/esp_littlefs @@ -0,0 +1 @@ +Subproject commit 8fb0290e329ce84064e793755a88f48fd3ee4857 diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 55b6e14..c9df518 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -2,7 +2,7 @@ idf_component_register( SRCS mruby_main.c INCLUDE_DIRS . REQUIRES mruby_component - PRIV_REQUIRES nvs_flash + PRIV_REQUIRES nvs_flash esp_littlefs ) littlefs_create_partition_image(storage ./storage FLASH_IN_PROJECT) diff --git a/main/idf_component.yml b/main/idf_component.yml deleted file mode 100644 index 7f2872c..0000000 --- a/main/idf_component.yml +++ /dev/null @@ -1,17 +0,0 @@ -## IDF Component Manager Manifest File -dependencies: - joltwallet/littlefs: "==1.5.5" - ## Required IDF version - idf: - version: ">=4.1.0" - # # Put list of dependencies here - # # For components maintained by Espressif: - # component: "~1.0.0" - # # For 3rd party components: - # username/component: ">=1.0.0,<2.0.0" - # username2/component2: - # version: "~1.0.0" - # # For transient dependencies `public` flag can be set. - # # `public` flag doesn't have an effect dependencies of the `main` component. - # # All dependencies of `main` are public by default. - # public: true From 5e60669b8a7f4e9c25f79d8014284b46cd23b284 Mon Sep 17 00:00:00 2001 From: vickash Date: Sun, 9 Jul 2023 16:48:18 -0400 Subject: [PATCH 13/14] Improve filesystem example. Demonstrates unusual append mode for littlefs --- main/examples/filesystem.rb | 38 +++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/main/examples/filesystem.rb b/main/examples/filesystem.rb index ff38ab2..967b8fc 100644 --- a/main/examples/filesystem.rb +++ b/main/examples/filesystem.rb @@ -1,12 +1,34 @@ -# Wait a bit for startup to complete. -ESP32::System.delay(500) +# Get the number of previous boots from a file. +boot_count = nil +File.open('/storage/boot_count.txt', 'a+') { |f| boot_count = f.gets } -string1 = "testing " -string2 = "1,2,3..." +# Increment it. +boot_count = 0 unless boot_count +boot_count = boot_count.to_i +boot_count += 1 -puts "Writing to /storage/test.txt: \"#{string1}#{string2}\"" -File.open('/storage/test.txt', 'w') { |f| f.write(string1) } -File.open('/storage/test.txt', 'a') { |f| f.write(string2) } +# Write new count back to file. +File.open('/storage/boot_count.txt', 'w') { |f| f.puts(boot_count) } -print "Read from /storage/text.txt: " +# Display it. +puts "Boot count: #{boot_count}" + +# If first boot, write then append to /storage/test.txt. +if boot_count == 1 + string1 = "testing " + string2 = "1,2,3..." + puts "Writing to /storage/test.txt: \"#{string1}#{string2}\"" + + File.open('/storage/test.txt', 'w') { |f| f.write(string1) } + + # WARNING: littlefs append mode starts with the file pointer at 0 (start of file). + File.open('/storage/test.txt', 'a') do |f| + # Move file pointer to the end with #seek. + f.seek(0, File::SEEK_END) + f.write(string2) + end +end + +# Read from /storage/test.txt on every boot. +print "Read from /storage/test.txt: " File.open('/storage/test.txt', 'r') { |f| f.each_line { |l| puts l } } From 0082029f1a2b2b24d675ec66400b8abfa161a261 Mon Sep 17 00:00:00 2001 From: vickash Date: Sun, 9 Jul 2023 18:21:02 -0400 Subject: [PATCH 14/14] Update filesystem example Modified the 'mruby-fileio' gem so File#write auto seeks to EOF when in append mode, which littlefs was expecting it to handle. Shouldn't hurt other filesystems regardless --- main/examples/filesystem.rb | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/main/examples/filesystem.rb b/main/examples/filesystem.rb index 967b8fc..f16117f 100644 --- a/main/examples/filesystem.rb +++ b/main/examples/filesystem.rb @@ -13,22 +13,30 @@ # Display it. puts "Boot count: #{boot_count}" -# If first boot, write then append to /storage/test.txt. +# PERSISTENCE DEMO +persistence_file = "/storage/test.txt" + +# If first boot, write then append to the test file. if boot_count == 1 string1 = "testing " string2 = "1,2,3..." - puts "Writing to /storage/test.txt: \"#{string1}#{string2}\"" - File.open('/storage/test.txt', 'w') { |f| f.write(string1) } + puts "Writing to #{persistence_file}: \"#{string1}#{string2}\"" - # WARNING: littlefs append mode starts with the file pointer at 0 (start of file). - File.open('/storage/test.txt', 'a') do |f| - # Move file pointer to the end with #seek. - f.seek(0, File::SEEK_END) - f.write(string2) - end + File.open(persistence_file, 'w') { |f| f.write(string1) } + File.open(persistence_file, 'a') { |f| f.write(string2) } end -# Read from /storage/test.txt on every boot. -print "Read from /storage/test.txt: " -File.open('/storage/test.txt', 'r') { |f| f.each_line { |l| puts l } } +# Read the file back on every boot. +print "Read from #{persistence_file}: " +File.open(persistence_file, 'r') { |f| f.each_line { |l| puts l } } + +# OVERWRITE DEMO +overwrite_file = '/storage/overwrite.txt' + +File.open(overwrite_file, 'w') { |f| f.puts "12345678" } +File.open(overwrite_file, 'w') { |f| f.puts "1234" } + +puts "#{overwrite_file} should contain: 1234" +print "#{overwrite_file} contains: " +File.open(overwrite_file, 'r') { |f| f.each_line { |l| puts l } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy