ESP8266 ESP-01 WiFi Repeater

by sims.mike in Circuits > Arduino

1553 Views, 4 Favorites, 0 Comments

ESP8266 ESP-01 WiFi Repeater

InstructablesBanner.png

This is a simple WiFi repeater that anyone can use to communicate with your Arduino over WiFi. It uses the UDP protocol to communicate to a workstation and serial to communicate with the Arduino. In this Instructable, I will be using an Arduino Nano, but any Arduino should work just fine. I've even used this with the ATTiny85 via SoftSerial.

Supplies

You will need:

1) An Arduino with serial pins (or use the SoftSerial library)

2) An ESP8266 module of some kind, I used the ESP-01 module.

3) 10k Resistor

4) 2k Resistor

5) 1k Resistor

6) 3.3v Power Source

7) Possibly a 100µF Cap depending on item 6

8) A program that will communicate with the ESP module. I have supplied a link to a program that I wrote in Java for this purpose.

The first thing you need to do is program the ESP-01 with this code:


#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <BlockNot.h>

#define WIFI_RETRIES 30

const IPAddress espIPAddress(10, 10, 11, 10);
const IPAddress subnetMask(255, 255, 255, 0);
const IPAddress defaultGateway(10, 10, 11, 1);
const IPAddress workstationIPAddress(10, 10, 10, 90);

const char *SSID = "ssid";
const char *wifiPassword = "password";
const uint16_t port = 7373;
String serialData = "";
bool stringComplete = false;

BlockNot pingTimer(5, SECONDS);

#define SEND_SERIAL Serial.println
#define PROCESS_UDP processUDP()
#define SEND_UDP sendUDP
#define PROCESS_SERIAL processSerial()

WiFiUDP UDP;

String getIPAddress(IPAddress ipAddress) {
String ipaddy = String(ipAddress[0]) + ".";
ipaddy += String(ipAddress[1]) + ".";
ipaddy += String(ipAddress[2]) + ".";
ipaddy += String(ipAddress[3]);
return ipaddy;
}

void processUDP() {
static int bufferSize = 500;
char packetBuffer[bufferSize]; //buffer to hold incoming packet,
int packetSize = UDP.parsePacket();
if (packetSize) {
int n = UDP.read(packetBuffer, packetSize);
if (n > 2) {
SEND_SERIAL(packetBuffer);
}
}
}

void sendUDP(String msg) {
int packetSize = msg.length() + 1;
char packet[packetSize];
msg.toCharArray(packet, packetSize);
UDP.beginPacket(workstationIPAddress, port);
UDP.write(packet);
UDP.endPacket();
}

