Arduino Uno+ENC28J60 POST请求异常:服务器日志出现乱码格式
Hey there! Let's break down why your POST request is spitting out those garbled \xa9\x020\x91\xa7\x020\x93\xa9\x02... characters while GET works perfectly. Since your server works with Postman, the issue is definitely in how your Arduino+ENC28J60 is formatting the POST request. Here are the most likely culprits and fixes:
1. Your POST request is missing critical HTTP headers or has malformed formatting
Unlike GET (which only needs a request line and basic headers), POST requires explicit headers to tell the server how to parse the data you're sending. The most common mistakes here are:
- Forgetting the
Content-Typeheader (e.g.,application/x-www-form-urlencodedfor form data) - Skipping the mandatory empty line (
\r\n\r\n) between headers and the POST body - Using
\ninstead of\r\nfor line breaks (HTTP requires CRLF line endings)
Without these, the server can't distinguish where headers end and data begins, so it tries to parse random bytes as part of the request—hence the garbage characters.
2. Incorrect Content-Length calculation
This is the #1 cause of POST parsing failures. The Content-Length header must exactly match the number of bytes in your POST body. If you miscalculate this (e.g., hardcoding a wrong value, or using character count instead of byte count for non-ASCII data), the server will either:
- Read too few bytes and treat leftover TCP stream data as part of the request, or
- Wait for more bytes than you send, timing out and logging partial garbage data
3. Buggy ENC28J60 library handling of POST data
Some older versions of ENC28J60 libraries have quirks with sending longer POST requests or handling binary data. For example, if you split the request into separate header and body sends instead of sending the full request as a single buffer, the library might introduce extra bytes or truncate data.
Fixes to try
First, let's make sure your POST request follows the strict HTTP 1.1 format. Here's a template of what it should look like:
POST /your-endpoint.php HTTP/1.1 Host: your-server-ip-or-domain Content-Type: application/x-www-form-urlencoded Content-Length: [EXACT BYTE COUNT OF YOUR POST DATA] [YOUR POST DATA HERE, e.g., key1=value1&key2=value2]
Step 1: Build the request correctly in code
Use this example as a reference for your Arduino code:
#include <EtherCard.h> // Network config (adjust to your setup) static byte mymac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; static byte serverip[] = { 192, 168, 1, 100 }; // Your server IP // POST data and its byte length char postBody[] = "sensor=temp&value=25.5"; int postBodyLen = strlen(postBody); // Buffer to hold the full POST request char postRequest[256]; void setup() { Serial.begin(9600); if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0) { Serial.println("ENC28J60 init failed!"); return; } ether.staticSetup({192, 168, 1, 50}, {192, 168, 1, 1}, {8, 8, 8, 8}); } void loop() { // Build the full POST request with correct formatting sprintf(postRequest, "POST /data.php HTTP/1.1\r\n" "Host: 192.168.1.100\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "Content-Length: %d\r\n" "\r\n" "%s", postBodyLen, postBody); // Send the request if (ether.clientConnect(serverip, 80)) { ether.httpPost(postRequest, sizeof(postRequest)-1); // Wait for response while (ether.clientConnected()) { if (ether.packetLoop(ether.packetReceive())) { Serial.println("POST request succeeded!"); break; } } ether.clientDisconnect(); } else { Serial.println("Failed to connect to server"); } delay(10000); }
Step 2: Verify Content-Length
Double-check that postBodyLen is the actual byte count of your data. For ASCII strings, strlen() works, but if you're using non-ASCII characters, you'll need to calculate bytes manually.
Step 3: Update your ENC28J60 library
If you're using an older library version, grab the latest stable release—many POST-related bugs have been fixed in recent updates.
Why the garbled characters?
When the server receives a malformed POST request, it can't properly parse the boundaries between headers, body, and leftover TCP data. Those \xa9\x020... values are raw binary bytes from the ENC28J60's buffer or incomplete TCP stream, which the server tries (and fails) to interpret as part of the request.
内容的提问来源于stack exchange,提问作者Itamar Ribeiro

