|
1 |
| -# Example of mruby on the ESP32 |
| 1 | +# mruby on the ESP32 |
2 | 2 |
|
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. |
6 | 4 |
|
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. |
10 | 8 |
|
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: |
13 | 15 |
|
14 | 16 | ```
|
15 | 17 | git clone --recursive https://github.com/mruby-esp32/mruby-esp32.git
|
16 | 18 | ```
|
17 | 19 |
|
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. |
23 | 24 |
|
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 |
27 | 26 |
|
28 | 27 | ```
|
29 |
| -cp main/examples/$(YOU_WISH_TO_TRY_FILE) main/spiffs/main.rb |
30 | 28 | idf.py build
|
31 | 29 | idf.py -p $(YOUR_SERIAL_PORT) flash monitor
|
32 | 30 | ```
|
| 31 | +Your ESP32 should write 2 lines of numbers to the console. |
33 | 32 |
|
34 |
| -The valiable `YOU_WISH_TO_TRY_FILE` can be replaced with one of the following: |
| 33 | +### Examples |
35 | 34 |
|
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. |
42 | 36 |
|
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: |
44 | 38 |
|
45 | 39 | ```
|
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 |
47 | 43 | ```
|
48 | 44 |
|
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 |
50 | 63 | configuration file found in
|
51 | 64 | `components/mruby_component/esp32_build_config.rb`:
|
52 | 65 |
|
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` |
56 | 109 |
|
| 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. |
57 | 112 |
|
0 commit comments