# A guide for installing ESP32 board for arduino on FreeBSD12[update-2021-08-17]



## Raffeale (Jan 13, 2021)

Hi all, there is a lot Arduino fans want to use ESP32 on FreeBSD but there is no solution now.
Today I installed ESP32 with Arduino on FreeBSD. It works very well. Ok let's start the guide.

You need to compile arduino18 from ports devel/arduino18

Keep the following patch files in /usr/ports/devel/arduino18/files directory and delete the other files:

```
patch-arduino-core_src_cc_arduino_contributions_packages_ContributionInstaller.java
patch-arduino-core_src_cc_arduino_contributions_packages_ContributionsIndexer.java
patch-arduino-core_src_cc_arduino_contributions_packages_HostDependentDownloadableContribution.java
patch-arduino-core_src_processing_app_BaseNoGui.java
patch-arduino-core_src_processing_app_SerialPortList.java
patch-build_build.xml
```


```
cd /usr/ports/devel/arduino18/
make clean all install       or  make clean reinstall (when you have already  installed arduino18 package)
```

When you finish installing arduino18 package port. you need to download "ESP32 for Arduino" package. Do the following commands with root or use sudo:

```
cd  /usr/local/arduino/hardware
mkdir -p espressif
cd  /usr/local/arduino/hardware/espressif
git clone https://github.com/espressif/arduino-esp32.git esp32
cd esp32
git submodule update --init --recursive
```

When you finish above steps you can see a directory /usr/local/arduino/hardware/espressif/esp32 and there are a lot of files in it.

Because the ESP32 does not support FreeBSD you need to change get.py to download the xtensa tools chain to compile ESP32 project.
This step is downloading xtensa tools chain for linux64. If you use xtensa for linux64 you have to put `linux_load="YES"` in your /boot/loader.conf.
Modify  tools/get.py

```
add a line in get.py  in 149 lines

def identify_platform():
147     arduino_platform_names = {'Darwin'  : {32 : 'i386-apple-darwin',   64 : 'x86_64-apple-darwin'},
148                               'Linux'   : {32 : 'i686-pc-linux-gnu',   64 : 'x86_64-pc-linux-gnu'},
149                               'FreeBSD'   : {32 : 'i686-pc-linux-gnu',   64 : 'x86_64-pc-linux-gnu'},
150                               'LinuxARM': {32 : 'arm-linux-gnueabihf', 64 : 'aarch64-linux-gnu'},
151                               'Windows' : {32 : 'i686-mingw32',        64 : 'i686-mingw32'}}
```

Run get.py to download the xtensa tools chain OR you also could use xtensa-esp32-elf package on FreeBSD


```
cd  /usr/local/arduino/hardware/espressif/esp32/tools
python get.py
```

When you finish above steps you will see a directory in your /usr/local/arduino/hardware/espressif/esp32/tools/xtensa-esp32-elf/ there are some directory in it ( bin , lib , include etc...).

If you want to use xtensa-esp32-elf to compile esp32 rather than xtensa for linux64, just install devel/xtensa-esp32-elf package.

```
pkg install xtensa-esp32-elf
cd  /usr/local/arduino/hardware/espressif/esp32/tools
ln -s /usr/local/xtensa-esp32-elf  .
```


change this file :/usr/local/arduino/hardware/espressif/esp32/platform.txt

```
change this line:
tools.esptool_py.cmd=esptool

to like this:
tools.esptool_py.cmd=esptool.py
```

When you finish above steps. Congratulation  you are almost done.

Now we must patch arduino-builder to compile esp32 project properly.
Compile arduino-builder from ports before do it you need create a patch file in /usr/ports/devel/arduino-builder/files, the filename is patch-src_arduino_cc_builder_utils_utils.go.
This step is the most important, if you do not patch it you can't compile a library in your esp32 arduino project[/B]

```
--- src/arduino.cc/builder/utils/utils.go.orig    2021-05-29 19:46:43.035414000 +0800
+++ src/arduino.cc/builder/utils/utils.go    2021-05-29 19:46:53.857019000 +0800
@@ -404,7 +404,7 @@ func NULLFile() string {
     if runtime.GOOS == "windows" {
         return "nul"
     }
-    return "/dev/null"
+    return "/tmp/null"
 }
 
 func MD5Sum(data []byte) string {
```

