1

I'm trying to set up a HTTP and HTTPS server at the same time on an ESP8266 NodeMCU V3. The compiler however doesn't accept any other name than client otherwise no new clients will be catched. But client cannot be used twice and I cannot get it to switch every loop because of the different data-type; WiFiClient and WiFiClientSecure.

Is what I'm trying to accomplish even possible?

Sketch.ino

// Configuration
const char* CONFIG_SSID      = "mywifi";
const char* CONFIG_PSK       = "wifipassword";
const int   CONFIG_SERIAL    = 115200;
const int   CONFIG_HTTPPORT  = 80;
const int   CONFIG_HTTPSPORT = 443;

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

// Include libraries
#include <ESP8266WiFi.h>

// Create webserver object
WiFiServer HTTPserver(CONFIG_HTTPPORT);
WiFiServerSecure HTTPSserver(CONFIG_HTTPSPORT);
static const uint8_t rsakey[] PROGMEM = {
  #include "key.h"
};
static const uint8_t x509[] PROGMEM = {
  #include "x509.h"
};

// Setup instructions
void setup() {

  // Start serial and LED
  Serial.begin(CONFIG_SERIAL);
  Serial.println();
  Serial.println("Starting setup");
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 1);

  // Start WiFi connection
  Serial.print("Connecting to: ");
  Serial.println(CONFIG_SSID);
  WiFi.mode(WIFI_STA);
  WiFi.begin(CONFIG_SSID, CONFIG_PSK);

  // Wait for WiFi connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected!");

  // Print WiFi connection information
  Serial.print("  MAC Address: ");
  Serial.println(WiFi.macAddress());
  Serial.print("  SSID: ");
  Serial.println(WiFi.SSID());
  Serial.print("  RSSI: ");
  Serial.print(WiFi.RSSI());
  Serial.println(" dBm");
  Serial.print("  IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.print("  Gateway: ");
  Serial.println(WiFi.gatewayIP());

  // Start webserver
  Serial.println("Starting webserver...");
  HTTPserver.begin();
  HTTPSserver.setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
  HTTPSserver.begin();
  Serial.println("Webserver started!");

  // Print webserver information
  Serial.print("  Hostname: ");
  Serial.println(WiFi.hostname());
  Serial.print("  HTTP Port: ");
  Serial.println(CONFIG_HTTPPORT);
  Serial.print("  HTTPS Port: ");
  Serial.println(CONFIG_HTTPSPORT);

}

// Loop instructions
void loop() {

  // Wait for new client
  WiFiClient HTTPclient = HTTPserver.available();
  WiFiClientSecure HTTPSclient = HTTPSserver.available();
  if (HTTPclient || HTTPSclient) {
  }

}

UPDATE (2019-06-05):

I've tried @KIIV's method which works! However it brings a new problem into the equation. HTTPS requests take pretty long te complete, currently about 2 seconds. And it makes the HTTPS request twice for some reason (see log; I made only one request after setup).

Sketch-updated.ino

// Configuration
const char* CONFIG_SSID      = "mywifi";
const char* CONFIG_PSK       = "wifipassword";
const int   CONFIG_SERIAL    = 115200;
const int   CONFIG_HTTPPORT  = 80;
const int   CONFIG_HTTPSPORT = 443;

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

// Include libraries
#include <ESP8266WiFi.h>

// Create webserver object
WiFiServer HTTPserver(CONFIG_HTTPPORT);
WiFiServerSecure HTTPSserver(CONFIG_HTTPSPORT);
static const uint8_t rsakey[] PROGMEM = {
  #include "key.h"
};
static const uint8_t x509[] PROGMEM = {
  #include "x509.h"
};

// Setup instructions
void setup() {

  // Start serial and LED
  Serial.begin(CONFIG_SERIAL);
  Serial.println();
  Serial.println("Starting setup");
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 1);

  // Start WiFi connection
  Serial.print("Connecting to: ");
  Serial.println(CONFIG_SSID);
  WiFi.mode(WIFI_STA);
  WiFi.begin(CONFIG_SSID, CONFIG_PSK);

  // Wait for WiFi connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected!");

  // Print WiFi connection information
  Serial.print("  MAC Address: ");
  Serial.println(WiFi.macAddress());
  Serial.print("  SSID: ");
  Serial.println(WiFi.SSID());
  Serial.print("  RSSI: ");
  Serial.print(WiFi.RSSI());
  Serial.println(" dBm");
  Serial.print("  IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.print("  Gateway: ");
  Serial.println(WiFi.gatewayIP());

  // Start webserver
  Serial.println("Starting webserver...");
  HTTPserver.begin();
  HTTPSserver.setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
  HTTPSserver.begin();
  Serial.println("Webserver started!");

  // Print webserver information
  Serial.print("  Hostname: ");
  Serial.println(WiFi.hostname());
  Serial.print("  HTTP Port: ");
  Serial.println(CONFIG_HTTPPORT);
  Serial.print("  HTTPS Port: ");
  Serial.println(CONFIG_HTTPSPORT);

  // Setup complete
  Serial.println("Setup completed");
  digitalWrite(LED_BUILTIN, 0);
  Serial.println();

}

// Loop instructions
void loop() {

  // New wait for new client
  if (HTTPserver.hasClient()) {

    // Set client to HTTP for this loop
    //auto client = HTTPserver.available();
    WiFiClient client = HTTPserver.available();

    // Response
    Serial.println("This is HTTP");
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/plain");
    client.println("Connection: close");
    client.println();
    client.println("This is HTTP");

  } else if (HTTPSserver.hasClient()) {

    // Set client to HTTP for this loop
    //auto client = HTTPSserver.available();
    WiFiClientSecure client = HTTPSserver.available();

    // Response
    Serial.println("This is HTTP Secure");
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/plain");
    client.println("Connection: close");
    client.println();
    client.println("This is HTTP Secure");

  }

}

Serial Log

15:59:34.121 -> Starting setup
15:59:34.121 -> Connecting to: mywifi
15:59:34.452 -> .............
15:59:38.205 -> WiFi connected!
15:59:38.205 ->   MAC Address: XX:XX:XX:XX:XX:XX
15:59:38.238 ->   SSID: mywifi
15:59:38.238 ->   RSSI: -21 dBm
15:59:38.238 ->   IP Address: 192.168.1.121
15:59:38.238 ->   Gateway: 192.168.1.1
15:59:38.238 -> Starting webserver...
15:59:38.238 -> Webserver started!
15:59:38.238 ->   Hostname: ESP_XXXXXX
15:59:38.238 ->   HTTP Port: 80
15:59:38.238 ->   HTTPS Port: 443
15:59:38.271 -> Setup completed
15:59:38.271 -> 
16:00:38.069 -> This is HTTP Secure
16:00:38.833 -> This is HTTP Secure
2
  • I'd try if there is client awaiting first: if (HTTPserver.hasClient()) { auto client = HTTPSserver.available(); /* do something; */ } and the same for HTTPS. (and maybe optimistic_yield(1000) as it's used inside of available so it'll be almost the same – KIIV Jun 2 '19 at 19:55
  • 1
    @KIIV your method works! It however also causes a problem; HTTPS requests take a really long time to complete. Between 5-10 seconds, which also triggers a timeout function (not present in example code). This long time is not happening when trying a sketch which only uses a HTTPS server. – Rick Jun 2 '19 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.