esp32-ricoh-gr2/main/main.c

191 lines
5.1 KiB
C
Raw Normal View History

2019-05-06 19:55:10 -04:00
/*
* Copyright 2019 by Morgan Allen
*
* This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International
* https://creativecommons.org/licenses/by-nc/4.0/
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "driver/spi_master.h"
#include "esp_system.h"
#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"
2019-05-06 19:55:10 -04:00
#define TAG "CACO"
#define BASE_URL "http://192.168.0.1/"
#define CMD_BEEP "cmd=audio resplay 0 1 3\n"
2019-05-06 19:55:10 -04:00
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);
}
}
2019-05-06 19:55:10 -04:00
void app_main() {
esp_err_t ret;
// Initialize NVS.
ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
ESP_LOGI(TAG, "Erasing flash memory");
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
uint8_t *mac;
mac = (uint8_t *)malloc(6);
esp_efuse_mac_get_default(mac);
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);
2019-05-06 19:55:10 -04:00
};