Compile arduino-builder

```
cd /usr/ports/devel/arduino-builder
make clean install
```

For use serial port to download program to esp board , you  need add serial port  driver into kernel
put these into /boot/loader.conf


```
uftdi_load="YES"
uchcom_load="YES"
uslcom_load="YES"
uarduno_load="YES"
```


Install python serial library

```
pkg install py27-pyserial
pkg install py38-pyserial
```

Reboot you machine , and run arduino , you will see about esp32 board in you board menu, that mean you have installed successfully
Congratulation installation is done!

if you have install both openjdk8 and openjdk11 , you will get some problem to start arduino.  or you could use SerialPort monitor you should do this.
edit your /usr/local/arduino/arduino files, add a line into it  like below

```
#!/usr/bin/env bash
export JAVA_HOME=/usr/local/openjdk8      #### add this line
```

#Note
1. There are some Arduino libraries which conflict with the esp32 library. You have to move /usr/local/arduino/libraries/Wifi  and SD modules to /usr/local/arduino/hardware/arduino/avr/libraries
2. upload is ok , bug program is not running correctly
(1) choose incorrectly partition scheme, choose the first scheme is okay for standard esp32s board
(2) esptools version is too old. Use the  latest esptools (esptools is upload tools for esp32, you can find it in /usr/local/arduino/hardware/espressif/esp32/tools)
3.esptools download link https://github.com/espressif/esptool
4.you need make link for python when you use FreeBSD13 ,  there is no python execution file in it , use "ln -s /usr/local/bin/python3.8 */usr/local/bin/python*"


*[update]
2021-02-11 fix configuration file incorrect to cause can't compile bug . (esptool.py)
2021-05-29 fix patch file incorrect and patch file in last thread
2021-08-17 fix a issuse when you install both openjdk8 and openjdk11 , you could run arduino properly.*


----------



## cederom (Feb 9, 2021)

Great work Raffeale ! Can you please patch upstream Arduino project so it works out of the box on FreeBSD? 

I have problem with `esptool` - Arduino GUI seems to call the binary while this is a python module.. is this necessary to finish the build?


```
Compiling libraries...
Compiling core...
Using precompiled core
Linking everything together...
"/usr/local/arduino/hardware/espressif/esp32/tools/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc" -nostdlib "-L/usr/local/arduino/hardware/espressif/esp32/tools/sdk/lib" "-L/usr/local/arduino/hardware/espressif/esp32/tools/sdk/ld" -T esp32_out.ld -T esp32.project.ld -T esp32.rom.ld -T esp32.peripherals.ld -T esp32.rom.libgcc.ld -T esp32.rom.spiram_incompatible_fns.ld -u ld_include_panic_highint_hdl -u call_user_start_cpu0 -Wl,--gc-sections -Wl,-static -Wl,--undefined=uxTopUsedPriority  -u __cxa_guard_dummy -u __cxx_fatal_exception   -Wl,--start-group "/tmp/arduino_build_568801/sketch/sketch_feb09a.ino.cpp.o" "/tmp/arduino_cache_347466/core/core_espressif_esp32_esp32doit-devkit-v1_FlashFreq_80,UploadSpeed_921600,DebugLevel_none_26e68511b094a3fa210bdb05375af441.a" -lgcc -lesp32-camera -lpe -lfatfs -lesp-tls -lnet80211 -lsoc -lwpa_supplicant -lapp_update -ldetection -lmicro-ecc -lesp_adc_cal -ltcpip_adapter -lapp_trace -lwear_levelling -lcoap -lesp32 -lcore -lesp_ringbuf -llibsodium -lsmartconfig -lprotobuf-c -lxtensa-debug-module -limage_util -lnewlib -lunity -lm -lcoexist -lface_detection -ljson -lcxx -lesp_websocket_client -lwpa2 -ltcp_transport -lulp -lesp_event -lesp_http_server -lmqtt -lhal -lmbedtls -lfreertos -lwps -lnghttp -lface_recognition -lethernet -lbootloader_support -ldetection_cat_face -lespcoredump -lnvs_flash -lfr -lphy -lopenssl -ljsmn -lpp -llwip -lwifi_provisioning -llog -lespnow -lc_nano -lvfs -lesp_https_server -lmdns -lbtdm_app -lfreemodbus -lsmartconfig_ack -lfd -lasio -lfb_gfx -lsdmmc -lrtc -lpthread -lconsole -lmesh -lc -lesp_http_client -lspiffs -lprotocomm -lesp_https_ota -lwpa -ldriver -lbt -lspi_flash -lexpat -lheap -ldl -lefuse -lod  -lstdc++ -Wl,--end-group -Wl,-EL -o "/tmp/arduino_build_568801/sketch_feb09a.ino.elf"
"/usr/local/arduino/hardware/espressif/esp32/tools/esptool/esptool" --chip esp32 elf2image --flash_mode "dio" --flash_freq "80m" --flash_size "4MB" -o "/tmp/arduino_build_568801/sketch_feb09a.ino.bin" "/tmp/arduino_build_568801/sketch_feb09a.ino.elf"
fork/exec /usr/local/arduino/hardware/espressif/esp32/tools/esptool/esptool: no such file or directory
Error compiling for board DOIT ESP32 DEVKIT V1.
```


