How to remotely control devices using ESP8266 and LPC2148
Source: InternetPublisher:公子李 Keywords: microcontroller led wi-fi ESP8266 Updated: 2024/12/31
The ESP8266 Wi-Fi transceiver provides a way to connect a microcontroller to a network. It is widely used in IoT projects because it is cheap, small, and easy to use.
In this tutorial, we will interface the ESP8266 Wi-Fi module with an ARM7- LPC2148 microcontroller and create a web server to control the LEDs connected to the LPC2148. The workflow will be as follows:
Send AT commands from LPC2148 to ESP8266 to configure ESP8266 into AP mode
Connect your laptop or computer Wi-Fi to the ESP8266 access point
Create an HTML page on the PC using the access point IP address of the ESP8266 web server
Create a program for LPC2148 to control the LED based on the value received from ESP8266
Required Components
hardware:
ARM7-LPC2148
ESP8266 Wi-Fi Module
FTDI ( USB to UART TTL)
Leading
3.3V regulator IC
Breadboard
software:
KEIL uVision
Flash Magic Tool
putty
ESP8266 Wi-Fi Module
ESP8266 is a low-cost and widely used Wi-Fi module for embedded projects that requires low power consumption of 3.3V. It uses only two wires TX and RX for serial communication and data transfer between ESP8266 and any microcontroller with UART port .
ESP8266 Wi-Fi Module Pinout
Setting up the ESP8266 board
ESP8266 requires a constant 3.3V power supply, it is not breadboard friendly. So in our previous tutorial about ESP8266, we made a board for ESP8266 with a 3.3V regulator, a RESET button and jumper settings for switching modes (AT command or flash mode). It can also be set up on a breadboard without using a perf board.
Here we soldered all the components on a breadboard to make our own ESP8266 Wi-Fi board
Connecting LPC2148 and ESP8266 for serial communication
In order to connect ESP8266 with LPC2148, we have to establish UART serial communication between these two devices to send AT commands from LPC2148 to ESP8266 to configure the ESP8266 Wi-Fi module.
So, in order to use UART communication in LPC2148, we need to initialize the UART port in LPC2148. LPC2148 has two built-in UART ports (UART0 and UART1).
UART pins in LPC2148
Initializing UART0 in LPC2148
We know that the pins of LPC2148 are general purpose pins, so we need to use the PINSEL0 register to use UART0. Before initializing UART0, let us understand these UART registers in LPC2148 for using the UART function.
UART registers in LPC2148
The following table shows some of the important registers used in programming. In our future tutorials, we will briefly look at other registers used for UART in LPC2148.
x-0 of UART0 and x-1 of UART1:
Circuit Diagram and Connections
The connections between LPC2148, ESP8266 and FTDI are shown in the figure below
The ESP8266 is powered via a 3.3V regulator , and the FTDI and LPC2148 are powered via USB.
Why is FTDI here?
In this tutorial, we connect the RX pin of FTDI (USB to UART TTL) to ESP8266 TX pin which is further connected to LPC2148 RX pin so that we can see the response of ESP8266 module using any terminal software like putty, Arduino IDE. But for this, set the baud rate according to the baud rate of ESP8266 Wi-Fi module. (My baud rate is 9600).
Steps to program UART0 in LPC2148 to connect to ESP8266
Following are the programming steps to interface ESP8266 with LPC2148 which will make it compatible with IoT.
Step 1:- First we need to initialize UART0 TX and RX pins in PINSEL0 register.
(P0.0 as TX, P0.1 as RX)
BRUSH0 = BRUSH0 | 0x00000005;
Step 2:- Next in U0LCR (Line Control Register), set DLAB (Divisor Latch Access Bit) to 1 as it enables them and then set the stop bit to 1 and the data frame length to 8 bits.
U0LCR = 0x83;
Step 3:- Now the important step to note is to set the value of U0DLL and U0DLM according to the PCLK value and the desired baud rate. Usually for ESP8266 we use a baud rate of 9600. So let's see how to set a baud rate of 9600 for UART0.
Baud rate calculation formula:
Where,
PLCK: Peripheral clock frequency (MHz)
U0DLM, U0DLL: Baud rate generator divider registers
MULVAL, DIV ADDVAL: These registers are the fractional generator values
For PCLK=15MHZ, the baud rate is 9600
MULVAL =1 & DIVADDVAL=0
256*U0DLM+U0DLL=97.65
So U0DLM=0, we get U0DLL=97 (fractions not allowed)
So we use the following code:
U0DLM = 0x00;
U0DLL = 0x61; (hex value 97)
Step 4:- Finally, we have to set DLA(Divisor Latch Access) disable to 0 in LCR.
So we have
U0LCR &= 0x0F;
Step 5:- For Transmit ting a Char ac ter, load the byte to be transmitted into U0THR and wait for the byte to be transmitted which is indicated by THRE going HIGH.
void UART0_TxChar(char ch)
{
U0THR = ch;
while ((U0LSR & 0x40) == 0);
}
Step 6:- For transmitting string, use the following function. To send string data one by one, we have used the character function in the above step.
void UART0_SendString(char* str)
{
uint8_t i = 0;
while ( str[i] != '�' )
{
UART0_TxChar(str[i]);
I++;
}
}
Step 7:- For receiving string, interrupt service routine function is used here because whenever we send AT command or whenever ESP8266 sends data to LPC2148, ESP8266 Wi-Fi module will send data back to RX pin of LPC2148 just like we send data to web server of ESP8266.
Example: When we send an AT command from LPC2148 (“AT”) to ESP8266, we receive an “OK” reply from the Wi-Fi module.
So we are using interrupt here to check the value received from ESP8266 Wi-Fi module because ISR Interrupt Service Routine has the highest priority.
So, whenever ESP8266 sends data to the RX pin of LPC2148, an interrupt is set and the ISR function is executed.
Step 8:- To enable interrupt for UART0, use the following code
VICintEnable is the vector interrupt enable register, which is used to enable the interrupt of UART0.
VICIntEnable |= (1《《6);
VICVecCnt10 is the vector interrupt control register that is assigned a slot for UART0.
VICVectCntl0 = (1《《5) | 6;
Next, VICVectaddr0 is the Vectored Interrupt Address Register, which has the Interrupt Service Routine ISR address.
VICVectAddr0 = (unsigned)UART0_ISR;
Then we have to assign interrupt for RBR receive buffer register. So in interrupt enable register (U0IER) we set for RBR. So when we receive data, interrupt service routine (ISR) will be called.
U0IER=IER_RBR;
Finally, we need an ISR function that performs certain tasks when we receive data from the ESP8266 Wi-Fi module. Here, we simply read the received values from the ESP8266 in U0RBR and store those values in UART0_BUFFER. Finally, at the end of the ISR, VICVectAddr should be set to zero or a dummy value.
void UART0_ISR() __irq
{
unsigned char IIRValue;
IIR value = U0IIR; IIR value >>=1; IIR value &=0x02; if (IIRValue == IIR_RDA) { UART_BUFFER[uart0_count]=U0RBR; uart0_count++; if (uart0_count == BUFFER_SIZE) { uart0_count=0; } } VICVectAddr = 0x0; }
Step 9:- Since the ESP8266 Wi-Fi module should be set to AP mode, we need to send the appropriate AT commands from LPC2148 using UART0_SendString() function.
The AT commands sent from LPC2148 to ESP8266 are as follows. After sending each AT command, ESP8266 responds with "OK"
1. Send AT to ESP8266
UART0_SendString(“AT ”);
delay_milliseconds(3000);
2. Send AT+CWMODE=2 (set ESP8266 to AP mode).
UART0_SendString(“AT+CWMODE=2 ”);
delay_milliseconds(3000);
3. Send AT+CIFSR (get AP IP)
UART0_SendString(“AT+CIFSR ”);
delay_milliseconds(3000);
4. Send AT+CIPMUX=1 (for multiple connections)
UART0_SendString(“AT+CIPMUX=1 ”);
delay_milliseconds(3000);
5. Send AT+CIPSERVER=1,80 (to enable ESP8266 SERVER with OPEN PORT)
UART0_SendString(“AT+CIPSERVER=1,80 ”);
delay_milliseconds(3000);
Programming and Flashing a Hex File to the LPC2148
To program the ARM7-LPC2148 we need keil uVision and Flash Magic tools. Here the ARM7 Stick is programmed through the micro USB port using a USB cable. We write the code using Keil and create a hex file and then use Flash Magic to flash the HEX file to the ARM7 stick. To know more about installing keil uVision and Flash Magic and how to use them , click on the link Getting Started With ARM7 LPC2148 Microcontroller and Program it using Keil uVision.
The complete program is given at the end of the tutorial.
Note: When uploading the HEX file to LPC2148, the ESP8266 Wi-Fi module and the FTDI module connected to the LPC2148 must not be powered.
Controlling LEDs using ESP8266 IoT Webserver with LPC2148
Step 1:- After uploading the HEX file to LPC2148, connect the FTDI module to the PC via USB cable and open the putty terminal software.
Select Serial and then select the COM port (COM3) according to your PC or LAPTOP. The baud rate is 9600.
Step 2:- Now reset the ESP8266 Wi-Fi module or power off and power on again, the putty terminal will display the response of the ESP8266 Wi-Fi module as shown in the following figure.
Step 3:- Now press RESET button on LPC2148. After that LPC2148 starts sending AT commands to ESP8266. We can see its response in putty terminal.
Step 4:- As shown in the above figure, ESP8266 is set to MODE 2, which is AP mode, and the address of APIP is 192.168.4.1. Please note this address because this address will be hard-coded in the web page HTML code to control the LED connected to LPC2148.
Important: You must connect the PC to the ESP8266 AP when the ESP8266 is in AP mode. See the picture below, my ESP8266 module shows the AP name as ESP_06217B (it is turned on and has no password).
Finally, save the Notepad document with a .html extension
The web page will be displayed in your web browser as shown below.
Here the address is the AP IP address 192.168.4.1 and we use the following logic in LPC2148 to send the values @ and % to turn the LED on and off.
while(1)
{
if(uart0_count !=0)
{
COMMAND[0]=UART0_BUFFER[uart0_count-1];
if (COMMAND[0] == LEDON) // Set the logic of LED on or off according to the value received from ESP8266
{
IOSET1=(1《《20); //Set output
high delay_ms(100);
}
else if(COMMAND[0]==LEDOFF)
{
IOCLR1=(1《《20); //Set output low
delay_ms(100);
}
}
}
This is how you can remotely control a device using ESP8266 and ARM7 microcontroller LPC2148.
#include
#include
#define IER_RBR 0X01
#define IIR_RDA 0X02
#define BUFFER_SIZE 0X40
unsigned char UART0_BUFFER[BUFFER_SIZE];
unsigned integer uart0_count;
unsigned char command[1];
unsigned char LEDON='@',LEDOFF='%';
void UART0_ISR() __irq //ISR interrupt service routine function
{
unsigned char IIR value;
IIR value = U0IIR;
IIR value >>=1;
IIR value &=0x02;
if (IIRValue == IIR_RDA)
{
UART0_BUFFER[uart0_count]=U0RBR;
uart0_count++;
if (uart0_count == BUFFER_SIZE)
{
uart0_count=0;
}
}
VICVectAddr = 0x0;
}
void delay_ms(uint16_t j) // Delay function in milliseconds
{
uint16_t x,i;
for (i=0;i
{
for (x=0; x<6000; x++); // Loop to generate 1 ms delay, Cclk = 60MHz
}
}
void UART0_initilize(void)
{
VPBDIV = 0x00;
PINSEL0 = PINSEL0 | 0x00000005; // Enable UART0 Rx0 and Tx0 pins of UART0
U0LCR = 0x83; // DLAB = 1, 1 stop bit, 8-bit character length
U0DLM = 0x00; // For 9600 baud rate, Pclk = 15MHz
U0DLL = 0x61; // We get these values of U0DLL and U0DLM from the formula
U0LCR &= 0x0F; // DLAB = 0
VICIntEnable |= (1<<6); // Enable UART0 interrupt
VICVectCntl0 = (1<<5) | 6; // Enable UART IRQ slot
VICVectAddr0 = (unsigned)UART0_ISR; // UART ISR function address
U0IER = IER_RBR; // Enable RBR interrupt
}
void UART0_TxChar(char ch) // Function to send a byte on UART0
{
U0THR = channel;
while ((U0LSR & 0x40) == 0); // Wait until THRE bit becomes 1 to indicate transmission is complete
}
void UART0_SendString(char* str) // Function to send a string on UART0
{
uint8_t i = 0;
while ( str[i] != '�' )
{
UART0_TxChar(str[i]);
i++;
}
}
void wifi_sendATcommands(void)
{
delay_milliseconds(5000);
UART0_SendString("AT "); //send AT to ESP8266
delay_milliseconds(3000);
UART0_SendString("AT+CWMODE=2 "); //send AT+CWMODE=2 (set ESP8266 to AP mode)
delay_milliseconds(3000);
UART0_SendString("AT+CIFSR "); //send AT+CIFSR (get AP's IP)
delay_milliseconds(3000);
UART0_SendString("AT+CIPMUX=1 "); //send AT+CIPMUX=1 (for multi-connection)
delay_milliseconds(3000);
UART0_SendString("AT+CIPSERVER=1,80 "); //send AT+CIPSERVER=1,80 (for enabling ESP8266 SERVER with OPEN PORT)
delay_milliseconds(3000);
}
int main(void)
{
IODIR1=(1<<20); //Set pin P1.20 as output pin
UART0_initilize(); //Function call to initialize UART0
wifi_sendATcommands(); //Function call to send AT commands to ESP8266
while(1)
{ if(uart0_count !=0)
{
commands[0]=UART0_BUFFER[uart0_count-1];
if(COMMAND[0] == LEDON) //Set the logic of LED on or off according to the value received from ESP8266
{
IOSET1=(1<<20); //Set output high
delay in milliseconds(100);
}
else if(COMMAND[0]==LEDOFF)
{
IOCLR1=(1<<20); //Set output
low delay in milliseconds(100);
}
}
}
}
;i++)
- How to remotely control devices using ESP8266 and LPC2148
- Tutorial for building a remote-controlled Arduino Air-Boat
- How can we make the robot move precisely on a predefined path?
- An automated model railroad layout project using a microcontroller
- A small improvement to the temperature and water level indicator alarm
- Manual automatic air compressor control circuit
- Light-controlled circuit design and analysis
- Simple three-phase motor phase loss protection circuit
- Audio interface protection circuit
- Vibration anti-theft alarm circuit
- Do you know about the YTSwitch-6 LED driver IC with high efficiency and extremely low standby power?
- Analysis of various forms of packaging structures of LED light-emitting diodes
- SG1524 driver LED circuit
- LED drive circuit based on switching power supply
- Circuit using NCP1216 to drive LED
- Detailed explanation of ambulance electronic flash signal circuit diagram
- LED constant current source circuit diagram using LM317
- Dark activated LED flash circuit
- Optocoupler isolation circuit with PNP transistor current amplification
- Power outage sound and light alarm circuit