如何通过代码获取Arduino板卡信息与SN序列号?
Hey there! Let's tackle your Arduino questions one by one— I've worked through similar needs for projects, so here's what you need to know:
There are a few practical ways to pull board details, depending on the Arduino model you're using:
- Use built-in core library functions: Most official Arduino boards have pre-defined macros or functions that let you identify the board type and hardware specs. Here's a quick example:
#include <Arduino.h> void setup() { Serial.begin(9600); while (!Serial); // Wait for serial monitor to connect // Print CPU frequency (defined in core library) Serial.print("CPU Frequency: "); Serial.print(F_CPU / 1000000); Serial.println(" MHz"); // Detect and print board type #ifdef ARDUINO_AVR_UNO Serial.println("Board Type: Arduino UNO"); #elif ARDUINO_AVR_MEGA2560 Serial.println("Board Type: Arduino MEGA 2560"); #elif ARDUINO_SAM_DUE Serial.println("Board Type: Arduino Due"); #elif ARDUINO_MKR_WIFI_1010 Serial.println("Board Type: Arduino MKR WiFi 1010"); #else Serial.println("Board Type: Unknown or Custom Board"); #endif } void loop() { // No loop action needed here }
- Read hardware registers: For lower-level details (like reset reason, chip model), you can directly access the microcontroller's registers. For example, on AVR chips, the
MCUSRregister holds reset cause info— just reference your chip's datasheet for the correct register addresses.
First, a quick heads-up: Not all Arduino boards have a pre-programmed serial number (SN). Entry-level models like the UNO don't have an official SN, but you can use the microcontroller's unique chip ID as a reliable substitute. Higher-end boards (like Due, MKR series) have built-in unique IDs you can read directly. Here's how to do both, plus send the data to a database:
Step 1: Read the Unique ID/SN
#include <Arduino.h> // For Arduino Due (SAM3X chip) void printDueUniqueID() { uint32_t chipID[4]; // Read the chip's unique ID registers chipID[0] = *(uint32_t *)(0x0080A00C); chipID[1] = *(uint32_t *)(0x0080A010); chipID[2] = *(uint32_t *)(0x0080A014); chipID[3] = *(uint32_t *)(0x0080A018); Serial.print("Board SN: "); for (int i = 0; i < 4; i++) { Serial.print(chipID[i], HEX); if (i != 3) Serial.print("-"); } Serial.println(); } // For AVR boards (UNO, MEGA) void printAVRUniqueID() { uint8_t chipID[8]; // Read the AVR chip's unique ID from program memory for (int i = 0; i < 8; i++) { chipID[i] = pgm_read_byte(0x00800000 + i); } Serial.print("Board SN (Chip ID): "); for (int i = 0; i < 8; i++) { Serial.print(chipID[i], HEX); if (i != 7) Serial.print("-"); } Serial.println(); } void setup() { Serial.begin(9600); while (!Serial); // Detect board type and print ID #ifdef ARDUINO_SAM_DUE printDueUniqueID(); #elif defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_MEGA2560) printAVRUniqueID(); #else Serial.println("SN reading not supported for this board by default"); #endif }
If you're using a custom board or need a dedicated, user-programmable SN, you can solder a small EEPROM chip to the board, program a unique ID into it during assembly, then read that ID in your code.
Step 2: Send the SN to a Database
Once you have the SN, you can send it to your database using WiFi, Ethernet, or even a serial-to-WiFi module (like ESP8266). Here's a simplified example using WiFi on an Arduino MKR WiFi 1010:
#include <Arduino.h> #include <WiFiNINA.h> #include <HTTPClient.h> const char* ssid = "YOUR_WIFI_SSID"; const char* password = "YOUR_WIFI_PASSWORD"; const char* databaseAPI = "http://your-database-endpoint.com/submit-sn"; String getBoardSN() { // Reuse the ID-reading logic from Step 1 here uint8_t chipID[8]; for (int i = 0; i < 8; i++) { chipID[i] = pgm_read_byte(0x00800000 + i); } String sn; for (int i = 0; i < 8; i++) { sn += String(chipID[i], HEX); if (i != 7) sn += "-"; } return sn; } void setup() { Serial.begin(9600); while (!Serial); // Connect to WiFi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi connected"); // Get SN and send to database String sn = getBoardSN(); HTTPClient http; http.begin(databaseAPI); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); String postData = "sn=" + sn; int httpCode = http.POST(postData); if (httpCode > 0) { Serial.print("Data sent! Response code: "); Serial.println(httpCode); String response = http.getString(); Serial.println("Server response: " + response); } else { Serial.print("Failed to send data. Error code: "); Serial.println(httpCode); } http.end(); } void loop() { // No repeated action needed unless you want to re-send periodically }
Just replace YOUR_WIFI_SSID, YOUR_WIFI_PASSWORD, and your-database-endpoint.com/submit-sn with your actual credentials and database API URL.
内容的提问来源于stack exchange,提问作者Ali Wehbi

