# A guide for installing esp8266 for Arduino on FreeBSD



## Raffeale (Jan 14, 2021)

Today I will tell you how to install ESP8266 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 "ESP8266 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/esp8266/Arduino.git esp8266
cd esp8266
git submodule update --init --recursive
```

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

Because the ESP8266 does not support FreeBSD you need to change get.py to download the xtensa tools chain to compile ESP8266 project.
This step is downloading xtensa tools chain for linux64 , so you have to put `linux_load="YES"` in your /boot/loader.conf to enable linux  ELF file support
Modify  tools/get.py
add a line in get.py  in 104 lines


```
100
101 def identify_platform():
102     arduino_platform_names = {'Darwin'  : {32 : 'i386-apple-darwin',   64 : 'x86_64-apple-darwin'},
103                               'Linux'   : {32 : 'i686-pc-linux-gnu',   64 : 'x86_64-pc-linux-gnu'},
104                               'FreeBSD'   : {32 : 'i686-pc-linux-gnu',   64 : 'x86_64-pc-linux-gnu'},
105                               'LinuxARM': {32 : 'arm-linux-gnueabihf', 64 : 'aarch64-linux-gnu'},
106                               'Windows' : {32 : 'i686-mingw32',        64 : 'x86_64-mingw32'}}
```

set permission for esp8266 directory


```
chmod -R 755 /usr/local/arduino/hardware/espressif/esp8266
```

Run get.py to download the xtensa tools chain


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

Install esptools

```
cd /usr/local/arduino/hardware/espressif/esp8266/tools
rm -r esptool
git clone https://github.com/espressif/esptool.git esptool
```

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


Now we must patch arduino-builder to compile esp8266 project properly . the arduino 1.8.5 use arduino-builder to build arduino project ,there is a bug ,so you have to fix it with the patch
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.

```
--- src/arduino.cc/builder/utils/utils.go.orig    2021-01-13 22:48:24.097459000 +0800
+++ src/arduino.cc/builder/utils/utils.go    2021-01-13 22:49:18.264758000 +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 py37-pyserial
```

Make a python3 soft link in /usr/local/arduino/hardware/espressif/esp8266/tools/python3

```
cd /usr/local/arduino/hardware/espressif/esp8266/tools
mkdir -p python3
cd python3
ln -s /usr/local/bin/python3.6 python3
```
Reboot you machine and run arduino, you will see about esp8266 board in your board menu (NODE Mcu , ES8266 Gerneric etc...), that means you have installed successfully.
Congratulation installation is done!

Now you can compile esp8266 example in arduino

#Note
1. There are some Arduino libraries which conflict with the esp8266 library. You have to move /usr/local/arduino/libraries/Wifi  and SD modules to /usr/local/arduino/hardware/arduino/avr/libraries
2.esptools download link https://github.com/espressif/esptool
3.all steps need superuser to do that or use sudo


----------



## Alexander Mishin (May 31, 2022)

I got an error while trying to compile:

```
*** File preferences.txt not found on FreeBSD

*** 'preferences.txt' file missing from well known locations.
exit status 1
```

Looks like espressif/esp8266/tools/mkbuildoptglobals.py needs to be changed too.

```
-- espressif/esp8266/tools/mkbuildoptglobals.py.orig 2022-05-31 12:41:12.777025000 +0400
+++ espressif/esp8266/tools/mkbuildoptglobals.py 2022-05-31 12:41:40.992427000 +0400
@@ -430,7 +430,7 @@
       from: https://www.arduino.cc/en/hacking/preferences
     """
     platform_name = platform.system()
-    if "Linux" == platform_name:
+    if "Linux" == platform_name or "FreeBSD" == platform_name:
         # Test for portable 1ST
         # <Arduino IDE installation folder>/portable/preferences.txt (when used in portable mode)
         # For more on portable mode see https://docs.arduino.cc/software/ide-v1/tutorials/PortableIDE
```
Now I see:

```
/home/username/sketchbook/WiFi_Sensors/WiFi_Sensors.ino:1:10: fatal error: OneWire.h: No such file or directory
    1 | #include <OneWire.h>
      |          ^~~~~~~~~~~
compilation terminated.
exit status 1
```
Looking for a proper way to determine the path to libraries added by the user (to the string "~/sketchbook/libraries/")...


----------



## Raffeale (Jul 1, 2022)

Alexander Mishin said:


> I got an error while trying to compile:
> 
> ```
> *** File preferences.txt not found on FreeBSD
> ...



it looks like you forgot to patch src/arduino.cc/builder/utils/utils.go , you need install Arduino from ports and patch this file.


----------



## SirDice (Jul 1, 2022)

Mind if I move this to "Userland programming and Scripting"? You're not installing or running FreeBSD on the ESP8266 itself 

The "Embedded" section is for embedded systems running FreeBSD.


----------



## jbo (Jul 1, 2022)

SirDice said:


> Mind if I move this to "Userland programming and Scripting"?


Or maybe the "HowTo" section?


----------



## SirDice (Jul 1, 2022)

jbodenmann said:


> Or maybe the "HowTo" section?


That would be fine too, it certainly looks like a "how to".


----------

