Commit ba25b3a2 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Merge branch '10-make-miecrocontroller-hit-api' into 'master'

Resolve "make miecrocontroller hit API"

Closes #10

See merge request kretschmar/doorcode!22
parents 7da127ca 4a475b31
Pipeline #2828 passed with stages
in 1 minute and 55 seconds
......@@ -7,3 +7,6 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \
-subj "/C=US/ST=Washington/L=College Place/O=WWU/CN=$1" \
-keyout "${DIR}/certs/webserver.key" -out "${DIR}/certs/webserver.cert"
cp "${DIR}/certs/webserver.cert" "${DIR}/../src/embedded/main/doorcode_root_cert.pem"
ESP-IDF template app
====================
# Useful commands
- Building
- `idf build`
- To flash
- On my local machine run idfinit which runs `source /opt/esp-idf/export.sh`
- `idf -p /dev/ttyUSB0 flash`
- To Monitor:
- `idf -p /dev/ttyUSB0 monitor`
This is a template application to be used with [Espressif IoT Development Framework](https://github.com/espressif/esp-idf).
Please check [ESP-IDF docs](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for getting started instructions.
*Code in this repository is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.*
I also somehow got CLion to build and flash for me. No idea how though...
config.h
\ No newline at end of file
config.h
doorcode_root_cert.pem
\ No newline at end of file
# Edit following two lines to set component requirements (see docs)
set(COMPONENT_REQUIRES )
set(COMPONENT_PRIV_REQUIRES )
set(COMPONENT_SRCS "wifi.c"
"main.c")
set(COMPONENT_ADD_INCLUDEDIRS ".")
set(COMPONENT_SRCS "main.c config.h")
set(COMPONENT_ADD_INCLUDEDIRS "")
# Embed the server root certificate into the final binary
#
# (If this was a component, we would set COMPONENT_EMBED_TXTFILES here.)
set(COMPONENT_EMBED_TXTFILES doorcode_root_cert.pem)
register_component()
#
# Main component makefile.
#
# This Makefile can be left empty. By default, it will take the sources in the
# src/ directory, compile them and link them into lib(subdirectory_name).a
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#
......@@ -7,6 +7,8 @@
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include <esp_sntp.h>
#include <esp_http_client.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
......@@ -15,87 +17,88 @@
#include "esp_log.h"
#include "nvs_flash.h"
#include "config.h"
#include "wifi.h"
static const char* TAG = "HTTPS_MODULE";
#define EXAMPLE_ESP_WIFI_SSID DOOR_WIFI_SSID
#define EXAMPLE_ESP_WIFI_PASS DOOR_WIFI_PASSWORD
#define EXAMPLE_ESP_MAXIMUM_RETRY 3
extern const char doorcode_root_cert_pem_start[] asm("_binary_doorcode_root_cert_pem_start");
//extern const char doorcode_root_cert_pem_end[] asm("_binary_doorcode_root_cert_pem_end");
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
/* The event group allows multiple bits for each event, but we only care about one event
* - are we connected to the AP with an IP? */
const int WIFI_CONNECTED_BIT = BIT0;
static const char *TAG = "wifi station";
static int s_retry_num = 0;
static esp_err_t event_handler(void *ctx, system_event_t *event)
esp_err_t _http_event_handler(esp_http_client_event_t* evt)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "got ip:%s",
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
{
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
s_retry_num++;
ESP_LOGI(TAG,"retry to connect to the AP");
}
ESP_LOGI(TAG,"connect to the AP fail\n");
break;
}
default:
break;
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_LOGD(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);
ESP_LOGI(TAG, "%.*s", evt->data_len, (char*)evt->data);
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;
}
void wifi_init_sta()
static void http_task(void* pvParameters)
{
s_wifi_event_group = xEventGroupCreate();
(void)pvParameters;
door_wifi_wait_connected();
ESP_LOGI(TAG, "Connected to AP, begin connecting to DoorCode API");
esp_http_client_config_t config = {
.url = "https://192.168.2.1:8080/api",
.event_handler = _http_event_handler,
// .cert_pem = doorcode_root_cert_pem_start,
.skip_cert_common_name_check = true,
};
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
esp_http_client_handle_t client = esp_http_client_init(&config);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS
},
};
esp_err_t err = esp_http_client_perform(client);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
if (err == ESP_OK)
{
ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
}
else
{
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
}
esp_http_client_cleanup(client);
ESP_LOGI(TAG, "wifi_init_sta finished.");
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
ESP_LOGI(TAG, "Finished Request");
vTaskDelete(NULL);
}
void app_main()
{
//Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_init_sta();
door_wifi_initialize();
xTaskCreate(&http_task, "http_task", 8192, NULL, 5, NULL);
}
#include "wifi.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "freertos/event_groups.h"
#include "config.h"
static const char* TAG = "WIFI_MODULE";
/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group;
/* The event group allows multiple bits for each event,
but we only care about one event - are we connected
to the AP with an IP? */
const int CONNECTED_BIT = BIT0;
static esp_err_t event_handler(void* ctx, system_event_t* event)
{
(void)ctx;
switch (event->event_id)
{
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/* This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
ESP_LOGI(TAG, "Lost connection to Wireless AP, Reconnecting...\n");
break;
default:
break;
}
return ESP_OK;
}
void door_wifi_initialize(void)
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(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))
wifi_config_t wifi_config = {
.sta = {
.ssid = DOOR_WIFI_SSID,
.password = DOOR_WIFI_PASSWORD,
},
};
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA))
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config))
ESP_ERROR_CHECK(esp_wifi_start())
}
void door_wifi_wait_connected(void)
{
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY);
}
#ifndef DOOR_WIFI_H
#define DOOR_WIFI_H
void door_wifi_initialize(void);
void door_wifi_wait_connected(void);
#endif //DOOR_WIFI_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment