minimal function example, it shoot, it spin, it repeat

This commit is contained in:
Morgan 'ARR\!' Allen 2019-05-07 10:01:16 -07:00
parent e3acf612b7
commit f39139f910

View file

@ -17,11 +17,144 @@
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_http_client.h"
#include "driver/mcpwm.h"
#include "soc/mcpwm_reg.h"
#include "soc/mcpwm_struct.h"
#include "esp32-wifi-manager.h"
#define TAG "CACO"
#define BASE_URL "http://192.168.0.1/"
#define CMD_BEEP "cmd=audio resplay 0 1 3\n"
static uint8_t id;
EventGroupHandle_t wm_event_group;
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 ping() {
esp_http_client_config_t config = {
.url = "http://192.168.0.1/_gr",
.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 shoot() {
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 loop() {
xEventGroupWaitBits(wm_event_group, WIFI_CONNECTED, false, true, portMAX_DELAY);
ping();
while(true) {
shoot();
vTaskDelay(3000 / portTICK_PERIOD_MS);
mcpwm_set_duty_in_us(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, 1200);
vTaskDelay(100 / portTICK_PERIOD_MS);
mcpwm_set_duty_in_us(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, 1500);
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
}
void servo() {
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
while(true) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void app_main() {
esp_err_t ret;
@ -40,4 +173,18 @@ void app_main() {
id = 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();
wifi_manager_reset_store();
if(wifi_manager_ap_count() == 0) {
ESP_LOGI(TAG, "Adding new AP");
if(strlen(CONFIG_WIFI_MANAGER_TEST_AP) > 0) {
wifi_manager_add_ap(CONFIG_WIFI_MANAGER_TEST_AP, CONFIG_WIFI_MANAGER_TEST_PWD);
}
}
xTaskCreate(&loop, "loop", 2048, NULL, 6, NULL);
xTaskCreate(&servo, "servo", 2048, NULL, 6, NULL);
};