Esp32-Stick+Oled Display: Displaying Connected Network Characteristics With Esp32-Stick
by alexok997 in Circuits > Arduino
240 Views, 0 Favorites, 0 Comments
Esp32-Stick+Oled Display: Displaying Connected Network Characteristics With Esp32-Stick
![mSSO4rUjfxk.jpg](/proxy/?url=https://content.instructables.com/FVM/TXRL/LID5DHV0/FVMTXRLLID5DHV0.jpg&filename=mSSO4rUjfxk.jpg)
This is a short project in which I show how to display characteristics of any network you are connected to, using Esp32-Stick Ethernet capabilities. Just plug the cable in, and the characteristics are displayed on the screen! No SSID or password required!
Supplies
What you need is:
Esp32-Stick-PoE-P if you want to use Passive PoE for powering your board.(Don't use PoE when you are programming the board!)
Esp32-Stick-PoE-A if you want to use Active PoE for powering your board.(Don't use PoE when you are programming the board!)
Esp32-Stick-Eth if you don't need PoE.
If you live in Czech Republic you can get the board much faster here
Connect Hardware
![ZXhDWow7CfY.jpg](/proxy/?url=https://content.instructables.com/F46/YXS9/LID5DI9Z/F46YXS9LID5DI9Z.jpg&filename=ZXhDWow7CfY.jpg)
First lets connect the hardware,
Oled VCC->Esp32-Stick 3.3V
Oled GND->Esp32-Stick GND
Oled SDA->Esp32-Stick pin 33
Oled SCK->Esp32-Stick pin 14
The final setup should looks like the one on the picture
Prepare Arduino Ide
![Screenshot from 2023-06-01 18-17-36.png](/proxy/?url=https://content.instructables.com/FPP/10Q9/LID5G62A/FPP10Q9LID5G62A.png&filename=Screenshot from 2023-06-01 18-17-36.png)
![Screenshot from 2023-06-01 18-17-42.png](/proxy/?url=https://content.instructables.com/FCI/FG5T/LID5G629/FCIFG5TLID5G629.png&filename=Screenshot from 2023-06-01 18-17-42.png)
Skip this step, if you want to use PlatformIO
Create Arduino sketch and add Adafruit SSD1306 library using Sketch->Include library->Manage libraries.
Also set Tools->Board to Esp32 Dev Module
Prepare PlatformIO Project
![Screenshot from 2023-06-01 18-21-42.png](/proxy/?url=https://content.instructables.com/FVU/JWVD/LID5GD4D/FVUJWVDLID5GD4D.png&filename=Screenshot from 2023-06-01 18-21-42.png)
If you want to use Arduino IDE, skip this step.
Create a project in PlatformIO, use Adafruit Esp32 Feather as the board. (In most cases, It doesn't matter what board you choose as long as the core chip is the same(Esp32 in our case))
Check the plaformio.ini file and copy the configuration (it sets the board=esp32dev, and adds all the necessary libraries). platformio.ini should look something like this:
[env:featheresp32]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
adafruit/Adafruit BusIO @ ^1.14.1
adafruit/Adafruit GFX Library @ ^1.11.3
adafruit/Adafruit SSD1306 @ ^2.5.3
Write Some Code
When your IDE is ready, it's time to write some code!
First, we add all the necessary libraries.
#include "Wire.h"
#include "SPI.h"
#include <ETH.h>
#define SSD1306_NO_SPLASH
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
then to define pins we use:
#ifdef ETH_CLK_MODE
#undef ETH_CLK_MODE
#endif
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
#define ETH_POWER_PIN -1
#define ETH_TYPE ETH_PHY_LAN8720
#define ETH_ADDR 1
#define ETH_MDC_PIN 23
#define ETH_MDIO_PIN 18
#define LED_PIN 2
#define DISPLAY_SDA 33
#define DISPLAY_SCL 14
After that, it is necessary to create global display variable that we will use in our code later and a variable called eth_connected that will become true when we connect to the Internet.
static bool eth_connected = false;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
Now comes the hardest part. The Arduino framework generates what are known as 'events' when certain specific actions occur. We need to 'catch' those events and react accordingly. For this purpose, we create a function that will be called every time the event occurs.
void EthEvent(WiFiEvent_t event) {
switch (event) {
case ARDUINO_EVENT_ETH_START:
display.clearDisplay();
display.setCursor(0,0);
display.println("ETH Started");
ETH.setHostname("esp32-ethernet");
display.display();
break;
case ARDUINO_EVENT_ETH_CONNECTED:
display.clearDisplay();
display.setCursor(0,0);
display.println("ETH Connected");
display.display();
break;
case ARDUINO_EVENT_ETH_GOT_IP:
display.clearDisplay();
display.setCursor(0,0);
display.print("MAC:");
display.println(ETH.macAddress());
display.print("IPv4:");
display.println(ETH.localIP());
display.print("Gtw:");
display.println(ETH.gatewayIP());
display.print("Nid:");
display.println(ETH.networkID());
display.print("Speed:");
display.print(ETH.linkSpeed());
display.println("Mbps");
display.display();
eth_connected = true;
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
display.clearDisplay();
display.setCursor(0,0);
display.println("ETH Disconnected");
display.display();
eth_connected = false;
break;
case ARDUINO_EVENT_ETH_STOP:
display.clearDisplay();
display.setCursor(0,0);
display.println("ETH Stopped");
display.display();
eth_connected = false;
break;
default:
break;
}
}
As you can see, we react to these events by displaying information on the screen.Now, we are ready to perform the initialization in the setup() function.
void setup() {
pinMode(DISPLAY_SDA,PULLUP);
pinMode(DISPLAY_SCL,PULLUP);
Wire.begin(DISPLAY_SDA,DISPLAY_SCL);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(1000);
// Clear the buffer.
display.clearDisplay();
display.display();
// text display tests
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.display();
// pinMode(LED_PIN,OUTPUT);
Serial.begin(115200);
Serial.println("ESP32-Stick begin\n");
display.println("ESP32-Stick begin\n");
display.display();
WiFi.onEvent(EthEvent);
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE);
}
First, we activate the PullUp resistors for I2C communication by setting the mode of DISPLAY_SDA and DISPLAY_SCL to PULLUP.
Then, we initialize Wire and the display.
Next, we bind the 'EthEvent' function to 'WiFi' to handle all the incoming events.
The last line is the Ethernet initialization function.
In the loop() function, we don't need to perform any specific actions since everything is handled within the EthEvent function.
void loop() {
delay(1000);
}
The full code looks like this:
#include "Wire.h"
#include "SPI.h"
#include <ETH.h>
#define SSD1306_NO_SPLASH
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#ifdef ETH_CLK_MODE
#undef ETH_CLK_MODE
#endif
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
#define ETH_POWER_PIN -1
#define ETH_TYPE ETH_PHY_LAN8720
#define ETH_ADDR 1
#define ETH_MDC_PIN 23
#define ETH_MDIO_PIN 18
#define LED_PIN 2
#define DISPLAY_SDA 33
#define DISPLAY_SCL 14
static bool eth_connected = false;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
void EthEvent(WiFiEvent_t event) {
switch (event) {
case ARDUINO_EVENT_ETH_START:
display.clearDisplay();
display.setCursor(0,0);
display.println("ETH Started");
ETH.setHostname("esp32-ethernet");
display.display();
break;
case ARDUINO_EVENT_ETH_CONNECTED:
display.clearDisplay();
display.setCursor(0,0);
display.println("ETH Connected");
display.display();
break;
case ARDUINO_EVENT_ETH_GOT_IP:
display.clearDisplay();
display.setCursor(0,0);
display.print("MAC:");
display.println(ETH.macAddress());
display.print("IPv4:");
display.println(ETH.localIP());
display.print("Gtw:");
display.println(ETH.gatewayIP());
display.print("Nid:");
display.println(ETH.networkID());
display.print("Speed:");
display.print(ETH.linkSpeed());
display.println("Mbps");
display.display();
eth_connected = true;
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
display.clearDisplay();
display.setCursor(0,0);
display.println("ETH Disconnected");
display.display();
eth_connected = false;
break;
case ARDUINO_EVENT_ETH_STOP:
display.clearDisplay();
display.setCursor(0,0);
display.println("ETH Stopped");
display.display();
eth_connected = false;
break;
default:
break;
}
}
void setup() {
pinMode(DISPLAY_SDA,PULLUP);
pinMode(DISPLAY_SCL,PULLUP);
Wire.begin(DISPLAY_SDA,DISPLAY_SCL);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(1000);
// Clear the buffer.
display.clearDisplay();
display.display();
// text display tests
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.display();
// pinMode(LED_PIN,OUTPUT);
Serial.begin(115200);
Serial.println("ESP32-Stick begin\n");
display.println("ESP32-Stick begin\n");
display.display();
WiFi.onEvent(EthEvent);
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE);
}
void loop() {
delay(1000);
}
Now upload the code to your board!
Testing
![mSSO4rUjfxk.jpg](/proxy/?url=https://content.instructables.com/F3R/EDOV/LID5GL33/F3REDOVLID5GL33.jpg&filename=mSSO4rUjfxk.jpg)
If everything has been done correctly, upon inserting the Ethernet cable into the board, all the network parameters will be displayed!