Kolban, Thanks for showing us how to get curl working. I'm working on a project that requires me to use https. I'm connecting to a server with a self signed cert. I use
to get passed any warnings. The code below works on my mac. I tried three different ways to make it work on the ESP32. If I use xTaskCreatePinnedToCore(&login_test, "login_test", 10000, NULL, 5, NULL, 0) I get a watch dog timer not feed error. If I just call login_test directly I get a stack overflow. If I call login_test from another place in my code, it runs but doesn't do anything. The stack overflow and watch dog timer errors occur after "Before Perform" is printed.
For testing, I disabled https on the server, and it worked if I commented out the curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
Code: Select all
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "curl/curl.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include "curl.h"
#define SSID "SSID"
#define PASSWORD "PASSWORD"
static char tag[] = "curl";
static void print_cookies(CURL *curl)
{
CURLcode res;
struct curl_slist *cookies;
struct curl_slist *nc;
int i;
printf("Cookies, curl knows:\n");
res = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
if(res != CURLE_OK) {
fprintf(stderr, "Curl curl_easy_getinfo failed: %s\n",
curl_easy_strerror(res));
exit(1);
}
nc = cookies, i = 1;
while(nc) {
printf("[%d]: %s\n", i, nc->data);
nc = nc->next;
i++;
}
if(i == 1) {
printf("(none)\n");
}
curl_slist_free_all(cookies);
}
void login_test() {
printf("Begining Login Test**\n");
CURLcode ret;
CURL *hnd;
hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_URL, "https://192.168.1.20/");
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.50.3");
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(hnd, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
printf("Before Perform\n");
ret = curl_easy_perform(hnd);
printf("After Perform\n");
print_cookies(hnd);
struct curl_httppost *post1;
struct curl_httppost *postend;
struct curl_slist *slist1;
post1 = NULL;
postend = NULL;
curl_formadd(&post1, &postend,
CURLFORM_COPYNAME, "uri",
CURLFORM_COPYCONTENTS, "/",
CURLFORM_END);
curl_formadd(&post1, &postend,
CURLFORM_COPYNAME, "username",
CURLFORM_COPYCONTENTS, "admin",
CURLFORM_END);
curl_formadd(&post1, &postend,
CURLFORM_COPYNAME, "password",
CURLFORM_COPYCONTENTS, "passpass",
CURLFORM_END);
curl_formadd(&post1, &postend,
CURLFORM_COPYNAME, "country",
CURLFORM_COPYCONTENTS, "56",
CURLFORM_END);
curl_formadd(&post1, &postend,
CURLFORM_COPYNAME, "ui_language",
CURLFORM_COPYCONTENTS, "en_US",
CURLFORM_END);
curl_formadd(&post1, &postend,
CURLFORM_COPYNAME, "lang_changed",
CURLFORM_COPYCONTENTS, "no",
CURLFORM_END);
slist1 = NULL;
slist1 = curl_slist_append(slist1, "Expect:");
curl_easy_setopt(hnd, CURLOPT_URL, "https://192.168.1.20/login.cgi");
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_HTTPPOST, post1);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.50.3");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
ret = curl_easy_perform(hnd);
curl_formfree(post1);
post1 = NULL;
curl_slist_free_all(slist1);
slist1 = NULL;
post1 = NULL;
postend = NULL;
curl_formadd(&post1, &postend,
CURLFORM_COPYNAME, "essid",
CURLFORM_COPYCONTENTS, "SSID 123",
CURLFORM_END);
curl_formadd(&post1, &postend,
CURLFORM_COPYNAME, "security",
CURLFORM_COPYCONTENTS, "wpa2aes",
CURLFORM_END);
curl_formadd(&post1, &postend,
CURLFORM_COPYNAME, "wpa_key",
CURLFORM_COPYCONTENTS, "password",
CURLFORM_END);
curl_formadd(&post1, &postend,
CURLFORM_COPYNAME, "change",
CURLFORM_COPYCONTENTS, "Change",
CURLFORM_END);
curl_formadd(&post1, &postend,
CURLFORM_COPYNAME, "action",
CURLFORM_COPYCONTENTS, "change",
CURLFORM_END);
slist1 = NULL;
slist1 = curl_slist_append(slist1, "Expect:");
curl_easy_setopt(hnd, CURLOPT_URL, "https://192.168.1.20/link.cgi");
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_HTTPPOST, post1);
curl_easy_setopt(hnd, CURLOPT_REFERER, "https://192.168.1.20/link.cgi");
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.50.3");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
ret = curl_easy_perform(hnd);
curl_easy_setopt(hnd, CURLOPT_URL, "https://192.168.1.20/signal.cgi");
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.50.3");
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
ret = curl_easy_perform(hnd);
curl_easy_cleanup(hnd);
hnd = NULL;
curl_formfree(post1);
post1 = NULL;
curl_slist_free_all(slist1);
slist1 = NULL;
printf("Completed All\n");
}
esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
{
if (event->event_id == SYSTEM_EVENT_STA_GOT_IP) {
ESP_LOGI(tag, "Got an IP ... ready to go!");
//Calling login_test() here causes a stack overflow
login_test();
//Calling xTaskCreatePinnedToCore causes the watch dog timer to be called
// xTaskCreatePinnedToCore(&login_test, "login_test", 10000, NULL, 5, NULL, 0);
return ESP_OK;
}
return ESP_OK;
}
int app_main(void)
{
nvs_flash_init();
system_init();
tcpip_adapter_init();
ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
wifi_config_t sta_config = {
.sta = {
.ssid = SSID,
.password = PASSWORD,
.bssid_set = 0
}
};
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK( esp_wifi_connect() );
ESP_LOGI(tag, "Registering test vfs");
return 0;
}