----------



## Raffeale (Feb 9, 2021)

fork/exec /usr/local/arduino/hardware/espressif/esp32/tools/esptool/esptool: no such file or directory
could you find this file in  /usr/local/arduino/hardware/espressif/esp32/tools/esptool/esptool . 
you need use this tools  3.esptools download link https://github.com/espressif/esptool


----------



## cederom (Feb 9, 2021)

Thanks Raffeale 

From what I can see the esptool converts ELF to BIN. I have created a simple wrapper:


```
#!/bin/sh
/usr/bin/env python3.7 /usr/local/arduino/hardware/espressif/esp32/tools/esptool.py $1
```

for this esptool but both for bundled 2.9-dev and 3.0 version I have:


```
Linking everything together...
"/usr/local/arduino/hardware/espressif/esp32/tools/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc" -nostdlib "-L/usr/local/arduino/hardware/espressif/esp32/tools/sdk/lib" "-L/usr/local/arduino/hardware/espressif/esp32/tools/sdk/ld" -T esp32_out.ld -T esp32.project.ld -T esp32.rom.ld -T esp32.peripherals.ld -T esp32.rom.libgcc.ld -T esp32.rom.spiram_incompatible_fns.ld -u ld_include_panic_highint_hdl -u call_user_start_cpu0 -Wl,--gc-sections -Wl,-static -Wl,--undefined=uxTopUsedPriority  -u __cxa_guard_dummy -u __cxx_fatal_exception   -Wl,--start-group "/tmp/arduino_build_54872/sketch/sketch_feb09a.ino.cpp.o" "/tmp/arduino_build_54872/core/core.a" -lgcc -lesp32-camera -lpe -lfatfs -lesp-tls -lnet80211 -lsoc -lwpa_supplicant -lapp_update -ldetection -lmicro-ecc -lesp_adc_cal -ltcpip_adapter -lapp_trace -lwear_levelling -lcoap -lesp32 -lcore -lesp_ringbuf -llibsodium -lsmartconfig -lprotobuf-c -lxtensa-debug-module -limage_util -lnewlib -lunity -lm -lcoexist -lface_detection -ljson -lcxx -lesp_websocket_client -lwpa2 -ltcp_transport -lulp -lesp_event -lesp_http_server -lmqtt -lhal -lmbedtls -lfreertos -lwps -lnghttp -lface_recognition -lethernet -lbootloader_support -ldetection_cat_face -lespcoredump -lnvs_flash -lfr -lphy -lopenssl -ljsmn -lpp -llwip -lwifi_provisioning -llog -lespnow -lc_nano -lvfs -lesp_https_server -lmdns -lbtdm_app -lfreemodbus -lsmartconfig_ack -lfd -lasio -lfb_gfx -lsdmmc -lrtc -lpthread -lconsole -lmesh -lc -lesp_http_client -lspiffs -lprotocomm -lesp_https_ota -lwpa -ldriver -lbt -lspi_flash -lexpat -lheap -ldl -lefuse -lod  -lstdc++ -Wl,--end-group -Wl,-EL -o "/tmp/arduino_build_54872/sketch_feb09a.ino.elf"
"/usr/local/arduino/hardware/espressif/esp32/tools/esptool/esptool" --chip esp32 elf2image --flash_mode "dio" --flash_freq "80m" --flash_size "4MB" -o "/tmp/arduino_build_54872/sketch_feb09a.ino.bin" "/tmp/arduino_build_54872/sketch_feb09a.ino.elf"
usage: esptool [-h] [--chip {auto,esp8266,esp32,esp32s2beta}] [--port PORT]
               [--baud BAUD]
               [--before {default_reset,no_reset,no_reset_no_sync}]
               [--after {hard_reset,soft_reset,no_reset}] [--no-stub]
               [--trace] [--override-vddsdio [{1.8V,1.9V,OFF}]]
               [--connect-attempts CONNECT_ATTEMPTS]
               {load_ram,dump_mem,read_mem,write_mem,write_flash,run,image_info,make_image,elf2image,read_mac,chip_id,flash_id,read_flash_status,write_flash_status,read_flash,verify_flash,erase_flash,erase_region,version,get_security_info}
               ...
esptool: error: argument --chip/-c: expected one argument
exit status 2
Error compiling for board DOIT ESP32 DEVKIT V1.
```

