In embedded development, we have Arduino
and CircuitPython
as software initiatives to improve development productivity. Today, a new standard in embedded development is born.
It is the RESTuino
, interactive embedded development firmware for ESP32.
I created an external API layer for the hardware abstraction layer to enable GPIO operations from outside the microcontroller.
In this case, I created RESTful API
for the Arduino
hardware abstraction layer inside the ESP32
microcontroller to enable GPIO operations.
Specifically, you can specify a GPIO number in the URL, POST
to define a pin state (digitalWrite, digitalRead, etc.), PUT
to update the value (digitalWrite to HIGH, LOW, etc.), GET
to get current information, DELETE
to disable a pin state. And if you do save
, the pin state is saved in EEPROM, so it is easy to operate as client-side software in IoT.
🌱 RESTuino
makes it possible to communicate system functions & data in the IoT.
✨ RESTuino
allows us to make...
IoT client
Arduino GPIOs can be manipulated via REST API.Interactive Microcomputer Programming
via curl, Talend API Tester or similer.
✍ RESTuino
can be operated with...
- curl
- Homebridge
- Python(requests)
- Talend API Tester
or similer.
-
ESP32-based board
The tested boards are as follows. Other products can also be used if they are equipped with esp32.
-
PlatformIO or Arduino IDE
See PlatformIO example, Arduino IDE example
You can install the library in two ways.
-
Clone this repository.
$ git clone https://github.com/takeyamayuki/RESTuino.git
-
Open the
esp32_platformio
example in PlatformIO oresp32_basic
example in Arduino IDE. -
Define
ssid
,password
of your wifi router by changingssid_def
,ssid_pass
. If more than one RESTuino is to be installed, changehost_name
.serial_baud
is the baud rate of the serial monitor.9600
is the default value.restuino.host_name = "restuino"; restuino.ssid_def = "YOUR_SSID"; restuino.ssid_pass = "YOUR_PASSWORD"; restuino.serial_baud = 9600;
-
Build and upload.
-
Clear EEPROM.
Initialize the RESTuino system when you see
WiFi connected.
on the serial monitor.$ curl restuino.local/ -X DELETE Change all pins to nan status...
-
Use this the way you want to use it.
- Specify the GPIO number in the URL
POST
to define the pin status (digitalWrite, digitalRead, etc.)PUT
to update the value (HIGH for digitalWrite, etc.)GET
to obtain current information (digitalRead, etc.)DELETE
to disable the pin status
Note
When sending the request body, always specifyContent-Type: text/plain
as the header.
http://restuino.local/gpio(pin_number)
(pin_number) is the pin number of the GPIO. For example, http://restuino.local/gpio15
is the GPIO15 of ESP32.
It is recommended to use http://restuino.local
instead of accessing the IP address, because of the heavy use of ESP.restart()
in the RESTuino process.
Specify the target GPIO pin by URL.
Use POST
to set the status of a pin.
Request body: digitalRead
| digitalWrite
| analogRead
| ledcWrite
| Servo
| (touch)
| (dacWrite)
# e.g. digitalWrite
$ curl restuino.local/gpio15 -X POST -H 'Content-Type: text/plain' -d 'digitalWrite'
digitalWrite
Use PUT
to change or define the output value of any pin.
The following request body can be sent when the GPIO pin set by POST
is in the following state.
-
digitalWrite
Request body:
HIGH
|LOW
|0
|1
# e.g. $ curl restuino.local/gpio15 -X PUT -H 'Content-Type: text/plain' -d 'LOW' LOW
-
ledcWrite
(alternative toanalogWrite
in esp32)Request body:
0~256 numbers
# e.g. $ curl restuino.local/gpio15 -X PUT -H 'Content-Type: text/plain' -d '100' 100
-
Servo
Request body:0~180 numbers
|switch
0~180 numbers
: a servo motor moves to the angle specified by value.switch
: Each time the following command is sent, the servo motor moves back and forth betweenangle
andangle0
.
# e.g. $ curl restuino.local/gpio15 -X PUT -H 'Content-Type: text/plain' -d '88' 88
Use GET
to get the status of any pin.
The following response body is received when the pin set by POST
is in the following state.
analogRead
:0~4095 numbers
digitalRead
:0
|1
Servo
:0~180 numbers
# e.g. digitalRead
$ curl restuino.local/gpio1 -X GET
0
Use DELETE
to disable any pin (actually, save the setting in EEPROM and restart).
# e.g.
$ curl restuino.local/gpio1 -X DELETE
Change the pin to nan status...
-
http://(IP_address)
The IP address can be obtained via serial communication orhttp://restuino.local
viaGET
method (It's easy with a browser).
It is recommended to usehttp://restuino.local
instead of accessing the IP address, because of the heavy use ofESP.restart()
in the RESTuino process.
Request body: save
| load
| reboot
save
: Save the current GPIO setting status to EEPROM.load
: Load GPIO settings stored in EEPROM and reflect to actual pin settings.reboot
: Reboot ESP32.
Note
Theload
is done automatically at startup, but the user mustsave
before turning off the power.
When operating as an IoT client, once the pin state is saved, it will be saved in eeprom, so there is no need tosave
after that.
# e.g.
$ curl restuino.local/ -X POST -H 'Content-Type: text/plain' -d 'save'
Wrote
Obtain IP address and GPIO status. GPIO status is output in chunks of 40. Starting from the first left digit, GPIO0, 1,2.... and its value is defined as follows.
$ curl restuino.local/ -X GET
{
"IP address": "192.168.0.29",
"GPIO status": "2000000050000002200000000000000000000000"
}
number | meaning |
---|---|
0 | nan |
1 | digitalread |
2 | digitalwrite |
3 | analogread |
4 | ledcwrite |
5 | servo |
6 | (touch) |
7 | (dacwrite) |
e.g.
Looking at the first digit of GPIO status(GPIO0), there is a number 2, which means digitalwrite
. Therefore, pinMode(0,OUTPUT)
is executed internally so that GPIO0
becomes digitalwrite
.
Disable all pins (actually, save the setting in EEPROM and restart).
$ curl restuino.local/ -X DELETE
Change all pins to nan status...
Not defined.
See examples.
-
python-L-chika : Programs to turn on/off the LED connected to GPIO15 every second.
-
python-Servo-switch : Programs to move the servo (connected to GPIO15) to the angle defined by
angle
,angle0
every second.
-
Run setup.py
$ python setup.py
Configure GPIO settings. Run it only once.
-
Run main.py
$ python main.py
This is the main program. Use only this program when incorporating it.
RESTuino_demo_python.mp4
The program is here.
-
curl-digitalRead: Scripts that executes
digitalRead
to GPIO15 and obtains its status. -
curl-Servo: Programs to move the servo (connected to GPIO15) to 88°.
-
Run setup.sh
$ chmod +x setup.sh $ ./setup.sh
Configure GPIO settings. Run it only once.
-
Run main.sh
$ chmod +x main.sh $ ./main.sh
This is the main program. Use only this program when incorporating it.
RESTuino_curl.mp4
The program is here.
homebridge is a lightweight NodeJS server you can run on your home network that emulates the iOS HomeKit API. This allows you to use iOS apps to control devices that do not natively support HomeKit.
In the homebridge config editor, add the following to the accessories
section:
"accessories": [
{
"accessory": "CMD",
"name": "light",
"on_cmd": "curl restuino.local/gpio15 -X PUT -H 'Content-Type: text/plain' -d '56'",
"off_cmd": "curl restuino.local/gpio15 -X PUT -H 'Content-Type: text/plain' -d '6'"
}
]