How to setup WiFi hotspot in raspberry pi and connect with ESP8266

The Hardware is

  • Esp8266 with arduino sdk
  • Raspberry pi 3b+

System requirements

  • arduino-cli
  • picocom
  • python3

Configurations for Raspberry Pi

Configurations for ESP8266

  • Install esp8266 for arduino
  • Copy the code and upload to esp8266 board

Raspberry Pi Setup

$ curl -LO https://downloads.raspberrypi.org/raspios_lite_armhf/images/raspios_lite_armhf-2020-08-24/2020-08-20-raspios-buster-armhf-lite.zip
$ unzip -p 2020-08-20-raspios-buster-armhf-lite.zip | sudo dd of=/dev/mmcblk0 status=progress
$ sudo systemctl enable --now ssh
$ ip addr show eth0 # copy ip address
$ exit
$ ssh pi@{ip_address}
$ sudo useradd -m -s /bin/bash --groups $(groups pi | cut -d ' ' -f 4- --output-delimiter ',') new-user
$ sudo passwd new-user
$ echo "new-user ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/010_new-user-nopasswd
$ su new-user
$ sudo apt update
$ groups new-user
$ exit
$ ssh-keygen -f ~/.ssh/rpi-key
$ ssh-copy-id -i ~/.ssh/rpi-key.pub new-user@{raspberry-pi-ip}
$ ssh new-user@{raspberry-pi-ip}
$ sudo userdel -rf pi
$ sudo apt update && sudo apt upgrade -y
$ sudo reboot

Setup hotspot

$ sudo apt-get -y install hostapd dnsmasq netfilter-persistent iptables-persistent
$ sudo systemctl unmask hostapd
$ sudo systemctl enable hostapd
interface wlan0
static ip_address=192.168.7.1/24
nohook wpa_supplicant
net.ipv4.ip_forward=1
$ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
$ sudo netfilter-persistent save
$ sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
interface=wlan0 
dhcp-range=192.168.7.2,192.168.7.40,255.255.255.0,24h
domain=wlan
address=/gw.wlan/192.168.7.1
$ sudo raspi-config
$ sudo rfkill unblock wlan
country_code=MX
interface=wlan0
ssid=rpi-hotspot
hw_mode=g
channel=8
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=rasp-p@s2w0rd
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
$ sudo apt install -y python3
$ sudo rm /usr/bin/python
$ sudo ln -s /usr/bin/python3 /usr/bin/python

ESP8266 Configurations

  • arduino-cli
  • picocom
# arduino-cli.yaml
board_manager:
additional_urls:
- http://arduino.esp8266.com/stable/package_esp8266com_index.json
$ arduino-cli --config-file ~/Arduino/arduino-cli.yml core update-index
$ arduino-cli --config-file ~/Arduino/arduino-cli.yml core search esp8266
$ arduino-cli --config-file ~/Arduino/arduino-cli.yml core install esp8266:esp8266
$ arduino-cli compile -b esp8266:esp8266:nodemcuv2 -u -p /dev/ttyUSB0 arduinoClient/

Test communications

$ python ~/pythonServer/server.py
$ picocom -b 115200 /dev/ttyUSB0

Raspberry pi output

Intialize socket server, listen port: 35000
Connection from: ('192.168.7.17', 59552)
From connected user: Init connection from ESP8266
Write response -> hi from server
From connected user: hi from client
Write response -> send quit sequence
Recive disconnect from client
Close connection...

Arduino output

Wait for WiFi... .....
WiFi connected
IP address:
192.168.7.17
Connecting to 192.168.7.1:35000
Wait response from server ...
Server response: hi from server
You response -> hi from client
Server response: send quit sequence
You response -> quit!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ivan Moreno

Ivan Moreno

Engineer || MSc student || DevOps in progress