void checkSerial() {
while (Serial.available()) {
char inChar = (char) Serial.read();
serialData += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}

void processSerial() {
checkSerial();
if (stringComplete) {
SEND_UDP(serialData);
serialData = "";
stringComplete = false;
}
}

void startWiFi() {
int conn_tries = 0;
WiFi.forceSleepWake();
WiFi.mode(WIFI_STA);
WiFi.config(espIPAddress, defaultGateway, subnetMask);
WiFi.begin(SSID, wifiPassword);
Serial.print("\nConnecting.");
while ((WiFi.status() != WL_CONNECTED) && (conn_tries++ < WIFI_RETRIES)) {
Serial.print(".");
delay(200);
}
if (WiFi.status() == WL_CONNECTED) {
UDP.begin(port);
Serial.println("\nConnected!");
Serial.println("localIP: " + getIPAddress(WiFi.localIP()));
Serial.println("Subnet: " + getIPAddress(WiFi.subnetMask()));
Serial.println("Gateway: " + getIPAddress(WiFi.gatewayIP()));
}
}

void setup() {
Serial.begin(57600);
delay(500);
startWiFi();
yield();
SEND_UDP("WiFi Repeater Ready!");
}

void loop() {
PROCESS_UDP;
PROCESS_SERIAL;
if (pingTimer.TRIGGERED) {
SEND_UDP("pingwifi");
}
}


There are plenty of tutorials out there for programming an ESP module, so I won't cover how to do that here.

In the above sketch, you will need to fill in the appropriate settings for your WiFis SSID and the wifi password. Also, the IP addresses will need to be changed to match your network. Most people use 192.168.1.x so change the addresses accordingly. The subnet mask will almost always be 255.255.255.0.

You will need to install the BlockNot library into your Arduino IDE. This is a library that I wrote that makes it easy to use non-blocking timers - as opposed to Delay which stops the execution of all your code until the timer finishes. BlockNot will let you create timers that run from a trigger instead of pausing code execution.

Downloads

Program the Arduino

Next, you need to upload a sketch into your Arduino that properly handles serial communication to and from the ESP module. This will be based on your own needs, but this sketch can get you started.

This sketch is a typical blink sketch that we can enable and disable from the computer.

You can see that it uses two BlockNot timers. One is to send out a ping beacon every 5 seconds and the other will blink the LED on the Nano if the blink boolean variable is set to true, and we toggle that variable by sending the command 'blink' over the serial port via the WiFi repeater.

The ping is used by the workstation app to know that the device is online and able to receive data. The ESP-01 and the Nano send different pings which the program can then use to know that both devices are online and working properly.

The ping is also used by the software to discover what the IP address is of the ESP-01 module, in case you configure yours to get its IP address via DHCP instead of statically assigning it.


#include <Arduino.h>
#include <BlockNot.h>

#define SEND_SERIAL sendSerial
#define PROCESS_SERIAL processSerial()

BlockNot pingTimer(20, SECONDS);

void sendSerial(const String& message) {
Serial.println(message);
}

void processSerial() {
if(Serial.available()) {
String serialData = Serial.readString();
if(serialData.startsWith("hi")) {
SEND_SERIAL("Hello!");
}
else if (serialData.startsWith("com")) {
SEND_SERIAL("Command Sent");
}
}
}

void setup() {
Serial.begin(57600);
delay(5000);
SEND_SERIAL("Arduino Ready!");
}

void loop() {
PROCESS_SERIAL;
if(pingTimer.TRIGGERED) {
SEND_SERIAL("ping");
}
}



Downloads

Buck Converter

BuckConverterMini.png

Let's talk about powering these devices

In this Instructable, I use a very handy little buck converter that has jumper pads on the bottom that you can use to set the voltage instead of needing to use the potentiometer which can get tricky since they are so sensitive. You simply cut a specific trace next to the jumpers then put a little solder bead across the jumper for whichever voltage you want.

Wiring

Screenshot 2022-12-26 at 11.32.33 AM.png

Next comes the wiring, which I will explain.

The ESP-01 as with all ESP modules uses 3.3 volts. However, the Arduino typically uses 5 volts. The voltage divider at the serial input pin on the ESP-01 needs to be there so that we don't feed it a 5-volt signal from the Arduino. The ratio of 2.1k to 1k is a good ratio for this purpose. You could, however, use a 12k resistor in place of the 2k and a 10k in place of the 1k if needed. What matters is that the ratio is the same.

The CH_PD pin on the ESP module must be at logic level high (3.3v) before it will boot. So the 10k pullup resistor serves this purpose. You always want to bring digital pins high through a pull-up resistor to limit the current flowing into the microcontroller.

Since the ESP is 3.3v, a voltage divider is not needed from its serial output pin to the Arduino since the Arduino can handle 3.3 volts without any issue. If you're concerned about the Rx pin on the Arduino going high during power-up, you can always use another voltage divider on that line, but I have never had any issues with this.

Important: If you do not receive any data from the Nano, but you are able to toggle the blinking of the LED by sending the word 'blink' through the program, then your voltage at the ESP-01's Rx pin might be too low. Check it with a meter and if it's not close to 3 volts, then adjust your voltage divider as needed. There are many free voltage divider calculators out on the Internet that can help with this.

I have listed here, a 5-volt power source feeding both the Arduino and the buck converter, but that can go as high as 12 volts as the Arduino VIN pin is rated for voltages up to 19 volts. The buck converter can handle voltages as high as 24 volts.

If you are powering your Arduino from an external power source and not the USB port, then you should connect a 100µF capacitor between its VIN pin and ground to offset any minor voltage fluctuations that may occur. The same is true for the VCC pin on the ESP module.

Software

Screenshot 2022-12-26 at 5.22.35 PM.png

You can click here to access the application installer.

The program is a self-contained package. There is no need to install Java on your machine to run the program.

Once the program is installed, run it, then power on your circuit and you should see the Wifi and Nano status turn green as in the image above. The Nano should start blinking on power up and typing the word blink into the program and hitting enter will stop the blinking whereas typing it again will start it back up again.

You can use this program with any project as long as you keep the framework of the sketches that I posted in this Instructable.