ESP8266 AP模式TCP server 串口透传

背景:Linux设备装整机后出问题需要调试必须拆壳,不方便,预留调试线影响设备密封性,所以想到使用无线模块把调试串口透传到PC。

解决方案:

1、ESP8266开机进入AP模式,AP的SSID关联设备SN,调试人员根据SSID关联的SN可以知道需要接入的待调试产品。

2、ESP8266开机启动TCP server,该SERVER只能接入一个客户端,当客户端接入后循环接收串口数据发给客户端,接收客户端数据发给串口,实现数据透传。

实现代码如下,AP SSID关联SN部分因涉及公司产品细节已略去,一个简单的demo,待完善。

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WiFiServer.h>

#ifndef APSSID
#define APSSID "ESP8266ap"
#define APPSK  "12345678"
#endif

/* Set these to your desired credentials. */
const char *ssid = APSSID;
const char *password = APPSK;

WiFiServer server(22333); //声明服务器对象

void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();
  Serial.print("Configuring access point...");
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

  server.begin(); //服务器启动监听端口号22333
}

void loop()
{
    WiFiClient client = server.available(); //尝试建立客户对象
    if (client) //如果当前客户可用
    {
        Serial.println("[Client connected]");
        uint8_t readBuf[256];

        //!!!注意:此处为处理一次接收大于256字节情况

        while (client.connected()) //如果客户端处于连接状态
        {
            int inComingBytes = client.available();  //接收客户端数据转发给串口
            if(inComingBytes && inComingBytes < 256){
              client.read(readBuf, inComingBytes);
              Serial.write(readBuf, inComingBytes);
            }
   
            inComingBytes = Serial.available();  //接收串口数据转发给客户端
            if(inComingBytes && inComingBytes < 256){
              Serial.read((char*)readBuf, inComingBytes);
              client.write(readBuf, inComingBytes);
            }
        }
        client.stop(); //结束当前连接:
        Serial.println("[Client disconnected]");
    }
}

 

  • 6
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
The ESP32 IDF (Integrated Development Framework) allows developers to create a TCP server on an ESP32 device. Here are the basic steps to create a TCP server: 1. Include the required libraries and initialize the network stack. ```c #include <stdio.h> #include <string.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "tcpip_adapter.h" /* Initialize Wi-Fi */ static esp_err_t event_handler(void *ctx, system_event_t *event) { switch(event->event_id) { case SYSTEM_EVENT_STA_START: esp_wifi_connect(); break; case SYSTEM_EVENT_STA_DISCONNECTED: esp_wifi_connect(); break; default: break; } return ESP_OK; } void wifi_init_sta(void) { tcpip_adapter_init(); ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); wifi_config_t wifi_config = { .sta = { .ssid = "your-ssid", .password = "your-password", }, }; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); } ``` 2. Create a socket and bind it to a local IP address and port number. ```c /* Create a TCP server socket */ int server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (server_socket == -1) { ESP_LOGE(TAG, "Failed to create socket: errno %d", errno); return; } /* Bind the socket to a port */ struct sockaddr_in server_address; server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = htonl(INADDR_ANY); server_address.sin_port = htons(80); int err = bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)); if (err != 0) { ESP_LOGE(TAG, "Socket binding failed: errno %d", errno); close(server_socket); return; } ``` 3. Listen for incoming connections. ```c /* Listen for incoming connections */ err = listen(server_socket, 1); if (err != 0) { ESP_LOGE(TAG, "Error occurred while listening for incoming connections: errno %d", errno); close(server_socket); return; } ``` 4. Accept incoming connections and handle client requests. ```c /* Accept incoming connections and handle client requests */ while (1) { struct sockaddr_in client_address; socklen_t client_address_len = sizeof(client_address); int client_socket = accept(server_socket, (struct sockaddr *)&client_address, &client_address_len); if (client_socket == -1) { ESP_LOGE(TAG, "Error occurred while accepting incoming connection: errno %d", errno); continue; } char request_buffer[1024] = {0}; read(client_socket, request_buffer, sizeof(request_buffer)-1); ESP_LOGI(TAG, "Received request: %s", request_buffer); char response_buffer[1024] = {0}; sprintf(response_buffer, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body><h1>Hello, World!</h1></body></html>\r\n"); write(client_socket, response_buffer, strlen(response_buffer)); shutdown(client_socket, 0); close(client_socket); } ``` In this example, we create a simple HTTP server that responds with "Hello, World!" to any incoming request. You can modify the response to suit your needs. Note that this is a very basic example and you'll need to add error handling, thread safety, and other features to make it production-ready.
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值