This may indicate that maybe arduino-build tools are too old? Or my local configuration of the Board is invalid? Maybe we need to push this 1.8.12 PR 247124 update?


----------



## SirDice (Feb 9, 2021)

Maybe help out the maintainer by providing patches?


----------



## cederom (Feb 9, 2021)

Allright I made it working! I have put a quick instruction on FreeBSD WIKI with a reference to this Forum thread 



			electronics/arduino/esp32 - FreeBSD Wiki


----------



## cederom (Feb 9, 2021)

SirDice said:


> Maybe help out the maintainer by providing patches?


Yes I do lots of ports and patches, now in a bigger process, may come back into this in a free moment ;-) You cannot really solve all of the problems you encounter on the road because you won't achieve your own goal on time


----------



## Raffeale (Feb 10, 2021)

how to put some thread into wiki ?does it need some permission ?


----------



## cederom (Feb 10, 2021)

Yes, its not a public-write wiki, you can ask for access in #freebsd-wiki room at freenode IRC server 

You can write a change in PM and I can put that on WIKI no problem


----------



## cederom (Feb 10, 2021)

Flashing works too over `CP2102 USB to UART Bridge Controller` (USB-C `uslcom0` to UART kernel driver) 


```
python "/usr/local/arduino/hardware/espressif/esp32/tools/gen_esp32part.py" -q "/tmp/arduino_build_856062/partitions.csv" "/tmp/arduino_build_856062/02LoadConfigBitstream.ino.partitions.bin"
Using library spartan-edge-esp32-boot-master at version 1.0.0 in folder: /XXX/sketchbook/libraries/spartan-edge-esp32-boot-master
Using library SD_MMC at version 1.0 in folder: /usr/local/arduino/hardware/espressif/esp32/libraries/SD_MMC
Using library FS at version 1.0 in folder: /usr/local/arduino/hardware/espressif/esp32/libraries/FS
Sketch uses 293276 bytes (22%) of program storage space. Maximum is 1310720 bytes.
Global variables use 13928 bytes (4%) of dynamic memory, leaving 313752 bytes for local variables. Maximum is 327680 bytes.
/usr/local/arduino/hardware/espressif/esp32/tools/esptool/esptool --chip esp32 --port /dev/cuaU0 --baud 115200 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size detect 0xe000 /usr/local/arduino/hardware/espressif/esp32/tools/partitions/boot_app0.bin 0x1000 /usr/local/arduino/hardware/espressif/esp32/tools/sdk/bin/bootloader_dio_80m.bin 0x10000 /tmp/arduino_build_856062/02LoadConfigBitstream.ino.bin 0x8000 /tmp/arduino_build_856062/02LoadConfigBitstream.ino.partitions.bin
esptool.py v2.9-dev
Serial port /dev/cuaU0
Connecting........_____....._____....._
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 26MHz
MAC: XX:XX:XX:XX:XX:XX
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 8192 bytes to 47...
Writing at 0x0000e000... (100 %)
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.0 seconds (effective 7872.5 kbit/s)...
Hash of data verified.
Compressed 17120 bytes to 11160...
Writing at 0x00001000... (100 %)
Wrote 17120 bytes (11160 compressed) at 0x00001000 in 1.0 seconds (effective 138.9 kbit/s)...
Hash of data verified.
Compressed 293392 bytes to 147495...
Writing at 0x00010000... (10 %)
Writing at 0x00014000... (20 %)
Writing at 0x00018000... (30 %)
Writing at 0x0001c000... (40 %)
Writing at 0x00020000... (50 %)
Writing at 0x00024000... (60 %)
Writing at 0x00028000... (70 %)
Writing at 0x0002c000... (80 %)
Writing at 0x00030000... (90 %)
Writing at 0x00034000... (100 %)
Wrote 293392 bytes (147495 compressed) at 0x00010000 in 13.1 seconds (effective 179.8 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 128...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (128 compressed) at 0x00008000 in 0.0 seconds (effective 1598.8 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
```


