ESP32 Raspberry Pi

ESP32 Setup on a Raspberry Pi

Today I decided to see if I could get the Espressif ESP32 Development Toolkit to work on my Raspberry Pi 4. The Raspberry Pi is running Linux (albeit an Arm Architecture) so naturally the best starting places was to follow the Linux instructions at and hope that it was a dual binary.

Once it had been installed it was quite obvious that it only supported the x86 architecture – which was confirmed using the ‘file’ command on one of the executables in the bin/ directory.

$ file ~/esm/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc
xtensa-esp32-elf-gcc: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/, for GNU/Linux 2.6.32, BuildID[sha1]=81a0b55c5c0c1bae1bb9d9ef2ffafc0a054bedaf, stripped

I noticed at the bottom of the Linux Setup page a section titled “Related Documents” and that section has a link Setup Linux Toolchain from Scratch.

Install Prerequisites

I noted that the instruction were for Debian 9 but the Raspberry Pi was running Debian 10 however there didn’t seem to be any missing packages.

sudo apt-get install gcc git wget make libncurses-dev flex bison gperf python python-pip python-setuptools python-serial python-cryptography python-future python-pyparsing

sudo apt-get install gawk gperf grep gettext libncurses-dev python python-dev automake bison flex texinfo help2man libtool libtool-bin

Building the Toolchain from Source

I followed the instructions and entered the following commands:-

mkdir ~/esp
cd ~/esp
git clone -b xtensa-1.22.x
cd crosstool-NG
./bootstrap && ./configure --enable-local && make install

I expected the last command in the above section to take quite a while since it had a configuration and “make install” commands. I was expecting a coffee break so was a bit concerned when it finished in a matter of seconds.

Anyway it didn’t indicate that there had been any errors so I proceeded with the next section.

./ct-ng xtensa-esp32-elf
./ct-ng build
chmod -R u+w builds/xtensa-esp32-elf

I needn’t have worried about how quick the earlier step had been because the second command ( “./ct-ng build” ) took exactly 66 minutes 30 seconds on a 4GB Raspberry Pi 4.

After that I continued with their instructions and downloaded esp-idf.

cd ~/esp
git clone -b v3.3 --recursive

The last thing to do was to add the paths to the end of ~.profile

export PATH=~/esp/crosstool-NG/builds/xtensa-esp32-elf/bin:${PATH}
export IDF_PATH=~/esp/esp-idf

At this point we must login again so that the paths are picked up from the update .profile file.

Time to Test the Build

I connected the ESP32 to one of the spare USB2 ports on the Raspberry Pi and checked to see if the device had appear “/dev/ttyUSB0”

I then navigated to “~/esp/esp-idf/examples/get-started/hello_world” and then ran the “make” command.

After a brief period a configuration menu appeared and I checked that the Serial Flasher Config was set to the correct serial port (/dev/ttyUSB0).

The build continued without problem, I entered “make flash” and held the boot button down on the ESP32 until it started to download.

Monitoring the output of the ESP32 over the serial port

There is a tool include with the framework that allow you to monitor the output from the ESP32. Simply enter the following command in the directory containing the Makefile.

make simple_monitor

After starting the monitor it proceeded to print a list of information received over the serial port.

Hello world!
 This is ESP32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 4MB external flash
 Restarting in 10 seconds…
 Restarting in 9 seconds…
 Restarting in 8 seconds…

It was not obvious how to exit the monitor and the usual CTRL + C didn’t work. After a bit of research it turned out the CTRL + ] is the option to quit the monitor.

Other Notes

  • If you need to the run configuration menu again then you can do so by entering “make menuconfig”.
  • You can enter “make help” to see what other options are available.
Raspberry Pi

Using the Raspberry Pi’s built-in VNC Server with iMac

The Raspberry Pi already has a VNC server installed but there are a couple of things that need to be done to use it.

Firstly you must enable it by running sudo rasp-config, selecting Interfacing Options and then making sure that VNC is enabled.

If you are going to use an iMac to access your Raspberry Pi then you must change the authentication mode on the Raspberry Pi VNC Server:-

  • sudo vncpasswd
  • sudo vi /root/.vnc/config.d/vncserver-x11
  • Add “Authentication=VncAuth”
  • For some reason the VNC server won’t send the display with the default screen resolution. Start raspi-config (sudo raspi-config) and select “Advanced Options / Resolution”. Now choose another resolution e.g. 1024×768.
  • Restart VNC Server using: sudo raspi-config

You should now be able to access your Raspberry Pi using your iMac’s built-in VNC viewer:-

  • Open Finder
  • Select “Go / Connect to Server” from the menu.
  • Enter “vnc://raspberruypi/” in the Server Address field and then click on connect.
  • You will be prompted to enter a password. Enter the password you supplied when you executed vncpasswd earlier.

Raspberry Pi

Raspberry Pi Headless Setup

A headless setup is one which isn’t going to be connected to a monitor, keyboard or mouse. In general it is appropriate if you are planning on using the device as a server rather than a general desktop computer.

So how can you configure your Raspberry Pi if it hasn’t got a screen, keyboard or mouse?

The answer is that you to access it via ssh (Secure Shell).

Firstly download an operating system image from you will want the “lite” version for a headless setup. Next install the image onto a microSD card using a tool such as balenaEtcher.

