我正在尝试将 ESP8266 与 HTTPS python 服务器连接,但 ESP 未与服务器连接并给出服务器名称错误的 bearssl 错误代码
I am trying to connect ESP8266 with HTTPS python server but ESP is not connecting with server and giving bearssl error code that server name is bad
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#ifndef STASSID
#define STASSID "ssid"
#define STAPSK "pwd"
#endif
WiFiClientSecure client;
const char* ssid = STASSID;
const char* password = STAPSK;
const char* host = "server ip";
const int httpsPort = 443;
// DigiCert High Assurance EV Root CA
static const char trustRoot[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDZzCCAk8CFC6I3N+ayMvUYtM5fV3dgVter3/HMA0GCSqGSIb3DQEBCwUAMHAx
...
...
5yZi0b12t0nRhP9/nfqjrq18qQvuNpFMkEZoX55TU1j+6FTIHlcBSBEzAwRlcy6z
ayyQAen4YGq3fMk=
-----END CERTIFICATE-----
)EOF";
X509List cert(trustRoot);
const char client_cert[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDZzCCAk8CFEHOqlzoEw8VZ9UyBUh+Pg9ZGT0cMA0GCSqGSIb3DQEBCwUAMHAx
...
...
K6VmXQciQQuk7V0i75CyxevY9GVDVHs0hpoG7kfXVUVnBo+wQII7p7tEbcJBiQrG
tmQix+1gLmYmsH4=
-----END CERTIFICATE-----
)EOF";
/*-----------------------------------------------------------------*/
const char client_private_key[] PROGMEM = R"KEY(
-----BEGIN PRIVATE KEY-----
MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQC5UPJy4qfYeFzd
...
...
cajJThoAW7ihH96fZW7vK0ejSeAxwBgorwzbhWvEORt6be5j2eUycT5q6eOYGXCg
IbCUotZgJRw81XO0uNeYCFyClwM=
-----END PRIVATE KEY-----
)KEY";
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP())
// Set time via NTP, as required for x.509 validation
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
Serial.print(".");
now = time(nullptr);
}
Serial.println("");
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Serial.print(asctime(&timeinfo));
Serial.print("Connecting to ");
Serial.println(host);
client.setTrustAnchors(&cert);
X509List serverCertList(client_cert);
PrivateKey serverPrivKey(client_private_key);
client.setClientRSACert(&serverCertList,&serverPrivKey);
if (!client.connect(host, httpsPort)) {
Serial.println("Connection failed");
Serial.print(client.getLastSSLError());
//delay(1000);
} else {}
//client.connect(host, httpsPort);
//String url = "/repos/esp8266/Arduino/commits/master/status";
//Serial.print("Requesting URL: ");
//Serial.println(url);
//client.print("Hello from esp8266");
/*client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");*/
//Serial.println("Request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("Headers received");
break;
}
}
String line = client.readStringUntil('\n');
/* if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("esp8266/Arduino CI successfull!");
} else {
Serial.println("esp8266/Arduino CI has failed");
}*/
Serial.println("Reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("Closing connection");
}
void loop() {
}
我试图将服务器和客户端证书以及私钥加载到 运行 但它没有连接到服务器我还附加了 BEARSSL 的日志。
日志:
Wifi连接IP地址:
192.168.34.12
等待 NTP 时间同步:
当前时间:10 月 27 日星期二 04:06:24 2020
正在连接到 192.168.15.123(服务器 ip)
连接失败
错误code:56
您正在将 IP 地址作为 const char*
传递给 client.connect()
,但该函数需要一个 4 字节的 IP 地址数组;它现在认为它正在获取一个 URL,它将尝试解析为一个 IP 地址(但失败)。
如果您声明任一
IPAddress host(192, 168, 15, 123);
或 const char* host = "hostname";
那么 client.connect(host, httpsPort);
就可以了;它不会理解这一点:
const char* host = "192.168.15.123";
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#ifndef STASSID
#define STASSID "ssid"
#define STAPSK "pwd"
#endif
WiFiClientSecure client;
const char* ssid = STASSID;
const char* password = STAPSK;
const char* host = "server ip";
const int httpsPort = 443;
// DigiCert High Assurance EV Root CA
static const char trustRoot[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDZzCCAk8CFC6I3N+ayMvUYtM5fV3dgVter3/HMA0GCSqGSIb3DQEBCwUAMHAx
...
...
5yZi0b12t0nRhP9/nfqjrq18qQvuNpFMkEZoX55TU1j+6FTIHlcBSBEzAwRlcy6z
ayyQAen4YGq3fMk=
-----END CERTIFICATE-----
)EOF";
X509List cert(trustRoot);
const char client_cert[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDZzCCAk8CFEHOqlzoEw8VZ9UyBUh+Pg9ZGT0cMA0GCSqGSIb3DQEBCwUAMHAx
...
...
K6VmXQciQQuk7V0i75CyxevY9GVDVHs0hpoG7kfXVUVnBo+wQII7p7tEbcJBiQrG
tmQix+1gLmYmsH4=
-----END CERTIFICATE-----
)EOF";
/*-----------------------------------------------------------------*/
const char client_private_key[] PROGMEM = R"KEY(
-----BEGIN PRIVATE KEY-----
MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQC5UPJy4qfYeFzd
...
...
cajJThoAW7ihH96fZW7vK0ejSeAxwBgorwzbhWvEORt6be5j2eUycT5q6eOYGXCg
IbCUotZgJRw81XO0uNeYCFyClwM=
-----END PRIVATE KEY-----
)KEY";
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP())
// Set time via NTP, as required for x.509 validation
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
Serial.print("Waiting for NTP time sync: ");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
Serial.print(".");
now = time(nullptr);
}
Serial.println("");
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print("Current time: ");
Serial.print(asctime(&timeinfo));
Serial.print("Connecting to ");
Serial.println(host);
client.setTrustAnchors(&cert);
X509List serverCertList(client_cert);
PrivateKey serverPrivKey(client_private_key);
client.setClientRSACert(&serverCertList,&serverPrivKey);
if (!client.connect(host, httpsPort)) {
Serial.println("Connection failed");
Serial.print(client.getLastSSLError());
//delay(1000);
} else {}
//client.connect(host, httpsPort);
//String url = "/repos/esp8266/Arduino/commits/master/status";
//Serial.print("Requesting URL: ");
//Serial.println(url);
//client.print("Hello from esp8266");
/*client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");*/
//Serial.println("Request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("Headers received");
break;
}
}
String line = client.readStringUntil('\n');
/* if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("esp8266/Arduino CI successfull!");
} else {
Serial.println("esp8266/Arduino CI has failed");
}*/
Serial.println("Reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("Closing connection");
}
void loop() {
}
我试图将服务器和客户端证书以及私钥加载到 运行 但它没有连接到服务器我还附加了 BEARSSL 的日志。
日志: Wifi连接IP地址: 192.168.34.12
等待 NTP 时间同步: 当前时间:10 月 27 日星期二 04:06:24 2020
正在连接到 192.168.15.123(服务器 ip) 连接失败
错误code:56
您正在将 IP 地址作为 const char*
传递给 client.connect()
,但该函数需要一个 4 字节的 IP 地址数组;它现在认为它正在获取一个 URL,它将尝试解析为一个 IP 地址(但失败)。
如果您声明任一
IPAddress host(192, 168, 15, 123);
或 const char* host = "hostname";
那么 client.connect(host, httpsPort);
就可以了;它不会理解这一点:
const char* host = "192.168.15.123";