Close

Connecting to the Raspberry Pi via the Pi's wifi interface

A project log for Remote environmental logging

simple way to log temperature, humidity, etc. with the help of one or more ESP8266 boards

mcunerdmcu_nerd 12/19/2019 at 17:270 Comments

This morning I've been working on getting my ESP8266 to connect to my Raspberry Pi's wifi interface.  Why would one want to do that?  For scenarios in which one wants to log some data, but the location doesn't have an Internet connection.

The first thing I did was to get my Pi configured as a wireless access point (AP.) I followed the instructions here, but I didn't do the NAT instructions.  NOTE: At the time of this writing, there seems to be no way to set the AP to auto select a channel (you can't set it to 0 for auto scan or it will fail.)  

Before one reconfigures the ESP8266 board to use the pi's wifi, a few things should be done while it's connected to our normal network.  The first thing is to create a new pyhton file (esp8266_piwifi_dht22.py), copy over the contents of the current python file (esp8266_dht22.py), and change the IP address to match the IP address that one assigned to the wlan0 interface of the Pi (in my case it was 192.168.19.1.)  The new file should then be uploaded to the board.

Now would be a great idea to set up the ESP8266 to run our script at boot. we can do that by creating a main.py file and add the following lines:

import esp8266_piwifi_dht22
esp8266_piwifi_dht22.go()

Save it and upload it to the board.  Now it's time to reconfigure the ESP8266 board to use the pi's wifi.  The instructions can be found here

At this point one should be able to disconnect the Ethernet cable from the Pi,reboot the ESP8266 board, and it all should work.  You can connect a PC/laptop/tablet to the Pi via wifi, log in via SSH, and be able to see updated readings in the .csv file.  There is one glaring issue though. The Raspberry Pi does not have a mealtime clock (RTC) so the time won't be accurate unless one manually sets the time/date after it's setup or add an RTC module.

So now how does one access the WebREPL interface on the ESP8266 board to make changes? There's a few ways to to about it. One way is by connecting the Pi back to the normal network, enable IPv4 forwarding, and adding a static route on normal router to point to the Pi. The IP address of the ESP8266 board would also need to be discovered. This can be discovered by running this command on the PI:

cat /var/lib/misc/dnsmasq.leases

If one doesn't want to/is unable to create a static route on the normal router, another option is to use SSH port forwarding.  If you're using PuTTY as your ssh client you need to expand the SSH category and click on tunnels. In the source port field put 8266. In the destination field put the IP address for your ESP8266 and add ":8266" to the end. Select IPV4 radio button then click add.  On the WebREPL page enter "ws://127.0.0.1:8266" and connect.


A third way (and a way that doesn't require the Pi to be connected to our normal network) is to connect to the Pi over wifi. For this method, you need to already have downloaded a copy of the WebREPL client files to begin with.

Discussions