----------



## Raffeale (Feb 10, 2021)

i have test it for esp32 with cp2102 serial chipset and esp8255 ftdi serial chipset! it works very well! thanks for your testing!


----------



## cederom (Feb 10, 2021)

Thank You Raffeale !!


----------



## Raffeale (Feb 11, 2021)

cederom said:


> Great work Raffeale ! Can you please patch upstream Arduino project so it works out of the box on FreeBSD?
> 
> I have problem with `esptool` - Arduino GUI seems to call the binary while this is a python module.. is this necessary to finish the build?
> 
> ...


i have already update the bug, it's my fault , i forgot to put a step which to update the esptool command in configuration file.
thanks you!


----------



## Raffeale (May 29, 2021)

upload the newest arduino-builder patch file,put this file into /usr/ports/devel/arduino-builder/files/  directory and change the name to patch-src_arduino_cc_builder_utils_utils.go


----------



## drhowarddrfine (May 29, 2021)

fwiw, a few years ago, I wrote software and built hardware for a theatrical production where a character on stage had to carry a bucket, pour lighter fluid into it, and act as if lighting a fire. Then carry the bucket elsewhere and pour more lighter fluid in giving the illusion of a flare up. All this had to be done wirelessly and controlled by the technical crew. I used FreeBSD for all development on the Arduino. It worked flawlessly and was a hit with the actors and crew.


----------



## kpedersen (May 29, 2021)

drhowarddrfine said:


> had to carry a bucket, pour lighter fluid into it, and act as if lighting a fire. Then carry the bucket elsewhere and pour more lighter fluid in giving the illusion of a flare up.



When I have to give the occasional talk at the university when the students return, I might have a chat with you. We could really spice these lectures up! XD


----------



## trombik (Aug 17, 2021)

if you are looking for toolchains for esp32 or esp8266, here is my private ports.


https://github.com/trombik/xtensa-esp32-elf (the official toolchain for esp-idf, supports arduino esp32 with `FLAVOR`)
https://github.com/trombik/freebsd-ports-xtensa-lx106-elf (the official toolchain for ESP8266 RTOS SDK, does not support arduino esp8266)
https://github.com/trombik/freebsd-ports-esp-quick-toolchain (a toolchain for arduino esp8266)

i've been working on the arduino GUI port, but not much progress because I actually don't use it.


----------



## trombik (Aug 18, 2021)

here is my summary post:

I have been developing applications for ESP32 and ESP8266 devices on FreeBSD for quite some time. Here is a summary of how to develop code for the embedded devices on FreeBSD.









						ESP32 and ESP8266 development on FreeBSD
					

I have been developing applications for ESP32 and ESP8266 devices on FreeBSD for quite some time. Here is a summary of how to develop code for the embedded devices on FreeBSD.



					info.mkrsgh.org


----------

