Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1501

Arduino ESP8266 HTTPS server How to install SSL certificate

$
0
0

I made HTTP server on my NodeMCU ESP8266 board using ESP8266WebServer library. I would like to convert it to the HTTPS server, so I used ESP8266WebServerSecure library, bought SSL certificate and got 3 files:

certificate.certdv_ca.pemprivate.pem

Could someone explain me how should I install it on my server? Below my code when I was trying to launch https server with self-generated certificate, but there was a problem to connect it using android application(Trust anchor not found for Android SSL connection).

#include <Arduino.h>#include <ESP8266WebServer.h>#include <ESP8266WebServerSecure.h>#include <ESP8266mDNS.h>#include <ArduinoJson.h>#define output4 4#define output5 5#define input0 A0const char* ssid = "SSID";const char* password = "password"; //HTTPS server object, port number 443BearSSL::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, "text/plain", 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, 0, "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", HTTP_GET, 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();}

Viewing all articles
Browse latest Browse all 1501

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>