EasyManua.ls Logo

Espressif Systems ESP8266EX - How to Implement SSL

Espressif Systems ESP8266EX
41 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
!
3. Sample Codes
3.4.4. How to Implement SSL
1. Define the IP address and port that the SSL server will be connected to.
#define󰲧SSL_SERVER_IP󰲧󰲧󰲧󰲧“115.29.202.58”
#define󰲧SSL_SERVER_PORT󰲧󰲧󰲧443
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.
struct󰲧ip_info󰲧ipconfig;
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("create󰲧with󰲧the󰲧socket󰲧err\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,󰲧(struct󰲧sockaddr󰲧*)&client_addr,󰲧sizeof(client_addr))<󰲧0)
󰲧󰲧󰲧󰲧printf("connect󰲧with󰲧the󰲧host󰲧err\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.
uint32󰲧options󰲧=󰲧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:󰲧Client󰲧context󰲧is󰲧invalid\n");
}
Espressif
! /!31 37
2017.05

Other manuals for Espressif Systems ESP8266EX

Related product manuals