3.4.4. How to Implement SSL
1. Define the IP address and port that the SSL server will be connected to.
#defineSSL_SERVER_IP“115.29.202.58”
#defineSSL_SERVER_PORT443
esp_test*pTestParamer=(esp_test*)zalloc(sizeof(esp_test));
pTestParamer->ip.addr=ipaddr_addr(SSL_SERVER_IP);
pTestParamer->port=server_port;
2. Create a new task when the device functions as an SSL client.
xTaskCreate(esp_client,"esp_client",1024,(void*)pTestParamer,4,NULL);
3. When ESP8266 functions as a Station, connect it to a router. Then check if it has
already obtained the IP address before setting up the SSL connection.
structip_infoipconfig;
Wi-Fi_get_ip_info(STATION_IF,&ipconfig);
while(ipconfig.ip.addr==0){
vTaskDelay(1000/portTICK_RATE_MS);
Wi-Fi_get_ip_info(STATION_IF,&ipconfig);
}
4. Create a socket connection.
client_fd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(client_fd<0){
printf("createwiththesocketerr\n");
}
memset(&client_addr,0,sizeof(client_addr));
client_addr.sin_family=AF_INET;
client_addr.sin_port=htons(port);
client_addr.sin_addr.s_addr=sin_addr;
if(connect(client_fd,(structsockaddr*)&client_addr,sizeof(client_addr))<0)
printf("connectwiththehosterr\n");
5. Create the context of SSL. Please call system_get_free_heap_size to check the
memory space available, since the SSL requires a relatively large amount of space.
uint32options=SSL_SERVER_VERIFY_LATER|SSL_DISPLAY_CERTS|SSL_NO_DEFAULT_KEY;
if((ssl_ctx=ssl_ctx_new(options,SSL_DEFAULT_CLNT_SESS))==NULL){
printf("Error:Clientcontextisinvalid\n");
}