ESP8266 Wi-Fi transceiver provides a way to connect a microcontroller to the network. It is widely used in IoT projects as it is cheap, tiny and easy to use. We have previously used it to create webserver using Raspberry webserver and Arduino webserver.
In this tutorial we will interface an ESP8266 Wi-Fi Module with the ARM7-LPC2148 microcontroller and create a webserver to control the LED connected to LPC2148. The workflow will go like this:
- Send AT commands from LPC2148 to ESP8266 to configure ESP8266 in AP mode
- Connect the laptop or computer Wi-Fi with the ESP8266 Access point
- Create a HTML webpage in PC with Access point IP address of the ESP8266 webserver
- Create a program for LPC2148 to control the LED according to the value received from ESP8266
The “LPC2129 Interfacing Temperature Sensor” wiki discussed here is in reference to the rhydoLABZ make LPC2129 Development Board-Mini, however it makes no difference even if the board used is either LPC2138 Development Board-Mini or LPC2148 Development Board-Mini.i.e, this interfacing guide is common for LPC2129/LPC2138/LPC2148 Mini Development Boards from rhydoLABZ. The MCP9700/9700A and MCP9701/9701A family of Linear Active Thermistor™ Intergrated Circuit (IC) is an analog temperature sensor that converts temperature to analog voltage. It has an accuracy of ±2°C from 0°C to +70°C (MCP9700A/9701A) ±4°C from 0°C to +70°C (MCP9700/9701) while consuming 6 µA (typical) of operating current.
If your completely new to ESP8266 Wi-Fi module visit the below links to get familiar with ESP8266 Wi-Fi module.
Components Required
Hardware:
- ARM7-LPC2148
- ESP8266 Wi-Fi Module
- FTDI (USB to UART TTL)
- LED
- 3.3V Voltage Regulator IC
- Breadboard
Software:
- KEIL uVision
- Flash Magic Tool
- Putty
ESP8266 Wi-Fi module
ESP8266 is a low cost widely used Wi-Fi module for embedded projects which requires a low power of 3.3V. It uses only two wires TX and RX for serial communication and data transfer between ESP8266 and any microcontroller having UART port.
Pin Diagram for ESP8266 Wi-Fi module
- GND, Ground (0 V)
- TX, Transmit data bit X
- GPIO 2, General-purpose input/output No. 2
- CH_PD, Chip power-down
- GPIO 0, General-purpose input/output No. 0
- RST, Reset
- RX, Receive data bit X
- VCC, Voltage (+3.3 V)
Setting up ESP8266 Circuit Board
ESP8266 requires a constant supply of 3.3V and it is not breadboard friendly. So in our previous tutorial on ESP8266, we made a circuit board for ESP8266 with 3.3V Voltage regulator, a RESET push button and jumper setup for switching modes (AT command or flash mode). It can also be setup on breadboard without using perf board.
Here we soldered all the components on breadboard to make our own ESP8266 Wi-Fi board
Learn interfacing of ESP8266 with various microcontrollers by following below links:
All the ESP8266 based projects can be found here.
Connecting LPC2148 with ESP8266 for Serial Communication
In order to interface ESP8266 with LPC2148 we must establish a UART serial communication between these two devices to send AT commands from LPC2148 to ESP8266 to configure the ESP8266 Wi-Fi module. To know more about ESP8266 AT commands follow the link.
So in order to use UART communication in LPC2148 we need to initialize UART port in LPC2148. LPC2148 has two inbuilt UART ports (UART0 and UART1).
UART Pins in LPC2148
UART_Port | TX_PIN | RX_PIN |
UART0 | P0.0 | P0.1 |
UART1 | P0.8 | P0.9 |
Initializing UART0 in LPC2148
As we know that the pins of LPC2148 are general purpose pins so we need to use PINSEL0 register for using UART0. Before initializing UART0 lets know about these UART registers used in LPC2148 for using UART feature.
UART Registers in LPC2148
The table below shows some important registers used in programming. In our future tutorials we will see briefly about other registers used for UART in LPC2148.
x-0 for UART0 & x-1 for UART1:
REGISTER | REGISTER NAME | USE |
UxRBR | Receive Buffer Register | Contains Recently Received Value |
UxTHR | Transmit Holding Register | Contains Data to be transmitted |
UxLCR | Line Control Register | Contains UART frame format (No of Data Bits, Stop bit) |
UxDLL | Divisor Latch LSB | LSB of UART baud rate generator value |
UxDLM | Divisor Latch MSB | MSB of UART baud rate generator value |
UxIER | Interrupt Enable Register | It is used to enable UART0 or UART1 interrupt sources |
UxIIR | Interrupt Identification Register | It contains the status code that has priority and source of pending interrupts |
Circuit Diagram and Connections
Connections between LPC2148, ESP8266 and FTDI is shown below
LPC2148 | ESP8266 | FTDI |
TX (P0.0) | RX | NC |
RX(P0.1) | TX | RX |
ESP8266 is powered via a 3.3V voltage Regulator and FTDI & LPC2148 are powered from USB.
In this tutorial we have connected the RX pin of FTDI (USB to UART TTL) to the 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 that set the baud rate as according to baud rate of ESP8266 Wi-Fi module. (My Baud Rate is 9600).
Steps involved in programming UART0 in LPC2148 for interfacing ESP8266
Below are the programming steps to connect ESP8266 with LPC2148 which will make it IoT compatible.
Step 1:- First we need to initialize the UART0 TX & RX pins in the PINSEL0 register.
Step 2:- Next in U0LCR (Line Control Register), set the DLAB (Divisor Latch Access Bit) to 1 as it enables them and then set no of stop bits as 1 and data frame length of 8-bit.
Step 3:- Now important step to be noted is to set the values of U0DLL & U0DLM depending upon the PCLK value and the desired baud rate. Normally for ESP8266 we use baud rate of 9600. So let’s see how to set 9600 baud rate for UART0.
Formula for baud rate calculation:
Where,
PLCK: Peripheral Clock in Frequency (MHz)
U0DLM, U0DLL: Baud Rate generator divider registers
MULVAL, DIVADDVAL: These registers are fraction generator values
For Baud Rate 9600 with PCLK=15MHZ
MULVAL =1 & DIVADDVAL=0
256*U0DLM+U0DLL=97.65
So U0DLM=0 and we get U0DLL=97 (Fraction not allowed)
So we use following code:
Step 4:- Finally, we must make DLA (Divisor Latch Access) disable set to 0 in LCR.
So we have
Step 5:-For Transmitting a Character, load the byte to be sent in U0THR and wait until the byte is transmitted, which is indicated by the THRE becoming HIGH.
Step 6:-For Transmitting a String, below function is used. To send string data one by one we used the character function from above step.
Step 7:-For Receiving a string, interrupt service routine function is used here because an ESP8266 Wi-Fi module will transmit data back to the RX pin of LPC2148 whenever we send AT command or whenever an ESP8266 sends data to LPC2148, like we send data to a webserver of ESP8266.
Example: When we send AT command to ESP8266 from LPC2148 (“ATrn”) then we get a reply “OK” from the Wi-Fi module.
So we use an interrupt here to check the value received from the ESP8266 Wi-Fi module as the ISR interrupt service routine has the highest priority.
So whenever an ESP8266 sends data to RX pin of LPC2148 the interrupt is set and ISR function gets executed.
Step 8:- To enable interrupts for UART0, use following code
The VICintEnable is vectored interrupt enable register used to enable interrupt for UART0.
The VICVecCnt10 is vectored interrupt control register that allocates slot for UART0.
Next the VICVectaddr0 is vectored interrupt address register that has the interrupt service routine ISR address.
Then we have to assign the interrupt for RBR Receive buffer register. So in Interrupt enable register (U0IER) we set for RBR. So that interrupt service routine (ISR) is called when we receive data.
Finally, we have the ISR function that needs to do certain task when we receive data from ESP8266 Wi-Fi Module. Here we just read the received value from the ESP8266 that is present in the U0RBR and store those value in the UART0_BUFFER. Finally at the end of ISR the VICVectAddr should be set with zero or dummy value.
Step 9:- As ESP8266 Wi-Fi module should be set in the AP mode, we need to send the respected AT commands from LPC2148 by using the UART0_SendString() function.
The AT commands which are sent to ESP8266 from LPC2148 are mentioned below. After sending each AT command ESP8266 responds with “OK”
1. Sends AT to ESP8266
2. Sends AT+CWMODE=2 (Setting ESP8266 in AP mode).
3. Sends AT+CIFSR (For getting IP of AP)
4. Sends AT+CIPMUX=1 (For Mutliple Connections)
5. Sends AT+CIPSERVER=1,80 (For ENABLING ESP8266 SERVER with OPEN PORT)
Programming and Flashing Hex File to LPC2148
To Program ARM7-LPC2148 we need keil uVision & Flash Magic tool. A USB Cable is used here to program ARM7 Stick via micro USB port. We write code using Keil and create a hex file and then the HEX file is flashed to ARM7 stick using Flash Magic. To know more about installing keil uVision and Flash Magic and how to use them follow the link Getting Started With ARM7 LPC2148 Microcontroller and Program it using Keil uVision.
Complete program is given at the end of the tutorial.
Note: While uploading HEX file to LPC2148 you must not power the ESP8266 Wi-Fi Module and the FTDI module that is connected with LPC2148.
Controlling the LED using ESP8266 IoT Webserver with LPC2148
Step 1:- After uploading HEX file to LPC2148, connect the FTDI module to PC via USB cable and open the putty terminal software.
Select Serial and then Select the COM port according to your PC or LAPTOP mine was (COM3). The baud rate is 9600.
Step 2:- Now reset the ESP8266 Wi-Fi Module or just POWER OFF and POWER ON it again, the putty terminal will show the response of the ESP8266 Wi-Fi module as shown below.
Step 3:- Now press RESET button on the LPC2148. After that LPC2148 begins to send AT commands to ESP8266. We can see the response of that in the putty terminal.
Step 4:- As you can see in the image above the ESP8266 is set in MODE 2 that is AP mode and the address of APIP is 192.168.4.1. Note this address because this address will be hard coded in the webpage HTML code to control the LED connected to LPC2148.
Important: When ESP8266 is in AP mode you must connect your PC to the ESP8266 AP. See the image below my ESP8266 module shows AP in the name of ESP_06217B (It is open and has no password).
Step 5:- After connecting the PC to the ESP8266 AP, open a notepad and copy-paste the following HTML programme webpage. Make sure to change the APIP address according to your ESP8266 Wi-Fi module
In this HTML page, we have created two hyperlinked buttons to Turn on and off the LED from webpage.
Finally save the notepad document as .html extension
The webpage will be shown as below in the web browser.
Here the address is the AP IP address 192.168.4.1 and we send values @ and % to switch ON and OFF the LED by using this logic below in LPC2148.
This is how a device can be controlled remotely using ESP8266 and ARM7 microcontroller LPC2148. Complete code and explanation video is given below.
Currently I am trying to read a byte from my dht11 sensor and write it to virtual terminal, but I couldn't find a simple datasheet for beginners. I found a bit complicated mikroC code and set-up which has pic16f628a and lcd. You can find it here That works, but if I add virtual terminal on it. There is nothing appear on virtual terminal even if I add required mikroC codes on it. While my second try, I replace my microcontroller with pic16f877a, I can see this text on virtual terminal 'No response from sensor' which is the error message of my mikroC code.
Here is some info about dht11;DHT11 uses a simplified single-bus communication. Single bus that only one data line, the system of data exchange, control by a single bus to complete. It sends 40 bit Data format:The 8bit humidity integer data + 8bit the Humidity decimal data +8 bit temperature integer data + 8bit fractional temperature data +8 bit parity bit.You can also find datasheet from: here
2 Answers
$begingroup$A couple of weeks ago i wanted to set up a simple circuit, which is going to measure humidity and temperature level in air. When i start to research I don’t have any knowledge about how to set up a circuit or how can i embed a code in a microcontroller unit and many similar question.
So, below I will explain my little challange step by step. I hope it will be helpfull. Before we start you should refill your cups it might be a little long.
Now here is my components list;
Pic 16F877A (40-Pin) Crystal 4.0 MHz 22pF Capacitor 100nJ Capacitor (0.1uF) 330nJ Capacitor (0.3uF) DHT11 (Humidity & Temperature sensor) 1uF Capacitor (1 Micro F) 10uF Capacitor max232 RS232 DB9 Connector 9V battery & battery snap
Setting up;
1) Breadboard
Some kind of breadboards have positive or negative line through the board, on the other hand the other types’ power and ground lines might be diveded in two. You can figure out this via looking at the red and blue lines on the breadboard. And inner part of it, there are letters and numbers. Numbers represent the columns which are connected with each others. So, if I connect 5V to A-0 then B-0, C-0, D-0 and E-0 have same voltage.
2) 7805 Regulator (9V to 5V)
If you read pic16f877a and dht11’s datasheets, you learned that they working with 5V but our battery is 9V.
As you can see above you can easily regulate your voltage. Now you can feed your positive terminal/s from output of regulator.
3) DHT11 (Sensor)
This is a cheap, digital humidity and temperature sensor with 4 pin. Here is nice datasheet (because of my reputation i couldn't post more than 2 links. You can find it on google, if you find, take a look at two or more datasheet because some of them might not be clear enough) which is vital to understand how dht11 works. While you working on electronic projects you will come across with Vcc, Vdd, Vss, Vee pins in a really short explanation Vcc and Vdd are positive voltage supply, Vee and Vss are negative ground supply. So you can assume that Vcc and Vdd are same for the following pictures.
4) Pic 16F877A (40 Pin)
This is a well known micro controller unit, easy to find and have enough leg for my project that’s why I prefer this pic. Here is datasheet: (unfortunately because of my reputation i couldn't post more than 2 links. You can find it on mikrochip's webpage) Datasheet show you the pin diagram. In diagram you will see numbers from 1 to 40. On following picture notice the small caved in point. It represents the first leg.
5) Crystal (Clock)
Just like every processor, 877a also needs to clock. Please don’t consider the scheme’s crystal Mhz, our circuit works with 4MHz Quartz crystal with 22pF Capacitors.
6) Max232
Ok, as i said that this project going to measure temperature - humidity and send these values to the computer. At this point, to able to get these values from our circuit to pc, we have to build a serial communication part. Do not be afraid of telecommunications words, with the help of RS-232 cable everything is really simple. Before connect the cable you should build the following scheme. On the picture you will see TTL serial TX and RX these two cable going to connect with the pic’s 25 (TX) and 26th (RX) pins.
Above scheme may be confusing imagine that at the bottom of the picture there is a ground and four black node (which are near to the bottom) connected to ground.
For complete proteus tutorial you can find my video below, but do not forget proteus is just a simulation and that is not guaranteed that your hardware is going to work properly. Especially frequency can cause some problems. Here is
![Interfacing Temperature Sensor With Lpc2148 Interfacing Temperature Sensor With Lpc2148](https://www.pantechsolutions.net/media/wysiwyg/circuit-diagram-to-interface-ds1820-with-lpc2148-arm7.jpg)
In mikroC code I set the leds according to the humidity if it is greater than 20% upper led turns on, and if humidity level pass over 30% 2nd led also turns on.
MikroC Code;
And final part:
Embeding code into the MPU;