├── .gitignore ├── .travis.yml ├── .vscode ├── extensions.json └── settings.json ├── images └── ui.png ├── include └── README ├── lib └── README ├── platformio.ini ├── readme.md ├── src └── main.cpp └── test └── README /.gitignore: -------------------------------------------------------------------------------- 1 | .pio 2 | .vscode/.browse.c_cpp.db* 3 | .vscode/c_cpp_properties.json 4 | .vscode/launch.json 5 | .vscode/ipch 6 | .vscode/extensions.json 7 | .vscode/settings.json 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - '3.6' 4 | sudo: false 5 | cache: 6 | directories: 7 | - "~/.platformio" 8 | install: 9 | - pip install -U platformio 10 | - platformio update 11 | script: 12 | - mkdir build 13 | - platformio ci --keep-build-dir --build-dir ./build --project-conf=platformio.ini 14 | src/*.cpp 15 | before_deploy: 16 | - platformio ci -h 17 | - ls -a 18 | - find . -name "*.bin" 19 | deploy: 20 | provider: releases 21 | api_key: 22 | secure: Hlpj55dtbINH3VjVDY3Bh9q89v3k/tTQoT7IJRYp/8r7Ry+mwXfu6Jrxg5YAh4faUvrVeh+rOLBgz1gbNYZr3qhAK7nLdrXH2BEDAeLmb7yezH5bUxx59Nzyv2usaurDeihubwG5I+ZJEt2daNeKf5/PLLWH3Phn1TCHhaelVl6ueGV8nOvNIiFfajw2CuJNYdl6rbQUPcmNNVuJ0cDNoVRCafT9DhhSvtAzLV2C4/luiYC6eoR962urcE0IXkyxywFebEdLN7nX2e2yYKMUb2tqvy++H9VsYsX05wUOXZBC8NeDZudQxoeK3hdpfls2KvuU/btBlitcsb66tVWCaHsPoAZoT5MrdF1jXPUsjSKCQlnl8ghwK1H+tYKBaXEEjLfccheK45IvLObTKdbYtsPWHUDbTdU7VPonRfmGFBv3yUIVtepPbDcHfjjv6+jizNP2JnUG4mHrqFK0Cg2iJQijZe2n/fTIwGz15e75IdcqK09MVBaKTkzPUORFmqzlvhKtXESn2HsScPzdApmJVMmgOLMX43Vnh7N3cu2zIMeUL227bpSmpjz3IWjA5h40jnQ7SllEvFrjHcIbhUQPJxViZ96NFgPGTnleQiUFAjSZ4BWibQhmgwioqN004HX88hHgEvqUYpB1qD4yCAFtrfgDHf8Qvd4Yy9lYdizSAiY= 23 | file: "./build/.pio/build/nodemcuv2/firmware.bin" 24 | skip_cleanup: true 25 | on: 26 | repo: IoTDevice/UART2TCP 27 | tags: true 28 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // See http://go.microsoft.com/fwlink/?LinkId=827846 3 | // for the documentation about the extensions.json format 4 | "recommendations": [ 5 | "platformio.platformio-ide" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "terminal.integrated.env.windows": { 3 | "PATH": "C:\\Users\\Farry\\.platformio\\penv\\Scripts;C:\\Users\\Farry\\.platformio\\penv;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\ProgramData\\DockerDesktop\\version-bin;C:\\Program Files\\Docker\\Docker\\Resources\\bin;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Go\\bin;C:\\Program Files\\Git\\cmd;C:\\Program Files\\Microsoft VS Code\\bin;C:\\git\\flutter\\bin;C:\\Users\\Farry\\AppData\\Local\\Android\\Sdk\\platform-tools;C:\\Python27;C:\\Python27\\Scripts;C:\\Program Files\\nodejs\\;C:\\Program Files\\dotnet\\;C:\\git\\flutter\\bin\\cache\\dart-sdk\\bin;C:\\git\\protoc\\bin;C:\\Program Files\\Java\\jdk1.8.0_131\\bin;C:\\Users\\Farry\\AppData\\Roaming\\Pub\\Cache\\bin;C:\\Program Files (x86)\\GnuWin32\\bin;C:\\Program Files\\PowerShell\\7-preview\\preview;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\MinGW\\bin;C:\\Users\\Farry\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\Farry\\go\\bin;C:\\Users\\Farry\\AppData\\Roaming\\npm;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\ProgramData\\DockerDesktop\\version-bin;C:\\Program Files\\Docker\\Docker\\Resources\\bin;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Go\\bin;C:\\Program Files\\Git\\cmd;C:\\Program Files\\Microsoft VS Code\\bin;C:\\git\\flutter\\bin;C:\\Users\\Farry\\AppData\\Local\\Android\\Sdk\\platform-tools;C:\\Python27;C:\\Python27\\Scripts;C:\\Program Files\\nodejs\\;C:\\Program Files\\dotnet\\;C:\\git\\flutter\\bin\\cache\\dart-sdk\\bin;C:\\git\\protoc\\bin;C:\\Program Files\\Java\\jdk1.8.0_131\\bin;C:\\Users\\Farry\\AppData\\Roaming\\Pub\\Cache\\bin;C:\\Program Files (x86)\\GnuWin32\\bin;C:\\Program Files\\PowerShell\\7-preview\\preview;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\MinGW\\bin;C:\\Users\\Farry\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\Farry\\go\\bin;C:\\Users\\Farry\\AppData\\Roaming\\npm", 4 | "PLATFORMIO_CALLER": "vscode" 5 | }, 6 | "python.linting.pylintEnabled": false, 7 | "python.linting.flake8Enabled": true, 8 | "python.linting.enabled": true, 9 | "files.associations": { 10 | "system_error": "cpp" 11 | } 12 | } -------------------------------------------------------------------------------- /images/ui.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/while0l1/UART2TCP/91902f6a2c894af8479a4e9d908a00fd3f41d03f/images/ui.png -------------------------------------------------------------------------------- /include/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for project header files. 3 | 4 | A header file is a file containing C declarations and macro definitions 5 | to be shared between several project source files. You request the use of a 6 | header file in your project source file (C, C++, etc) located in `src` folder 7 | by including it, with the C preprocessing directive `#include'. 8 | 9 | ```src/main.c 10 | 11 | #include "header.h" 12 | 13 | int main (void) 14 | { 15 | ... 16 | } 17 | ``` 18 | 19 | Including a header file produces the same results as copying the header file 20 | into each source file that needs it. Such copying would be time-consuming 21 | and error-prone. With a header file, the related declarations appear 22 | in only one place. If they need to be changed, they can be changed in one 23 | place, and programs that include the header file will automatically use the 24 | new version when next recompiled. The header file eliminates the labor of 25 | finding and changing all the copies as well as the risk that a failure to 26 | find one copy will result in inconsistencies within a program. 27 | 28 | In C, the usual convention is to give header files names that end with `.h'. 29 | It is most portable to use only letters, digits, dashes, and underscores in 30 | header file names, and at most one dot. 31 | 32 | Read more about using header files in official GCC documentation: 33 | 34 | * Include Syntax 35 | * Include Operation 36 | * Once-Only Headers 37 | * Computed Includes 38 | 39 | https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html 40 | -------------------------------------------------------------------------------- /lib/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for project specific (private) libraries. 3 | PlatformIO will compile them to static libraries and link into executable file. 4 | 5 | The source code of each library should be placed in a an own separate directory 6 | ("lib/your_library_name/[here are source files]"). 7 | 8 | For example, see a structure of the following two libraries `Foo` and `Bar`: 9 | 10 | |--lib 11 | | | 12 | | |--Bar 13 | | | |--docs 14 | | | |--examples 15 | | | |--src 16 | | | |- Bar.c 17 | | | |- Bar.h 18 | | | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html 19 | | | 20 | | |--Foo 21 | | | |- Foo.c 22 | | | |- Foo.h 23 | | | 24 | | |- README --> THIS FILE 25 | | 26 | |- platformio.ini 27 | |--src 28 | |- main.c 29 | 30 | and a contents of `src/main.c`: 31 | ``` 32 | #include 33 | #include 34 | 35 | int main (void) 36 | { 37 | ... 38 | } 39 | 40 | ``` 41 | 42 | PlatformIO Library Dependency Finder will find automatically dependent 43 | libraries scanning project source files. 44 | 45 | More information about PlatformIO Library Dependency Finder 46 | - https://docs.platformio.org/page/librarymanager/ldf.html 47 | -------------------------------------------------------------------------------- /platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env:nodemcuv2] 12 | platform = espressif8266 13 | board = nodemcuv2 14 | framework = arduino 15 | upload_speed = 921600 16 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # UART2TCP 2 | ## 简介 3 | 一个利用esp8266,实现串口到TCP,TCP到串口的数据转发的项目,可以理解为串口透传 4 | 5 | #### 功能 6 | 手机APP自动发现该esp8266设备,app发送数据到esp8266,esp8266自动转发到串口,esp8266串口接收到数据之后自动转发到APP(见本页软件主界面截图) 7 | 8 | ## 注意 9 | 本项目是使用VS Code + Platform IO 来构建的,需要在其它IDE上编译的,请自行解决库的问题 10 | 11 | > ## control with app(使用以下app操控): 12 | > * https://github.com/OpenIoTHub/OpenIoTHub 13 | > * 此程序会自动发现设备,将设备放入智能设备列表 14 | 15 | #### 配网和发现 16 | * 本固件使用smartconfig配网方式 17 | * 直接在APP"+"进入配网界面输入wifi密码配网 18 | * APP在同一个局域网自动发现该设备 19 | 20 | #### 支持的功能: 21 | - [x] 1.与esp8266串口收发数据 22 | 23 | #### 项目使用的硬件 24 | * esp8266 25 | 26 | #### 波特率 27 | * 默认波特率为115200 28 | 29 | #### 远程 30 | * 支持云易连APP通过网关方式远程 31 | 32 | #### 界面 33 | * 主界面 34 | ![image](./images/ui.png) 35 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define SYSRuntime D4 5 | 6 | int tcpPort = 8266; //服务器端口 7 | char valFromPC; //接收TCP发过来的数据 8 | String valFromMCU = ""; //接收串口发过来的数据 9 | int exitFlag; //退出配置模式标志 10 | unsigned int Baud = 115200; //串口波特率 11 | 12 | WiFiServer server(tcpPort); 13 | WiFiClient client; 14 | 15 | //开启热点 16 | //开启TCP服务 17 | void StartServer() 18 | { 19 | // 连接WIFI 20 | WiFi.mode(WIFI_STA); 21 | // 选取一种连接路由器的方式 22 | // WiFi.begin(ssid, password); 23 | WiFi.beginSmartConfig(); 24 | 25 | server.begin(tcpPort); //开启TCP服务器 26 | server.setNoDelay(true); 27 | } 28 | 29 | void setup() 30 | { 31 | //初始化串口 32 | Serial.begin(Baud); 33 | Serial.setRxBufferSize(1024 * 10); 34 | Serial.setTimeout(2); 35 | pinMode(SYSRuntime, OUTPUT); 36 | delay(10); 37 | //启动TCP服务 38 | StartServer(); 39 | 40 | // Wait for connection 41 | while (WiFi.status() != WL_CONNECTED) { 42 | delay(500); 43 | // Serial.print("."); 44 | } 45 | 46 | MDNS.begin("UART2TCP-"+WiFi.macAddress()); 47 | MDNS.addService("iotdevice", "tcp", tcpPort); 48 | // MDNS.addServiceTxt("iotdevice", "tcp", "mykEY","myvALUE"); 49 | MDNS.addServiceTxt("iotdevice", "tcp", "name", "串口转TCP"); 50 | MDNS.addServiceTxt("iotdevice", "tcp", "model", "com.iotserv.devices.UART2TCP"); 51 | MDNS.addServiceTxt("iotdevice", "tcp", "mac", WiFi.macAddress()); 52 | MDNS.addServiceTxt("iotdevice", "tcp", "id", ESP.getSketchMD5()); 53 | MDNS.addServiceTxt("iotdevice", "tcp", "ui-support", "native"); 54 | MDNS.addServiceTxt("iotdevice", "tcp", "ui-first", "native"); 55 | MDNS.addServiceTxt("iotdevice", "tcp", "author", "Farry"); 56 | MDNS.addServiceTxt("iotdevice", "tcp", "email", "newfarry@126.com"); 57 | MDNS.addServiceTxt("iotdevice", "tcp", "home-page", "https://github.com/iotdevice"); 58 | MDNS.addServiceTxt("iotdevice", "tcp", "firmware-respository", "https://github.com/iotdevice/UART2TCP"); 59 | MDNS.addServiceTxt("iotdevice", "tcp", "firmware-version", "1.0"); 60 | MDNS.addServiceTxt("iotdevice", "tcp", "mdns-only", "true"); 61 | } 62 | 63 | void loop() 64 | { 65 | MDNS.update(); 66 | if (server.hasClient()) { //有新的设备连接上 67 | client = server.available(); 68 | } 69 | while (client.available()) //TCP接收单片机发过来的信息,串口发给电脑 70 | { 71 | digitalWrite(SYSRuntime, LOW); 72 | valFromPC = client.read(); 73 | Serial.print(valFromPC); 74 | } 75 | 76 | while (Serial.available()) { //串口接收电脑发送过来的信息,TCP发送给单片机 77 | digitalWrite(SYSRuntime, LOW); 78 | valFromMCU = Serial.readString(); 79 | client.print(valFromMCU); //把串口数据通过TCP发出去 80 | } 81 | } -------------------------------------------------------------------------------- /test/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for PIO Unit Testing and project tests. 3 | 4 | Unit Testing is a software testing method by which individual units of 5 | source code, sets of one or more MCU program modules together with associated 6 | control data, usage procedures, and operating procedures, are tested to 7 | determine whether they are fit for use. Unit testing finds problems early 8 | in the development cycle. 9 | 10 | More information about PIO Unit Testing: 11 | - https://docs.platformio.org/page/plus/unit-testing.html 12 | --------------------------------------------------------------------------------