Compare commits

..

No commits in common. "befbd369cf98c8eea8c58e5efc63d5606107d52e" and "e756e5405ff9127e3f9e3fb0ddba90bd561501b3" have entirely different histories.

3 changed files with 201 additions and 214 deletions

View file

@ -1,13 +0,0 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
add_compile_options(-fdiagnostics-color=always)
project(dustbutt)
target_add_binary_data(dustbutt.elf "main/certs/priv.pem" TEXT)
target_add_binary_data(dustbutt.elf "main/certs/pub.pem" TEXT)
target_add_binary_data(dustbutt.elf "main/certs/certs.pem" TEXT)

View file

@ -1,10 +0,0 @@
set(COMPONENT_SRCS "\
main.c\
")
set(COMPONENT_ADD_INCLUDEDIRS "\
.\
../include\
")
register_component()

View file

@ -28,23 +28,21 @@
#include "argtable3/argtable3.h" #include "argtable3/argtable3.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "driver/spi_master.h" #include "driver/spi_master.h"
#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#include "esp_ota_ops.h"
#include "esp_http_client.h"
#include "esp_https_ota.h"
#include "mqtt_client.h"
#include "esp32-wifi-manager.h" #include "esp32-wifi-manager.h"
#define TAG "BTN" #define TAG "CACO"
#define GPIO_INPUT_IO_0 33 #define GPIO_INPUT_IO_0 33
#define GPIO_INPUT_PIN_SEL ((1ULL<<GPIO_INPUT_IO_0)) #define GPIO_INPUT_PIN_SEL ((1ULL<<GPIO_INPUT_IO_0))
#define ESP_INTR_FLAG_DEFAULT 0 #define ESP_INTR_FLAG_DEFAULT 0
#define BASE_URL "http://192.168.0.1/"
#define CMD_BEEP "cmd=audio resplay 0 1 3\n"
#define CONFIG_SERVO_ENABLE (0)
#define CONFIG_SHOOT_DELAY_MS (30000)
#define CONFIG_TIMEOUT_SHOOT (100 / portTICK_PERIOD_MS)
#define CONFIG_SLEEP_TIMEOUT (60000 / portTICK_PERIOD_MS) #define CONFIG_SLEEP_TIMEOUT (60000 / portTICK_PERIOD_MS)
#define RTC_WAKE_IO RTC_GPIO8 #define RTC_WAKE_IO RTC_GPIO8
@ -56,12 +54,6 @@
#define LED_COUNT (1) #define LED_COUNT (1)
#define SPI_BUF (LED_COUNT + 2) #define SPI_BUF (LED_COUNT + 2)
#define TOPIC_POWER_STATUS "stat/tasmota_E74A79/POWER"
#define TOPIC_OTA "buttn32/ota"
extern const uint8_t certs_pem_start[] asm("_binary_certs_pem_start");
extern const uint8_t certs_pem_end[] asm("_binary_certs_pem_end");
static uint8_t id; static uint8_t id;
static EventGroupHandle_t wm_event_group; static EventGroupHandle_t wm_event_group;
@ -70,6 +62,12 @@ static xTimerHandle sleep_timer;
static spi_device_handle_t spi; static spi_device_handle_t spi;
xQueueHandle gpio_evt_queue = NULL; xQueueHandle gpio_evt_queue = NULL;
static struct {
struct arg_int *value; // how fast it will move in uS
struct arg_int *duration; // for how long it will move
struct arg_end *end;
} servo_args;
enum led_states { enum led_states {
POR, POR,
CONNECTING, CONNECTING,
@ -79,7 +77,39 @@ enum led_states {
enum led_states led_state; enum led_states led_state;
esp_mqtt_client_handle_t client; esp_err_t _http_event_handle(esp_http_client_event_t *evt) {
//ESP_LOGI(TAG, "Returning request for %s", (char*)evt->user_data);
switch(evt->event_id) {
case HTTP_EVENT_ERROR:
ESP_LOGI(TAG, "HTTP_EVENT_ERROR");
break;
case HTTP_EVENT_ON_CONNECTED:
ESP_LOGI(TAG, "HTTP_EVENT_ON_CONNECTED");
break;
case HTTP_EVENT_HEADER_SENT:
ESP_LOGI(TAG, "HTTP_EVENT_HEADER_SENT");
break;
case HTTP_EVENT_ON_HEADER:
ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER");
printf("%.*s", evt->data_len, (char*)evt->data);
break;
case HTTP_EVENT_ON_DATA:
ESP_LOGI(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
if (!esp_http_client_is_chunked_response(evt->client)) {
printf("%.*s", evt->data_len, (char*)evt->data);
}
break;
case HTTP_EVENT_ON_FINISH:
ESP_LOGI(TAG, "HTTP_EVENT_ON_FINISH");
break;
case HTTP_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
break;
}
return ESP_OK;
}
void enter_sleep() { void enter_sleep() {
ESP_LOGI(TAG, "Enter sleep"); ESP_LOGI(TAG, "Enter sleep");
@ -117,49 +147,72 @@ void reset_sleep_timer() {
xTimerStart(sleep_timer, portMAX_DELAY); xTimerStart(sleep_timer, portMAX_DELAY);
} }
uint8_t led_state_new[4]; void ping() {
uint8_t led_state_cur[4]; esp_http_client_config_t config = {
.url = "http://192.168.0.1/_gr",
.method = HTTP_METHOD_POST,
};
void led_display_loop() { esp_http_client_handle_t client = esp_http_client_init(&config);
static spi_transaction_t trans[SPI_BUF]; esp_http_client_set_post_field(client, CMD_BEEP, strlen(CMD_BEEP));
esp_err_t err = esp_http_client_perform(client);
int i; if (err == ESP_OK) {
for (i = 0; i < SPI_BUF; i++) { ESP_LOGI(TAG, "Status = %d, content_length = %d",
memset(&trans[i], 0, sizeof(spi_transaction_t)); esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
trans[i].length=8*4; }
trans[i].flags = SPI_TRANS_USE_TXDATA;
} }
trans[1].tx_data[0] = (0b11100000 | 1); void shoot() {
memset(&trans[SPI_BUF - 1].tx_data, 0xFF, 4); ESP_LOGI(TAG, "shooting");
esp_http_client_config_t config = {
.url = "http://192.168.0.1/v1/camera/shoot",
.method = HTTP_METHOD_POST,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_http_client_set_post_field(client, CMD_BEEP, strlen(CMD_BEEP));
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
}
}
void request(char *path) {
char *url = malloc(strlen(BASE_URL) + strlen(path) + 1);
strcpy(url, BASE_URL);
strcat(url, path);
ESP_LOGI(TAG, "Making request to %s", url);
esp_http_client_config_t config = {
.url = url,
.event_handler = _http_event_handle
};
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
}
}
void led_loop() {
uint8_t v = 0;
while(true) { while(true) {
/// TODO check _new vs _cur, ramping the channel value per step until they match if(xQueueReceive(wm_event_queue, &v, portMAX_DELAY)) {
/// only transmit spi data if a change was made EventBits_t ev_bits = xEventGroupGetBits(wm_event_group);
if(led_state_new[1] != led_state_cur[1]) {
trans[1].tx_data[1] = 0;
}
trans[1].tx_data[2] = 127;
trans[1].tx_data[3] = 0;
for(i = 0; i < SPI_BUF; i++) {
spi_device_queue_trans(spi, &trans[i], portMAX_DELAY);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void led_control_loop() {
while(true) {
EventBits_t ev_bits = xEventGroupWaitBits(wm_event_group, 0xFF, pdFALSE, pdFALSE, portMAX_DELAY);
ESP_LOGI(TAG, "got event: %d", ev_bits); ESP_LOGI(TAG, "got event: %d", ev_bits);
// TODO XXX rewrite to only update led_state_new and let led_display_loop
// handle rendering
static spi_transaction_t trans[SPI_BUF]; static spi_transaction_t trans[SPI_BUF];
int i; int i;
@ -173,8 +226,6 @@ void led_control_loop() {
trans[1].tx_data[0] = (0b11100000 | 1); trans[1].tx_data[0] = (0b11100000 | 1);
if(ev_bits & WIFI_CONNECTED) { if(ev_bits & WIFI_CONNECTED) {
esp_mqtt_client_start(client);
trans[1].tx_data[1] = 0; trans[1].tx_data[1] = 0;
trans[1].tx_data[2] = 127; trans[1].tx_data[2] = 127;
trans[1].tx_data[3] = 0; trans[1].tx_data[3] = 0;
@ -199,6 +250,67 @@ void led_control_loop() {
} }
} }
} }
}
void pwm_init() {
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, 21);
mcpwm_config_t pwm_config;
pwm_config.frequency = 50; //frequency = 50Hz, i.e. for every servo motor time period should be 20ms
pwm_config.cmpr_a = 0; //duty cycle of PWMxA = 0
pwm_config.cmpr_b = 0; //duty cycle of PWMxb = 0
pwm_config.counter_mode = MCPWM_UP_COUNTER;
pwm_config.duty_mode = MCPWM_DUTY_MODE_0;
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config); //Configure PWM0A & PWM0B with above settings
}
void servo() {
ESP_LOGI(TAG, "value: %d duration: %d", servo_args.value->ival[0], servo_args.duration->ival[0]);
while(true) {
mcpwm_set_duty_in_us(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, servo_args.value->ival[0]);
vTaskDelay(servo_args.duration->ival[0] / portTICK_PERIOD_MS);
mcpwm_set_duty_in_us(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, 1500);
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
}
bool running = false;
static int servo_start(int argc, char **argv) {
int nerrors = arg_parse(argc, argv, (void**) &servo_args);
if (nerrors != 0) {
arg_print_errors(stderr, servo_args.end, argv[0]);
return 1;
}
if(!running) {
running = true;
xTaskCreate(&servo, "servo", 4096, NULL, 6, NULL);
}
return ESP_OK;
}
void register_servo_cmd() {
servo_args.value = arg_int0("v", "value", "<int>", "move value in us");
servo_args.value->ival[0] = 1200;
servo_args.duration = arg_int0("d", "duration", "<int>", "duration in ms");
servo_args.end = arg_end(2);
const esp_console_cmd_t start_cmd = {
.command = "start",
.help = "Starts the servo",
.hint = NULL,
.func = &servo_start,
.argtable = &servo_args
};
ESP_ERROR_CHECK( esp_console_cmd_register(&start_cmd) );
}
static void IRAM_ATTR gpio_isr_handler(void* arg) static void IRAM_ATTR gpio_isr_handler(void* arg)
{ {
@ -206,111 +318,10 @@ static void IRAM_ATTR gpio_isr_handler(void* arg)
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL); xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
} }
esp_err_t _http_event_handler(esp_http_client_event_t *evt) {
switch (evt->event_id) {
case HTTP_EVENT_ERROR:
ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
break;
case HTTP_EVENT_ON_CONNECTED:
ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
break;
case HTTP_EVENT_HEADER_SENT:
ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
break;
case HTTP_EVENT_ON_HEADER:
ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
break;
case HTTP_EVENT_ON_DATA:
ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
break;
case HTTP_EVENT_ON_FINISH:
ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
break;
case HTTP_EVENT_DISCONNECTED:
ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
break;
}
return ESP_OK;
}
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
esp_mqtt_event_handle_t event = event_data;
esp_mqtt_client_handle_t client = event->client;
switch ((esp_mqtt_event_id_t)event_id) {
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
esp_mqtt_client_subscribe(client, TOPIC_OTA, 0);
esp_mqtt_client_subscribe(client, TOPIC_POWER_STATUS, 0);
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
break;
case MQTT_EVENT_SUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_PUBLISHED:
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_DATA:
ESP_LOGI(TAG, "MQTT_EVENT_DATA [topic: %.*s]", event->topic_len, event->topic);
if(strncmp(event->topic, (const char*)TOPIC_POWER_STATUS, event->data_len) == 0) {
ESP_LOGI(TAG, "data_len: %d", event->data_len);
if(strncmp(event->data, "ON", 2) == 0) {
xTimerStop(sleep_timer, portMAX_DELAY);
} else if(strncmp(event->data, "OFF", 3) == 0) {
reset_sleep_timer();
}
} else if(strncmp(event->topic, (const char*)TOPIC_OTA, event->data_len) == 0) {
esp_http_client_config_t config = {
.url = "https://ota.oit.cloud/esp32-button/latest.bin",
.cert_pem = (char *)certs_pem_start,
.event_handler = _http_event_handler,
.keep_alive_enable = true,
};
esp_err_t err = esp_https_ota(&config);
if (err == ESP_OK) {
esp_restart();
} else {
ESP_LOGE(TAG, "Firmware upgrade failed");
}
}
break;
case MQTT_EVENT_ERROR:
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno));
}
break;
default:
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
break;
}
}
void gpio_loop() { void gpio_loop() {
int8_t state = -1; int8_t state = -1;
uint32_t io_num; uint32_t io_num;
esp_mqtt_client_config_t mqtt_cfg = {
.uri = "mqtt://192.168.1.1",
};
client = esp_mqtt_client_init(&mqtt_cfg);
/* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
for(;;) { for(;;) {
if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) { if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
uint8_t level = gpio_get_level(io_num); uint8_t level = gpio_get_level(io_num);
@ -326,6 +337,8 @@ void gpio_loop() {
if(level == 0 && (ev_bits & WIFI_CONNECTED) == 0) { if(level == 0 && (ev_bits & WIFI_CONNECTED) == 0) {
ESP_LOGI(TAG, "No connectiong, connecting"); ESP_LOGI(TAG, "No connectiong, connecting");
wifi_manager_scan();
continue; continue;
} }
@ -334,7 +347,7 @@ void gpio_loop() {
reset_sleep_timer(); reset_sleep_timer();
if(level == 0) { if(level == 0) {
xTimerChangePeriod(button_timer, 100 / portTICK_PERIOD_MS, portMAX_DELAY); xTimerChangePeriod(button_timer, CONFIG_TIMEOUT_SHOOT, portMAX_DELAY);
xTimerStart(button_timer, portMAX_DELAY); xTimerStart(button_timer, portMAX_DELAY);
} else { } else {
xTimerStop(button_timer, portMAX_DELAY); xTimerStop(button_timer, portMAX_DELAY);
@ -344,7 +357,7 @@ void gpio_loop() {
} }
void handle_button_timer() { void handle_button_timer() {
esp_mqtt_client_publish(client, "cmnd/tasmota_E74A79/POWER", "TOGGLE", 0, 1, 0); shoot();
} }
void gpio_init() { void gpio_init() {
@ -435,7 +448,7 @@ void app_main() {
ESP_LOGI(TAG, "MAC: %X:%X:%X:%X:%X:%X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); ESP_LOGI(TAG, "MAC: %X:%X:%X:%X:%X:%X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
wm_event_group = wifi_manager_start("dustbutt"); wm_event_group = wifi_manager_start();
wifi_manager_reset_store(); wifi_manager_reset_store();
if(wifi_manager_ap_count() == 0) { if(wifi_manager_ap_count() == 0) {
@ -449,10 +462,7 @@ void app_main() {
gpio_init(); gpio_init();
spi_init(); spi_init();
xTaskCreate(&led_display_loop, "led_display_loop", 4096, NULL, 6, NULL); xTaskCreate(&led_loop, "led_loop", 4096, NULL, 6, NULL);
xTaskCreate(&led_control_loop, "led_control_loop", 4096, NULL, 6, NULL);
while(true) { wifi_manager_scan();
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}; };