Prvni ulozeni z chegewara githubu
This commit is contained in:
116
libraries/WiFiProv/examples/WiFiProv/README.md
Normal file
116
libraries/WiFiProv/examples/WiFiProv/README.md
Normal file
@ -0,0 +1,116 @@
|
||||
# Provisioning for Arduino
|
||||
|
||||
This sketch implements provisioning using various IDF components
|
||||
|
||||
# Description
|
||||
|
||||
This example allows Arduino user to choose either BLE or SOFTAP as a mode of transport, over which the provisioning related communication is to take place, between the device (to be provisioned) and the client (owner of the device).
|
||||
|
||||
# APIs introduced for provisioning
|
||||
|
||||
## WiFi.onEvent()
|
||||
|
||||
Using this API user can register to receive WiFi Events and Provisioning Events
|
||||
|
||||
## WiFi.beginProvision()
|
||||
|
||||
WiFi.beginProvision(void ( * scheme_cb)(), wifi_prov_scheme_event_handler_t scheme_event_handler, wifi_prov_security_t security, char * pop, char * service_name, char * service_key, uint8_t * uuid);
|
||||
|
||||
#### Parameters passed
|
||||
|
||||
* function pointer : choose the mode of transfer
|
||||
* provSchemeBLE - Using BLE
|
||||
* provSchemeSoftAP - Using SoftAP
|
||||
|
||||
* security : choose security type
|
||||
* WIFI_PROV_SECURITY_1 - It allows secure communication which consists of secure handshake using key exchange and proof of possession (pop) and encryption/decryption of messages.
|
||||
|
||||
* WIFI_PROV_SECURITY_0 - It do not provide application level security, it involve simply plain text communication.
|
||||
|
||||
* scheme_event_handler : specify the handlers according to the mode chosen
|
||||
* BLE :
|
||||
- WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM - This scheme event handler is used when application doesn't need BT and BLE after provisioning is finised
|
||||
- WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BLE - This scheme event handler is used when application doesn't need BLE to be active after provisioning is finised
|
||||
- WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BT - This scheme event handler is used when application doesn't need BT to be active after provisioning is finised
|
||||
|
||||
* SoftAp :
|
||||
- WIFI_PROV_EVENT_HANDLER_NONE
|
||||
|
||||
* pop : It is the string that is used to provide the authentication.
|
||||
|
||||
* service_name : Specify service name for the device, if it is not specified then default chosen name is PROV_XXX where XXX are the last 3 bytes of the MAC address.
|
||||
|
||||
* service_key : Specify service key, if chosen mode of provisioning is BLE then service_key is always NULL
|
||||
|
||||
* uuid : user can specify there own 128 bit UUID while provisioning using BLE, if not specified then default value taken is
|
||||
- { 0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf,
|
||||
0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02, }
|
||||
|
||||
# NOTE
|
||||
|
||||
* If none of the parameters are specified in beginProvision then default provisioning takes place using SoftAP with
|
||||
* scheme = WIFI_PROV_SCHEME_SOFTAP
|
||||
* scheme_event_handler = WIFI_PROV_EVENT_HANDLER_NONE
|
||||
* security = WIFI_PROV_SECURITY_1
|
||||
* pop = "abcd1234"
|
||||
* service_name = "PROV_XXX"
|
||||
* service_key = NULL
|
||||
* uuid = NULL
|
||||
|
||||
# Log Output
|
||||
* Enable debuger : [ Tools -> Core Debug Level -> Info ]
|
||||
|
||||
# Provisioning Tools
|
||||
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/provisioning/wifi_provisioning.html#provisioning-tools
|
||||
|
||||
# Example output
|
||||
|
||||
## Provisioning using SoftAP
|
||||
|
||||
```
|
||||
[I][WiFiProv.cpp:117] beginProvision(): Starting AP using SOFTAP
|
||||
service_name : PROV_XXX
|
||||
password : 123456789
|
||||
pop : abcd1234
|
||||
|
||||
Provisioning started
|
||||
Give Credentials of your access point using " Android app "
|
||||
|
||||
Received Wi-Fi credentials
|
||||
SSID : GIONEE M2
|
||||
Password : 123456789
|
||||
|
||||
Connected IP address : 192.168.43.120
|
||||
Provisioning Successful
|
||||
Provisioning Ends
|
||||
|
||||
```
|
||||
|
||||
## Provisioning using BLE
|
||||
|
||||
```
|
||||
[I][WiFiProv.cpp:115] beginProvision(): Starting AP using BLE
|
||||
service_name : PROV_XXX
|
||||
pop : abcd1234
|
||||
|
||||
Provisioning started
|
||||
Give Credentials of your access point using " Android app "
|
||||
|
||||
Received Wi-Fi credentials
|
||||
SSID : GIONEE M2
|
||||
Password : 123456789
|
||||
|
||||
Connected IP address : 192.168.43.120
|
||||
Provisioning Successful
|
||||
Provisioning Ends
|
||||
|
||||
```
|
||||
|
||||
## Credentials are available on device
|
||||
|
||||
```
|
||||
[I][WiFiProv.cpp:146] beginProvision(): Aleardy Provisioned, starting Wi-Fi STA
|
||||
[I][WiFiProv.cpp:150] beginProvision(): SSID : Wce*****
|
||||
[I][WiFiProv.cpp:152] beginProvision(): CONNECTING TO THE ACCESS POINT :
|
||||
Connected IP address : 192.168.43.120
|
||||
```
|
57
libraries/WiFiProv/examples/WiFiProv/WiFiProv.ino
Normal file
57
libraries/WiFiProv/examples/WiFiProv/WiFiProv.ino
Normal file
@ -0,0 +1,57 @@
|
||||
#include "WiFiProv.h"
|
||||
#include "WiFi.h"
|
||||
void SysProvEvent(arduino_event_t *sys_event)
|
||||
{
|
||||
switch (sys_event->event_id) {
|
||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
||||
Serial.print("\nConnected IP address : ");
|
||||
Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr));
|
||||
break;
|
||||
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
|
||||
Serial.println("\nDisconnected. Connecting to the AP again... ");
|
||||
break;
|
||||
case ARDUINO_EVENT_PROV_START:
|
||||
Serial.println("\nProvisioning started\nGive Credentials of your access point using \" Android app \"");
|
||||
break;
|
||||
case ARDUINO_EVENT_PROV_CRED_RECV: {
|
||||
Serial.println("\nReceived Wi-Fi credentials");
|
||||
Serial.print("\tSSID : ");
|
||||
Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid);
|
||||
Serial.print("\tPassword : ");
|
||||
Serial.println((char const *) sys_event->event_info.prov_cred_recv.password);
|
||||
break;
|
||||
}
|
||||
case ARDUINO_EVENT_PROV_CRED_FAIL: {
|
||||
Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n");
|
||||
if(sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR)
|
||||
Serial.println("\nWi-Fi AP password incorrect");
|
||||
else
|
||||
Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()");
|
||||
break;
|
||||
}
|
||||
case ARDUINO_EVENT_PROV_CRED_SUCCESS:
|
||||
Serial.println("\nProvisioning Successful");
|
||||
break;
|
||||
case ARDUINO_EVENT_PROV_END:
|
||||
Serial.println("\nProvisioning Ends");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
//Sample uuid that user can pass during provisioning using BLE
|
||||
/* uint8_t uuid[16] = {0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf,
|
||||
0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02 };*/
|
||||
WiFi.onEvent(SysProvEvent);
|
||||
#if CONFIG_IDF_TARGET_ESP32 && CONFIG_BLUEDROID_ENABLED
|
||||
WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, "abcd1234", "Prov_123");
|
||||
#else
|
||||
WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, "abcd1234", "Prov_123");
|
||||
#endif
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
8
libraries/WiFiProv/library.properties
Normal file
8
libraries/WiFiProv/library.properties
Normal file
@ -0,0 +1,8 @@
|
||||
name=WiFiProv
|
||||
version=2.0.0
|
||||
author=Switi Mhaiske <sweetymhaiske@gmail.com>
|
||||
maintainer=Hristo Gochkov <hristo@espressif.com>
|
||||
sentence=Enables provisioning.
|
||||
paragraph=With this library you can perform provisioning on esp32 via SoftAP or BLE.
|
||||
url=
|
||||
architectures=esp32
|
161
libraries/WiFiProv/src/WiFiProv.cpp
Normal file
161
libraries/WiFiProv/src/WiFiProv.cpp
Normal file
@ -0,0 +1,161 @@
|
||||
/*
|
||||
WiFiProv.cpp - WiFiProv class for provisioning
|
||||
All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <esp_err.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_event.h>
|
||||
#include <esp32-hal.h>
|
||||
|
||||
#include <nvs_flash.h>
|
||||
#if CONFIG_BLUEDROID_ENABLED
|
||||
#include "wifi_provisioning/scheme_ble.h"
|
||||
#endif
|
||||
#include <wifi_provisioning/scheme_softap.h>
|
||||
#include <wifi_provisioning/manager.h>
|
||||
#undef IPADDR_NONE
|
||||
#include "WiFiProv.h"
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
#include "SimpleBLE.h"
|
||||
#endif
|
||||
|
||||
bool wifiLowLevelInit(bool persistent);
|
||||
|
||||
#if CONFIG_BLUEDROID_ENABLED
|
||||
static const uint8_t custom_service_uuid[16] = { 0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf,
|
||||
0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02, };
|
||||
#endif
|
||||
|
||||
#define SERV_NAME_PREFIX_PROV "PROV_"
|
||||
|
||||
static void get_device_service_name(prov_scheme_t prov_scheme, char *service_name, size_t max)
|
||||
{
|
||||
uint8_t eth_mac[6] = {0,0,0,0,0,0};
|
||||
if(esp_wifi_get_mac((wifi_interface_t)WIFI_IF_STA, eth_mac) != ESP_OK){
|
||||
log_e("esp_wifi_get_mac failed!");
|
||||
return;
|
||||
}
|
||||
#if CONFIG_IDF_TARGET_ESP32 && defined(CONFIG_BLUEDROID_ENABLED)
|
||||
if(prov_scheme == WIFI_PROV_SCHEME_BLE) {
|
||||
snprintf(service_name, max, "%s%02X%02X%02X",SERV_NAME_PREFIX_PROV, eth_mac[3], eth_mac[4], eth_mac[5]);
|
||||
} else {
|
||||
#endif
|
||||
snprintf(service_name, max, "%s%02X%02X%02X",SERV_NAME_PREFIX_PROV, eth_mac[3], eth_mac[4], eth_mac[5]);
|
||||
#if CONFIG_IDF_TARGET_ESP32 && defined(CONFIG_BLUEDROID_ENABLED)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void WiFiProvClass :: beginProvision(prov_scheme_t prov_scheme, scheme_handler_t scheme_handler, wifi_prov_security_t security, const char * pop, const char *service_name, const char *service_key, uint8_t * uuid)
|
||||
{
|
||||
bool provisioned = false;
|
||||
static char service_name_temp[32];
|
||||
|
||||
wifi_prov_mgr_config_t config;
|
||||
#if CONFIG_BLUEDROID_ENABLED
|
||||
if(prov_scheme == WIFI_PROV_SCHEME_BLE) {
|
||||
config.scheme = wifi_prov_scheme_ble;
|
||||
} else {
|
||||
#endif
|
||||
config.scheme = wifi_prov_scheme_softap;
|
||||
#if CONFIG_BLUEDROID_ENABLED
|
||||
}
|
||||
|
||||
if(scheme_handler == WIFI_PROV_SCHEME_HANDLER_NONE){
|
||||
#endif
|
||||
wifi_prov_event_handler_t scheme_event_handler = WIFI_PROV_EVENT_HANDLER_NONE;
|
||||
memcpy(&config.scheme_event_handler, &scheme_event_handler, sizeof(wifi_prov_event_handler_t));
|
||||
#if CONFIG_BLUEDROID_ENABLED
|
||||
} else if(scheme_handler == WIFI_PROV_SCHEME_HANDLER_FREE_BTDM){
|
||||
wifi_prov_event_handler_t scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM;
|
||||
memcpy(&config.scheme_event_handler, &scheme_event_handler, sizeof(wifi_prov_event_handler_t));
|
||||
} else if(scheme_handler == WIFI_PROV_SCHEME_HANDLER_FREE_BT){
|
||||
wifi_prov_event_handler_t scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BT;
|
||||
memcpy(&config.scheme_event_handler, &scheme_event_handler, sizeof(wifi_prov_event_handler_t));
|
||||
} else if(scheme_handler == WIFI_PROV_SCHEME_HANDLER_FREE_BLE){
|
||||
wifi_prov_event_handler_t scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BLE;
|
||||
memcpy(&config.scheme_event_handler, &scheme_event_handler, sizeof(wifi_prov_event_handler_t));
|
||||
} else {
|
||||
log_e("Unknown scheme handler!");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
config.app_event_handler.event_cb = NULL;
|
||||
config.app_event_handler.user_data = NULL;
|
||||
wifiLowLevelInit(true);
|
||||
if(wifi_prov_mgr_init(config) != ESP_OK){
|
||||
log_e("wifi_prov_mgr_init failed!");
|
||||
return;
|
||||
}
|
||||
if(wifi_prov_mgr_is_provisioned(&provisioned) != ESP_OK){
|
||||
log_e("wifi_prov_mgr_is_provisioned failed!");
|
||||
wifi_prov_mgr_deinit();
|
||||
return;
|
||||
}
|
||||
if(provisioned == false) {
|
||||
#if CONFIG_BLUEDROID_ENABLED
|
||||
if(prov_scheme == WIFI_PROV_SCHEME_BLE) {
|
||||
service_key = NULL;
|
||||
if(uuid == NULL) {
|
||||
uuid=(uint8_t *)custom_service_uuid;
|
||||
}
|
||||
wifi_prov_scheme_ble_set_service_uuid(uuid);
|
||||
}
|
||||
#endif
|
||||
|
||||
if(service_name == NULL) {
|
||||
get_device_service_name(prov_scheme, service_name_temp, 32);
|
||||
service_name = (const char *)service_name_temp;
|
||||
}
|
||||
|
||||
#if CONFIG_BLUEDROID_ENABLED
|
||||
if(prov_scheme == WIFI_PROV_SCHEME_BLE) {
|
||||
log_i("Starting AP using BLE. service_name : %s, pop : %s",service_name,pop);
|
||||
} else {
|
||||
#endif
|
||||
if(service_key == NULL) {
|
||||
log_i("Starting provisioning AP using SOFTAP. service_name : %s, pop : %s",service_name,pop);
|
||||
} else {
|
||||
log_i("Starting provisioning AP using SOFTAP. service_name : %s, password : %s, pop : %s",service_name,service_key,pop);
|
||||
}
|
||||
#if CONFIG_BLUEDROID_ENABLED
|
||||
}
|
||||
#endif
|
||||
if(wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key) != ESP_OK){
|
||||
log_e("wifi_prov_mgr_start_provisioning failed!");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
log_i("Already Provisioned");
|
||||
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO
|
||||
static wifi_config_t conf;
|
||||
esp_wifi_get_config((wifi_interface_t)WIFI_IF_STA,&conf);
|
||||
log_i("Attempting connect to AP: %s\n",conf.sta.ssid);
|
||||
#endif
|
||||
esp_wifi_start();
|
||||
wifi_prov_mgr_deinit();
|
||||
WiFi.begin();
|
||||
}
|
||||
}
|
||||
|
||||
WiFiProvClass WiFiProv;
|
55
libraries/WiFiProv/src/WiFiProv.h
Normal file
55
libraries/WiFiProv/src/WiFiProv.h
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
WiFiProv.h - Base class for provisioning support
|
||||
All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef WiFiProv_h
|
||||
#define WiFiProv_h
|
||||
|
||||
#include "WiFi.h"
|
||||
#include "wifi_provisioning/manager.h"
|
||||
//Select the scheme using which you want to provision
|
||||
typedef enum {
|
||||
WIFI_PROV_SCHEME_SOFTAP,
|
||||
#if CONFIG_BLUEDROID_ENABLED
|
||||
WIFI_PROV_SCHEME_BLE,
|
||||
#endif
|
||||
WIFI_PROV_SCHEME_MAX
|
||||
} prov_scheme_t;
|
||||
|
||||
typedef enum {
|
||||
WIFI_PROV_SCHEME_HANDLER_NONE,
|
||||
#if CONFIG_BLUEDROID_ENABLED
|
||||
WIFI_PROV_SCHEME_HANDLER_FREE_BTDM,
|
||||
WIFI_PROV_SCHEME_HANDLER_FREE_BLE,
|
||||
WIFI_PROV_SCHEME_HANDLER_FREE_BT,
|
||||
#endif
|
||||
WIFI_PROV_SCHEME_HANDLER_MAX
|
||||
} scheme_handler_t;
|
||||
|
||||
//Provisioning class
|
||||
class WiFiProvClass
|
||||
{
|
||||
public:
|
||||
|
||||
void beginProvision(prov_scheme_t prov_scheme = WIFI_PROV_SCHEME_SOFTAP, scheme_handler_t scheme_handler = WIFI_PROV_SCHEME_HANDLER_NONE,
|
||||
wifi_prov_security_t security = WIFI_PROV_SECURITY_1, const char * pop = "abcd1234", const char * service_name = NULL, const char * service_key = NULL, uint8_t *uuid = NULL);
|
||||
};
|
||||
|
||||
extern WiFiProvClass WiFiProv;
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user