提交 33858ab3 编写于 作者: W wm

添加smartconfig

上级 0c4f0284
# uNabto ESP32 cam demo
Using Nabto's SDK, the smartConfig network function has been added. Thanks to Nabto's technical support, I solved the problem of network delay.
This project turns the ESP32-CAM into a remote P2P accesible surveillance camera.
It uses the esp-idf FreeRTOS based core (vs. some other demos which uses the arduino core).
Also it uses the espressif esp32-cam library with slight modification (we could not get the reset to work).
......@@ -68,7 +70,7 @@ http://esp-idf.readthedocs.io/en/stable/get-started/index.html
```
git clone --recursive https://github.com/nabto/nabto-esp32cam.git
git clone --recursive https://github.com/AI-Thinker-wm/wan.git
```
## Step 3: Make menuconfig
......
idf_component_register(SRCS ./fp_acl_esp32_nvs.c ./main.c ./unabto_application.c
idf_component_register(SRCS ./fp_acl_esp32_nvs.c ./main.c ./unabto_application.c ./app_smart_wifi
INCLUDE_DIRS "." "../unabto/src" "../components/unabto-esp-idf" "../components/nabtotunnel" "../unabto/src/modules/log"
REQUIRES )
......@@ -39,5 +39,13 @@ menu "Camera configuration"
bool "M5Stack Camera A"
endchoice
menu "smart config"
config ESP_SMAERT_CONFIG
bool "smart config enabled"
default n
help
Enables smart config.
endmenu
endmenu
/* Esptouch example
This example code 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.
*/
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_wpa2.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "tcpip_adapter.h"
#include "esp_smartconfig.h"
#include <time.h>
#include <sys/time.h>
#include "esp_sntp.h"
#include "sdkconfig.h"
/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t s_wifi_event_group;
extern 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? */
static const int CONNECTED_BIT = BIT0;
static const int ESPTOUCH_DONE_BIT = BIT1;
static const char *TAG = "smartconfig_example";
static void smartconfig_example_task(void * parm);
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL);
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
//esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT);
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT);
//xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
} else if (event_base == SC_EVENT && event_id == SC_EVENT_SCAN_DONE) {
ESP_LOGI(TAG, "Scan done");
} else if (event_base == SC_EVENT && event_id == SC_EVENT_FOUND_CHANNEL) {
ESP_LOGI(TAG, "Found channel");
} else if (event_base == SC_EVENT && event_id == SC_EVENT_GOT_SSID_PSWD) {
ESP_LOGI(TAG, "Got SSID and password");
smartconfig_event_got_ssid_pswd_t *evt = (smartconfig_event_got_ssid_pswd_t *)event_data;
wifi_config_t wifi_config;
uint8_t ssid[33] = { 0 };
uint8_t password[65] = { 0 };
bzero(&wifi_config, sizeof(wifi_config_t));
memcpy(wifi_config.sta.ssid, evt->ssid, sizeof(wifi_config.sta.ssid));
memcpy(wifi_config.sta.password, evt->password, sizeof(wifi_config.sta.password));
wifi_config.sta.bssid_set = evt->bssid_set;
if (wifi_config.sta.bssid_set == true) {
memcpy(wifi_config.sta.bssid, evt->bssid, sizeof(wifi_config.sta.bssid));
}
memcpy(ssid, evt->ssid, sizeof(evt->ssid));
memcpy(password, evt->password, sizeof(evt->password));
ESP_LOGI(TAG, "SSID:%s", ssid);
ESP_LOGI(TAG, "PASSWORD:%s", password);
ESP_ERROR_CHECK( esp_wifi_disconnect() );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_connect() );
} else if (event_base == SC_EVENT && event_id == SC_EVENT_SEND_ACK_DONE) {
xEventGroupSetBits(s_wifi_event_group, ESPTOUCH_DONE_BIT);
}
}
void time_sync_notification_cb(struct timeval *tv)
{
ESP_LOGI(TAG, "SNTP OK ");
}
static void initialize_sntp(void)
{
ESP_LOGI(TAG, "Initializing SNTP");
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "pool.ntp.org");
sntp_set_time_sync_notification_cb(time_sync_notification_cb);
#ifdef CONFIG_SNTP_TIME_SYNC_METHOD_SMOOTH
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
#endif
sntp_init();
}
static void smartconfig_example_task(void * parm)
{
EventBits_t uxBits;
ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) );
smartconfig_start_config_t cfg = SMARTCONFIG_START_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_smartconfig_start(&cfg) );
while (1) {
uxBits = xEventGroupWaitBits(s_wifi_event_group, CONNECTED_BIT | ESPTOUCH_DONE_BIT, true, false, portMAX_DELAY);
if(uxBits & CONNECTED_BIT) {
ESP_LOGI(TAG, "WiFi Connected to ap");
}
if(uxBits & ESPTOUCH_DONE_BIT) {
ESP_LOGI(TAG, "smartconfig over");
#if CONFIG_ESP_SNTP_ENABLED
{
initialize_sntp();
// wait for time to be set
time_t now = 0;
struct tm timeinfo = { 0 };
int retry = 0;
const int retry_count = 10;
while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) {
//ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
time(&now);
localtime_r(&now, &timeinfo);
}
#endif
//LOG_INFO(("connected1!\n"));
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
esp_smartconfig_stop();
vTaskDelete(NULL);
}
}
}
void initialise_wifi(void)
{
tcpip_adapter_init();
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(SC_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2017 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS products only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef _APP_SMART_WIFI_H_
#define _APP_SMART_WIFI_H_
#ifdef __cplusplus
extern "C" {
#endif
void initialise_wifi(void);
#ifdef __cplusplus
}
#endif
#endif
......@@ -22,6 +22,9 @@
#include <unabto/unabto_app.h>
#include <unabto_tunnel_select.h>
#include "app_smart_wifi.h"
// Set in menuconfig .. or override here
#define WIFI_SSID CONFIG_SSID
#define WIFI_PASS CONFIG_SSID_PASSWORD
......@@ -30,7 +33,9 @@
// Event group
static EventGroupHandle_t wifi_event_group;
//static EventGroupHandle_t wifi_event_group;
EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;
#ifdef CONFIG_ESP_EYE
......@@ -100,7 +105,6 @@ const int CONNECTED_BIT = BIT0;
#define CAM_XCLK_FREQ 20000000
static camera_config_t camera_config = {
.pin_pwdn = CAM_PIN_PWDN,
.pin_reset = CAM_PIN_RESET,
......@@ -129,11 +133,11 @@ static camera_config_t camera_config = {
.frame_size = FRAMESIZE_VGA,//QQVGA-QXGA Do not use sizes above QVGA when not JPEG
.jpeg_quality = 10, //0-63 lower number means higher quality
.fb_count = 2 //if more than one, i2s runs in continuous mode. Use only with JPEG
.fb_count = 5 //if more than one, i2s runs in continuous mode. Use only with JPEG //{ 2
};
#ifndef CONFIG_ESP_SMAERT_CONFIG
/*
* WIFI event handler
......@@ -161,7 +165,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
return ESP_OK;
}
#endif
/*
* Convert hex to i (for converting the hex key to bytebuffer)
*/
......@@ -493,10 +497,11 @@ void app_main()
NABTO_LOG_ERROR(("!!!!!WARNING... HMAC_SHA256 test not completed"));
}
*/
// create the event group to handle wifi events
wifi_event_group = xEventGroupCreate();
#ifdef CONFIG_ESP_SMAERT_CONFIG
initialise_wifi();
#else
// initialize the tcp stack
tcpip_adapter_init();
......@@ -518,8 +523,9 @@ void app_main()
};
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
#endif
NABTO_LOG_INFO(("Connecting to %s\n", WIFI_SSID));
// start the main task
xTaskCreate(&main_task, "main_task", 8192, NULL, 5, NULL);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册