Postby crosofg » Tue Feb 02, 2016 12:16 am
Please find the code attached. I am using sdk 1.5.2
Code: Select all
#include"ets_sys.h"
#include"osapi.h"
#include"gpio.h"
#include"os_type.h"
#include"user_interface.h"
#include"mem.h"
#include"espconn.h"
#include"driver/uart.h"
// Make your own user_config.h
#include"user_config.h" // defines WIFI_SSID and WIFI_PSWD
// These files are created by makecert.sh
#include "ssl/cert.h" // defines myesp_crt_DER and myesp_crt_DER_len
#include "ssl/private_key.h" // defines myesp_key_DER and myesp_key_DER_len
// listener connection data
struct espconn listenConn;
esp_tcp listenConnTcp;
// standard HTTP reply
uint8_t* reply = "HTTP/1.1 200 OK\r\n"
"Connection: close\r\n" // make the client (browser) close the connection after receipt
"Content-type: text/html\r\n"
"Content-length: 30\r\n\r\n"
"<html><body>Test</body></html>\0";
void ICACHE_FLASH_ATTR
onRecv(void* arg, char* data, unsigned short length) {
os_printf("Received %d bytes from client:\n--begin data--\n", length);
uart0_tx_buffer(data, length);
os_printf("\n--end data--\n");
// send reply
struct espconn* clientConn = (struct espconn*) arg;
espconn_secure_send(clientConn, reply, os_strlen(reply));
}
void ICACHE_FLASH_ATTR
onSent(void* arg) {
os_printf("Sent data to client.\n");
}
void ICACHE_FLASH_ATTR
onError(void* arg, int8_t err) {
os_printf("Connection error: %d\n", err);
}
void ICACHE_FLASH_ATTR
onDisconn(void* arg) {
os_printf("Client disconnected.\n");
}
void ICACHE_FLASH_ATTR
onClientConnected(void* arg) {
struct espconn* clientConn = (struct espconn*) arg;
os_printf("Client connected from %d.%d.%d.%d:%d.\n",
clientConn->proto.tcp->remote_ip[0],
clientConn->proto.tcp->remote_ip[1],
clientConn->proto.tcp->remote_ip[2],
clientConn->proto.tcp->remote_ip[3],
clientConn->proto.tcp->remote_port);
// register callback functions
espconn_regist_recvcb(clientConn, onRecv);
espconn_regist_sentcb(clientConn, onSent);
espconn_regist_reconcb(clientConn, onError);
espconn_regist_disconcb(clientConn, onDisconn);
}
void ICACHE_FLASH_ATTR
onWifiEvent(System_Event_t *event) {
if (event->event != EVENT_SOFTAPMODE_STACONNECTED)
return;
os_printf("WiFi is up, starting HTTPS server...\n");
// set listener configuration and register callback
listenConn.type = ESPCONN_TCP;
listenConn.state = ESPCONN_NONE;
listenConn.proto.tcp = &listenConnTcp;
listenConnTcp.local_port = 443;
espconn_regist_connectcb(&listenConn, onClientConnected);
// SSL configuration
espconn_secure_set_size(0x02, 8192);
espconn_secure_ca_disable(0x02);
// set server certificate (defined in user_config.h and cert.h)
espconn_secure_set_default_certificate(myesp_crt_DER, myesp_crt_DER_len);
espconn_secure_set_default_private_key(myesp_key_DER, myesp_key_DER_len);
// start listening
espconn_secure_accept(&listenConn);
os_printf("HTTPS server started.\n");
}
void ICACHE_FLASH_ATTR
onInitDone() {
// WiFi configuration
struct softap_config apConfig;
char ssid[33];
char password[33];
wifi_set_opmode(SOFTAP_MODE);
wifi_softap_get_config(&apConfig);
os_memset(apConfig.ssid, 0, sizeof(apConfig.ssid));
os_sprintf(ssid, "%s", "APESP");
os_memcpy(apConfig.ssid, ssid, os_strlen(ssid));
os_memset(apConfig.password, 0, sizeof(apConfig.password));
os_sprintf(password, "%s", "");
os_memcpy(apConfig.password, password, os_strlen(password));
apConfig.authmode = AUTH_OPEN;
apConfig.channel = 7;
apConfig.max_connection = 4;
apConfig.ssid_hidden = 0;
wifi_softap_set_config(&apConfig);
wifi_set_event_handler_cb(onWifiEvent);
// wifi_station_connect();
}
void ICACHE_FLASH_ATTR
user_rf_pre_init() {
}
void ICACHE_FLASH_ATTR
user_init() {
// initialise UART
uart_init(BIT_RATE_9600, BIT_RATE_9600); // 9600 baud
system_set_os_print(1);
// register post-initialisation callback
system_init_done_cb(onInitDone);
}
-
Attachments
-
- sslCode.zip
- (18.82 KiB) Downloaded 486 times