Skip to content

Commit 5ab00de

Browse files
authored
Merge pull request #43 from mruby-esp32/0.5
Use updated gems and littlefs
2 parents 573a5e9 + 0082029 commit 5ab00de

18 files changed

+298
-51
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
dependencies.lock
12
build/
23
sdkconfig.old
34

@@ -7,5 +8,6 @@ main/spiffs/*.pem.key
78
main/spiffs/*.pem.crt
89

910
components/mruby_component/esp32_build_config.rb.lock
11+
managed_components
1012

1113
.vscode

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "mruby"]
22
path = components/mruby_component/mruby
33
url = https://github.com/mruby/mruby.git
4+
[submodule "components/esp_littlefs"]
5+
path = components/esp_littlefs
6+
url = https://github.com/joltwallet/esp_littlefs.git

README.md

Lines changed: 86 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,112 @@
1-
# Example of mruby on the ESP32
1+
# mruby on the ESP32
22

3-
Before you get started you will need to follow the setup documentation from
4-
the [esp-idf](https://github.com/espressif/esp-idf/tree/master/docs) project
5-
for your specific operating system.
3+
This is an ESP-IDF project template running mruby on the ESP32 microcontroller.
64

7-
I have only tested this on macOS and using a certain version of
8-
[esp-idf](https://github.com/espressif/esp-idf/tree/release/v5.0).
9-
You should try to use [more recent version](https://github.com/espressif/esp-idf#setting-up-esp-idf) if you have failed.
5+
To get started, you need to install the ESP-IDF, by following the instructions
6+
[here](https://docs.espressif.com/projects/esp-idf/en/release-v5.1/esp32/get-started/index.html),
7+
for your operating system.
108

11-
You will need to recursively clone this project with the recursive flag
12-
because it includes mruby as a submodule:
9+
This has been tested on macOS and Ubuntu Linux, using
10+
[ESP-IDF 5.1](https://github.com/espressif/esp-idf/tree/release/v5.1).
11+
12+
## Usage
13+
14+
Recursively clone this repo to ensure the mruby (3.2.0) submodule gets downloaded:
1315

1416
```
1517
git clone --recursive https://github.com/mruby-esp32/mruby-esp32.git
1618
```
1719

18-
The main ruby program can be found in the `main/simplest_mrb.rb` file. The
19-
makefile configuration in `main/component.mk` and the main entry point source
20-
file `mruby_main.c` will also be of interest if you want to change the name of
21-
the ruby script. The examples included are very simple scripts that only print
22-
to the ESP32's debug console.
20+
The makefile configuration is in `main/component.mk`. The entry point source
21+
file is `mruby_main.c`. Once that starts, it looks for `storage/main.rb` and runs it in mruby.
22+
You can change the expected filename in `mruby_main.c`, or simply save your scripts as `main.rb`
23+
inside the `storage` subfolder.
2324

24-
I'm assuming you have followed all the steps in the install documentation and
25-
are at least somewhat familiar with the building steps. With that in mind you
26-
can do something like the following and see the example running:
25+
### First Build
2726

2827
```
29-
cp main/examples/$(YOU_WISH_TO_TRY_FILE) main/spiffs/main.rb
3028
idf.py build
3129
idf.py -p $(YOUR_SERIAL_PORT) flash monitor
3230
```
31+
Your ESP32 should write 2 lines of numbers to the console.
3332

34-
The valiable `YOU_WISH_TO_TRY_FILE` can be replaced with one of the following:
33+
### Examples
3534

36-
* _simplest_mrb.rb_ - Simply prints two strings
37-
* _gpio.rb_ - An example of using GPIO
38-
* _wifi_example_mrb.rb_ - An example of connecting to WiFi, you will need to
39-
modify this file to include your SSID and password
40-
* _mqtt_publish.rb_ - An sample of publishing to MQTT broker
41-
* _system_mrb.rb_ - Examples of most of the system APIs
35+
The folder `main/examples` includes simple scripts demonstrating functionality.
4236

43-
The clean command will clean both the ESP32 build and the mruby build:
37+
Once you are familiar with the build process, try them with:
4438

4539
```
46-
idf.py fullclean
40+
cp main/examples/$(EXAMPLE_FILENAME) main/storage/main.rb
41+
idf.py build
42+
idf.py -p $(YOUR_SERIAL_PORT) flash monitor
4743
```
4844

49-
There are multiple GEMS that can be turned on and off via the mruby
45+
Replace `EXAMPLE_FILENAME` with one of the following:
46+
47+
* `simplest.rb` - Prints two strings
48+
* `system.rb` - Demonstrates most system APIs
49+
* `gpio.rb` - GPIO blink example
50+
* `wifi_connect.rb` - Connects to WiFi
51+
* `wifi_socket.rb` - Connects to WiFi and makes a TCPSocket connection
52+
* `mqtt_publish.rb` - Connects to WiFi and publishes to MQTT broker
53+
* `filesystem.rb` - Write/append/read a file on the virtual filesystem
54+
* `ledc_breathe.rb` - Gradually fades the brightness of an LED up and down
55+
* `ledc_buzzer.rb` - Plays a melody on a piezo-electric buzzer
56+
* `ledc_servo.rb` - Controls position of a 180 degree hobby servo motor
57+
58+
**Note**: Edit GPIO numbers to match ones you are connected to, insert your WiFI credentials, customize MQTT settings etc.
59+
60+
### Build Customization
61+
62+
There are multiple gems that can be turned on and off via the mruby
5063
configuration file found in
5164
`components/mruby_component/esp32_build_config.rb`:
5265

53-
* _mruby-esp32-system_ - ESP32 system calls
54-
* _mruby-esp32-wifi_ - ESP32 WiFi
55-
* _mruby-esp32-mqtt_ - ESP32 MQTT library
66+
* [_mruby-socket_](https://github.com/mruby-esp32/mruby-socket/tree/0.5) - ESP32 Socket library (modified from mruby)
67+
* [_mruby-esp32-system_](https://github.com/mruby-esp32/mruby-esp32-system/tree/0.5) - ESP32 system calls
68+
* [_mruby-esp32-wifi_](https://github.com/mruby-esp32/mruby-esp32-wifi/tree/0.5) - ESP32 WiFi
69+
* [_mruby-esp32-mqtt_](https://github.com/mruby-esp32/mruby-esp32-mqtt/tree/0.5) - ESP32 MQTT library
70+
* [_mruby-esp32-gpio_](https://github.com/mruby-esp32/mruby-esp32-gpio/tree/0.5) - ESP32 GPIO library
71+
* [_mruby-esp32-ledc_](https://github.com/mruby-esp32/mruby-esp32-ledc/tree/0.5) - ESP32 LEDC (PWM) library
72+
73+
To get gem changes to reflect in the build, `fullclean` the previous build, then build again:
74+
```
75+
idf.py fullclean
76+
idf.py build
77+
```
78+
79+
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.
80+
81+
## Hardware
82+
83+
Everything works on:
84+
- Original ESP32: `idf.py set-target esp32`
85+
86+
Everything except gpio gem works on:
87+
- ESP32-S2: `idf.py set-target esp32s2`
88+
- ESP32-S3: `idf.py set-target esp32s3`
89+
90+
If you followed the IDF installation instructions correctly for your chip,
91+
you can switch the project target with the corresponding command above.
92+
93+
You will probably not be able to build again, until the project partition table is reset to `partitions.csv`. To do this:
94+
95+
```
96+
idf.py menuconfig
97+
# Partition Table -> Partition Table (1st option) -> Custom partition Table CSV (Last options)
98+
# Enter to select. Q to exit. Y to save
99+
```
100+
101+
## Troubleshooting
102+
103+
The following files and folders are safe to delete when trying to solve build issues:
104+
- `build`
105+
- `components/mruby_component/build`
106+
- `components/mruby_component/esp32_build_config.rb.lock`
107+
- `managed_components`
108+
- `dependencies.lock`
56109

110+
This project uses [littlefs](https://github.com/littlefs-project/littlefs) through IDF component
111+
manager. If you see errors about files on disk changing, try deleting the last 2 items on this list.
57112

components/esp_littlefs

Submodule esp_littlefs added at 8fb0290

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 mqtt driver esp_timer
7+
REQUIRES esp_hw_support esp_rom esp_timer esp_wifi driver 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 mqtt driver
20+
PRIV_REQUIRES esp_hw_support esp_rom esp_timer esp_wifi driver mqtt
2121
)
2222
target_link_libraries(${COMPONENT_LIB} INTERFACE libmruby)
2323

components/mruby_component/esp32_build_config.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@
6161

6262
conf.gem :core => "mruby-print"
6363
conf.gem :core => "mruby-compiler"
64-
conf.gem :github => "mruby-esp32/mruby-esp32-system"
65-
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'
68-
conf.gem :github => "mruby-esp32/mruby-esp32-gpio"
64+
conf.gem :github => "mruby-esp32/mruby-io", :branch => '0.5'
65+
conf.gem :github => "mruby-esp32/mruby-fileio", :branch => '0.5'
66+
conf.gem :github => "mruby-esp32/mruby-socket", :branch => '0.5'
67+
68+
conf.gem :github => "mruby-esp32/mruby-esp32-system", :branch => '0.5'
69+
conf.gem :github => "mruby-esp32/mruby-esp32-wifi", :branch => '0.5'
70+
conf.gem :github => "mruby-esp32/mruby-esp32-mqtt", :branch => '0.5'
71+
72+
conf.gem :github => "mruby-esp32/mruby-esp32-gpio", :branch => '0.5'
73+
conf.gem :github => "mruby-esp32/mruby-esp32-ledc", :branch => '0.5'
6974
end

main/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ idf_component_register(
22
SRCS mruby_main.c
33
INCLUDE_DIRS .
44
REQUIRES mruby_component
5-
PRIV_REQUIRES nvs_flash spiffs
5+
PRIV_REQUIRES nvs_flash esp_littlefs
66
)
77

8-
spiffs_create_partition_image(storage ./spiffs FLASH_IN_PROJECT)
8+
littlefs_create_partition_image(storage ./storage FLASH_IN_PROJECT)

main/examples/filesystem.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Get the number of previous boots from a file.
2+
boot_count = nil
3+
File.open('/storage/boot_count.txt', 'a+') { |f| boot_count = f.gets }
4+
5+
# Increment it.
6+
boot_count = 0 unless boot_count
7+
boot_count = boot_count.to_i
8+
boot_count += 1
9+
10+
# Write new count back to file.
11+
File.open('/storage/boot_count.txt', 'w') { |f| f.puts(boot_count) }
12+
13+
# Display it.
14+
puts "Boot count: #{boot_count}"
15+
16+
# PERSISTENCE DEMO
17+
persistence_file = "/storage/test.txt"
18+
19+
# If first boot, write then append to the test file.
20+
if boot_count == 1
21+
string1 = "testing "
22+
string2 = "1,2,3..."
23+
24+
puts "Writing to #{persistence_file}: \"#{string1}#{string2}\""
25+
26+
File.open(persistence_file, 'w') { |f| f.write(string1) }
27+
File.open(persistence_file, 'a') { |f| f.write(string2) }
28+
end
29+
30+
# Read the file back on every boot.
31+
print "Read from #{persistence_file}: "
32+
File.open(persistence_file, 'r') { |f| f.each_line { |l| puts l } }
33+
34+
# OVERWRITE DEMO
35+
overwrite_file = '/storage/overwrite.txt'
36+
37+
File.open(overwrite_file, 'w') { |f| f.puts "12345678" }
38+
File.open(overwrite_file, 'w') { |f| f.puts "1234" }
39+
40+
puts "#{overwrite_file} should contain: 1234"
41+
print "#{overwrite_file} contains: "
42+
File.open(overwrite_file, 'r') { |f| f.each_line { |l| puts l } }

main/examples/ledc_breathe.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
group = ESP32::LEDC_LOW_SPEED_MODE
2+
channel = ESP32::LEDC_CHANNEL_0
3+
timer = ESP32::LEDC_TIMER_0
4+
resolution = ESP32::LEDC_TIMER_8_BIT
5+
frequency = 1000
6+
pin = ESP32::GPIO::GPIO_NUM_2 # Built in LED on original ESP32 devkit.
7+
8+
ESP32::LEDC.timer_config(group, timer, resolution, frequency)
9+
ESP32::LEDC.channel_config(pin, group, timer, channel)
10+
11+
# Fade the LED up and down.
12+
loop do
13+
i = 0
14+
while (i < 256) do
15+
ESP32::LEDC.set_duty(group, channel, i)
16+
i += 1
17+
ESP32::System.delay(10)
18+
end
19+
20+
i=255
21+
while (i > -1) do
22+
ESP32::LEDC.set_duty(group, channel, i)
23+
i -= 1
24+
ESP32::System.delay(10)
25+
end
26+
end

main/examples/ledc_buzzer.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
group = ESP32::LEDC_LOW_SPEED_MODE
2+
channel = ESP32::LEDC_CHANNEL_0
3+
timer = ESP32::LEDC_TIMER_0
4+
resolution = ESP32::LEDC_TIMER_8_BIT
5+
pin = ESP32::GPIO::GPIO_NUM_4
6+
7+
# Configure the channel once.
8+
ESP32::LEDC.channel_config(pin, group, timer, channel)
9+
10+
# Note frequencies.
11+
C4 = 262
12+
D4 = 294
13+
E4 = 330
14+
15+
# Melody to play.
16+
notes = [
17+
[E4, 1], [D4, 1], [C4, 1], [D4, 1], [E4, 1], [E4, 1], [E4, 2],
18+
[D4, 1], [D4, 1], [D4, 2], [E4, 1], [E4, 1], [E4, 2],
19+
[E4, 1], [D4, 1], [C4, 1], [D4, 1], [E4, 1], [E4, 1], [E4, 1], [E4, 1],
20+
[D4, 1], [D4, 1], [E4, 1], [D4, 1], [C4, 4],
21+
]
22+
23+
# Calculate length of one beat in milliseconds.
24+
bpm = 180
25+
beat_time = 60000.to_f / bpm
26+
27+
# Play the melody.
28+
notes.each do |note|
29+
# Set timer frequency to 0th element of the note array.
30+
ESP32::LEDC.timer_config(group, timer, resolution, note[0])
31+
# Duty cycle to 50% for square wave.
32+
ESP32::LEDC.set_duty(group, channel, 128)
33+
34+
# Wait for length of the note, 1st element.
35+
ESP32::System.delay(note[1] * beat_time)
36+
37+
# Duty cycle to 0 to stop note.
38+
ESP32::LEDC.set_duty(group, channel, 0)
39+
end

0 commit comments

Comments
 (0)
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