ESP's are SoC from Espressif https://www.espressif.com/en/products/socs
For a hobbyist take one of the many available modules, so no SMD soldering is required. The modules have as usual a USB connector, If necessary a USB to serial converter chip, 3.3V voltage regulator.
Everything is on the chip has to be 3.3V so keep away the damaging 5V.
Cheaper devices with the older ESP8266 chip require a programmer hardware containing a USB to UART chip.The ESP32 is a newer design and has USB already integrated.
ESP Home https://esphome.io/ allows to configure a ESP application using a
*.yaml file and then convert this into a running firmware (it also support RP2040 micro-controllers from the Raspberry foundation and others).
After having the device programmed it can be used in Home Assistant using the ESPHome integration.
When the esp device is plugged into the development computer lsusb will show what USB to serial chip is used on the esp board as:
QinHeng Electronics CH340 serial converter or Espressif USB JTAG/serial debug unit. It might be that the kernel is missing that driver and
ls /dev/tty* will not show it. In this case build a kernel with the CH341 module.
When having wireguard interfaces wg show turn them off, since those confuse ESPHome:
systemctl stop wg-quick@wg0
Give in the *.yaml file unique names, since Homeassistant requires it.
To work in a console and have it separated from Home Assistant, as regular user create and enter into a python virtual environment and do pip install esphome then esphome version or esphome --help.
esphome wizard <project name>.yaml initializes a project and creates the initial file<project name>.yaml
Now the file needs to be edited to get some peripheral functions as for the d1_mini_lite
board putting a switch from GPIO2 to ground.<project name>.yaml
switch:
- platform: gpio
name: "<switch name>"
pin: GPIO2
Now plug the device into the computer and when having attached to USB
esphome run <project name>.yaml --device /dev/tty<???>0
or when it was already programmed and has a working WLAN connection
esphome run <project name>.yaml
esphome logs espcam.yaml --device /dev/ttyACM0 will just print out the logs
To have it automatically found in Home Assistant, install the ESPHome Integration and make sure holds the api: line. Passwords or keys are optional. <project name>.yaml
# Enable Home Assistant API api: # password: ""
The ESPHome device builder is a IDE and can be used as http://<ip>:6052
It can be installed via "Add-on" store of the Home Assistant OS.
However the "Add-on" store is not available when Home Assistant got installed as docker application or if there is no Home Assistant.
Within the python virtual environment create a directory mkdir config and esphome dashboard config then open a WebSerial capable browser as chrome http://localhost:6052/
ESPHome device builder can be installed in a docker container:
If it takes part of a Home Assistant and go2rtc docker setup, then it should be added to a docker-compose.yml file:
services:
homeassistant:
container_name: homeassistant
image: "ghcr.io/home-assistant/home-assistant:stable"
volumes:
- /opt/smart-home/ha-config:/config
- /etc/localtime:/etc/localtime:ro
restart: unless-stopped
network_mode: host
go2rtc:
container_name: go2rtc
image: alexxit/go2rtc
volumes:
- /opt/smart-home/go2rtc-config:/config
restart: unless-stopped
network_mode: host
esphome:
container_name: esphome
image: ghcr.io/esphome/esphome:latest
volumes:
- /opt/smart-home/esphome-config:/config
- /etc/localtime:/etc/localtime:ro
environment:
- TZ=Europe/Zurich
restart: unless-stopped
network_mode: host
To run it as independent docker container create a directory sudo mkdir -p /opt/docker/esphome
Give permission for the local user sudo chown -R $(id -u):$(id -g) /opt/docker/esphome
docker run -d --name esphome --restart always --net=host -e TZ="Europe/Zurich" -v /opt/docker/esphome:/config ghcr.io/esphome/esphome:latest
Tasmota has firmware for the ESP chips. The firmware has a webserver that allows to configure it. There is no need to write code or install development tools. See:https://tasmota.github.io/docs/
Opening in a web browser supporting Web Serial (as Google Chrome, Firefox does not work) https://tasmota.github.io/install/ allows connecting the ESP and downloading the a Tasmota firmware.
Once the firmware is programmed the Tasmota device becomes a WLAN Web server. So connect to its network, there is no password and then type 192.168.4.1. A web page appears where the WLAN and Password for the local WLAN can be entered. After that it becomes a device of the hopefully protected local WLAN.
There are many different firmware version and variants around also in languages different from English.
OTA (Over the Air) updates are supported, to connect to a server and update itself.
Tasmota Web UI has spent a button to support the home automation system domoticz https://wiki.domoticz.com/Main_Page
Pressing the reset button for 40 seconds until the device's LED blinks rapidly will do a factory reset.
https://tasmota.github.io/install/ can also be used to have a console connection using USB and not using WLAN. Console commands can be sent or other gui commands as changing WLAN settings are possible.
Using a terminal with a serial link of 115200 baud gets the Tasmota prompt (pressing the reset button might be necessary).
Since memory on the ESP is limited not all what Tasmota offers can be put into a firmware. . Therefore it is not very clear what GPIOs configuratons and functions the uploaded firmware supports.
A simple test is to select a module type as Sonoff Basic (brand of smart home devices created by ITEAD) and set GPIO4 to Relay and verify that the number 1 appears right to it this makes it as RELAY1. For multiple relays assign the numbers uniquely. Attach a voltmeter on this pin. Then go to the console and do POWER1 ON to turn it on and POWER1 OFF to turn it off.
If Tasmota turns on POWER and therefor POWER1 after reset, then just use POWER2.
To attach a chip check https://tasmota.github.io/docs/Supported-Peripherals/ for its support and driver to be selected. As Example the Humidity Sensor DHT22 uses the AM2301 driver. Then select the GPIO pin (as GPIO4) to use the signal for the 1 wire DHT22 device.
Tasmota supports many different boards. How the boards connect the external peripherals creates lot of versions. To have a flexibility for this, and not required to recreate a additional firmware variant, templates have been introduced. Templates define how the GPIOx pins of the ESP32 are connected to the signals that Tasmota understands.
It is therefore not necessary to create a new firmware just because the hardware has its peripherals connected differently. Templates are json strings that can be entered in the Web UI → Configuration → Configure Other → Template
Templates can be exported also via this Web Ui or via the console command template.
Templates can be modified via Web UI → Configuration → Template
STATUS <n> will tell something about the firmware
Reset <n> can be used to do a factory reset
<n>
defines what will be wiped off. 4 attempts to keep the WLAN settings 5 not.
PowerOnState will show what will be done after power on. 0 means all outputs off, 3 means restore as after power down.
To have networking support as for Home Assistant add the IP, username and password for the MQTT broker. In Home Assistant add first MQTT and then add Tasmota.
To turn power 1 on using mqtt send mosquitto_pub -h <IP of broker> -t "cmnd/tasmota_<last 6 chars of the MAC address>/Power1" -m "ON" -u <user> -P <pass>
Tasmota devices are discovered in Home Assistant and ready to be used.
Inside the MQTT parameters window the default topics as tasmota_<last 6 chars of the MAC address> can be changed to a more meaningful name as kitchen_light_1
Sometimes Tasmota does not behave as desired, this can be modified using rules.
Rule shows the configured rules as Rule1
Rule1 shows Rule1
Examples:
Rule1 ON Wifi#Connected DO Power1 OFF ENDON
Rule1 ON Mqtt#Connected DO Power1 OFF ENDON
Rule1 ON System#Init DO Power1 OFF ENDON
Commands:
Rule1 ON activates the rule
Rule1 1 executes the rule right now without required event
Rule1 Off or Rule1 0 deactivates the rule
Rule1 "" makes it useless, it seems that there is no command to remove it from the list
The log will show when the rule has been executed:
log: 00:00:00.135 RUL: SYSTEM#INIT performs 'Power1 OFF'
There is a Master and a development branch available for the firmware. If not having a brand new device select Master. Download and unzip the source code https://tasmota.github.io/docs/Compile-your-build/ or https://github.com/arendst/Tasmota/tree/development
To have pio the platform io compiler create and enter to a virtual python environment in the tasmota folder and do pip install -U platformio
pio run -e tasmota32 compiles a simple firmware that can confirm that the environment is working.
The work goes in .pio and pio run --target clean to clean up
The firmware is found in build_output/firmware. There are two files tasmota32.bin used for web installer and OTA
tasmota32.factory.bin used for complete empty devices where the bootloader needs to be added. esptool.py write_flash 0x0 tasmota32.factory.bin would be the command.python3 -m pip install --upgrade esptool to get it.
pio home opens a browser http://127.0.0.1:8008/ where the work can be done.
Shellys usually use also ESP chips and have a nice case supporting 230Vac installations and all the required safety standards. They have a Shelly firmware that does and behave similar as the Tasmota firmware. If really desired, the Tasmota firmware could be downloaded to certain Shelly devices.
After factory reset (using a tooth peak press their button a couple of seconds until they flash) they create their own access point. It is best using a smart phone to connect to this WLAN using 192.168.33.1 and configure the device.
After configuration turn off the access point so not everybody can access it. It is also possible to keep the access point on but then set a password for the access point.
It can be accessed via web browser https://control.shelly.cloud or the Shelly App.
See https://shelly-api-docs.shelly.cloud/
export SHELLY=192.168.1.20 to not continuously have to type in the IP Address when working with curl
curl http://${SHELLY}/rpc/Shelly.GetStatus to check it on the console
curl http://${SHELLY}/rpc/Shelly.GetStatus curl -X POST -d '{"id":1,"method":"Cloud.GetConfig"}' http://${SHELLY}/rpc to get cloud info
curl -X POST -d '{"id":1,"method":"Cloud.SetConfig","params":{"config":{"enable":true}}}' http://${SHELLY}/rpc to set cloud
See https://shelly-api-docs.shelly.cloud/ and
Go to https://control.shelly.cloud/#/settings/user to get
The authorization cloud key (auth_key).
The auth_key does not expire, but can get revoked. Handle it as a password since it gets access to the users account and all device in the cloud
The cloud server uri as https://shelly-133-eu.shelly.cloud
The device id as cc8da244c471
It is a pure number and not shelly1minig3-cc8da244c471
To see if the cloud is in reached:
curl -I https://shelly-133-eu.shelly.cloud
ping -c3 shelly-133-eu.shelly.cloud
When working with curl export those things to not repeat them continuously
export=MmExMjYxxxxxxxxxxxxxxxxxxxxxxxxxxxxxC30
for the auth_key
export SHELLYURI=https://shelly-133-eu.shelly.cloud for the uri of the cloud server
export SHELLYID=cc8da144c474 for the unique id of the shelly device
The device must have the cloud enabled
curl -X POST "${SHELLYURI}/v2/devices/api/get?auth_key=${SHELLYKEY} -H 'Content-Type: application/json' -d '{"ids":["'"${SHELLYID}"'"]}' to see the device status
curl -X POST "${SHELLYURI}/v2/devices/api/set/switch?auth_key=${SHELLYKEY}" -H 'Content-Type: application/json' -d '{"id":"'"${SHELLYID}"'", "on":true}' to turn on its output
curl -X POST "${SHELLYURI}/v2/devices/api/set/switch?auth_key=${SHELLYKEY}" -H 'Content-Type: application/json' -d '{"id":"'"${SHELLYID}"'", "on":false}' to turn off its output
Usually Shellys are just used as input output devices. But certain devices allow to run scripts. The script language is similar to JavaScript.
See the tutorial https://shelly-api-docs.shelly.cloud/gen2/Scripts/Tutorial/
Features are https://shelly-api-docs.shelly.cloud/gen2/Scripts/ShellyScriptLanguageFeatures/
The script has an id number and can be managed with commands over the network see https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Script or more human friendly over its web interface.
See https://www.seeedstudio.com/XIAO-ESP32S3-Sense-p-5639.html and https://wiki.seeedstudio.com/xiao_esp32s3_getting_started/#for-seeed-studio-xiao-esp32s3 and the kit with e-paper and accelerometer https://www.seeedstudio.com/The-XIAOML-Kit.html
On 5V USB it uses about 170mA with camera and 100mA without
For video streaming use a quality power supply and a short cable otherwise it struggles so much that no stream occurs. A big capacitor on the 5V power supply might also help.
ML tutorial or exercise in the ML book https://mlsysbook.ai/kits/contents/seeed/xiao_esp32s3/xiao_esp32s3.html
A book https://mjrovai.github.io/XIAO_Big_Power_Small_Board-ebook/ that has also application for the additional kit but also application without additional hardware, as http, wlan, MQTT
Use a kernel that has CONFIG_USB_ACM enabled, that can be found under USB support => USB Modem. lsusb should show that the Seeed XIAO ESP32S3 is plugged in and ls -l /dev/ttyACM* should show that the device driver has been loaded.
Older ESP boards required a USB to UART chip this is no more required for the ESP32s3 but has the drawback that the UART functionality could get a conflict.
As IDE, arduino can be used and downloaded. The new versions of arduino come as AppImage and can be started without the hassle of doing an installation and dealing with dependencies. In the Tools => Boards => Boards Manager install esp32 by Espressif Systems
To have the tutorials working the esp32 by Espressif Systems version 2.0.17 needs to be installed and maybe an older version of arduino. However when the code becomes productive then the newest versions should be used.
Then select the board xiao_esp32s3
To see if all works select the File/Examples/Basics/Blink sketch and upload it
More Examples and code for the tutorials https://github.com/Mjrovai/XIAO-ESP32S3-Sense and https://github.com/limengdu/SeeedStudio-XIAO-ESP32S3-Sense-camera
pyserial must be installed on the system as for gentoo emerge -s pyserial
Since many examples require it, enable the pseudo static external RAM connected via SPI Tools->PSRAM:"OPI PSRAM"->OPI PSRAM
If updating a program does not work, make a reboot. Since everything is so tiny small the boot sequence is tricky. Use a USB hub with a power switch and turn it off and plug it into a slow USB socket. Plug in the board. With a tooth pick press the B (boot) button (next to yellow LED and on the left when USB is pointing down) then press the power switch of the USB hub and wait some seconds.
Example 3.2. Running from batteries
Two 3.7V 2600mAh LI-ION 18650 batteries store 19.24Wh Energy. Web streaming over WLAN from the camera takes 190mA this are 0.95W. This give 20h time and acceptable. Since web streaming is desired just for short moments the device could stay most of the time in sleep condition requiring just 25mA. This would then give up 154 h or 6 days.
When the device enters sleep mode it disappears from USB, This means it can not be reprogrammed anymore. Therefore the application should stay for about 10 seconds out of the sleep mode. The TimerWakeUP example https://wiki.seeedstudio.com/XIAO_ESP32S3_Consumption/has 1 second and this might not enough and brick the device. Applying the hardware factory reset procedure might also be critical since the attached PC needs time to assign a USB address. Don't use a fast USB-C port.
Table 3.1. Sleep mode values
| TimerWakeUP example without camera module | 0 mA | 14 mA |
| TimerWakeUP example with camera module | 17 mA | 43 mA |
| Camera DeepSleep example with SDcard | 22 mA | 127 mA |
| Web streaming from camera | 190 mA |
The tutorials have webcamera examples, some run some not. All are considered as demo or start to develop applications. They require to modify the code having WLAN username and password.
However there are many advanced project around that are ready to go as: https://github.com/rzeldent/esp32cam-rtsp/tree/develop (currently just the development branch holds support for XIAO ESP32-S3 Sense). Setup goes quick and easy see the web page or:
create a directory and go to it, create and enter a virtual python environment
pip install -U platformio to get pio that is also found when working in a subdirectory, so many platformio projects could be added underneath the top directory and pip is required to be installed just once.
git clone https://github.com/rzeldent/esp32cam-rtsp.git to get the application
and cd esp32cam-rtsp
pio pkg update -g -p espressif32 to make sure to have and update the espressif32 library
pio run -e esp32cam_seeed_xiao_esp32s3_sense to compile the application
pio run -t upload -e esp32cam_seeed_xiao_esp32s3_sense to upload the application in the device (pio run -t erase -e esp32cam_seeed_xiao_esp32s3_sense to erases all including previous set WLAN SSID and password)
To see what it tells pio device monitor
Access it using WLAN as from a smart-phone and if it does not show a web page open 192.158.4.1. Set the AP password of the Webcam and then type in the local SSID and its password. The monitor will show then the ip address received from the local WLAN.
Configuration page http://<replaceable><IP></replaceable>shows AP password and username: admin are used to change the settings
RTSP camera stream: rtsp://<replaceable><IP></replaceable>:554/mjpeg/1
JPEG Motion stream: http://<replaceable><IP></replaceable>/stream
Snapshot of the camera: http:/<replaceable><IP></replaceable>/snapshot
Restart the board: http://<replaceable><IP></replaceable>/restart
To access the camera in Homeassistant go to Settings → Devices & Services → Add Integration. Search for "MJPEG IP Camera" and add the links.