I recently started working with the ESP8266 module, primarily for communication between two ESP8266 devices. The main goal was to configure one as an Access Point (AP) and the other as a Station (STA). This setup allows them to communicate over a network. Since the module comes pre-configured with firmware, I mainly used a serial port debugging tool to send AT commands for configuration.
**Basic Configuration of ESP8266 and Serial Communication**
**AP Mode (Server): The steps are as follows:**
1. Set the ESP8266 to AP mode using the command `AT+CWMODE=2`. If it was previously configured, you can reset it to factory settings using `AT+RESTORE`.
2. Configure the AP parameters with `AT+CWSAP="ssid", "pwd", "chl", "ecn"`.
3. Restart the module using `AT+RST`.
4. Enable multiple connections and set up a TCP server with `AT+CIPMUX=1` and `AT+CIPSERVER=1,5000`.
5. Check the local IP address using `AT+CIFSR` to make it easier for the client to connect.
6. Send data using `AT+CIPSEND="link.ID", "length"` once the client is connected.
**STA Mode (Client): The steps are as follows:**
1. Set the ESP8266 to STA mode using `AT+CWMODE=1`.
2. Restart the module with `AT+RST`.
3. Connect to the AP using `AT+CWJAP="ssid", "pwd"`.
4. Establish a TCP connection with `AT+CIPSTART="type", "remote IP", "remote port"`. Once connected, you can use `AT+CIPSEND` to send data.
5. Set the transmission mode with `AT+CIPMODE=1`.
6. Send data using `AT+CIPSEND`.
Here’s an example of how the AP (server) sends information to the STA (client) via the serial port. Before sending each message, the command `AT+CIPSEND="link.ID", "length"` must be used. In the image, there are two messages sent, resulting in 16 bytes total.
For the STA (client) sending data to the AP (server), since the module is in transparent mode, you only need to send `AT+CIPSEND` once, and no further commands are needed after the message is sent. In the image, three consecutive `AT+CIPSEND` commands with the message "Hello" are shown.
**ESP8266 SDK: Serial Interrupt Reception and Transmission**
**1. Transmission**
To start, initialize the serial port using `uart_init(115200, 115200)`, which sets the baud rate for UART0 and UART. You can then use `uart0_tx_buffer(uint8 *buf, uint16 len)` to send data through UART0. Alternatively, you can use `os_printf()` to output text, but keep in mind that if you're using UART1, you'll need to change the default output.
I wanted to modify `os_printf()` to print from UART1 instead of the default UART0. To do this, I used `os_install_putc1((void *)uart1_write_char)` to redirect the output.
**2. Reception**
In the serial initialization function `uart_init()`, there's a task created using `system_os_task(uart_recvTask, uart_recvTaskPrio, uart_recvTaskQueue, uart_recvTaskQueueLen)`, which handles incoming data on UART0.
The function `uart_config(UART0)` configures the serial port registers and includes a callback function for handling received data. The interrupt handler `uart0_rx_intr_handler` is attached using `ETS_UART_INTR_ATTACH(&UartDev.rcv_buff)`.
Inside the interrupt handler, various flags are checked. When the RX FIFO timeout occurs (`UART_RXFIFO_TOUT_INT_ST`), the system posts a message to the `uart_recvTask` task. The task then processes the received data.
If the buffer is enabled (`UART_BUFF_EN`), the received data is enqueued into a buffer. Otherwise, the data is read directly from the FIFO and sent back using `uart_tx_one_char(UART0, d_tmp)`. This allows you to process the received data by storing it in a buffer for further use.
Xenon Lamp Aging Test Chamber,Xenon Lamp Aging Light Tester,Photovoltaic Module Test Box,Xenon Lamp Aging Testing Machine
Wuxi Juxingyao Trading Co., Ltd , https://www.juxingyao.com