Your microSD card will now have two partitions, a boot partition in FAT format and the main Linux partition in ext4. Insert the microSD card into your computer (using an adapter if necessary) and a drive named “boot” should appear.

Open the “boot” drive and create an empty file simply called “ssh” in the top level of the drive – this will enable the ssh server when you first boot your Raspberry Pi.

If you are connecting your Raspberry Pi to the network using a standard ethernet cable then connect it now and skip the WIFI configuration.

WIFI Configuration

If you are connecting your Raspberry Pi to your WIFI network then you will been to add another file called “wpa_supplicant.conf” to the top level of the boot drive. The file should contain the following information but obviously update the country code, WIFI SID and WIFI password as appropriate.

 ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

First Boot

The microSD card can now be inserted into the Raspberry Pi and the system can be booted. After a few minutes it should be possible to login to the Raspberry Pi using a ssh client. The IP address that has been assigned to the Raspberry PI can by checking the config of the internet router that it is connected to. If there is only a single Raspberry Pi on the network then it might be possible to login simply by entering:-

ssh pi@raspberrypi

You will be prompted to enter a password. The initial password for the ‘pi’ user is ‘raspberry’ which should be changed immediately upon login using the passwd command.


IFTTT automation using crontab on Debian / Raspberry Pi

We were planning an extended vacation and had concerns about leaving the house empty for so long. As usual you get family and friends to look in every so often but I decided that it would be nice to have some additional security to make the house look occupied.

After a bit of research I decided on Internet Enable D-Link DCS-5030L Camera and a set of Meross Smart Plugs. The D-LINK camera was position in such a way that the door could be monitored while the Meross Smart Plugs were connected to various lights strategically placed around the home.

I setup everything up and it all worked perfectly using the Mobile Apps that I installed onto my Motorola Moto G6 Play. It took a weekend away to realise the problem with this setup – If your mobile was not connected to the mobile network or a WIFI signal then all the automation provided by the Apps became worthless.

The D-LINK camera had a simple solution in that it had its own built in webserver. This meant that I could browse to the camera on my home network where I discovered some settings that would cause it to send a series of photo by email. These photos covered three seconds before and three seconds after any detected movement – which was perfect for monitoring anything dropping through the letterbox.

The Meross SmartPlugs had no such interface and it turned out that the Phone App was all that was controlling them. If you were out of coverage the moment a switch on/off event was scheduled then that event was missed causing either the lights to stay on or off until the next event.

As it happens the Meross Plugs stated that they were compatible with IFTTT so I decided to see if that could help the situation. It was straightforward to get IFTTT to control the plugs and so the problems was solved, or so I though… until I realised that IFTTT needed some external trigger and suddenly I was back to square one :- The phone could be out of coverage and thus unable to control the plugs using IFTTT.

It quickly became apparent that I would need some programmable server attached to my home broadband connection to control IFTTT autonomously. As I had an old unused Raspberry Pi (1st gen) I decided to see if there was anyway it could be used to control the switches via IFTTT.

IFTTT has a facility whereby the item (in my case plugs) can be controlled by posting a trigger event to the IFTTT server. I installed curl onto my Raspberry Pi:-

  • sudo apt update
  • sudo apt install curl

Curl allowed me to send a POST message to IFTTT directly from the command line:-

  • curl -X POST{KEY}

Obviously “lights_on” and {KEY} would need to be updated with values that have been provided by IFTTT for your own application.

The next step was to get the Raspberry Pi to send those messages at the appropriate time. Fortunatly the Raspberry Pi runs Linux which has a featured called the crontab. The crontab allows you to schedule commands to be executed at specific times and dates.

# m h  dom mon dow   command
 36 17 * * * curl -X POST -X POST{KEY}
 57 18 * * * curl -X POST -X POST{KEY}
 58 18 * * * curl -X POST -X POST{KEY}
 24 19 * * * curl -X POST -X POST{KEY}
 25 19 * * * curl -X POST -X POST{KEY}
 30 21 * * * curl -X POST -X POST{KEY}
 36 21 * * * curl -X POST -X POST{KEY}
 36 21 * * * curl -X POST -X POST{KEY}
 37 21 * * * curl -X POST -X POST{KEY}
 17 23 * * * curl -X POST -X POST{KEY}
 25 23 * * * curl -X POST -X POST{KEY}
 19 00 * * * curl -X POST -X POST{KEY}

The first five columns in a crontab are used to indicate:- Minute past the hour, Hour in the day, Day of Month, Month and Day in Week. A ‘*’ character in any field means don’t care. Anything after the fifth column is regarded as the command that will be executed.

If you leave open a ssh port on the Raspberry Pi and configure your home router to forward any ssh connections to the Raspberry Pi then it will be possible to login and modify the schedule while away.

In my case this setup worked perfectly for the first five weeks and then everything stopped responding. I suspected the router had gone down so asked a family member to reset it. It turned out that a small water leak from the upstairs bathroom had come through the ceiling and soaked the router.

“But Mousie, thou art no thy-lane,
In proving foresight may be vain:
The best laid schemes o’ Mice an’ Men
          Gang aft agley,
An’ lea’e us nought but grief an’ pain,
          For promis’d joy”

from “To a Mouse” by Robert Burns