Compare commits
1 Commits
9e8e4a4e74
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| df5ce3dcaf |
+56
-149
@@ -21,8 +21,6 @@
|
||||
#include "esp_tls_crypto.h"
|
||||
#include <esp_http_server.h>
|
||||
#include "driver/ledc.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
//LED defines.
|
||||
#define LEDC_TIMER LEDC_TIMER_0
|
||||
@@ -35,131 +33,16 @@
|
||||
|
||||
#define EXAMPLE_HTTP_QUERY_KEY_MAX_LEN (64)
|
||||
|
||||
//Global led toggle
|
||||
bool ledOn = false;
|
||||
|
||||
|
||||
/* A simple example that demonstrates how to create GET and POST
|
||||
* handlers for the web server.
|
||||
*/
|
||||
|
||||
static const char *TAG = "example";
|
||||
|
||||
#if CONFIG_EXAMPLE_BASIC_AUTH
|
||||
|
||||
typedef struct {
|
||||
char *username;
|
||||
char *password;
|
||||
} basic_auth_info_t;
|
||||
|
||||
#define HTTPD_401 "401 UNAUTHORIZED" /*!< HTTP Response 401 */
|
||||
|
||||
static char *http_auth_basic(const char *username, const char *password)
|
||||
{
|
||||
int out;
|
||||
char *user_info = NULL;
|
||||
char *digest = NULL;
|
||||
size_t n = 0;
|
||||
asprintf(&user_info, "%s:%s", username, password);
|
||||
if (!user_info) {
|
||||
ESP_LOGE(TAG, "No enough memory for user information");
|
||||
return NULL;
|
||||
}
|
||||
esp_crypto_base64_encode(NULL, 0, &n, (const unsigned char *)user_info, strlen(user_info));
|
||||
|
||||
/* 6: The length of the "Basic " string
|
||||
* n: Number of bytes for a base64 encode format
|
||||
* 1: Number of bytes for a reserved which be used to fill zero
|
||||
*/
|
||||
digest = calloc(1, 6 + n + 1);
|
||||
if (digest) {
|
||||
strcpy(digest, "Basic ");
|
||||
esp_crypto_base64_encode((unsigned char *)digest + 6, n, (size_t *)&out, (const unsigned char *)user_info, strlen(user_info));
|
||||
}
|
||||
free(user_info);
|
||||
return digest;
|
||||
}
|
||||
|
||||
/* An HTTP GET handler */
|
||||
static esp_err_t basic_auth_get_handler(httpd_req_t *req)
|
||||
{
|
||||
char *buf = NULL;
|
||||
size_t buf_len = 0;
|
||||
basic_auth_info_t *basic_auth_info = req->user_ctx;
|
||||
|
||||
buf_len = httpd_req_get_hdr_value_len(req, "Authorization") + 1;
|
||||
if (buf_len > 1) {
|
||||
buf = calloc(1, buf_len);
|
||||
if (!buf) {
|
||||
ESP_LOGE(TAG, "No enough memory for basic authorization");
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
if (httpd_req_get_hdr_value_str(req, "Authorization", buf, buf_len) == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Found header => Authorization: %s", buf);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "No auth value received");
|
||||
}
|
||||
|
||||
char *auth_credentials = http_auth_basic(basic_auth_info->username, basic_auth_info->password);
|
||||
if (!auth_credentials) {
|
||||
ESP_LOGE(TAG, "No enough memory for basic authorization credentials");
|
||||
free(buf);
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
if (strncmp(auth_credentials, buf, buf_len)) {
|
||||
ESP_LOGE(TAG, "Not authenticated");
|
||||
httpd_resp_set_status(req, HTTPD_401);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_set_hdr(req, "Connection", "keep-alive");
|
||||
httpd_resp_set_hdr(req, "WWW-Authenticate", "Basic realm=\"Hello\"");
|
||||
httpd_resp_send(req, NULL, 0);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Authenticated!");
|
||||
char *basic_auth_resp = NULL;
|
||||
httpd_resp_set_status(req, HTTPD_200);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_set_hdr(req, "Connection", "keep-alive");
|
||||
asprintf(&basic_auth_resp, "{\"authenticated\": true,\"user\": \"%s\"}", basic_auth_info->username);
|
||||
if (!basic_auth_resp) {
|
||||
ESP_LOGE(TAG, "No enough memory for basic authorization response");
|
||||
free(auth_credentials);
|
||||
free(buf);
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
httpd_resp_send(req, basic_auth_resp, strlen(basic_auth_resp));
|
||||
free(basic_auth_resp);
|
||||
}
|
||||
free(auth_credentials);
|
||||
free(buf);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "No auth header received");
|
||||
httpd_resp_set_status(req, HTTPD_401);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_set_hdr(req, "Connection", "keep-alive");
|
||||
httpd_resp_set_hdr(req, "WWW-Authenticate", "Basic realm=\"Hello\"");
|
||||
httpd_resp_send(req, NULL, 0);
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static httpd_uri_t basic_auth = {
|
||||
.uri = "/basic_auth",
|
||||
.method = HTTP_GET,
|
||||
.handler = basic_auth_get_handler,
|
||||
};
|
||||
|
||||
static void httpd_register_basic_auth(httpd_handle_t server)
|
||||
{
|
||||
basic_auth_info_t *basic_auth_info = calloc(1, sizeof(basic_auth_info_t));
|
||||
if (basic_auth_info) {
|
||||
basic_auth_info->username = CONFIG_EXAMPLE_BASIC_AUTH_USERNAME;
|
||||
basic_auth_info->password = CONFIG_EXAMPLE_BASIC_AUTH_PASSWORD;
|
||||
|
||||
basic_auth.user_ctx = basic_auth_info;
|
||||
httpd_register_uri_handler(server, &basic_auth);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* An HTTP GET handler */
|
||||
static esp_err_t hello_get_handler(httpd_req_t *req)
|
||||
{
|
||||
@@ -366,14 +249,28 @@ static esp_err_t led1_handler(httpd_req_t *req){
|
||||
// Ben geen fan van het hard crashen als het aanpassen van de LED niet goed gaat, maar voor nu houd ik het er in.
|
||||
// Kan goede logging bevatten.
|
||||
|
||||
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY));
|
||||
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
|
||||
ledOn = !ledOn;
|
||||
|
||||
if(ledOn){
|
||||
ESP_LOGI(TAG, "setting LED to: ON");
|
||||
}else{
|
||||
ESP_LOGI(TAG, "setting LED to: OFF");
|
||||
}
|
||||
// ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, ledOn * LEDC_DUTY));
|
||||
// ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
|
||||
|
||||
|
||||
|
||||
//TODO: cycle colour between calls
|
||||
// Do this via a const addition.
|
||||
|
||||
ESP_LOGI(TAG, "LED mode set!");
|
||||
|
||||
if(gpio_get_level(5)){
|
||||
ESP_LOGI(TAG, "LED is ON");
|
||||
}else{
|
||||
ESP_LOGI(TAG, "LED is OFF");
|
||||
}
|
||||
ESP_ERROR_CHECK(gpio_set_level(5, ledOn));
|
||||
|
||||
//send empty body back
|
||||
httpd_resp_sendstr(req, "LED Mode set!");
|
||||
@@ -444,31 +341,32 @@ static void connect_handler(void* arg, esp_event_base_t event_base,
|
||||
}
|
||||
|
||||
|
||||
static void example_ledc_init(void)
|
||||
{
|
||||
// Prepare and then apply the LEDC PWM timer configuration
|
||||
ledc_timer_config_t ledc_timer = {
|
||||
.speed_mode = LEDC_MODE,
|
||||
.timer_num = LEDC_TIMER,
|
||||
.duty_resolution = LEDC_DUTY_RES,
|
||||
.freq_hz = LEDC_FREQUENCY, // Set output frequency at 5 kHz
|
||||
.clk_cfg = LEDC_AUTO_CLK
|
||||
};
|
||||
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
|
||||
// static void example_ledc_init(void)
|
||||
// {
|
||||
// // Prepare and then apply the LEDC PWM timer configuration
|
||||
// ledc_timer_config_t ledc_timer = {
|
||||
// .speed_mode = LEDC_MODE,
|
||||
// .timer_num = LEDC_TIMER,
|
||||
// .duty_resolution = LEDC_DUTY_RES,
|
||||
// .freq_hz = LEDC_FREQUENCY, // Set output frequency at 5 kHz
|
||||
// .clk_cfg = LEDC_AUTO_CLK
|
||||
// };
|
||||
// ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
|
||||
|
||||
// // Prepare and then apply the LEDC PWM channel configuration
|
||||
// ledc_channel_config_t ledc_channel = {
|
||||
// .speed_mode = LEDC_MODE,
|
||||
// .channel = LEDC_CHANNEL,
|
||||
// .timer_sel = LEDC_TIMER,
|
||||
// .intr_type = LEDC_INTR_DISABLE,
|
||||
// .gpio_num = LEDC_OUTPUT_IO,
|
||||
// .duty = LEDC_DUTY, // Set duty to 0%
|
||||
// .hpoint = 0
|
||||
// };
|
||||
// ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
|
||||
// ESP_LOGI(TAG, "LEDC init done");
|
||||
// }
|
||||
|
||||
// Prepare and then apply the LEDC PWM channel configuration
|
||||
ledc_channel_config_t ledc_channel = {
|
||||
.speed_mode = LEDC_MODE,
|
||||
.channel = LEDC_CHANNEL,
|
||||
.timer_sel = LEDC_TIMER,
|
||||
.intr_type = LEDC_INTR_DISABLE,
|
||||
.gpio_num = LEDC_OUTPUT_IO,
|
||||
.duty = LEDC_DUTY, // Set duty to 0%
|
||||
.hpoint = 0
|
||||
};
|
||||
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
|
||||
ESP_LOGI(TAG, "LEDC init done");
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
@@ -485,7 +383,16 @@ void app_main(void)
|
||||
ESP_ERROR_CHECK(example_connect());
|
||||
|
||||
//Initialise LED.
|
||||
example_ledc_init();
|
||||
//example_ledc_init();
|
||||
gpio_config_t gpioconf = {
|
||||
.pin_bit_mask = 16, //...0010000
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_ENABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE
|
||||
};
|
||||
ESP_ERROR_CHECK(gpio_config(&gpioconf));
|
||||
|
||||
|
||||
/* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
|
||||
* and re-start it upon connection.
|
||||
|
||||
Reference in New Issue
Block a user