大佬教程收集整理的这篇文章主要介绍了Arduino ESP8266 HTTPS 服务器如何安装 SSL 证书,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用 ESP8266WebServer 库在我的 NodeMCU ESP8266 板上制作了 http 服务器。我想将其转换为httpS服务器,所以我使用了ESP8266WebServerSecure库,购买了SSL证书并获得了3个文件:
certificate.cert
dv_ca.pem
private.pem
有人能解释一下我应该如何在我的服务器上安装它吗?当我尝试使用自生成的证书启动 https 服务器时,在我的代码下方,但使用 androID 应用程序连接它时出现问题(未找到 AndroID SSL 连接的信任锚点)。
#include <Arduino.h>
#include <ESP8266WebServer.h>
#include <ESP8266WebServerSecure.h>
#include <ESP8266mDNs.h>
#include <ArduinoJson.h>
#define output4 4
#define output5 5
#define input0 A0
const char* ssID = "SSID";
const char* password = "password";
//httpS server object,port number 443
BeaRSSL::ESP8266WebServerSecure server(443);
static const char serverCert[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
)EOF";
static const char serverKeY[] PROGMEM = R"EOF(
-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----
)EOF";
voID handleNotFound() {
String message = "No such a data or incorrect request...";
server.send(404,"text/plain",messagE);
}
voID changeStatus(){
//Parsing request argument to String
String postbody = server.arg("plain");
//Changing output value condition
if(server.hasArg("number")&&server.hasArg("status")){
int pinnumber = server.arg("number").toInt();
if(server.arg("status")=="on"){
digitalWrite(pinnumber,HIGH);
} else if(server.arg("status")=="off"){
digitalWrite(pinnumber,LOW);
}
} else {
handleNotFound();
}
serial.println("Argument: "+postbody);
server.send(200,postbody);
}
voID getStatus(){
//CreaTing Json document
DynamicJsondocument doc(200);
//Pushing data to Json document
if(digitalRead(output4)==LOW){
JsonObject out4 = doc.createnestedobject("sensor 4");
out4["name"] = "output4";
out4["value"] = 0;
} else {
JsonObject out4 = doc.createnestedobject("sensor 4");
out4["name"] = "output4";
out4["value"] = 1;
}
if(digitalRead(output5)==LOW){
JsonObject out5 = doc.createnestedobject("sensor 5");
out5["name"] = "output5";
out5["value"] = 0;
} else {
JsonObject out5 = doc.createnestedobject("sensor 5");
out5["name"] = "output5";
out5["value"] = 1;
}
//GetTing analog input value
JsonObject in0 = doc.createnestedobject("sensor 0");
in0["name"] = "input0";
in0["value"] = analogRead(A0);
//Variable co
String outputData = "";
serializeJsonPretty(doc,outputData);
server.send(200,"application/Json",outputData);
}
voID setup() {
// put your setup code here,to run once:
serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output5,OUTPUT);
pinMode(output4,OUTPUT);
// Set outputs to LOW
digitalWrite(output5,LOW);
digitalWrite(output4,LOW);
//Get current time
configTime(3 * 3600,"pool.ntp.org","time.nist.gov");
//Start wifi connection
WiFi.begin(ssID,password);
delay(100);
serial.println("Trying to connect wifi network");
while(WiFi.status() != WL_CONNECTED){
serial.print(".");
delay(500);
}
serial.println("");
serial.print("Connected to ");
serial.println(WiFi.SSID());
serial.print("IP adress: ");
serial.println(WiFi.localiP());
//Handling requests
server.on("/",http_GET,getStatus);
server.on("/led",changeStatus);
//SetTing server certificate and key
server.getServer().setRSACert(new BeaRSSL::X509List(serverCert),new BeaRSSL::PrivateKey(serverKey));
// Start server
server.begin();
serial.println("Secured server ready");
}
voID loop() {
// put your main code here,to run repeatedly:
// Handling incoming clIEnt requests in loop
server.handleClIEnt();
}
代码来自 ESP8266 的示例。除测试目的外,更改证书。您可以从 Internet 获取证书或使用位于 @H_427_5@make-self-signed-cert.sh 的 ESP8266WiFi/examples/WiFihttpSServer
生成证书。在执行 @H_427_5@make-self-signed-cert.sh 之前,修改脚本。找到 your-name-here
并将其替换为您的姓名或组织名称。然后执行脚本。在执行结束时,将出现名为 key.h
和 x509.h
的新文件。证书代码位于两个新文件中。将它们复制并粘贴到 arduino 代码中。
#include <ESP8266WiFi.h>
#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
// The certificate is stored in PMEM
static const uint8_t x509[] PROGMEM = {
0x30,0x82,0x01,0x3d,0x30,0x81,0xe8,0x02,0x09,0x00,0xfe,0x56,0x46,0xf2,0x78,0xc6,0x51,0x17,0x0d,0x06,0x2a,0x86,0x48,0xf7,0x0b,0x05,0x26,0x31,0x10,0x0e,0x03,0x55,0x04,0x0a,0x0c,0x07,0x45,0x53,0x50,0x38,0x32,0x36,0x12,0x37,0x2e,0x1e,0x33,0x34,0x39,0x5a,0x35,0x5c,0x4b,0x41,0x72,0x6c,0xe1,0x20,0x4d,0x3a,0x49,0xe2,0x2d,0xc9,0x7a,0x63,0x1d,0xef,0xbb,0xa3,0xd6,0x6f,0x59,0xcb,0xd5,0xf6,0xbe,0x83,0x80,0xec,0xbf,0xee,0x94,0x67,0x8b,0x8d,0x85,0x23,0x24,0x52,0x76,0x9d,0x18,0xb3,0x3c,0x40,0xf3,0x69,0xdc,0x9b,0xa7,0x62,0x57,0x7e,0xad,0x9a,0x90,0xe7,0xdf,0x8f,0xd3,0x5f,0x2b,0xde,0x92,0x1b,0xc5,0xc0,0xf4,0x08,0x97,0x27,0xf1,0x60,0xc2,0xcd,0xca,0xfd,0x9e,0x6b,0x98,0x8f
};
// And so is the key. these could also be in DRAM
static const uint8_t rsakeY[] PROGMEM = {
0x30,0x74,0xff,0x15,0x44,0x0f,0x13,0x93,0x3f,0xfc,0xf9,0x5e,0xf0,0xb1,0x25,0x7c,0x7d,0xb5,0x99,0x22,0x68,0xc7,0x4e,0xb6,0x61,0x21,0xea,0x8c,0xd4,0x7f,0x91,0xfa,0xf8,0xb9,0x6e,0x87,0xaa,0x14,0x8e,0xd8,0x71,0xd1,0x70,0xab,0x58,0x9c,0xe0,0xc1,0xa2,0xbc,0x28,0x1a,0xb4,0x65,0x5b,0x73,0xeb,0x3e,0x4c,0xb7,0xd7,0x5d,0xb0,0xa0,0xac,0x2c,0xbd,0xe5,0xb8,0x42,0x9f,0x3b,0xf5,0x64,0xaf,0x6d,0x29,0x03
};
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServerSecure server(443);
void setup() {
serial.begin(115200);
// prepare GPIO2
pinMode(2,OUTPUT);
digitalWrite(2,0);
// Connect to WiFi network
serial.println();
serial.println();
serial.print("ConnecTing to ");
serial.println(ssid);
WiFi.begin(ssid,password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
serial.print(".");
}
serial.println("");
serial.println("WiFi connected");
// Set the certificates from PMEM (if using DRAM remove the _P from the call)
server.setServerKeyAndCert_P(rsakey,sizeof(rsakey),x509,sizeof(x509));
// Start the server
server.begin();
serial.println("Server started");
// Print the IP address
serial.println(WiFi.localIP());
}
void loop() {
// check if a client has connected
WiFiClientSecure client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
serial.println("new client");
unsigned long timeout = millis() + 3000;
while (!client.available() && millis() < timeout) {
delay(1);
}
if (millis() > timeout) {
serial.println("timeout");
client.flush();
client.stop();
return;
}
// Read the first line of the request
String req = client.readStringUntil('\r');
serial.println(req);
client.flush();
// Match the request
int val;
if (req.indexOf("/gpio/0") != -1) {
val = 0;
} else if (req.indexOf("/gpio/1") != -1) {
val = 1;
} else {
serial.println("invalid request");
client.print("http/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html><body>Not found</body></html>");
return;
}
// Set GPIO2 according to the request
digitalWrite(2,val);
client.flush();
// Prepare the response
String s = "http/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (val) ? "high" : "low";
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
serial.println("Client disconnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
以上是大佬教程为你收集整理的Arduino ESP8266 HTTPS 服务器如何安装 SSL 证书全部内容,希望文章能够帮你解决Arduino ESP8266 HTTPS 服务器如何安装 SSL 证书所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。