未验证 提交 23f653ad 编写于 作者: T Tomáš Pilný 提交者: GitHub

General examples updates (#7727)

* Remove inline code comments for the struct description

* Renamed ESPNow examples

* Changed example from C++ class to C struct

* Updated MultiHomedServers

* Updated BT Serial examples

* Added short overview of exaples - missing Enterprise

* Added draft of Enterprise header note

* Fixed note in README

* Fixed compilation error

* Changed FunctionalInterrupt example

* Fixes

* Returned FunctionalInterrupts to original state + added modified version as new example
上级 12bd5253
......@@ -18,26 +18,6 @@
uint32_t scanTime = 100; //In 10ms (1000ms)
BLEScan* pBLEScan;
/**
* @brief extend adv report parameters
*/
//typedef struct {
// esp_ble_gap_adv_type_t event_type; /*!< extend advertising type */
// uint8_t addr_type; /*!< extend advertising address type */
// esp_bd_addr_t addr; /*!< extend advertising address */
// esp_ble_gap_pri_phy_t primary_phy; /*!< extend advertising primary phy */
// esp_ble_gap_phy_t secondly_phy; /*!< extend advertising secondary phy */
// uint8_t sid; /*!< extend advertising sid */
// uint8_t tx_power; /*!< extend advertising tx power */
// int8_t rssi; /*!< extend advertising rssi */
// uint16_t per_adv_interval; /*!< periodic advertising interval */
// uint8_t dir_addr_type; /*!< direct address type */
// esp_bd_addr_t dir_addr; /*!< direct address */
// esp_ble_gap_ext_adv_data_status_t data_status; /*!< data type */
// uint8_t adv_data_len; /*!< extend advertising data length */
// uint8_t adv_data[251]; /*!< extend advertising data */
//} esp_ble_gap_ext_adv_reprot_t;
class MyBLEExtAdvertisingCallbacks: public BLEExtAdvertisingCallbacks {
void onResult(esp_ble_gap_ext_adv_reprot_t report) {
if(report.event_type & ESP_BLE_GAP_SET_EXT_ADV_PROP_LEGACY){
......
......@@ -25,26 +25,6 @@ static esp_ble_gap_periodic_adv_sync_params_t periodic_adv_sync_params = {
.sync_timeout = 1000, // timeout: 1000 * 10ms
};
/**
* @brief extend adv report parameters
*/
//typedef struct {
// esp_ble_gap_adv_type_t event_type; /*!< extend advertising type */
// uint8_t addr_type; /*!< extend advertising address type */
// esp_bd_addr_t addr; /*!< extend advertising address */
// esp_ble_gap_pri_phy_t primary_phy; /*!< extend advertising primary phy */
// esp_ble_gap_phy_t secondly_phy; /*!< extend advertising secondary phy */
// uint8_t sid; /*!< extend advertising sid */
// uint8_t tx_power; /*!< extend advertising tx power */
// int8_t rssi; /*!< extend advertising rssi */
// uint16_t per_adv_interval; /*!< periodic advertising interval */
// uint8_t dir_addr_type; /*!< direct address type */
// esp_bd_addr_t dir_addr; /*!< direct address */
// esp_ble_gap_ext_adv_data_status_t data_status; /*!< data type */
// uint8_t adv_data_len; /*!< extend advertising data length */
// uint8_t adv_data[251]; /*!< extend advertising data */
//} esp_ble_gap_ext_adv_reprot_t;
class MyBLEExtAdvertisingCallbacks : public BLEExtAdvertisingCallbacks
{
void onResult(esp_ble_gap_ext_adv_reprot_t params)
......
......@@ -6,6 +6,11 @@
#include "BluetoothSerial.h"
//#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
const char *pin = "1234"; // Change this to more secure PIN.
String device_name = "ESP32-BT-Slave";
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
......@@ -18,8 +23,13 @@ BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
SerialBT.begin(device_name); //Bluetooth device name
Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
//Serial.printf("The device with name \"%s\" and MAC address %s is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str(), SerialBT.getMacString()); // Use this after the MAC method is implemented
#ifdef USE_PIN
SerialBT.setPin(pin);
Serial.println("Using PIN");
#endif
}
void loop() {
......
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Victor Tchistiak - 2019
// This example code is in the Public Domain (or CC0 licensed, at your option.)
// By Victor Tchistiak - 2019
//
//This example demostrates master mode bluetooth connection and pin
//it creates a bridge between Serial and Classical Bluetooth (SPP)
//this is an extention of the SerialToSerialBT example by Evandro Copercini - 2018
// This example demonstrates master mode Bluetooth connection to a slave BT device using PIN (password)
// defined either by String "slaveName" by default "OBDII" or by MAC address
//
// This example creates a bridge between Serial and Classical Bluetooth (SPP)
// This is an extension of the SerialToSerialBT example by Evandro Copercini - 2018
//
// DO NOT try to connect to phone or laptop - they are master
// devices, same as the ESP using this code - it will NOT work!
//
// You can try to flash a second ESP32 with the example SerialToSerialBT - it should
// automatically pair with ESP32 running this code
#include "BluetoothSerial.h"
#define USE_NAME // Comment this to use MAC address instead of a slaveName
const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif
BluetoothSerial SerialBT;
String MACadd = "AA:BB:CC:11:22:33";
uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33};
//uint8_t address[6] = {0x00, 0x1D, 0xA5, 0x02, 0xC3, 0x22};
String name = "OBDII";
const char *pin = "1234"; //<- standard pin would be provided by default
bool connected;
#ifdef USE_NAME
String slaveName = "ESP32-BT-Slave"; // Change this to reflect the real name of your slave BT device
#else
String MACadd = "AA:BB:CC:11:22:33"; // This only for printing
uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; // Change this to reflect real MAC address of your slave BT device
#endif
String myName = "ESP32-BT-Master";
void setup() {
bool connected;
Serial.begin(115200);
//SerialBT.setPin(pin);
SerialBT.begin("ESP32test", true);
//SerialBT.setPin(pin);
Serial.println("The device started in master mode, make sure remote BT device is on!");
// connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs
// to resolve name to address first, but it allows to connect to different devices with the same name.
// Set CoreDebugLevel to Info to view devices bluetooth address and device names
connected = SerialBT.connect(name);
//connected = SerialBT.connect(address);
SerialBT.begin(myName, true);
Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
#ifndef USE_NAME
SerialBT.setPin(pin);
Serial.println("Using PIN");
#endif
// connect(address) is fast (up to 10 secs max), connect(slaveName) is slow (up to 30 secs max) as it needs
// to resolve slaveName to address first, but it allows to connect to different devices with the same name.
// Set CoreDebugLevel to Info to view devices Bluetooth address and device names
#ifdef USE_NAME
connected = SerialBT.connect(slaveName);
Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
#else
connected = SerialBT.connect(address);
Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd);
#endif
if(connected) {
Serial.println("Connected Succesfully!");
Serial.println("Connected Successfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
// disconnect() may take upto 10 secs max
// Disconnect() may take up to 10 secs max
if (SerialBT.disconnect()) {
Serial.println("Disconnected Succesfully!");
Serial.println("Disconnected Successfully!");
}
// this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
// This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
SerialBT.connect();
if(connected) {
Serial.println("Reconnected Successfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
}
}
}
void loop() {
......
......@@ -7,28 +7,28 @@
class Button
{
public:
Button(uint8_t reqPin) : PIN(reqPin){
pinMode(PIN, INPUT_PULLUP);
attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING);
};
~Button() {
detachInterrupt(PIN);
}
void ARDUINO_ISR_ATTR isr() {
numberKeyPresses += 1;
pressed = true;
}
void checkPressed() {
if (pressed) {
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
pressed = false;
}
}
Button(uint8_t reqPin) : PIN(reqPin){
pinMode(PIN, INPUT_PULLUP);
attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING);
};
~Button() {
detachInterrupt(PIN);
}
void ARDUINO_ISR_ATTR isr() {
numberKeyPresses += 1;
pressed = true;
}
void checkPressed() {
if (pressed) {
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
pressed = false;
}
}
private:
const uint8_t PIN;
const uint8_t PIN;
volatile uint32_t numberKeyPresses;
volatile bool pressed;
};
......@@ -42,6 +42,6 @@ void setup() {
}
void loop() {
button1.checkPressed();
button2.checkPressed();
button1.checkPressed();
button2.checkPressed();
}
#include <Arduino.h>
#define BUTTON1 16
#define BUTTON2 17
struct Button {
uint8_t PIN;
volatile uint32_t numberKeyPresses;
volatile int pressed;
};
void isr(void* param) {
struct Button *button = (struct Button*) param;
button->numberKeyPresses += 1;
button->pressed = 1;
}
void checkPressed(struct Button* button) {
if(button->pressed) {
Serial.printf("Button on pin %u has been pressed %u times\n", button->PIN, button->numberKeyPresses);
button->pressed = 0;
}
}
struct Button button1 = {BUTTON1, 0, 0};
struct Button button2 = {BUTTON2, 0, 0};
void setup() {
Serial.begin(115200);
pinMode(button1.PIN, INPUT_PULLUP);
pinMode(button2.PIN, INPUT_PULLUP);
attachInterruptArg(button1.PIN, isr, (void*)&button1, FALLING);
attachInterruptArg(button2.PIN, isr, (void*)&button2, FALLING);
}
void loop() {
checkPressed(&button1);
checkPressed(&button2);
}
......@@ -3,62 +3,17 @@
#include <WebServer.h>
#include <ESPmDNS.h>
/*
* MultiHomedServers
*
* MultiHomedServers tests support for multi-homed servers, i.e. a distinct web servers on each IP interface.
* It only tests the case n=2 because on a basic ESP32 device, we only have two IP interfaces, namely
* the WiFi station interfaces and the WiFi soft AP interface.
*
* For this to work, the WebServer and the WiFiServer classes must correctly handle the case where an
* IP address is passed to their relevant constructor. It also requires WebServer to work with multiple,
* simultaneous instances.
*
* Testing the WebServer and the WiFiServer constructors was the primary purpose of this script.
* The part of WebServer used by this sketch does seem to work with multiple, simultaneous instances.
* However there is much functionality in WebServer that is not tested here. It may all be well, but
* that is not proven here.
*
* This sketch starts the mDNS server, as did HelloServer, and it resolves esp32.local on both interfaces,
* but was not otherwise tested.
*
* This script also tests that a server not bound to a specific IP address still works.
*
* We create three, simultaneous web servers, one specific to each interface and one that listens on both:
*
* name IP Address Port
* ---- ---------- ----
* server0 INADDR_ANY 8080
* server1 station address 8081
* server2 soft AP address 8081
*
* The expected responses to a brower's requests are as follows:
*
* 1. when client connected to the same WLAN as the station:
* Request URL Response
* ----------- --------
* http://stationaddress:8080 "hello from server0"
* http://stationaddress:8081 "hello from server1"
*
* 2. when client is connected to the soft AP:
*
* Request URL Response
* ----------- --------
* http://softAPaddress:8080 "hello from server0"
* http://softAPaddress:8081 "hello from server2"
*
* 3. Repeat 1 and 2 above with esp32.local in place of stationaddress and softAPaddress, respectively.
*
* MultiHomedServers was originally based on HelloServer.
*/
const char* ssid = "........";
const char* password = "........";
const char* ssid = "WiFi_SSID";
const char* password = "WiFi_Password";
const char *apssid = "ESP32";
WebServer *server0, *server1, *server2;
#ifdef LED_BUILTIN
const int led = LED_BUILTIN;
#else
const int led = 13;
#endif
void handleRoot(WebServer *server, const char *content) {
digitalWrite(led, 1);
......@@ -67,15 +22,15 @@ void handleRoot(WebServer *server, const char *content) {
}
void handleRoot0() {
handleRoot(server0, "hello from server0");
handleRoot(server0, "Hello from server0 who listens on both WLAN and own Soft AP");
}
void handleRoot1() {
handleRoot(server1, "hello from server1");
handleRoot(server1, "Hello from server1 who listens only on WLAN");
}
void handleRoot2() {
handleRoot(server2, "hello from server2");
handleRoot(server2, "Hello from server2 who listens only on own Soft AP");
}
void handleNotFound(WebServer *server) {
......@@ -111,9 +66,12 @@ void setup(void) {
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
while(!Serial){ delay(100); }
Serial.println("Multi-homed Servers example starting");
delay(1000);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.print("Connecting ");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
......@@ -121,9 +79,9 @@ void setup(void) {
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.print("Connected to \"");
Serial.print(ssid);
Serial.print("\", IP address: \"");
Serial.println(WiFi.localIP());
if (!WiFi.softAP(apssid)) {
Serial.println("failed to start softAP");
......@@ -134,9 +92,9 @@ void setup(void) {
delay(100);
}
}
Serial.print("Soft AP: ");
Serial.print("Soft AP SSID: \"");
Serial.print(apssid);
Serial.print(" IP address: ");
Serial.print("\", IP address: ");
Serial.println(WiFi.softAPIP());
if (MDNS.begin("esp32")) {
......@@ -161,6 +119,10 @@ void setup(void) {
Serial.println("HTTP server1 started");
server2->begin();
Serial.println("HTTP server2 started");
Serial.printf("SSID: %s\n\thttp://", ssid); Serial.print(WiFi.localIP()); Serial.print(":8080\n\thttp://"); Serial.print(WiFi.localIP()); Serial.println(":8081");
Serial.printf("SSID: %s\n\thttp://", apssid); Serial.print(WiFi.softAPIP()); Serial.print(":8080\n\thttp://"); Serial.print(WiFi.softAPIP()); Serial.println(":8081");
Serial.printf("Any of the above SSIDs\n\thttp://esp32.local:8080\n\thttp://esp32.local:8081\n");
}
void loop(void) {
......
# Multi Homed Servers
This example tests support for multi-homed servers, i.e. a distinct web servers on distinct IP interface.
It only tests the case n=2 because on a basic ESP32 device, we only have two IP interfaces, namely the WiFi station interfaces and the WiFi soft AP interface.
For this to work, the WebServer and the WiFiServer classes must correctly handle the case where an IP address is passed to their relevant constructor.
It also requires WebServer to work with multiple, simultaneous instances.
Testing the WebServer and the WiFiServer constructors was the primary purpose of this script.
The part of WebServer used by this sketch does seem to work with multiple, simultaneous instances.
However there is much functionality in WebServer that is not tested here. It may all be well, but that is not proven here.
This sketch starts the mDNS server, as did HelloServer, and it resolves esp32.local on both interfaces, but was not otherwise tested.
This script also tests that a server not bound to a specific IP address still works.
We create three, simultaneous web servers, one specific to each interface and one that listens on both:
| name | IP Address | Port |
| ---- | ---------- | ---- |
| server0 | INADDR_ANY | 8080 |
| server1 | station address | 8081 |
| server2 | soft AP address | 8081 |
The expected responses to a browser's requests are as follows:
#### 1. The Client connected to the same WLAN as the station:
| Request URL | Response |
| ----------- | -------- |
| [http://stationaddress:8080](http://stationaddress:8080) | Hello from server0 who listens on both WLAN and own Soft AP |
| [http://stationaddress:8081](http://stationaddress:8081) | Hello from server1 who listens only on WLAN |
#### 2. The Client is connected to the soft AP:
| Request URL | Response |
| ----------- | -------- |
| [http://softAPaddress:8080](http://softAPaddress:8080) | Hello from server0 who listens on both WLAN and own Soft AP |
| [http://softAPaddress:8081](http://softAPaddress:8081) | Hello from server2 who listens only on own Soft AP |
#### 3. The Client is connect to either WLAN or SoftAP:
| Request URL | Response |
| ----------- | -------- |
| [http://esp32.local:8080](http://esp32.local:8080) | Hello from server0 who listens on both WLAN and own Soft AP |
| [http://esp32.local:8081](http://esp32.local:8081) | Hello from server1 who listens only on WLAN |
MultiHomedServers was originally based on HelloServer.
# Supported Targets
Currently, this example supports the following targets.
| Supported Targets | ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- |
## How to Use Example
Change the SSID and password in the example to your WiFi and flash the example.
Open a serial terminal and the example will write the exact addresses with used IP addresses you can use to test the servers.
* How to install the Arduino IDE: [Install Arduino IDE](https://github.com/espressif/arduino-esp32/tree/master/docs/arduino-ide).
#### Using Arduino IDE
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
* Before Compile/Verify, select the correct board: `Tools -> Board`.
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
#### Using Platform IO
* Select the COM port: `Devices` or set the `upload_port` option on the `platformio.ini` file.
## Example Log Output
```
Multi-homed Servers example starting
Connecting ...
Connected to "WiFi_SSID", IP address: "192.168.42.24
Soft AP SSID: "ESP32", IP address: 192.168.4.1
MDNS responder started
SSID: WiFi_SSID
http://192.168.42.24:8080
http://192.168.42.24:8081
SSID: ESP32
http://192.168.4.1:8080
http://192.168.4.1:8081
Any of the above SSIDs
http://esp32.local:8080
http://esp32.local:8081
HTTP server0 started
HTTP server1 started
HTTP server2 started
```
## Known issues
`http://esp32.local` Does not work on some Android phones
## Troubleshooting
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
## Contribute
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
If you have any **feedback** or **issue** to report on this example/library, please open an issue or fix it by creating a new PR. Contributions are more than welcome!
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
## Resources
* Official ESP32 Forum: [Link](https://esp32.com)
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
* ESP32 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf)
* ESP32-S2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf)
* ESP32-C3 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf)
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
......@@ -106,4 +106,28 @@ For example, this is used with AWS IoT Custom Authorizers where an MQTT client m
const char *aws_protos[] = {"mqtt", NULL};
...
wiFiClient.setAlpnProtocols(aws_protos);
```
\ No newline at end of file
```
Examples
--------
#### WiFiClientInsecure
Demonstrates usage of insecure connection using `WiFiClientSecure::setInsecure()`
#### WiFiClientPSK
Wifi secure connection example for ESP32 using a pre-shared key (PSK)
This is useful with MQTT servers instead of using a self-signed cert, tested with mosquitto.
Running on TLS 1.2 using mbedTLS
#### WiFiClientSecure
Wifi secure connection example for ESP32
Running on TLS 1.2 using mbedTLS
#### WiFiClientSecureEnterprise
This example demonstrates a secure connection to a WiFi network using WPA/WPA2 Enterprise (for example eduroam),
and establishing a secure HTTPS connection with an external server (for example arduino.php5.sk) using the defined anonymous identity, user identity, and password.
.. note::
This example is outdated and might not work. For more examples see [https://github.com/martinius96/ESP32-eduroam](https://github.com/martinius96/ESP32-eduroam)
#### WiFiClientShowPeerCredentials
Example of a establishing a secure connection and then showing the fingerprint of the certificate.
This can be useful in an IoT setting to know for sure that you are connecting to the right server.
Especially in situations where you cannot hardcode a trusted root certificate for long
periods of time (as they tend to get replaced more often than the lifecycle of IoT hardware).
\ No newline at end of file
......@@ -7,6 +7,12 @@
/*|Supported methods: PEAP + MsCHAPv2, EAP-TTLS + MsCHAPv2 |*/
/*|-----------------------------------------------------------|*/
// This example demonstrates a secure connection to a WiFi network using WPA/WPA2 Enterprise (for example eduroam),
// and establishing a secure HTTPS connection with an external server (for example arduino.php5.sk) using the defined anonymous identity, user identity, and password.
// Note: this example is outdated and may not work!
// For more examples see https://github.com/martinius96/ESP32-eduroam
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "esp_wpa2.h"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册