forked from espressif/arduino-esp32
Compare commits
183 Commits
Author | SHA1 | Date | |
---|---|---|---|
9be784f69b | |||
7cdfb8bc7c | |||
8134a42162 | |||
f13ff65691 | |||
e831680a41 | |||
d964873840 | |||
7e8993fc83 | |||
15bae92a72 | |||
6f23cd5988 | |||
ad4cf1461b | |||
5de03a3918 | |||
4b385690bc | |||
dd513df124 | |||
cee659563d | |||
55442a05a4 | |||
c9b3e512dd | |||
8d0e68db4f | |||
d2530850a3 | |||
7ecbb483da | |||
b0e896e9ae | |||
08f4665775 | |||
2452c1fb53 | |||
5f98370707 | |||
81b9130d8d | |||
434d02c49f | |||
15db297130 | |||
fe093a5e35 | |||
a0ef17a9dd | |||
be77bd4e27 | |||
ef99cd7fe7 | |||
b05bdf6904 | |||
442c63a4c6 | |||
d1a4b3b822 | |||
7d5bf9e385 | |||
aac26a4d1e | |||
804c221499 | |||
3236358ded | |||
82e71f9b50 | |||
2e12392721 | |||
6b0114366b | |||
18832bb418 | |||
97dcea2b99 | |||
dcff2e9774 | |||
6d256b6454 | |||
f6bf0f7aa2 | |||
a59eb5d51e | |||
adafd9d7c0 | |||
ac9fdeffe4 | |||
cee7b4237c | |||
954df2fc3e | |||
e41fb08b2a | |||
d8b1fc81c0 | |||
378b6ac032 | |||
cecef8e930 | |||
a8e99baeab | |||
b6cc108d49 | |||
8816bb5505 | |||
3274602eb0 | |||
534f0810a6 | |||
28a8073069 | |||
7494c4e76d | |||
486a4c66c4 | |||
c1951670d1 | |||
ad07d36932 | |||
c6a8da61f7 | |||
dd1a15478f | |||
bcb7012a32 | |||
3968821834 | |||
90f869e772 | |||
be4d3b6cb8 | |||
60606e5ad0 | |||
22b427df0f | |||
6e5be78838 | |||
e2452c0dfc | |||
56a7ae8712 | |||
e4b008e712 | |||
76afaf2d6b | |||
dccb4e8608 | |||
c2346c37da | |||
3491ca7845 | |||
0e341a6192 | |||
76cd2e2375 | |||
9f7ff009c6 | |||
704b71dabe | |||
7c0572172c | |||
f57c36782f | |||
3054bdf5a5 | |||
1014ba40af | |||
d6b91872cb | |||
d6b383f84b | |||
cadbad8850 | |||
3cbfa2ffef | |||
360e04fa36 | |||
57145ade6f | |||
f39024675c | |||
f7fb00632e | |||
7e40de226f | |||
ae240a3902 | |||
1287c52933 | |||
25bd585c25 | |||
d79a1f3d10 | |||
831f0ac29a | |||
ee3bb16c77 | |||
d8dca9c73b | |||
c3f3497048 | |||
18c3345451 | |||
b07f1c11fe | |||
2685a5dd7b | |||
675a40b257 | |||
3570d48eb9 | |||
f76ec4f50b | |||
fb6d5ad234 | |||
219ff3005b | |||
ccab428e4d | |||
d2d24a14e0 | |||
af11921535 | |||
82112384ca | |||
99aa866477 | |||
82670b96f8 | |||
9e7b13e46d | |||
2243081f85 | |||
1f4491f4ee | |||
aa529eb5a0 | |||
a9cb7c6d6f | |||
45d47e2be0 | |||
99c94bb482 | |||
f98fc7ee9f | |||
0957776855 | |||
d93245d0f5 | |||
c917ed2504 | |||
ee88c42c3b | |||
837cc3d271 | |||
9b2ae12fb7 | |||
86e221d087 | |||
663effa00e | |||
6f237a8415 | |||
c3c38a8eb5 | |||
8fcc914853 | |||
d03f8f1277 | |||
882b12c44e | |||
93d850f783 | |||
4f48caca2c | |||
80e9e42c3b | |||
494061af26 | |||
2fd3d042b2 | |||
b551310c37 | |||
f30edd040e | |||
b7c5e502e7 | |||
fa8a1c38d5 | |||
d56267bd8c | |||
1f6b0b35f8 | |||
342b9cf2d8 | |||
11d071b1c8 | |||
f48d9016fd | |||
a55265f74b | |||
c1a7198e7d | |||
4d4a1fde36 | |||
d219e56872 | |||
19ccc479c3 | |||
c18d50cb91 | |||
80418fadcf | |||
8b6d020352 | |||
5197916983 | |||
7e9d42da68 | |||
e34e0b45de | |||
ef2b54547e | |||
9856f0cc28 | |||
daa8c55667 | |||
af7ec4ead1 | |||
9e65ed1af1 | |||
7a92f89d12 | |||
5871ca9ce9 | |||
0dfa5babc2 | |||
e4b2ce4e81 | |||
7af4490fcc | |||
4204869ec9 | |||
ab23e8a656 | |||
5999b7ba46 | |||
7b613c1238 | |||
cee2359e33 | |||
37a7fb3d6a | |||
9d547a8a44 | |||
1fd5cd79c5 |
12
.github/scripts/install-arduino-ide.sh
vendored
12
.github/scripts/install-arduino-ide.sh
vendored
@ -48,16 +48,22 @@ else
|
||||
export ARDUINO_USR_PATH="$HOME/Arduino"
|
||||
fi
|
||||
|
||||
# Updated as of Nov 3rd 2020
|
||||
ARDUINO_IDE_URL="https://github.com/espressif/arduino-esp32/releases/download/1.0.4/arduino-nightly-"
|
||||
|
||||
# Currently not working
|
||||
#ARDUINO_IDE_URL="https://www.arduino.cc/download.php?f=/arduino-nightly-"
|
||||
|
||||
if [ ! -d "$ARDUINO_IDE_PATH" ]; then
|
||||
echo "Installing Arduino IDE on $OS_NAME ..."
|
||||
echo "Downloading 'arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT' to 'arduino.$ARCHIVE_FORMAT' ..."
|
||||
echo "Downloading '$ARDUINO_IDE_URL$OS_NAME.$ARCHIVE_FORMAT' to 'arduino.$ARCHIVE_FORMAT' ..."
|
||||
if [ "$OS_IS_LINUX" == "1" ]; then
|
||||
wget -O "arduino.$ARCHIVE_FORMAT" "https://www.arduino.cc/download.php?f=/arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT" > /dev/null 2>&1
|
||||
wget -O "arduino.$ARCHIVE_FORMAT" "$ARDUINO_IDE_URL$OS_NAME.$ARCHIVE_FORMAT" > /dev/null 2>&1
|
||||
echo "Extracting 'arduino.$ARCHIVE_FORMAT' ..."
|
||||
tar xf "arduino.$ARCHIVE_FORMAT" > /dev/null
|
||||
mv arduino-nightly "$ARDUINO_IDE_PATH"
|
||||
else
|
||||
curl -o "arduino.$ARCHIVE_FORMAT" -L "https://www.arduino.cc/download.php?f=/arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT" > /dev/null 2>&1
|
||||
curl -o "arduino.$ARCHIVE_FORMAT" -L "$ARDUINO_IDE_URL$OS_NAME.$ARCHIVE_FORMAT" > /dev/null 2>&1
|
||||
echo "Extracting 'arduino.$ARCHIVE_FORMAT' ..."
|
||||
unzip "arduino.$ARCHIVE_FORMAT" > /dev/null
|
||||
if [ "$OS_IS_MACOS" == "1" ]; then
|
||||
|
9
.github/scripts/install-platformio-esp32.sh
vendored
9
.github/scripts/install-platformio-esp32.sh
vendored
@ -12,19 +12,14 @@ echo "Installing Platform ESP32 ..."
|
||||
python -m platformio platform install https://github.com/platformio/platform-espressif32.git > /dev/null 2>&1
|
||||
|
||||
echo "Replacing the framework version ..."
|
||||
if [[ "$OSTYPE" == "darwin"* ]]; then
|
||||
sed 's/https:\/\/github\.com\/espressif\/arduino-esp32\.git/*/' "$HOME/.platformio/platforms/espressif32/platform.json" > "platform.json"
|
||||
mv -f "platform.json" "$HOME/.platformio/platforms/espressif32/platform.json"
|
||||
else
|
||||
sed -i 's/https:\/\/github\.com\/espressif\/arduino-esp32\.git/*/' "$HOME/.platformio/platforms/espressif32/platform.json"
|
||||
fi
|
||||
python -c "import json; import os; fp=open(os.path.expanduser('~/.platformio/platforms/espressif32/platform.json'), 'r+'); data=json.load(fp); data['packages']['framework-arduinoespressif32']['version'] = '*'; del data['packages']['framework-arduinoespressif32']['owner']; fp.seek(0); fp.truncate(); json.dump(data, fp); fp.close()"
|
||||
|
||||
if [ "$GITHUB_REPOSITORY" == "espressif/arduino-esp32" ]; then
|
||||
echo "Linking Core..."
|
||||
ln -s $GITHUB_WORKSPACE "$PLATFORMIO_ESP32_PATH"
|
||||
else
|
||||
echo "Cloning Core Repository ..."
|
||||
git clone https://github.com/espressif/arduino-esp32.git "$PLATFORMIO_ESP32_PATH" > /dev/null 2>&1
|
||||
git clone --recursive https://github.com/espressif/arduino-esp32.git "$PLATFORMIO_ESP32_PATH" > /dev/null 2>&1
|
||||
fi
|
||||
|
||||
echo "PlatformIO for ESP32 has been installed"
|
||||
|
27
.github/scripts/on-release.sh
vendored
27
.github/scripts/on-release.sh
vendored
@ -171,18 +171,19 @@ mkdir -p "$PKG_DIR/tools"
|
||||
|
||||
# Copy all core files to the package folder
|
||||
echo "Copying files for packaging ..."
|
||||
cp -f "$GITHUB_WORKSPACE/boards.txt" "$PKG_DIR/"
|
||||
cp -f "$GITHUB_WORKSPACE/programmers.txt" "$PKG_DIR/"
|
||||
cp -Rf "$GITHUB_WORKSPACE/cores" "$PKG_DIR/"
|
||||
cp -Rf "$GITHUB_WORKSPACE/libraries" "$PKG_DIR/"
|
||||
cp -Rf "$GITHUB_WORKSPACE/variants" "$PKG_DIR/"
|
||||
cp -f "$GITHUB_WORKSPACE/tools/espota.exe" "$PKG_DIR/tools/"
|
||||
cp -f "$GITHUB_WORKSPACE/tools/espota.py" "$PKG_DIR/tools/"
|
||||
cp -f "$GITHUB_WORKSPACE/tools/esptool.py" "$PKG_DIR/tools/"
|
||||
cp -f "$GITHUB_WORKSPACE/tools/gen_esp32part.py" "$PKG_DIR/tools/"
|
||||
cp -f "$GITHUB_WORKSPACE/tools/gen_esp32part.exe" "$PKG_DIR/tools/"
|
||||
cp -Rf "$GITHUB_WORKSPACE/tools/partitions" "$PKG_DIR/tools/"
|
||||
cp -Rf "$GITHUB_WORKSPACE/tools/sdk" "$PKG_DIR/tools/"
|
||||
cp -f "$GITHUB_WORKSPACE/boards.txt" "$PKG_DIR/"
|
||||
cp -f "$GITHUB_WORKSPACE/programmers.txt" "$PKG_DIR/"
|
||||
cp -Rf "$GITHUB_WORKSPACE/cores" "$PKG_DIR/"
|
||||
cp -Rf "$GITHUB_WORKSPACE/libraries" "$PKG_DIR/"
|
||||
cp -Rf "$GITHUB_WORKSPACE/variants" "$PKG_DIR/"
|
||||
cp -f "$GITHUB_WORKSPACE/tools/espota.exe" "$PKG_DIR/tools/"
|
||||
cp -f "$GITHUB_WORKSPACE/tools/espota.py" "$PKG_DIR/tools/"
|
||||
cp -f "$GITHUB_WORKSPACE/tools/esptool.py" "$PKG_DIR/tools/"
|
||||
cp -f "$GITHUB_WORKSPACE/tools/gen_esp32part.py" "$PKG_DIR/tools/"
|
||||
cp -f "$GITHUB_WORKSPACE/tools/gen_esp32part.exe" "$PKG_DIR/tools/"
|
||||
cp -Rf "$GITHUB_WORKSPACE/tools/partitions" "$PKG_DIR/tools/"
|
||||
cp -Rf "$GITHUB_WORKSPACE/tools/sdk" "$PKG_DIR/tools/"
|
||||
cp -f "$GITHUB_WORKSPACE/tools/platformio-build.py" "$PKG_DIR/tools/"
|
||||
|
||||
# Remove unnecessary files in the package folder
|
||||
echo "Cleaning up folders ..."
|
||||
@ -330,7 +331,7 @@ fi
|
||||
if [ ! -z "$COMMITS_SINCE_RELEASE" ] && [ "$COMMITS_SINCE_RELEASE" != "null" ]; then
|
||||
echo "Getting commits since $COMMITS_SINCE_RELEASE ..."
|
||||
commitFile=$OUTPUT_DIR/commits.txt
|
||||
git -C "$GITHUB_WORKSPACE" log --oneline $COMMITS_SINCE_RELEASE.. > "$OUTPUT_DIR/commits.txt"
|
||||
git -C "$GITHUB_WORKSPACE" log --oneline "$COMMITS_SINCE_RELEASE..HEAD" > "$OUTPUT_DIR/commits.txt"
|
||||
releaseNotes+=$'\r\n##### Commits\r\n'
|
||||
IFS=$'\n'
|
||||
for next in `cat $commitFile`
|
||||
|
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
@ -11,6 +11,8 @@ jobs:
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@master
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- uses: actions/setup-python@v1
|
||||
with:
|
||||
python-version: '3.x'
|
||||
|
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,9 +1,10 @@
|
||||
tools/xtensa-esp32-elf
|
||||
tools/xtensa-esp32s2-elf
|
||||
tools/dist
|
||||
tools/esptool
|
||||
tools/esptool.exe
|
||||
tools/mkspiffs/mkspiffs
|
||||
tools/mkspiffs/mkspiffs.exe
|
||||
tools/mkspiffs
|
||||
tools/mklittlefs
|
||||
.DS_Store
|
||||
|
||||
#Ignore files built by Visual Studio/Visual Micro
|
||||
|
@ -60,6 +60,7 @@ set(LIBRARY_SRCS
|
||||
libraries/SPI/src/SPI.cpp
|
||||
libraries/Ticker/src/Ticker.cpp
|
||||
libraries/Update/src/Updater.cpp
|
||||
libraries/Update/src/HttpsOTAUpdate.cpp
|
||||
libraries/WebServer/src/WebServer.cpp
|
||||
libraries/WebServer/src/Parsing.cpp
|
||||
libraries/WebServer/src/detail/mimetable.cpp
|
||||
@ -71,11 +72,11 @@ set(LIBRARY_SRCS
|
||||
libraries/WiFi/src/WiFi.cpp
|
||||
libraries/WiFi/src/WiFiGeneric.cpp
|
||||
libraries/WiFi/src/WiFiMulti.cpp
|
||||
libraries/WiFi/src/WiFiProv.cpp
|
||||
libraries/WiFi/src/WiFiScan.cpp
|
||||
libraries/WiFi/src/WiFiServer.cpp
|
||||
libraries/WiFi/src/WiFiSTA.cpp
|
||||
libraries/WiFi/src/WiFiUdp.cpp
|
||||
libraries/WiFiProv/src/WiFiProv.cpp
|
||||
libraries/Wire/src/Wire.cpp
|
||||
)
|
||||
|
||||
@ -201,13 +202,14 @@ set(COMPONENT_ADD_INCLUDEDIRS
|
||||
libraries/WebServer/src
|
||||
libraries/WiFiClientSecure/src
|
||||
libraries/WiFi/src
|
||||
libraries/WiFiProv/src
|
||||
libraries/Wire/src
|
||||
)
|
||||
|
||||
set(COMPONENT_PRIV_INCLUDEDIRS cores/esp32/libb64)
|
||||
|
||||
set(COMPONENT_REQUIRES spi_flash mbedtls mdns ethernet esp_adc_cal wifi_provisioning)
|
||||
set(COMPONENT_PRIV_REQUIRES fatfs nvs_flash app_update spiffs bootloader_support openssl bt)
|
||||
set(COMPONENT_PRIV_REQUIRES fatfs nvs_flash app_update spiffs bootloader_support openssl bt esp_http_client esp_https_ota)
|
||||
|
||||
register_component()
|
||||
|
||||
|
@ -40,6 +40,12 @@ config ARDUINO_RUNNING_CORE
|
||||
default 1 if ARDUINO_RUN_CORE1
|
||||
default -1 if ARDUINO_RUN_NO_AFFINITY
|
||||
|
||||
config ARDUINO_LOOP_STACK_SIZE
|
||||
int "Loop thread stack size"
|
||||
default 8192
|
||||
help
|
||||
Amount of stack available for the Arduino task.
|
||||
|
||||
choice ARDUINO_EVENT_RUNNING_CORE
|
||||
bool "Core on which Arduino's event handler is running"
|
||||
default ARDUINO_EVENT_RUN_CORE1
|
||||
@ -76,13 +82,18 @@ choice ARDUINO_UDP_RUNNING_CORE
|
||||
|
||||
endchoice
|
||||
|
||||
config ARDUINO_UDP_TASK_PRIORITY
|
||||
int "Priority of the UDP task"
|
||||
default 3
|
||||
help
|
||||
Select at what priority you want the UDP task to run.
|
||||
|
||||
config ARDUINO_UDP_RUNNING_CORE
|
||||
int
|
||||
default 0 if ARDUINO_UDP_RUN_CORE0
|
||||
default 1 if ARDUINO_UDP_RUN_CORE1
|
||||
default -1 if ARDUINO_UDP_RUN_NO_AFFINITY
|
||||
|
||||
|
||||
config DISABLE_HAL_LOCKS
|
||||
bool "Disable mutex locks for HAL"
|
||||
default "n"
|
||||
|
@ -11,9 +11,11 @@
|
||||
- [ESP32Dev Board PINMAP](#esp32dev-board-pinmap)
|
||||
|
||||
### Development Status
|
||||
[Latest stable release  ](https://github.com/espressif/arduino-esp32/releases/latest/) 
|
||||
|
||||
[Latest development release  ](https://github.com/espressif/arduino-esp32/releases/latest/) 
|
||||
Latest Stable Release [](https://github.com/espressif/arduino-esp32/releases/latest/) [](https://github.com/espressif/arduino-esp32/releases/latest/) [](https://github.com/espressif/arduino-esp32/releases/latest/)
|
||||
|
||||
Latest Development Release [](https://github.com/espressif/arduino-esp32/releases/latest/) [](https://github.com/espressif/arduino-esp32/releases/latest/) [](https://github.com/espressif/arduino-esp32/releases/latest/)
|
||||
|
||||
|
||||
### Installation Instructions
|
||||
- Using Arduino IDE Boards Manager (preferred)
|
||||
|
1379
boards.txt
1379
boards.txt
File diff suppressed because it is too large
Load Diff
@ -78,7 +78,7 @@
|
||||
#define interrupts() sei()
|
||||
#define noInterrupts() cli()
|
||||
|
||||
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
|
||||
#define clockCyclesPerMicrosecond() ( (long int)getCpuFrequencyMhz() )
|
||||
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
|
||||
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
|
||||
|
||||
@ -88,7 +88,7 @@
|
||||
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
|
||||
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
|
||||
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
|
||||
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
|
||||
#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit))
|
||||
|
||||
// avr-libc defines _NOP() since 1.6.2
|
||||
#ifndef _NOP
|
||||
|
@ -218,6 +218,33 @@ uint8_t EspClass::getChipRevision(void)
|
||||
return chip_info.revision;
|
||||
}
|
||||
|
||||
const char * EspClass::getChipModel(void)
|
||||
{
|
||||
uint32_t chip_ver = REG_GET_FIELD(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_VER_PKG);
|
||||
uint32_t pkg_ver = chip_ver & 0x7;
|
||||
switch (pkg_ver) {
|
||||
case EFUSE_RD_CHIP_VER_PKG_ESP32D0WDQ6 :
|
||||
return "ESP32-D0WDQ6";
|
||||
case EFUSE_RD_CHIP_VER_PKG_ESP32D0WDQ5 :
|
||||
return "ESP32-D0WDQ5";
|
||||
case EFUSE_RD_CHIP_VER_PKG_ESP32D2WDQ5 :
|
||||
return "ESP32-D2WDQ5";
|
||||
case EFUSE_RD_CHIP_VER_PKG_ESP32PICOD2 :
|
||||
return "ESP32-PICO-D2";
|
||||
case EFUSE_RD_CHIP_VER_PKG_ESP32PICOD4 :
|
||||
return "ESP32-PICO-D4";
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t EspClass::getChipCores(void)
|
||||
{
|
||||
esp_chip_info_t chip_info;
|
||||
esp_chip_info(&chip_info);
|
||||
return chip_info.cores;
|
||||
}
|
||||
|
||||
const char * EspClass::getSdkVersion(void)
|
||||
{
|
||||
return esp_get_idf_version();
|
||||
@ -309,6 +336,20 @@ bool EspClass::flashRead(uint32_t offset, uint32_t *data, size_t size)
|
||||
return spi_flash_read(offset, (uint32_t*) data, size) == ESP_OK;
|
||||
}
|
||||
|
||||
bool EspClass::partitionEraseRange(const esp_partition_t *partition, uint32_t offset, size_t size)
|
||||
{
|
||||
return esp_partition_erase_range(partition, offset, size) == ESP_OK;
|
||||
}
|
||||
|
||||
bool EspClass::partitionWrite(const esp_partition_t *partition, uint32_t offset, uint32_t *data, size_t size)
|
||||
{
|
||||
return esp_partition_write(partition, offset, data, size) == ESP_OK;
|
||||
}
|
||||
|
||||
bool EspClass::partitionRead(const esp_partition_t *partition, uint32_t offset, uint32_t *data, size_t size)
|
||||
{
|
||||
return esp_partition_read(partition, offset, data, size) == ESP_OK;
|
||||
}
|
||||
|
||||
uint64_t EspClass::getEfuseMac(void)
|
||||
{
|
||||
|
@ -21,6 +21,7 @@
|
||||
#define ESP_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <esp_partition.h>
|
||||
|
||||
/**
|
||||
* AVR macros for WDT managment
|
||||
@ -75,6 +76,8 @@ public:
|
||||
uint32_t getMaxAllocPsram();
|
||||
|
||||
uint8_t getChipRevision();
|
||||
const char * getChipModel();
|
||||
uint8_t getChipCores();
|
||||
uint32_t getCpuFreqMHz(){ return getCpuFrequencyMhz(); }
|
||||
inline uint32_t getCycleCount() __attribute__((always_inline));
|
||||
const char * getSdkVersion();
|
||||
@ -97,6 +100,10 @@ public:
|
||||
bool flashWrite(uint32_t offset, uint32_t *data, size_t size);
|
||||
bool flashRead(uint32_t offset, uint32_t *data, size_t size);
|
||||
|
||||
bool partitionEraseRange(const esp_partition_t *partition, uint32_t offset, size_t size);
|
||||
bool partitionWrite(const esp_partition_t *partition, uint32_t offset, uint32_t *data, size_t size);
|
||||
bool partitionRead(const esp_partition_t *partition, uint32_t offset, uint32_t *data, size_t size);
|
||||
|
||||
uint64_t getEfuseMac();
|
||||
|
||||
};
|
||||
|
@ -53,6 +53,8 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
|
||||
}
|
||||
|
||||
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert);
|
||||
_tx_pin = txPin;
|
||||
_rx_pin = rxPin;
|
||||
|
||||
if(!baud) {
|
||||
uartStartDetectBaudrate(_uart);
|
||||
@ -70,6 +72,8 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
|
||||
} else {
|
||||
log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible");
|
||||
_uart = NULL;
|
||||
_tx_pin = 255;
|
||||
_rx_pin = 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -84,7 +88,8 @@ void HardwareSerial::end()
|
||||
if(uartGetDebug() == _uart_nr) {
|
||||
uartSetDebug(0);
|
||||
}
|
||||
uartEnd(_uart);
|
||||
log_v("pins %d %d",_tx_pin, _rx_pin);
|
||||
uartEnd(_uart, _tx_pin, _rx_pin);
|
||||
_uart = 0;
|
||||
}
|
||||
|
||||
@ -179,3 +184,8 @@ HardwareSerial::operator bool() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void HardwareSerial::setRxInvert(bool invert)
|
||||
{
|
||||
uartSetRxInvert(_uart, invert);
|
||||
}
|
||||
|
@ -100,10 +100,14 @@ public:
|
||||
|
||||
size_t setRxBufferSize(size_t);
|
||||
void setDebugOutput(bool);
|
||||
|
||||
void setRxInvert(bool);
|
||||
|
||||
protected:
|
||||
int _uart_nr;
|
||||
uart_t* _uart;
|
||||
uint8_t _tx_pin;
|
||||
uint8_t _rx_pin;
|
||||
};
|
||||
|
||||
extern void serialEventRun(void) __attribute__((weak));
|
||||
|
@ -111,18 +111,12 @@ size_t Print::print(unsigned int n, int base)
|
||||
|
||||
size_t Print::print(long n, int base)
|
||||
{
|
||||
if(base == 0) {
|
||||
return write(n);
|
||||
} else if(base == 10) {
|
||||
if(n < 0) {
|
||||
int t = print('-');
|
||||
n = -n;
|
||||
return printNumber(n, 10) + t;
|
||||
}
|
||||
return printNumber(n, 10);
|
||||
} else {
|
||||
return printNumber(n, base);
|
||||
int t = 0;
|
||||
if (base == 10 && n < 0) {
|
||||
t = print('-');
|
||||
n = -n;
|
||||
}
|
||||
return printNumber(static_cast<unsigned long>(n), base) + t;
|
||||
}
|
||||
|
||||
size_t Print::print(unsigned long n, int base)
|
||||
@ -134,6 +128,25 @@ size_t Print::print(unsigned long n, int base)
|
||||
}
|
||||
}
|
||||
|
||||
size_t Print::print(long long n, int base)
|
||||
{
|
||||
int t = 0;
|
||||
if (base == 10 && n < 0) {
|
||||
t = print('-');
|
||||
n = -n;
|
||||
}
|
||||
return printNumber(static_cast<unsigned long long>(n), base) + t;
|
||||
}
|
||||
|
||||
size_t Print::print(unsigned long long n, int base)
|
||||
{
|
||||
if (base == 0) {
|
||||
return write(n);
|
||||
} else {
|
||||
return printNumber(n, base);
|
||||
}
|
||||
}
|
||||
|
||||
size_t Print::print(double n, int digits)
|
||||
{
|
||||
return printFloat(n, digits);
|
||||
@ -226,6 +239,20 @@ size_t Print::println(unsigned long num, int base)
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(long long num, int base)
|
||||
{
|
||||
size_t n = print(num, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(unsigned long long num, int base)
|
||||
{
|
||||
size_t n = print(num, base);
|
||||
n += println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t Print::println(double num, int digits)
|
||||
{
|
||||
size_t n = print(num, digits);
|
||||
@ -251,7 +278,7 @@ size_t Print::println(struct tm * timeinfo, const char * format)
|
||||
|
||||
size_t Print::printNumber(unsigned long n, uint8_t base)
|
||||
{
|
||||
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
|
||||
char buf[8 * sizeof(n) + 1]; // Assumes 8-bit chars plus zero byte.
|
||||
char *str = &buf[sizeof(buf) - 1];
|
||||
|
||||
*str = '\0';
|
||||
@ -262,11 +289,34 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
|
||||
}
|
||||
|
||||
do {
|
||||
unsigned long m = n;
|
||||
char c = n % base;
|
||||
n /= base;
|
||||
|
||||
*--str = c < 10 ? c + '0' : c + 'A' - 10;
|
||||
} while (n);
|
||||
|
||||
return write(str);
|
||||
}
|
||||
|
||||
size_t Print::printNumber(unsigned long long n, uint8_t base)
|
||||
{
|
||||
char buf[8 * sizeof(n) + 1]; // Assumes 8-bit chars plus zero byte.
|
||||
char* str = &buf[sizeof(buf) - 1];
|
||||
|
||||
*str = '\0';
|
||||
|
||||
// prevent crash if called with base == 1
|
||||
if (base < 2) {
|
||||
base = 10;
|
||||
}
|
||||
|
||||
do {
|
||||
auto m = n;
|
||||
n /= base;
|
||||
char c = m - base * n;
|
||||
|
||||
*--str = c < 10 ? c + '0' : c + 'A' - 10;
|
||||
} while(n);
|
||||
} while (n);
|
||||
|
||||
return write(str);
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ class Print
|
||||
private:
|
||||
int write_error;
|
||||
size_t printNumber(unsigned long, uint8_t);
|
||||
size_t printNumber(unsigned long long, uint8_t);
|
||||
size_t printFloat(double, uint8_t);
|
||||
protected:
|
||||
void setWriteError(int err = 1)
|
||||
@ -72,6 +73,11 @@ public:
|
||||
}
|
||||
|
||||
size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
|
||||
|
||||
// add availableForWrite to make compatible with Arduino Print.h
|
||||
// default to zero, meaning "a single write may block"
|
||||
// should be overriden by subclasses with buffering
|
||||
virtual int availableForWrite() { return 0; }
|
||||
size_t print(const __FlashStringHelper *);
|
||||
size_t print(const String &);
|
||||
size_t print(const char[]);
|
||||
@ -81,6 +87,8 @@ public:
|
||||
size_t print(unsigned int, int = DEC);
|
||||
size_t print(long, int = DEC);
|
||||
size_t print(unsigned long, int = DEC);
|
||||
size_t print(long long, int = DEC);
|
||||
size_t print(unsigned long long, int = DEC);
|
||||
size_t print(double, int = 2);
|
||||
size_t print(const Printable&);
|
||||
size_t print(struct tm * timeinfo, const char * format = NULL);
|
||||
@ -94,6 +102,8 @@ public:
|
||||
size_t println(unsigned int, int = DEC);
|
||||
size_t println(long, int = DEC);
|
||||
size_t println(unsigned long, int = DEC);
|
||||
size_t println(long long, int = DEC);
|
||||
size_t println(unsigned long long, int = DEC);
|
||||
size_t println(double, int = 2);
|
||||
size_t println(const Printable&);
|
||||
size_t println(struct tm * timeinfo, const char * format = NULL);
|
||||
|
@ -87,22 +87,22 @@ unsigned long Stream::getTimeout(void) {
|
||||
}
|
||||
|
||||
// find returns true if the target string is found
|
||||
bool Stream::find(const char *target)
|
||||
bool Stream::find(const char *target)
|
||||
{
|
||||
return findUntil(target, (char*) "");
|
||||
return findUntil(target, strlen(target), NULL, 0);
|
||||
}
|
||||
|
||||
// reads data from the stream until the target string of given length is found
|
||||
// returns true if target string is found, false if timed out
|
||||
bool Stream::find(const char *target, size_t length)
|
||||
{
|
||||
return findUntil(target, length, NULL, 0);
|
||||
return findUntil(target, length, NULL, 0);
|
||||
}
|
||||
|
||||
// as find but search ends if the terminator string is found
|
||||
bool Stream::findUntil(const char *target, const char *terminator)
|
||||
bool Stream::findUntil(const char *target, const char *terminator)
|
||||
{
|
||||
return findUntil(target, strlen(target), terminator, strlen(terminator));
|
||||
return findUntil(target, strlen(target), terminator, strlen(terminator));
|
||||
}
|
||||
|
||||
// reads data from the stream until the target string of the given length is found
|
||||
@ -110,35 +110,78 @@ bool Stream::findUntil(const char *target, const char *terminator)
|
||||
// returns true if target string is found, false if terminated or timed out
|
||||
bool Stream::findUntil(const char *target, size_t targetLen, const char *terminator, size_t termLen)
|
||||
{
|
||||
size_t index = 0; // maximum target string length is 64k bytes!
|
||||
size_t termIndex = 0;
|
||||
int c;
|
||||
if (terminator == NULL) {
|
||||
MultiTarget t[1] = {{target, targetLen, 0}};
|
||||
return findMulti(t, 1) == 0 ? true : false;
|
||||
} else {
|
||||
MultiTarget t[2] = {{target, targetLen, 0}, {terminator, termLen, 0}};
|
||||
return findMulti(t, 2) == 0 ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
if(*target == 0) {
|
||||
return true; // return true if target is a null string
|
||||
int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
|
||||
// any zero length target string automatically matches and would make
|
||||
// a mess of the rest of the algorithm.
|
||||
for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
|
||||
if (t->len <= 0)
|
||||
return t - targets;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
int c = timedRead();
|
||||
if (c < 0)
|
||||
return -1;
|
||||
|
||||
for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
|
||||
// the simple case is if we match, deal with that first.
|
||||
if (c == t->str[t->index]) {
|
||||
if (++t->index == t->len)
|
||||
return t - targets;
|
||||
else
|
||||
continue;
|
||||
}
|
||||
|
||||
// if not we need to walk back and see if we could have matched further
|
||||
// down the stream (ie '1112' doesn't match the first position in '11112'
|
||||
// but it will match the second position so we can't just reset the current
|
||||
// index to 0 when we find a mismatch.
|
||||
if (t->index == 0)
|
||||
continue;
|
||||
|
||||
int origIndex = t->index;
|
||||
do {
|
||||
--t->index;
|
||||
// first check if current char works against the new current index
|
||||
if (c != t->str[t->index])
|
||||
continue;
|
||||
|
||||
// if it's the only char then we're good, nothing more to check
|
||||
if (t->index == 0) {
|
||||
t->index++;
|
||||
break;
|
||||
}
|
||||
|
||||
// otherwise we need to check the rest of the found string
|
||||
int diff = origIndex - t->index;
|
||||
size_t i;
|
||||
for (i = 0; i < t->index; ++i) {
|
||||
if (t->str[i] != t->str[i + diff])
|
||||
break;
|
||||
}
|
||||
|
||||
// if we successfully got through the previous loop then our current
|
||||
// index is good.
|
||||
if (i == t->index) {
|
||||
t->index++;
|
||||
break;
|
||||
}
|
||||
|
||||
// otherwise we just try the next index
|
||||
} while (t->index);
|
||||
}
|
||||
while((c = timedRead()) > 0) {
|
||||
|
||||
if(c != target[index]) {
|
||||
index = 0; // reset index if any char does not match
|
||||
}
|
||||
|
||||
if(c == target[index]) {
|
||||
//////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
|
||||
if(++index >= targetLen) { // return true if all chars in the target match
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if(termLen > 0 && c == terminator[termIndex]) {
|
||||
if(++termIndex >= termLen) {
|
||||
return false; // return false if terminate string found before target string
|
||||
}
|
||||
} else {
|
||||
termIndex = 0;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// unreachable
|
||||
return -1;
|
||||
}
|
||||
|
||||
// returns the first valid (long) integer value from the current position.
|
||||
|
@ -60,6 +60,7 @@ public:
|
||||
|
||||
void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
|
||||
unsigned long getTimeout(void);
|
||||
|
||||
bool find(const char *target); // reads data from the stream until the target string is found
|
||||
bool find(uint8_t *target)
|
||||
{
|
||||
@ -123,6 +124,17 @@ protected:
|
||||
// this allows format characters (typically commas) in values to be ignored
|
||||
|
||||
float parseFloat(char skipChar); // as above but the given skipChar is ignored
|
||||
|
||||
struct MultiTarget {
|
||||
const char *str; // string you're searching for
|
||||
size_t len; // length of string you're searching for
|
||||
size_t index; // index used by the search routine.
|
||||
};
|
||||
|
||||
// This allows you to search for an arbitrary number of strings.
|
||||
// Returns index of the target that is found first or -1 if timeout occurs.
|
||||
int findMulti(struct MultiTarget *targets, int tCount);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -43,6 +43,7 @@ class UDP: public Stream
|
||||
|
||||
public:
|
||||
virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
|
||||
virtual uint8_t beginMulticast(IPAddress, uint16_t) { return 0; } // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure
|
||||
virtual void stop() =0; // Finish with the UDP socket
|
||||
|
||||
// Sending UDP packets
|
||||
|
@ -44,7 +44,7 @@ long random(long howbig)
|
||||
uint32_t t = -howbig;
|
||||
if (t >= howbig) {
|
||||
t -= howbig;
|
||||
if (t >= howbig)
|
||||
if (t >= howbig)
|
||||
t %= howbig;
|
||||
}
|
||||
while (l < t) {
|
||||
@ -66,11 +66,11 @@ long random(long howsmall, long howbig)
|
||||
}
|
||||
|
||||
long map(long x, long in_min, long in_max, long out_min, long out_max) {
|
||||
long divisor = (in_max - in_min);
|
||||
if(divisor == 0){
|
||||
return -1; //AVR returns -1, SAM returns 0
|
||||
}
|
||||
return (x - in_min) * (out_max - out_min) / divisor + out_min;
|
||||
const long dividend = out_max - out_min;
|
||||
const long divisor = in_max - in_min;
|
||||
const long delta = x - in_min;
|
||||
|
||||
return (delta * dividend + (divisor / 2)) / divisor + out_min;
|
||||
}
|
||||
|
||||
unsigned int makeWord(unsigned int w)
|
||||
|
@ -945,6 +945,14 @@ static void IRAM_ATTR i2c_isr_handler_default(void* arg)
|
||||
activeInt &=~I2C_RXFIFO_FULL_INT_ST;
|
||||
}
|
||||
|
||||
if(activeInt & I2C_RXFIFO_OVF_INT_ST) {
|
||||
emptyRxFifo(p_i2c);
|
||||
p_i2c->dev->int_clr.rx_fifo_full=1;
|
||||
p_i2c->dev->int_ena.rx_fifo_full=1; //why?
|
||||
|
||||
activeInt &=~I2C_RXFIFO_OVF_INT_ST;
|
||||
}
|
||||
|
||||
if (activeInt & I2C_ACK_ERR_INT_ST_M) {//fatal error, abort i2c service
|
||||
if (p_i2c->mode == I2C_MASTER) {
|
||||
i2c_update_error_byte_cnt(p_i2c); // calc which byte caused ack Error, check if address or data
|
||||
|
@ -94,6 +94,7 @@ struct rmt_obj_s
|
||||
transaction_state_t tx_state;
|
||||
rmt_rx_data_cb_t cb;
|
||||
bool data_alloc;
|
||||
void * arg;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -104,14 +105,14 @@ static xSemaphoreHandle g_rmt_objlocks[MAX_CHANNELS] = {
|
||||
};
|
||||
|
||||
static rmt_obj_t g_rmt_objects[MAX_CHANNELS] = {
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false, NULL},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false, NULL},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false, NULL},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false, NULL},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false, NULL},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false, NULL},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false, NULL},
|
||||
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false, NULL},
|
||||
};
|
||||
|
||||
/**
|
||||
@ -240,7 +241,6 @@ bool rmtLoop(rmt_obj_t* rmt, rmt_data_t* data, size_t size)
|
||||
return false;
|
||||
}
|
||||
|
||||
int channel = rmt->channel;
|
||||
int allocated_size = MAX_DATA_PER_CHANNEL * rmt->buffers;
|
||||
|
||||
if (size > allocated_size) {
|
||||
@ -324,6 +324,7 @@ bool rmtReadData(rmt_obj_t* rmt, uint32_t* data, size_t size)
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool rmtBeginReceive(rmt_obj_t* rmt)
|
||||
{
|
||||
if (!rmt) {
|
||||
@ -357,7 +358,7 @@ bool rmtReceiveCompleted(rmt_obj_t* rmt)
|
||||
}
|
||||
}
|
||||
|
||||
bool rmtRead(rmt_obj_t* rmt, rmt_rx_data_cb_t cb)
|
||||
bool rmtRead(rmt_obj_t* rmt, rmt_rx_data_cb_t cb, void * arg)
|
||||
{
|
||||
if (!rmt && !cb) {
|
||||
return false;
|
||||
@ -365,6 +366,7 @@ bool rmtRead(rmt_obj_t* rmt, rmt_rx_data_cb_t cb)
|
||||
int channel = rmt->channel;
|
||||
|
||||
RMT_MUTEX_LOCK(channel);
|
||||
rmt->arg = arg;
|
||||
rmt->intr_mode = E_RX_INTR;
|
||||
rmt->tx_state = E_FIRST_HALF;
|
||||
rmt->cb = cb;
|
||||
@ -391,6 +393,19 @@ bool rmtRead(rmt_obj_t* rmt, rmt_rx_data_cb_t cb)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rmtEnd(rmt_obj_t* rmt) {
|
||||
if (!rmt) {
|
||||
return false;
|
||||
}
|
||||
int channel = rmt->channel;
|
||||
|
||||
RMT_MUTEX_LOCK(channel);
|
||||
RMT.conf_ch[channel].conf1.rx_en = 1;
|
||||
RMT_MUTEX_UNLOCK(channel);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rmtReadAsync(rmt_obj_t* rmt, rmt_data_t* data, size_t size, void* eventFlag, bool waitForData, uint32_t timeout)
|
||||
{
|
||||
if (!rmt) {
|
||||
@ -523,6 +538,8 @@ rmt_obj_t* rmtInit(int pin, bool tx_not_rx, rmt_reserve_memsize_t memsize)
|
||||
rmt->tx_not_rx = tx_not_rx;
|
||||
rmt->buffers =buffers;
|
||||
rmt->channel = channel;
|
||||
rmt->arg = NULL;
|
||||
|
||||
_initPin(pin, channel, tx_not_rx);
|
||||
|
||||
// Initialize the registers in default mode:
|
||||
@ -544,6 +561,7 @@ rmt_obj_t* rmtInit(int pin, bool tx_not_rx, rmt_reserve_memsize_t memsize)
|
||||
RMT.conf_ch[channel].conf1.idle_out_lv = 0; // signal level for idle
|
||||
RMT.conf_ch[channel].conf1.idle_out_en = 1; // enable idle
|
||||
RMT.conf_ch[channel].conf1.ref_always_on = 0; // base clock
|
||||
|
||||
RMT.apb_conf.fifo_mask = 1;
|
||||
|
||||
if (tx_not_rx) {
|
||||
@ -659,7 +677,7 @@ static void IRAM_ATTR _rmt_isr(void* arg)
|
||||
}
|
||||
if (g_rmt_objects[ch].cb) {
|
||||
// actually received data ptr
|
||||
(g_rmt_objects[ch].cb)(data_received, _rmt_get_mem_len(ch));
|
||||
(g_rmt_objects[ch].cb)(data_received, _rmt_get_mem_len(ch), g_rmt_objects[ch].arg);
|
||||
|
||||
// restart the reception
|
||||
RMT.conf_ch[ch].conf1.mem_owner = 1;
|
||||
|
@ -40,7 +40,7 @@ typedef enum {
|
||||
|
||||
typedef struct rmt_obj_s rmt_obj_t;
|
||||
|
||||
typedef void (*rmt_rx_data_cb_t)(uint32_t *data, size_t len);
|
||||
typedef void (*rmt_rx_data_cb_t)(uint32_t *data, size_t len, void *arg);
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
@ -90,8 +90,13 @@ bool rmtReadAsync(rmt_obj_t* rmt, rmt_data_t* data, size_t size, void* eventFlag
|
||||
* and callback with data from ISR
|
||||
*
|
||||
*/
|
||||
bool rmtRead(rmt_obj_t* rmt, rmt_rx_data_cb_t cb);
|
||||
bool rmtRead(rmt_obj_t* rmt, rmt_rx_data_cb_t cb, void * arg);
|
||||
|
||||
/***
|
||||
* Ends async receive started with rmtRead(); but does not
|
||||
* rmtDeInit().
|
||||
*/
|
||||
bool rmtEnd(rmt_obj_t* rmt);
|
||||
|
||||
/* Additional interface */
|
||||
|
||||
|
@ -402,9 +402,9 @@ void spiStopBus(spi_t * spi)
|
||||
if(!spi) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
removeApbChangeCallback(spi, _on_apb_change);
|
||||
|
||||
|
||||
SPI_MUTEX_LOCK();
|
||||
spiInitBus(spi);
|
||||
SPI_MUTEX_UNLOCK();
|
||||
@ -428,14 +428,14 @@ spi_t * spiStartBus(uint8_t spi_num, uint32_t clockDiv, uint8_t dataMode, uint8_
|
||||
#endif
|
||||
|
||||
if(spi_num == HSPI) {
|
||||
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_SPI_CLK_EN);
|
||||
DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_SPI_RST);
|
||||
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_SPI2_CLK_EN);
|
||||
DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_SPI2_RST);
|
||||
} else if(spi_num == VSPI) {
|
||||
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_SPI_CLK_EN_2);
|
||||
DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_SPI_RST_2);
|
||||
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_SPI3_CLK_EN);
|
||||
DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_SPI3_RST);
|
||||
} else {
|
||||
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_SPI_CLK_EN_1);
|
||||
DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_SPI_RST_1);
|
||||
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_SPI01_CLK_EN);
|
||||
DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_SPI01_RST);
|
||||
}
|
||||
|
||||
SPI_MUTEX_LOCK();
|
||||
|
@ -54,7 +54,7 @@ extern "C" {
|
||||
struct spi_struct_t;
|
||||
typedef struct spi_struct_t spi_t;
|
||||
|
||||
spi_t * spiStartBus(uint8_t spi_num, uint32_t freq, uint8_t dataMode, uint8_t bitOrder);
|
||||
spi_t * spiStartBus(uint8_t spi_num, uint32_t clockDiv, uint8_t dataMode, uint8_t bitOrder);
|
||||
void spiStopBus(spi_t * spi);
|
||||
|
||||
//Attach/Detach Signal Pins
|
||||
|
@ -90,56 +90,56 @@ void IRAM_ATTR __timerISR(void * arg){
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t timerRead(hw_timer_t *timer){
|
||||
uint64_t IRAM_ATTR timerRead(hw_timer_t *timer){
|
||||
timer->dev->update = 1;
|
||||
uint64_t h = timer->dev->cnt_high;
|
||||
uint64_t l = timer->dev->cnt_low;
|
||||
return (h << 32) | l;
|
||||
}
|
||||
|
||||
uint64_t timerAlarmRead(hw_timer_t *timer){
|
||||
uint64_t IRAM_ATTR timerAlarmRead(hw_timer_t *timer){
|
||||
uint64_t h = timer->dev->alarm_high;
|
||||
uint64_t l = timer->dev->alarm_low;
|
||||
return (h << 32) | l;
|
||||
}
|
||||
|
||||
void timerWrite(hw_timer_t *timer, uint64_t val){
|
||||
void IRAM_ATTR timerWrite(hw_timer_t *timer, uint64_t val){
|
||||
timer->dev->load_high = (uint32_t) (val >> 32);
|
||||
timer->dev->load_low = (uint32_t) (val);
|
||||
timer->dev->reload = 1;
|
||||
}
|
||||
|
||||
void timerAlarmWrite(hw_timer_t *timer, uint64_t alarm_value, bool autoreload){
|
||||
void IRAM_ATTR timerAlarmWrite(hw_timer_t *timer, uint64_t alarm_value, bool autoreload){
|
||||
timer->dev->alarm_high = (uint32_t) (alarm_value >> 32);
|
||||
timer->dev->alarm_low = (uint32_t) alarm_value;
|
||||
timer->dev->config.autoreload = autoreload;
|
||||
}
|
||||
|
||||
void timerSetConfig(hw_timer_t *timer, uint32_t config){
|
||||
void IRAM_ATTR timerSetConfig(hw_timer_t *timer, uint32_t config){
|
||||
timer->dev->config.val = config;
|
||||
}
|
||||
|
||||
uint32_t timerGetConfig(hw_timer_t *timer){
|
||||
uint32_t IRAM_ATTR timerGetConfig(hw_timer_t *timer){
|
||||
return timer->dev->config.val;
|
||||
}
|
||||
|
||||
void timerSetCountUp(hw_timer_t *timer, bool countUp){
|
||||
void IRAM_ATTR timerSetCountUp(hw_timer_t *timer, bool countUp){
|
||||
timer->dev->config.increase = countUp;
|
||||
}
|
||||
|
||||
bool timerGetCountUp(hw_timer_t *timer){
|
||||
bool IRAM_ATTR timerGetCountUp(hw_timer_t *timer){
|
||||
return timer->dev->config.increase;
|
||||
}
|
||||
|
||||
void timerSetAutoReload(hw_timer_t *timer, bool autoreload){
|
||||
void IRAM_ATTR timerSetAutoReload(hw_timer_t *timer, bool autoreload){
|
||||
timer->dev->config.autoreload = autoreload;
|
||||
}
|
||||
|
||||
bool timerGetAutoReload(hw_timer_t *timer){
|
||||
bool IRAM_ATTR timerGetAutoReload(hw_timer_t *timer){
|
||||
return timer->dev->config.autoreload;
|
||||
}
|
||||
|
||||
void timerSetDivider(hw_timer_t *timer, uint16_t divider){//2 to 65536
|
||||
void IRAM_ATTR timerSetDivider(hw_timer_t *timer, uint16_t divider){//2 to 65536
|
||||
if(!divider){
|
||||
divider = 0xFFFF;
|
||||
} else if(divider == 1){
|
||||
@ -151,41 +151,41 @@ void timerSetDivider(hw_timer_t *timer, uint16_t divider){//2 to 65536
|
||||
timer->dev->config.enable = timer_en;
|
||||
}
|
||||
|
||||
uint16_t timerGetDivider(hw_timer_t *timer){
|
||||
uint16_t IRAM_ATTR timerGetDivider(hw_timer_t *timer){
|
||||
return timer->dev->config.divider;
|
||||
}
|
||||
|
||||
void timerStart(hw_timer_t *timer){
|
||||
void IRAM_ATTR timerStart(hw_timer_t *timer){
|
||||
timer->dev->config.enable = 1;
|
||||
}
|
||||
|
||||
void timerStop(hw_timer_t *timer){
|
||||
void IRAM_ATTR timerStop(hw_timer_t *timer){
|
||||
timer->dev->config.enable = 0;
|
||||
}
|
||||
|
||||
void timerRestart(hw_timer_t *timer){
|
||||
void IRAM_ATTR timerRestart(hw_timer_t *timer){
|
||||
timer->dev->config.enable = 0;
|
||||
timer->dev->reload = 1;
|
||||
timer->dev->config.enable = 1;
|
||||
}
|
||||
|
||||
bool timerStarted(hw_timer_t *timer){
|
||||
bool IRAM_ATTR timerStarted(hw_timer_t *timer){
|
||||
return timer->dev->config.enable;
|
||||
}
|
||||
|
||||
void timerAlarmEnable(hw_timer_t *timer){
|
||||
void IRAM_ATTR timerAlarmEnable(hw_timer_t *timer){
|
||||
timer->dev->config.alarm_en = 1;
|
||||
}
|
||||
|
||||
void timerAlarmDisable(hw_timer_t *timer){
|
||||
void IRAM_ATTR timerAlarmDisable(hw_timer_t *timer){
|
||||
timer->dev->config.alarm_en = 0;
|
||||
}
|
||||
|
||||
bool timerAlarmEnabled(hw_timer_t *timer){
|
||||
bool IRAM_ATTR timerAlarmEnabled(hw_timer_t *timer){
|
||||
return timer->dev->config.alarm_en;
|
||||
}
|
||||
|
||||
static void _on_apb_change(void * arg, apb_change_ev_t ev_type, uint32_t old_apb, uint32_t new_apb){
|
||||
static void IRAM_ATTR _on_apb_change(void * arg, apb_change_ev_t ev_type, uint32_t old_apb, uint32_t new_apb){
|
||||
hw_timer_t * timer = (hw_timer_t *)arg;
|
||||
if(ev_type == APB_BEFORE_CHANGE){
|
||||
timer->dev->config.enable = 0;
|
||||
@ -197,7 +197,7 @@ static void _on_apb_change(void * arg, apb_change_ev_t ev_type, uint32_t old_apb
|
||||
}
|
||||
}
|
||||
|
||||
hw_timer_t * timerBegin(uint8_t num, uint16_t divider, bool countUp){
|
||||
hw_timer_t * IRAM_ATTR timerBegin(uint8_t num, uint16_t divider, bool countUp){
|
||||
if(num > 3){
|
||||
return NULL;
|
||||
}
|
||||
@ -222,13 +222,13 @@ hw_timer_t * timerBegin(uint8_t num, uint16_t divider, bool countUp){
|
||||
return timer;
|
||||
}
|
||||
|
||||
void timerEnd(hw_timer_t *timer){
|
||||
void IRAM_ATTR timerEnd(hw_timer_t *timer){
|
||||
timer->dev->config.enable = 0;
|
||||
timerAttachInterrupt(timer, NULL, false);
|
||||
removeApbChangeCallback(timer, _on_apb_change);
|
||||
}
|
||||
|
||||
void timerAttachInterrupt(hw_timer_t *timer, void (*fn)(void), bool edge){
|
||||
void IRAM_ATTR timerAttachInterrupt(hw_timer_t *timer, void (*fn)(void), bool edge){
|
||||
static bool initialized = false;
|
||||
static intr_handle_t intr_handle = NULL;
|
||||
if(intr_handle){
|
||||
@ -279,29 +279,29 @@ void timerAttachInterrupt(hw_timer_t *timer, void (*fn)(void), bool edge){
|
||||
}
|
||||
}
|
||||
|
||||
void timerDetachInterrupt(hw_timer_t *timer){
|
||||
void IRAM_ATTR timerDetachInterrupt(hw_timer_t *timer){
|
||||
timerAttachInterrupt(timer, NULL, false);
|
||||
}
|
||||
|
||||
uint64_t timerReadMicros(hw_timer_t *timer){
|
||||
uint64_t IRAM_ATTR timerReadMicros(hw_timer_t *timer){
|
||||
uint64_t timer_val = timerRead(timer);
|
||||
uint16_t div = timerGetDivider(timer);
|
||||
return timer_val * div / (getApbFrequency() / 1000000);
|
||||
}
|
||||
|
||||
double timerReadSeconds(hw_timer_t *timer){
|
||||
double IRAM_ATTR timerReadSeconds(hw_timer_t *timer){
|
||||
uint64_t timer_val = timerRead(timer);
|
||||
uint16_t div = timerGetDivider(timer);
|
||||
return (double)timer_val * div / getApbFrequency();
|
||||
}
|
||||
|
||||
uint64_t timerAlarmReadMicros(hw_timer_t *timer){
|
||||
uint64_t IRAM_ATTR timerAlarmReadMicros(hw_timer_t *timer){
|
||||
uint64_t timer_val = timerAlarmRead(timer);
|
||||
uint16_t div = timerGetDivider(timer);
|
||||
return timer_val * div / (getApbFrequency() / 1000000);
|
||||
}
|
||||
|
||||
double timerAlarmReadSeconds(hw_timer_t *timer){
|
||||
double IRAM_ATTR timerAlarmReadSeconds(hw_timer_t *timer){
|
||||
uint64_t timer_val = timerAlarmRead(timer);
|
||||
uint16_t div = timerGetDivider(timer);
|
||||
return (double)timer_val * div / getApbFrequency();
|
||||
|
@ -124,21 +124,21 @@ void uartDisableInterrupt(uart_t* uart)
|
||||
UART_MUTEX_UNLOCK();
|
||||
}
|
||||
|
||||
void uartDetachRx(uart_t* uart)
|
||||
void uartDetachRx(uart_t* uart, uint8_t rxPin)
|
||||
{
|
||||
if(uart == NULL) {
|
||||
return;
|
||||
}
|
||||
pinMatrixInDetach(UART_RXD_IDX(uart->num), false, false);
|
||||
pinMatrixInDetach(rxPin, false, false);
|
||||
uartDisableInterrupt(uart);
|
||||
}
|
||||
|
||||
void uartDetachTx(uart_t* uart)
|
||||
void uartDetachTx(uart_t* uart, uint8_t txPin)
|
||||
{
|
||||
if(uart == NULL) {
|
||||
return;
|
||||
}
|
||||
pinMatrixOutDetach(UART_TXD_IDX(uart->num), false, false);
|
||||
pinMatrixOutDetach(txPin, false, false);
|
||||
}
|
||||
|
||||
void uartAttachRx(uart_t* uart, uint8_t rxPin, bool inverted)
|
||||
@ -226,7 +226,7 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
|
||||
return uart;
|
||||
}
|
||||
|
||||
void uartEnd(uart_t* uart)
|
||||
void uartEnd(uart_t* uart, uint8_t txPin, uint8_t rxPin)
|
||||
{
|
||||
if(uart == NULL) {
|
||||
return;
|
||||
@ -243,8 +243,8 @@ void uartEnd(uart_t* uart)
|
||||
|
||||
UART_MUTEX_UNLOCK();
|
||||
|
||||
uartDetachRx(uart);
|
||||
uartDetachTx(uart);
|
||||
uartDetachRx(uart, rxPin);
|
||||
uartDetachTx(uart, txPin);
|
||||
}
|
||||
|
||||
size_t uartResizeRxBuffer(uart_t * uart, size_t new_size) {
|
||||
@ -257,6 +257,7 @@ size_t uartResizeRxBuffer(uart_t * uart, size_t new_size) {
|
||||
vQueueDelete(uart->queue);
|
||||
uart->queue = xQueueCreate(new_size, sizeof(uint8_t));
|
||||
if(uart->queue == NULL) {
|
||||
UART_MUTEX_UNLOCK();
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@ -265,6 +266,17 @@ size_t uartResizeRxBuffer(uart_t * uart, size_t new_size) {
|
||||
return new_size;
|
||||
}
|
||||
|
||||
void uartSetRxInvert(uart_t* uart, bool invert)
|
||||
{
|
||||
if (uart == NULL)
|
||||
return;
|
||||
|
||||
if (invert)
|
||||
uart->dev->conf0.rxd_inv = 1;
|
||||
else
|
||||
uart->dev->conf0.rxd_inv = 0;
|
||||
}
|
||||
|
||||
uint32_t uartAvailable(uart_t* uart)
|
||||
{
|
||||
if(uart == NULL || uart->queue == NULL) {
|
||||
@ -359,7 +371,7 @@ void uartWriteBuf(uart_t* uart, const uint8_t * data, size_t len)
|
||||
|
||||
void uartFlush(uart_t* uart)
|
||||
{
|
||||
uartFlushTxOnly(uart,false);
|
||||
uartFlushTxOnly(uart,true);
|
||||
}
|
||||
|
||||
void uartFlushTxOnly(uart_t* uart, bool txOnly)
|
||||
|
@ -52,7 +52,7 @@ struct uart_struct_t;
|
||||
typedef struct uart_struct_t uart_t;
|
||||
|
||||
uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rxPin, int8_t txPin, uint16_t queueLen, bool inverted);
|
||||
void uartEnd(uart_t* uart);
|
||||
void uartEnd(uart_t* uart, uint8_t rxPin, uint8_t txPin);
|
||||
|
||||
uint32_t uartAvailable(uart_t* uart);
|
||||
uint32_t uartAvailableForWrite(uart_t* uart);
|
||||
@ -70,6 +70,8 @@ uint32_t uartGetBaudRate(uart_t* uart);
|
||||
|
||||
size_t uartResizeRxBuffer(uart_t* uart, size_t new_size);
|
||||
|
||||
void uartSetRxInvert(uart_t* uart, bool invert);
|
||||
|
||||
void uartSetDebug(uart_t* uart);
|
||||
int uartGetDebug();
|
||||
|
||||
|
@ -3,6 +3,10 @@
|
||||
#include "esp_task_wdt.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
#ifndef CONFIG_ARDUINO_LOOP_STACK_SIZE
|
||||
#define CONFIG_ARDUINO_LOOP_STACK_SIZE 8192
|
||||
#endif
|
||||
|
||||
TaskHandle_t loopTaskHandle = NULL;
|
||||
|
||||
#if CONFIG_AUTOSTART_ARDUINO
|
||||
@ -25,7 +29,7 @@ extern "C" void app_main()
|
||||
{
|
||||
loopTaskWDTEnabled = false;
|
||||
initArduino();
|
||||
xTaskCreateUniversal(loopTask, "loopTask", 8192, NULL, 1, &loopTaskHandle, CONFIG_ARDUINO_RUNNING_CORE);
|
||||
xTaskCreateUniversal(loopTask, "loopTask", CONFIG_ARDUINO_LOOP_STACK_SIZE, NULL, 1, &loopTaskHandle, CONFIG_ARDUINO_RUNNING_CORE);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -53,6 +53,8 @@ typedef unsigned long prog_uint32_t;
|
||||
*(void * const *)(_addr); \
|
||||
})
|
||||
|
||||
#define pgm_get_far_address(x) ((uint32_t)(&(x)))
|
||||
|
||||
#define pgm_read_byte_near(addr) pgm_read_byte(addr)
|
||||
#define pgm_read_word_near(addr) pgm_read_word(addr)
|
||||
#define pgm_read_dword_near(addr) pgm_read_dword(addr)
|
||||
|
@ -16,7 +16,7 @@
|
||||
Boston, MA 02111-1307 USA
|
||||
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
|
||||
*/
|
||||
|
||||
|
||||
#include "esp32-hal.h"
|
||||
#include "wiring_private.h"
|
||||
|
||||
|
@ -1,26 +1,28 @@
|
||||
Make your question, not a Statement, inclusive. Include all pertinent information:
|
||||
|
||||
What you are trying to do.
|
||||
Describe your system( Hardware, computer, O/S, core version, environment)
|
||||
Describe what is failing
|
||||
Show the shortest possible code that will duplicate the error
|
||||
Show the EXACT error message(it doesn't work is not enough)
|
||||
What you are trying to do
|
||||
Describe your system (Hardware, computer, O/S, core version, environment)
|
||||
Describe what is failing
|
||||
Show the shortest possible code that will duplicate the error
|
||||
Show the EXACT error message (it doesn't work is not enough)
|
||||
Then if someone is interested and knowledgeable you might get a answer. All of this work on your part shows us that you have worked to solve YOUR problem. The more complete your issue posting is, the more likely someone will volunteer their time to help you.
|
||||
|
||||
If you have a Guru Meditation Error or Backtrace, ***please decode it***:
|
||||
https://github.com/me-no-dev/EspExceptionDecoder
|
||||
[ExceptionDecoder](https://github.com/me-no-dev/EspExceptionDecoder)
|
||||
|
||||
----------------------------- Remove above -----------------------------
|
||||
|
||||
|
||||
### Hardware:
|
||||
Board: ?ESP32 Dev Module? ?node32? ?ttgo_lora?
|
||||
Core Installation/update date: ?11/jul/2017?
|
||||
IDE name: ?Arduino IDE? ?Platform.io? ?IDF component?
|
||||
Flash Frequency: ?40Mhz?
|
||||
PSRAM enabled: ?no?
|
||||
Upload Speed: ?115200?
|
||||
Computer OS: ?Windows 10? ?Mac OSX? ?Ubuntu?
|
||||
|||||||
|
||||
|:---|---|---|---|---|---|
|
||||
|<B>Board</B>|ESP32 Dev Module|node32|ttgo_lora|ESP32-S2-Saola|Custom w/ ESP32-S2-WROVER 16MB|
|
||||
|<B>Version/Date</B>|1.0.4|2.0.0|0badbeef|11/jul/2017|today's master|
|
||||
|<B>IDE name</B>|Arduino IDE|Atom + Platform.io|IDF component|VSCode|
|
||||
|<B>Flash Frequency</B>|40Mhz|80Mhz|
|
||||
|<B>PSRAM enabled</B>|yes|no|
|
||||
|<B>Upload Speed</B>|115200|
|
||||
|<B>Computer OS</B>|Windows 10|Mac OSX|Ubuntu|
|
||||
|
||||
### Description:
|
||||
Describe your problem here
|
||||
|
@ -4,7 +4,7 @@
|
||||
- Stable release link: `https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json`
|
||||
- Development release link: `https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json`
|
||||
|
||||
Starting with 1.6.4, Arduino allows installation of third-party platform packages using Boards Manager. We have packages available for Windows, Mac OS, and Linux (32, 64 bit and ARM).
|
||||
Starting with 1.6.4, Arduino allows installation of third-party platform packages using Boards Manager. We have packages available for Windows, Mac OS, and Linux (x86, amd64, armhf and arm64).
|
||||
|
||||
- Install the current upstream Arduino IDE at the 1.8 level or later. The current version is at the [Arduino website](http://www.arduino.cc/en/main/software).
|
||||
- Start Arduino and open Preferences window.
|
||||
|
@ -7,9 +7,9 @@ Installation instructions for Mac OS
|
||||
```bash
|
||||
mkdir -p ~/Documents/Arduino/hardware/espressif && \
|
||||
cd ~/Documents/Arduino/hardware/espressif && \
|
||||
git clone https://github.com/espressif/arduino-esp32.git esp32 && \
|
||||
git clone https://github.com/espressif/arduino-esp32.git esp32 --depth 1 && \
|
||||
cd esp32 && \
|
||||
git submodule update --init --recursive && \
|
||||
git submodule update --init --recursive --depth 1 && \
|
||||
cd tools && \
|
||||
python get.py
|
||||
```
|
||||
|
@ -72,6 +72,11 @@ If you are writing code that does not require Arduino to compile and you want yo
|
||||
#endif
|
||||
```
|
||||
|
||||
## FreeRTOS Tick Rate (Hz)
|
||||
|
||||
You might notice that Arduino-esp32's `delay()` function will only work in multiples of 10ms. That is because, by default, esp-idf handles task events 100 times per second.
|
||||
To fix that behavior you need to set FreeRTOS tick rate to 1000Hz in `make menuconfig` -> `Component config` -> `FreeRTOS` -> `Tick rate`.
|
||||
|
||||
## Compilation Errors
|
||||
|
||||
As commits are made to esp-idf and submodules, the codebases can develop incompatibilities which cause compilation errors. If you have problems compiling, follow the instructions in [Issue #1142](https://github.com/espressif/arduino-esp32/issues/1142) to roll esp-idf back to a known good version.
|
||||
|
@ -14,7 +14,7 @@ WebServer server(80);
|
||||
* Login page
|
||||
*/
|
||||
|
||||
const char* loginIndex =
|
||||
const char* loginIndex =
|
||||
"<form name='loginForm'>"
|
||||
"<table width='20%' bgcolor='A09F9F' align='center'>"
|
||||
"<tr>"
|
||||
@ -25,8 +25,9 @@ const char* loginIndex =
|
||||
"<br>"
|
||||
"<br>"
|
||||
"</tr>"
|
||||
"<td>Username:</td>"
|
||||
"<td><input type='text' size=25 name='userid'><br></td>"
|
||||
"<tr>"
|
||||
"<td>Username:</td>"
|
||||
"<td><input type='text' size=25 name='userid'><br></td>"
|
||||
"</tr>"
|
||||
"<br>"
|
||||
"<br>"
|
||||
@ -54,12 +55,12 @@ const char* loginIndex =
|
||||
"}"
|
||||
"}"
|
||||
"</script>";
|
||||
|
||||
|
||||
/*
|
||||
* Server Index Page
|
||||
*/
|
||||
|
||||
const char* serverIndex =
|
||||
|
||||
const char* serverIndex =
|
||||
"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
|
||||
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
|
||||
"<input type='file' name='update'>"
|
||||
@ -88,7 +89,7 @@ const char* serverIndex =
|
||||
"return xhr;"
|
||||
"},"
|
||||
"success:function(d, s) {"
|
||||
"console.log('success!')"
|
||||
"console.log('success!')"
|
||||
"},"
|
||||
"error: function (a, b, c) {"
|
||||
"}"
|
||||
|
@ -88,6 +88,17 @@ ArduinoOTAClass& ArduinoOTAClass::setPasswordHash(const char * password) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
ArduinoOTAClass& ArduinoOTAClass::setPartitionLabel(const char * partition_label) {
|
||||
if (!_initialized && !_partition_label.length() && partition_label) {
|
||||
_partition_label = partition_label;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
String ArduinoOTAClass::getPartitionLabel() {
|
||||
return _partition_label;
|
||||
}
|
||||
|
||||
ArduinoOTAClass& ArduinoOTAClass::setRebootOnSuccess(bool reboot){
|
||||
_rebootOnSuccess = reboot;
|
||||
return *this;
|
||||
@ -235,7 +246,8 @@ void ArduinoOTAClass::_onRx(){
|
||||
}
|
||||
|
||||
void ArduinoOTAClass::_runUpdate() {
|
||||
if (!Update.begin(_size, _cmd)) {
|
||||
const char *partition_label = _partition_label.length() ? _partition_label.c_str() : NULL;
|
||||
if (!Update.begin(_size, _cmd, -1, LOW, partition_label)) {
|
||||
|
||||
log_e("Begin ERROR: %s", Update.errorString());
|
||||
|
||||
@ -358,7 +370,7 @@ void ArduinoOTAClass::end() {
|
||||
|
||||
void ArduinoOTAClass::handle() {
|
||||
if (!_initialized) {
|
||||
return;
|
||||
return;
|
||||
}
|
||||
if (_state == OTA_RUNUPDATE) {
|
||||
_runUpdate();
|
||||
|
@ -44,6 +44,10 @@ class ArduinoOTAClass
|
||||
//Sets the password as above but in the form MD5(password). Default NULL
|
||||
ArduinoOTAClass& setPasswordHash(const char *password);
|
||||
|
||||
//Sets the partition label to write to when updating SPIFFS. Default NULL
|
||||
ArduinoOTAClass &setPartitionLabel(const char *partition_label);
|
||||
String getPartitionLabel();
|
||||
|
||||
//Sets if the device should be rebooted after successful update. Default true
|
||||
ArduinoOTAClass& setRebootOnSuccess(bool reboot);
|
||||
|
||||
@ -80,6 +84,7 @@ class ArduinoOTAClass
|
||||
int _port;
|
||||
String _password;
|
||||
String _hostname;
|
||||
String _partition_label;
|
||||
String _nonce;
|
||||
WiFiUDP _udp_ota;
|
||||
bool _initialized;
|
||||
|
@ -150,7 +150,7 @@ static bool _udp_task_start(){
|
||||
}
|
||||
}
|
||||
if(!_udp_task_handle){
|
||||
xTaskCreateUniversal(_udp_task, "async_udp", 4096, NULL, 3, (TaskHandle_t*)&_udp_task_handle, CONFIG_ARDUINO_UDP_RUNNING_CORE);
|
||||
xTaskCreateUniversal(_udp_task, "async_udp", 4096, NULL, CONFIG_ARDUINO_UDP_TASK_PRIORITY, (TaskHandle_t*)&_udp_task_handle, CONFIG_ARDUINO_UDP_RUNNING_CORE);
|
||||
if(!_udp_task_handle){
|
||||
return false;
|
||||
}
|
||||
@ -277,6 +277,16 @@ void AsyncUDPMessage::flush()
|
||||
_index = 0;
|
||||
}
|
||||
|
||||
AsyncUDPPacket::AsyncUDPPacket(AsyncUDPPacket &packet){
|
||||
_udp = packet._udp;
|
||||
_pb = packet._pb;
|
||||
_if = packet._if;
|
||||
_data = packet._data;
|
||||
_len = packet._len;
|
||||
_index = 0;
|
||||
|
||||
pbuf_ref(_pb);
|
||||
}
|
||||
|
||||
AsyncUDPPacket::AsyncUDPPacket(AsyncUDP *udp, pbuf *pb, const ip_addr_t *raddr, uint16_t rport, struct netif * ntif)
|
||||
{
|
||||
@ -479,6 +489,7 @@ AsyncUDP::AsyncUDP()
|
||||
{
|
||||
_pcb = NULL;
|
||||
_connected = false;
|
||||
_lastErr = ERR_OK;
|
||||
_handler = NULL;
|
||||
}
|
||||
|
||||
@ -517,8 +528,8 @@ bool AsyncUDP::connect(const ip_addr_t *addr, uint16_t port)
|
||||
}
|
||||
close();
|
||||
UDP_MUTEX_LOCK();
|
||||
err_t err = _udp_connect(_pcb, addr, port);
|
||||
if(err != ERR_OK) {
|
||||
_lastErr = _udp_connect(_pcb, addr, port);
|
||||
if(_lastErr != ERR_OK) {
|
||||
UDP_MUTEX_UNLOCK();
|
||||
return false;
|
||||
}
|
||||
@ -646,7 +657,7 @@ size_t AsyncUDP::writeTo(const uint8_t * data, size_t len, const ip_addr_t * add
|
||||
if(len > CONFIG_TCP_MSS) {
|
||||
len = CONFIG_TCP_MSS;
|
||||
}
|
||||
err_t err = ERR_OK;
|
||||
_lastErr = ERR_OK;
|
||||
pbuf* pbt = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
|
||||
if(pbt != NULL) {
|
||||
uint8_t* dst = reinterpret_cast<uint8_t*>(pbt->payload);
|
||||
@ -656,16 +667,16 @@ size_t AsyncUDP::writeTo(const uint8_t * data, size_t len, const ip_addr_t * add
|
||||
void * nif = NULL;
|
||||
tcpip_adapter_get_netif((tcpip_adapter_if_t)tcpip_if, &nif);
|
||||
if(!nif){
|
||||
err = _udp_sendto(_pcb, pbt, addr, port);
|
||||
_lastErr = _udp_sendto(_pcb, pbt, addr, port);
|
||||
} else {
|
||||
err = _udp_sendto_if(_pcb, pbt, addr, port, (struct netif *)nif);
|
||||
_lastErr = _udp_sendto_if(_pcb, pbt, addr, port, (struct netif *)nif);
|
||||
}
|
||||
} else {
|
||||
err = _udp_sendto(_pcb, pbt, addr, port);
|
||||
_lastErr = _udp_sendto(_pcb, pbt, addr, port);
|
||||
}
|
||||
UDP_MUTEX_UNLOCK();
|
||||
pbuf_free(pbt);
|
||||
if(err < ERR_OK) {
|
||||
if(_lastErr < ERR_OK) {
|
||||
return 0;
|
||||
}
|
||||
return len;
|
||||
@ -682,9 +693,8 @@ void AsyncUDP::_recv(udp_pcb *upcb, pbuf *pb, const ip_addr_t *addr, uint16_t po
|
||||
if(_handler) {
|
||||
AsyncUDPPacket packet(this, this_pb, addr, port, netif);
|
||||
_handler(packet);
|
||||
} else {
|
||||
pbuf_free(this_pb);
|
||||
}
|
||||
pbuf_free(this_pb);
|
||||
}
|
||||
}
|
||||
|
||||
@ -870,6 +880,10 @@ bool AsyncUDP::connected()
|
||||
return _connected;
|
||||
}
|
||||
|
||||
esp_err_t AsyncUDP::lastErr() {
|
||||
return _lastErr;
|
||||
}
|
||||
|
||||
void AsyncUDP::onPacket(AuPacketHandlerFunctionWithArg cb, void * arg)
|
||||
{
|
||||
onPacket(std::bind(cb, arg, std::placeholders::_1));
|
||||
|
@ -58,6 +58,7 @@ protected:
|
||||
size_t _len;
|
||||
size_t _index;
|
||||
public:
|
||||
AsyncUDPPacket(AsyncUDPPacket &packet);
|
||||
AsyncUDPPacket(AsyncUDP *udp, pbuf *pb, const ip_addr_t *addr, uint16_t port, struct netif * netif);
|
||||
virtual ~AsyncUDPPacket();
|
||||
|
||||
@ -95,6 +96,7 @@ protected:
|
||||
udp_pcb *_pcb;
|
||||
//xSemaphoreHandle _lock;
|
||||
bool _connected;
|
||||
esp_err_t _lastErr;
|
||||
AuPacketHandlerFunction _handler;
|
||||
|
||||
bool _init();
|
||||
@ -144,6 +146,7 @@ public:
|
||||
IPAddress listenIP();
|
||||
IPv6Address listenIPv6();
|
||||
bool connected();
|
||||
esp_err_t lastErr();
|
||||
operator bool();
|
||||
|
||||
static void _s_recv(void *arg, udp_pcb *upcb, pbuf *p, const ip_addr_t *addr, uint16_t port, struct netif * netif);
|
||||
|
Submodule libraries/AzureIoT updated: 67dfa4f31e...5e8ffb2111
153
libraries/BLE/examples/BLE_Beacon_Scanner/BLE_Beacon_Scanner.ino
Normal file
153
libraries/BLE/examples/BLE_Beacon_Scanner/BLE_Beacon_Scanner.ino
Normal file
@ -0,0 +1,153 @@
|
||||
/*
|
||||
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
|
||||
Ported to Arduino ESP32 by Evandro Copercini
|
||||
Changed to a beacon scanner to report iBeacon, EddystoneURL and EddystoneTLM beacons by beegee-tokyo
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEUtils.h>
|
||||
#include <BLEScan.h>
|
||||
#include <BLEAdvertisedDevice.h>
|
||||
#include <BLEEddystoneURL.h>
|
||||
#include <BLEEddystoneTLM.h>
|
||||
#include <BLEBeacon.h>
|
||||
|
||||
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00) >> 8) + (((x)&0xFF) << 8))
|
||||
|
||||
int scanTime = 5; //In seconds
|
||||
BLEScan *pBLEScan;
|
||||
|
||||
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
|
||||
{
|
||||
void onResult(BLEAdvertisedDevice advertisedDevice)
|
||||
{
|
||||
if (advertisedDevice.haveName())
|
||||
{
|
||||
Serial.print("Device name: ");
|
||||
Serial.println(advertisedDevice.getName().c_str());
|
||||
Serial.println("");
|
||||
}
|
||||
|
||||
if (advertisedDevice.haveServiceUUID())
|
||||
{
|
||||
BLEUUID devUUID = advertisedDevice.getServiceUUID();
|
||||
Serial.print("Found ServiceUUID: ");
|
||||
Serial.println(devUUID.toString().c_str());
|
||||
Serial.println("");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (advertisedDevice.haveManufacturerData() == true)
|
||||
{
|
||||
std::string strManufacturerData = advertisedDevice.getManufacturerData();
|
||||
|
||||
uint8_t cManufacturerData[100];
|
||||
strManufacturerData.copy((char *)cManufacturerData, strManufacturerData.length(), 0);
|
||||
|
||||
if (strManufacturerData.length() == 25 && cManufacturerData[0] == 0x4C && cManufacturerData[1] == 0x00)
|
||||
{
|
||||
Serial.println("Found an iBeacon!");
|
||||
BLEBeacon oBeacon = BLEBeacon();
|
||||
oBeacon.setData(strManufacturerData);
|
||||
Serial.printf("iBeacon Frame\n");
|
||||
Serial.printf("ID: %04X Major: %d Minor: %d UUID: %s Power: %d\n", oBeacon.getManufacturerId(), ENDIAN_CHANGE_U16(oBeacon.getMajor()), ENDIAN_CHANGE_U16(oBeacon.getMinor()), oBeacon.getProximityUUID().toString().c_str(), oBeacon.getSignalPower());
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Found another manufacturers beacon!");
|
||||
Serial.printf("strManufacturerData: %d ", strManufacturerData.length());
|
||||
for (int i = 0; i < strManufacturerData.length(); i++)
|
||||
{
|
||||
Serial.printf("[%X]", cManufacturerData[i]);
|
||||
}
|
||||
Serial.printf("\n");
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *payLoad = advertisedDevice.getPayload();
|
||||
|
||||
BLEUUID checkUrlUUID = (uint16_t)0xfeaa;
|
||||
|
||||
if (advertisedDevice.getServiceUUID().equals(checkUrlUUID))
|
||||
{
|
||||
if (payLoad[11] == 0x10)
|
||||
{
|
||||
Serial.println("Found an EddystoneURL beacon!");
|
||||
BLEEddystoneURL foundEddyURL = BLEEddystoneURL();
|
||||
std::string eddyContent((char *)&payLoad[11]); // incomplete EddystoneURL struct!
|
||||
|
||||
foundEddyURL.setData(eddyContent);
|
||||
std::string bareURL = foundEddyURL.getURL();
|
||||
if (bareURL[0] == 0x00)
|
||||
{
|
||||
size_t payLoadLen = advertisedDevice.getPayloadLength();
|
||||
Serial.println("DATA-->");
|
||||
for (int idx = 0; idx < payLoadLen; idx++)
|
||||
{
|
||||
Serial.printf("0x%08X ", payLoad[idx]);
|
||||
}
|
||||
Serial.println("\nInvalid Data");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.printf("Found URL: %s\n", foundEddyURL.getURL().c_str());
|
||||
Serial.printf("Decoded URL: %s\n", foundEddyURL.getDecodedURL().c_str());
|
||||
Serial.printf("TX power %d\n", foundEddyURL.getPower());
|
||||
Serial.println("\n");
|
||||
}
|
||||
else if (payLoad[11] == 0x20)
|
||||
{
|
||||
Serial.println("Found an EddystoneTLM beacon!");
|
||||
BLEEddystoneTLM foundEddyURL = BLEEddystoneTLM();
|
||||
std::string eddyContent((char *)&payLoad[11]); // incomplete EddystoneURL struct!
|
||||
|
||||
eddyContent = "01234567890123";
|
||||
|
||||
for (int idx = 0; idx < 14; idx++)
|
||||
{
|
||||
eddyContent[idx] = payLoad[idx + 11];
|
||||
}
|
||||
|
||||
foundEddyURL.setData(eddyContent);
|
||||
Serial.printf("Reported battery voltage: %dmV\n", foundEddyURL.getVolt());
|
||||
Serial.printf("Reported temperature from TLM class: %.2fC\n", (double)foundEddyURL.getTemp());
|
||||
int temp = (int)payLoad[16] + (int)(payLoad[15] << 8);
|
||||
float calcTemp = temp / 256.0f;
|
||||
Serial.printf("Reported temperature from data: %.2fC\n", calcTemp);
|
||||
Serial.printf("Reported advertise count: %d\n", foundEddyURL.getCount());
|
||||
Serial.printf("Reported time since last reboot: %ds\n", foundEddyURL.getTime());
|
||||
Serial.println("\n");
|
||||
Serial.print(foundEddyURL.toString().c_str());
|
||||
Serial.println("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("Scanning...");
|
||||
|
||||
BLEDevice::init("");
|
||||
pBLEScan = BLEDevice::getScan(); //create new scan
|
||||
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
|
||||
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
|
||||
pBLEScan->setInterval(100);
|
||||
pBLEScan->setWindow(99); // less or equal setInterval value
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// put your main code here, to run repeatedly:
|
||||
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
|
||||
Serial.print("Devices found: ");
|
||||
Serial.println(foundDevices.getCount());
|
||||
Serial.println("Scan done!");
|
||||
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
|
||||
delay(2000);
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
## BLE Beacon Scanner
|
||||
|
||||
Initiates a BLE device scan.
|
||||
Checks if the discovered devices are
|
||||
- an iBeacon
|
||||
- an Eddystone TLM beacon
|
||||
- an Eddystone URL beacon
|
||||
|
||||
and sends the decoded beacon information over Serial log
|
@ -0,0 +1,116 @@
|
||||
/*
|
||||
EddystoneTLM beacon by BeeGee based on https://github.com/pcbreflux/espressif/blob/master/esp32/arduino/sketchbook/ESP32_Eddystone_TLM_deepsleep/ESP32_Eddystone_TLM_deepsleep.ino
|
||||
EddystoneTLM frame specification https://github.com/google/eddystone/blob/master/eddystone-tlm/tlm-plain.md
|
||||
*/
|
||||
|
||||
/*
|
||||
Create a BLE server that will send periodic Eddystone URL frames.
|
||||
The design of creating the BLE server is:
|
||||
1. Create a BLE Server
|
||||
2. Create advertising data
|
||||
3. Start advertising.
|
||||
4. wait
|
||||
5. Stop advertising.
|
||||
6. deep sleep
|
||||
|
||||
*/
|
||||
#include "sys/time.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BLEDevice.h"
|
||||
#include "BLEUtils.h"
|
||||
#include "BLEBeacon.h"
|
||||
#include "BLEAdvertising.h"
|
||||
#include "BLEEddystoneURL.h"
|
||||
|
||||
#include "esp_sleep.h"
|
||||
|
||||
#define GPIO_DEEP_SLEEP_DURATION 10 // sleep x seconds and then wake up
|
||||
RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory
|
||||
RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory
|
||||
|
||||
// See the following for generating UUIDs:
|
||||
// https://www.uuidgenerator.net/
|
||||
BLEAdvertising *pAdvertising;
|
||||
struct timeval nowTimeStruct;
|
||||
|
||||
time_t lastTenth;
|
||||
|
||||
#define BEACON_UUID "8ec76ea3-6668-48da-9866-75be8bc86f4d" // UUID 1 128-Bit (may use linux tool uuidgen or random numbers via https://www.uuidgenerator.net/)
|
||||
|
||||
// Check
|
||||
// https://github.com/google/eddystone/blob/master/eddystone-tlm/tlm-plain.md
|
||||
// and http://www.hugi.scene.org/online/coding/hugi%2015%20-%20cmtadfix.htm
|
||||
// for the temperature value. It is a 8.8 fixed-point notation
|
||||
void setBeacon()
|
||||
{
|
||||
char beacon_data[25];
|
||||
uint16_t beconUUID = 0xFEAA;
|
||||
uint16_t volt = random(2800, 3700); // 3300mV = 3.3V
|
||||
float tempFloat = random(2000, 3100) / 100.0f;
|
||||
Serial.printf("Random temperature is %.2fC\n", tempFloat);
|
||||
int temp = (int)(tempFloat * 256); //(uint16_t)((float)23.00);
|
||||
Serial.printf("Converted to 8.8 format %0X%0X\n", (temp >> 8), (temp & 0xFF));
|
||||
|
||||
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
|
||||
BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
|
||||
|
||||
oScanResponseData.setFlags(0x06); // GENERAL_DISC_MODE 0x02 | BR_EDR_NOT_SUPPORTED 0x04
|
||||
oScanResponseData.setCompleteServices(BLEUUID(beconUUID));
|
||||
|
||||
beacon_data[0] = 0x20; // Eddystone Frame Type (Unencrypted Eddystone-TLM)
|
||||
beacon_data[1] = 0x00; // TLM version
|
||||
beacon_data[2] = (volt >> 8); // Battery voltage, 1 mV/bit i.e. 0xCE4 = 3300mV = 3.3V
|
||||
beacon_data[3] = (volt & 0xFF); //
|
||||
beacon_data[4] = (temp >> 8); // Beacon temperature
|
||||
beacon_data[5] = (temp & 0xFF); //
|
||||
beacon_data[6] = ((bootcount & 0xFF000000) >> 24); // Advertising PDU count
|
||||
beacon_data[7] = ((bootcount & 0xFF0000) >> 16); //
|
||||
beacon_data[8] = ((bootcount & 0xFF00) >> 8); //
|
||||
beacon_data[9] = (bootcount & 0xFF); //
|
||||
beacon_data[10] = ((lastTenth & 0xFF000000) >> 24); // Time since power-on or reboot as 0.1 second resolution counter
|
||||
beacon_data[11] = ((lastTenth & 0xFF0000) >> 16); //
|
||||
beacon_data[12] = ((lastTenth & 0xFF00) >> 8); //
|
||||
beacon_data[13] = (lastTenth & 0xFF); //
|
||||
|
||||
oScanResponseData.setServiceData(BLEUUID(beconUUID), std::string(beacon_data, 14));
|
||||
oAdvertisementData.setName("TLMBeacon");
|
||||
pAdvertising->setAdvertisementData(oAdvertisementData);
|
||||
pAdvertising->setScanResponseData(oScanResponseData);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
Serial.begin(115200);
|
||||
gettimeofday(&nowTimeStruct, NULL);
|
||||
|
||||
Serial.printf("start ESP32 %d\n", bootcount++);
|
||||
|
||||
Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n", nowTimeStruct.tv_sec, nowTimeStruct.tv_sec - last);
|
||||
|
||||
last = nowTimeStruct.tv_sec;
|
||||
lastTenth = nowTimeStruct.tv_sec * 10; // Time since last reset as 0.1 second resolution counter
|
||||
|
||||
// Create the BLE Device
|
||||
BLEDevice::init("TLMBeacon");
|
||||
|
||||
BLEDevice::setPower(ESP_PWR_LVL_N12);
|
||||
|
||||
pAdvertising = BLEDevice::getAdvertising();
|
||||
|
||||
setBeacon();
|
||||
// Start advertising
|
||||
pAdvertising->start();
|
||||
Serial.println("Advertizing started for 10s ...");
|
||||
delay(10000);
|
||||
pAdvertising->stop();
|
||||
Serial.printf("enter deep sleep for 10s\n");
|
||||
esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION);
|
||||
Serial.printf("in deep sleep\n");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
## Eddystone TLM beacon
|
||||
EddystoneTLM beacon by BeeGee based on
|
||||
[pcbreflux ESP32 Eddystone TLM deepsleep](https://github.com/pcbreflux/espressif/blob/master/esp32/arduino/sketchbook/ESP32_Eddystone_TLM_deepsleep/ESP32_Eddystone_TLM_deepsleep.ino)
|
||||
|
||||
[EddystoneTLM frame specification](https://github.com/google/eddystone/blob/master/eddystone-tlm/tlm-plain.md)
|
||||
|
||||
Create a BLE server that will send periodic Eddystone TLM frames.
|
||||
The design of creating the BLE server is:
|
||||
1. Create a BLE Server
|
||||
2. Create advertising data
|
||||
3. Start advertising.
|
||||
4. wait
|
||||
5. Stop advertising.
|
||||
6. deep sleep
|
@ -0,0 +1,192 @@
|
||||
/*
|
||||
EddystoneURL beacon by BeeGee
|
||||
EddystoneURL frame specification https://github.com/google/eddystone/blob/master/eddystone-url/README.md
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
Create a BLE server that will send periodic Eddystone URL frames.
|
||||
The design of creating the BLE server is:
|
||||
1. Create a BLE Server
|
||||
2. Create advertising data
|
||||
3. Start advertising.
|
||||
4. wait
|
||||
5. Stop advertising.
|
||||
6. deep sleep
|
||||
|
||||
*/
|
||||
#include "sys/time.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "BLEDevice.h"
|
||||
#include "BLEUtils.h"
|
||||
#include "BLEBeacon.h"
|
||||
#include "BLEAdvertising.h"
|
||||
#include "BLEEddystoneURL.h"
|
||||
|
||||
#include "esp_sleep.h"
|
||||
|
||||
#define GPIO_DEEP_SLEEP_DURATION 10 // sleep x seconds and then wake up
|
||||
RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory
|
||||
RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory
|
||||
|
||||
// See the following for generating UUIDs:
|
||||
// https://www.uuidgenerator.net/
|
||||
BLEAdvertising *pAdvertising;
|
||||
struct timeval now;
|
||||
|
||||
#define BEACON_UUID "8ec76ea3-6668-48da-9866-75be8bc86f4d" // UUID 1 128-Bit (may use linux tool uuidgen or random numbers via https://www.uuidgenerator.net/)
|
||||
|
||||
static const char *eddystone_url_prefix_subs[] = {
|
||||
"http://www.",
|
||||
"https://www.",
|
||||
"http://",
|
||||
"https://",
|
||||
"urn:uuid:",
|
||||
NULL
|
||||
};
|
||||
|
||||
static const char *eddystone_url_suffix_subs[] = {
|
||||
".com/",
|
||||
".org/",
|
||||
".edu/",
|
||||
".net/",
|
||||
".info/",
|
||||
".biz/",
|
||||
".gov/",
|
||||
".com",
|
||||
".org",
|
||||
".edu",
|
||||
".net",
|
||||
".info",
|
||||
".biz",
|
||||
".gov",
|
||||
NULL
|
||||
};
|
||||
|
||||
static int string_begin_with(const char *str, const char *prefix)
|
||||
{
|
||||
int prefix_len = strlen(prefix);
|
||||
if (strncmp(prefix, str, prefix_len) == 0)
|
||||
{
|
||||
return prefix_len;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void setBeacon()
|
||||
{
|
||||
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
|
||||
BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
|
||||
|
||||
const char url[] = "https://d.giesecke.tk";
|
||||
|
||||
int scheme_len, ext_len = 1, i, idx, url_idx;
|
||||
char *ret_data;
|
||||
int url_len = strlen(url);
|
||||
|
||||
ret_data = (char *)calloc(1, url_len + 13);
|
||||
|
||||
ret_data[0] = 2; // Len
|
||||
ret_data[1] = 0x01; // Type Flags
|
||||
ret_data[2] = 0x06; // GENERAL_DISC_MODE 0x02 | BR_EDR_NOT_SUPPORTED 0x04
|
||||
ret_data[3] = 3; // Len
|
||||
ret_data[4] = 0x03; // Type 16-Bit UUID
|
||||
ret_data[5] = 0xAA; // Eddystone UUID 2 -> 0xFEAA LSB
|
||||
ret_data[6] = 0xFE; // Eddystone UUID 1 MSB
|
||||
ret_data[7] = 19; // Length of Beacon Data
|
||||
ret_data[8] = 0x16; // Type Service Data
|
||||
ret_data[9] = 0xAA; // Eddystone UUID 2 -> 0xFEAA LSB
|
||||
ret_data[10] = 0xFE; // Eddystone UUID 1 MSB
|
||||
ret_data[11] = 0x10; // Eddystone Frame Type
|
||||
ret_data[12] = 0xF4; // Beacons TX power at 0m
|
||||
|
||||
i = 0, idx = 13, url_idx = 0;
|
||||
|
||||
//replace prefix
|
||||
scheme_len = 0;
|
||||
while (eddystone_url_prefix_subs[i] != NULL)
|
||||
{
|
||||
if ((scheme_len = string_begin_with(url, eddystone_url_prefix_subs[i])) > 0)
|
||||
{
|
||||
ret_data[idx] = i;
|
||||
idx++;
|
||||
url_idx += scheme_len;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
while (url_idx < url_len)
|
||||
{
|
||||
i = 0;
|
||||
ret_data[idx] = url[url_idx];
|
||||
ext_len = 1;
|
||||
while (eddystone_url_suffix_subs[i] != NULL)
|
||||
{
|
||||
if ((ext_len = string_begin_with(&url[url_idx], eddystone_url_suffix_subs[i])) > 0)
|
||||
{
|
||||
ret_data[idx] = i;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
ext_len = 1; //inc 1
|
||||
}
|
||||
i++;
|
||||
}
|
||||
url_idx += ext_len;
|
||||
idx++;
|
||||
}
|
||||
ret_data[7] = idx - 8;
|
||||
|
||||
Serial.printf("struct size %d url size %d reported len %d\n",
|
||||
url_len + 13,
|
||||
url_len, ret_data[7]);
|
||||
|
||||
Serial.printf("URL in data %s\n", &ret_data[13]);
|
||||
|
||||
std::string eddyStoneData(ret_data);
|
||||
|
||||
oAdvertisementData.addData(eddyStoneData);
|
||||
oScanResponseData.setName("URLBeacon");
|
||||
pAdvertising->setAdvertisementData(oAdvertisementData);
|
||||
pAdvertising->setScanResponseData(oScanResponseData);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
Serial.begin(115200);
|
||||
gettimeofday(&now, NULL);
|
||||
|
||||
Serial.printf("start ESP32 %d\n", bootcount++);
|
||||
|
||||
Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n", now.tv_sec, now.tv_sec - last);
|
||||
|
||||
last = now.tv_sec;
|
||||
|
||||
// Create the BLE Device
|
||||
BLEDevice::init("URLBeacon");
|
||||
|
||||
BLEDevice::setPower(ESP_PWR_LVL_N12);
|
||||
|
||||
// Create the BLE Server
|
||||
// BLEServer *pServer = BLEDevice::createServer(); // <-- no longer required to instantiate BLEServer, less flash and ram usage
|
||||
|
||||
pAdvertising = BLEDevice::getAdvertising();
|
||||
|
||||
setBeacon();
|
||||
// Start advertising
|
||||
pAdvertising->start();
|
||||
Serial.println("Advertizing started...");
|
||||
delay(10000);
|
||||
pAdvertising->stop();
|
||||
Serial.printf("enter deep sleep\n");
|
||||
esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION);
|
||||
Serial.printf("in deep sleep\n");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
## Eddystone URL beacon
|
||||
EddystoneURL beacon by BeeGee based on
|
||||
[pcbreflux ESP32 Eddystone URL deepsleep](https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_Eddystone_URL_deepsleep)
|
||||
|
||||
[EddystoneURL frame specification](https://github.com/google/eddystone/blob/master/eddystone-url/README.md)
|
||||
|
||||
Create a BLE server that will send periodic Eddystone URL frames.
|
||||
The design of creating the BLE server is:
|
||||
1. Create a BLE Server
|
||||
2. Create advertising data
|
||||
3. Start advertising.
|
||||
4. wait
|
||||
5. Stop advertising.
|
||||
6. deep sleep
|
@ -25,6 +25,7 @@ BLEAdvertisedDevice::BLEAdvertisedDevice() {
|
||||
m_manufacturerData = "";
|
||||
m_name = "";
|
||||
m_rssi = -9999;
|
||||
m_serviceUUIDs = {};
|
||||
m_serviceData = {};
|
||||
m_serviceDataUUIDs = {};
|
||||
m_txPower = 0;
|
||||
@ -34,8 +35,6 @@ BLEAdvertisedDevice::BLEAdvertisedDevice() {
|
||||
m_haveManufacturerData = false;
|
||||
m_haveName = false;
|
||||
m_haveRSSI = false;
|
||||
m_haveServiceData = false;
|
||||
m_haveServiceUUID = false;
|
||||
m_haveTXPower = false;
|
||||
|
||||
} // BLEAdvertisedDevice
|
||||
@ -107,11 +106,7 @@ BLEScan* BLEAdvertisedDevice::getScan() {
|
||||
* @return Number of service data discovered.
|
||||
*/
|
||||
int BLEAdvertisedDevice::getServiceDataCount() {
|
||||
if (m_haveServiceData)
|
||||
return m_serviceData.size();
|
||||
else
|
||||
return 0;
|
||||
|
||||
return m_serviceData.size();
|
||||
} //getServiceDataCount
|
||||
|
||||
/**
|
||||
@ -119,7 +114,7 @@ int BLEAdvertisedDevice::getServiceDataCount() {
|
||||
* @return The ServiceData of the advertised device.
|
||||
*/
|
||||
std::string BLEAdvertisedDevice::getServiceData() {
|
||||
return m_serviceData[0];
|
||||
return m_serviceData.empty() ? std::string() : m_serviceData.front();
|
||||
} //getServiceData
|
||||
|
||||
/**
|
||||
@ -130,12 +125,20 @@ std::string BLEAdvertisedDevice::getServiceData(int i) {
|
||||
return m_serviceData[i];
|
||||
} //getServiceData
|
||||
|
||||
/**
|
||||
* @brief Get the number of service data UUIDs.
|
||||
* @return Number of service data UUIDs discovered.
|
||||
*/
|
||||
int BLEAdvertisedDevice::getServiceDataUUIDCount() {
|
||||
return m_serviceDataUUIDs.size();
|
||||
} //getServiceDataUUIDCount
|
||||
|
||||
/**
|
||||
* @brief Get the service data UUID.
|
||||
* @return The service data UUID.
|
||||
*/
|
||||
BLEUUID BLEAdvertisedDevice::getServiceDataUUID() {
|
||||
return m_serviceDataUUIDs[0];
|
||||
return m_serviceDataUUIDs.empty() ? BLEUUID() : m_serviceDataUUIDs.front();
|
||||
} // getServiceDataUUID
|
||||
|
||||
/**
|
||||
@ -146,12 +149,20 @@ BLEUUID BLEAdvertisedDevice::getServiceDataUUID(int i) {
|
||||
return m_serviceDataUUIDs[i];
|
||||
} // getServiceDataUUID
|
||||
|
||||
/**
|
||||
* @brief Get the number of service UUIDs.
|
||||
* @return Number of service UUIDs discovered.
|
||||
*/
|
||||
int BLEAdvertisedDevice::getServiceUUIDCount() {
|
||||
return m_serviceUUIDs.size();
|
||||
} //getServiceUUIDCount
|
||||
|
||||
/**
|
||||
* @brief Get the Service UUID.
|
||||
* @return The Service UUID of the advertised device.
|
||||
*/
|
||||
BLEUUID BLEAdvertisedDevice::getServiceUUID() {
|
||||
return m_serviceUUIDs[0];
|
||||
return m_serviceUUIDs.empty() ? BLEUUID() : m_serviceUUIDs.front();
|
||||
} // getServiceUUID
|
||||
|
||||
/**
|
||||
@ -167,7 +178,7 @@ BLEUUID BLEAdvertisedDevice::getServiceUUID(int i) {
|
||||
* @return Return true if service is advertised
|
||||
*/
|
||||
bool BLEAdvertisedDevice::isAdvertisingService(BLEUUID uuid){
|
||||
for (int i = 0; i < m_serviceUUIDs.size(); i++) {
|
||||
for (int i = 0; i < getServiceUUIDCount(); i++) {
|
||||
if (m_serviceUUIDs[i].equals(uuid)) return true;
|
||||
}
|
||||
return false;
|
||||
@ -224,7 +235,7 @@ bool BLEAdvertisedDevice::haveRSSI() {
|
||||
* @return True if there is a service data value present.
|
||||
*/
|
||||
bool BLEAdvertisedDevice::haveServiceData() {
|
||||
return m_haveServiceData;
|
||||
return !m_serviceData.empty();
|
||||
} // haveServiceData
|
||||
|
||||
|
||||
@ -233,7 +244,7 @@ bool BLEAdvertisedDevice::haveServiceData() {
|
||||
* @return True if there is a service UUID value present.
|
||||
*/
|
||||
bool BLEAdvertisedDevice::haveServiceUUID() {
|
||||
return m_haveServiceUUID;
|
||||
return !m_serviceUUIDs.empty();
|
||||
} // haveServiceUUID
|
||||
|
||||
|
||||
@ -388,6 +399,15 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
|
||||
} // !finished
|
||||
} // parseAdvertisement
|
||||
|
||||
/**
|
||||
* @brief Parse the advertising payload.
|
||||
* @param [in] payload The payload of the advertised device.
|
||||
* @param [in] total_len The length of payload
|
||||
*/
|
||||
void BLEAdvertisedDevice::setPayload(uint8_t* payload, size_t total_len) {
|
||||
m_payload = payload;
|
||||
m_payloadLength = total_len;
|
||||
} // setPayload
|
||||
|
||||
/**
|
||||
* @brief Set the address of the advertised device.
|
||||
@ -477,7 +497,6 @@ void BLEAdvertisedDevice::setServiceUUID(const char* serviceUUID) {
|
||||
*/
|
||||
void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
|
||||
m_serviceUUIDs.push_back(serviceUUID);
|
||||
m_haveServiceUUID = true;
|
||||
log_d("- addServiceUUID(): serviceUUID: %s", serviceUUID.toString().c_str());
|
||||
} // setServiceUUID
|
||||
|
||||
@ -487,7 +506,6 @@ void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
|
||||
* @param [in] data ServiceData value.
|
||||
*/
|
||||
void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
|
||||
m_haveServiceData = true; // Set the flag that indicates we have service data.
|
||||
m_serviceData.push_back(serviceData); // Save the service data that we received.
|
||||
} //setServiceData
|
||||
|
||||
@ -497,7 +515,6 @@ void BLEAdvertisedDevice::setServiceData(std::string serviceData) {
|
||||
* @param [in] data ServiceDataUUID value.
|
||||
*/
|
||||
void BLEAdvertisedDevice::setServiceDataUUID(BLEUUID uuid) {
|
||||
m_haveServiceData = true; // Set the flag that indicates we have service data.
|
||||
m_serviceDataUUIDs.push_back(uuid);
|
||||
log_d("- addServiceDataUUID(): serviceDataUUID: %s", uuid.toString().c_str());
|
||||
} // setServiceDataUUID
|
||||
@ -533,7 +550,7 @@ std::string BLEAdvertisedDevice::toString() {
|
||||
free(pHex);
|
||||
}
|
||||
if (haveServiceUUID()) {
|
||||
for (int i=0; i < m_serviceUUIDs.size(); i++) {
|
||||
for (int i=0; i < getServiceUUIDCount(); i++) {
|
||||
res += ", serviceUUID: " + getServiceUUID(i).toString();
|
||||
}
|
||||
}
|
||||
|
@ -42,6 +42,8 @@ public:
|
||||
BLEUUID getServiceUUID();
|
||||
BLEUUID getServiceUUID(int i);
|
||||
int getServiceDataCount();
|
||||
int getServiceDataUUIDCount();
|
||||
int getServiceUUIDCount();
|
||||
int8_t getTXPower();
|
||||
uint8_t* getPayload();
|
||||
size_t getPayloadLength();
|
||||
@ -64,6 +66,7 @@ private:
|
||||
friend class BLEScan;
|
||||
|
||||
void parseAdvertisement(uint8_t* payload, size_t total_len=62);
|
||||
void setPayload(uint8_t* payload, size_t total_len=62);
|
||||
void setAddress(BLEAddress address);
|
||||
void setAdFlag(uint8_t adFlag);
|
||||
void setAdvertizementResult(uint8_t* payload);
|
||||
@ -82,8 +85,6 @@ private:
|
||||
bool m_haveManufacturerData;
|
||||
bool m_haveName;
|
||||
bool m_haveRSSI;
|
||||
bool m_haveServiceData;
|
||||
bool m_haveServiceUUID;
|
||||
bool m_haveTXPower;
|
||||
|
||||
|
||||
|
@ -28,7 +28,9 @@
|
||||
* @brief Construct a default advertising object.
|
||||
*
|
||||
*/
|
||||
BLEAdvertising::BLEAdvertising() {
|
||||
BLEAdvertising::BLEAdvertising()
|
||||
: m_scanRespData{}
|
||||
{
|
||||
m_advData.set_scan_rsp = false;
|
||||
m_advData.include_name = true;
|
||||
m_advData.include_txpower = true;
|
||||
@ -215,10 +217,15 @@ void BLEAdvertising::start() {
|
||||
}
|
||||
|
||||
if (!m_customScanResponseData && m_scanResp) {
|
||||
m_advData.set_scan_rsp = true;
|
||||
m_advData.include_name = m_scanResp;
|
||||
m_advData.include_txpower = m_scanResp;
|
||||
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
|
||||
// Set the configuration for scan response.
|
||||
memcpy(&m_scanRespData, &m_advData, sizeof(esp_ble_adv_data_t)); // Copy the content of m_advData.
|
||||
m_scanRespData.set_scan_rsp = true; // Define this struct as scan response data
|
||||
m_scanRespData.include_name = true; // Caution: This may lead to a crash if the device name has more than 29 characters
|
||||
m_scanRespData.include_txpower = true;
|
||||
m_scanRespData.appearance = 0; // If defined the 'Appearance' attribute is already included in the advertising data
|
||||
m_scanRespData.flag = 0; // 'Flags' attribute should no be included in the scan response
|
||||
|
||||
errRc = ::esp_ble_gap_config_adv_data(&m_scanRespData);
|
||||
if (errRc != ESP_OK) {
|
||||
log_e("<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
return;
|
||||
|
@ -30,7 +30,7 @@ public:
|
||||
void setPartialServices(BLEUUID uuid);
|
||||
void setServiceData(BLEUUID uuid, std::string data);
|
||||
void setShortName(std::string name);
|
||||
void addData(std::string data); // Add data to the payload.
|
||||
void addData(std::string data); // Add data to the payload.
|
||||
std::string getPayload(); // Retrieve the current advert payload.
|
||||
|
||||
private:
|
||||
@ -68,12 +68,13 @@ public:
|
||||
|
||||
private:
|
||||
esp_ble_adv_data_t m_advData;
|
||||
esp_ble_adv_data_t m_scanRespData; // Used for configuration of scan response data when m_scanResp is true
|
||||
esp_ble_adv_params_t m_advParams;
|
||||
std::vector<BLEUUID> m_serviceUUIDs;
|
||||
bool m_customAdvData = false; // Are we using custom advertising data?
|
||||
bool m_customScanResponseData = false; // Are we using custom scan response data?
|
||||
FreeRTOS::Semaphore m_semaphoreSetAdv = FreeRTOS::Semaphore("startAdvert");
|
||||
bool m_scanResp = true;
|
||||
bool m_scanResp = true;
|
||||
|
||||
};
|
||||
#endif /* CONFIG_BT_ENABLED */
|
||||
|
@ -219,19 +219,22 @@ void BLECharacteristic::handleGATTServerEvent(
|
||||
// - uint8_t exec_write_flag - Either ESP_GATT_PREP_WRITE_EXEC or ESP_GATT_PREP_WRITE_CANCEL
|
||||
//
|
||||
case ESP_GATTS_EXEC_WRITE_EVT: {
|
||||
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
|
||||
m_value.commit();
|
||||
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
|
||||
} else {
|
||||
m_value.cancel();
|
||||
}
|
||||
// ???
|
||||
esp_err_t errRc = ::esp_ble_gatts_send_response(
|
||||
gatts_if,
|
||||
param->write.conn_id,
|
||||
param->write.trans_id, ESP_GATT_OK, nullptr);
|
||||
if (errRc != ESP_OK) {
|
||||
log_e("esp_ble_gatts_send_response: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
if(m_writeEvt){
|
||||
m_writeEvt = false;
|
||||
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
|
||||
m_value.commit();
|
||||
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler.
|
||||
} else {
|
||||
m_value.cancel();
|
||||
}
|
||||
// ???
|
||||
esp_err_t errRc = ::esp_ble_gatts_send_response(
|
||||
gatts_if,
|
||||
param->write.conn_id,
|
||||
param->write.trans_id, ESP_GATT_OK, nullptr);
|
||||
if (errRc != ESP_OK) {
|
||||
log_e("esp_ble_gatts_send_response: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
}
|
||||
}
|
||||
break;
|
||||
} // ESP_GATTS_EXEC_WRITE_EVT
|
||||
@ -277,6 +280,7 @@ void BLECharacteristic::handleGATTServerEvent(
|
||||
if (param->write.handle == m_handle) {
|
||||
if (param->write.is_prep) {
|
||||
m_value.addPart(param->write.value, param->write.len);
|
||||
m_writeEvt = true;
|
||||
} else {
|
||||
setValue(param->write.value, param->write.len);
|
||||
}
|
||||
|
@ -107,6 +107,7 @@ private:
|
||||
BLEService* m_pService;
|
||||
BLEValue m_value;
|
||||
esp_gatt_perm_t m_permissions = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE;
|
||||
bool m_writeEvt = false; // If we have started a long write, this tells the commit code that we were the target
|
||||
|
||||
void handleGATTServerEvent(
|
||||
esp_gatts_cb_event_t event,
|
||||
|
@ -60,6 +60,7 @@ BLEClient::~BLEClient() {
|
||||
delete myPair.second;
|
||||
}
|
||||
m_servicesMap.clear();
|
||||
m_servicesMapByInstID.clear();
|
||||
} // ~BLEClient
|
||||
|
||||
|
||||
@ -105,10 +106,21 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
|
||||
esp_err_t errRc = ::esp_ble_gattc_app_register(m_appId);
|
||||
if (errRc != ESP_OK) {
|
||||
log_e("esp_ble_gattc_app_register: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
BLEDevice::removePeerDevice(m_appId, true);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_semaphoreRegEvt.wait("connect");
|
||||
uint32_t rc = m_semaphoreRegEvt.wait("connect");
|
||||
|
||||
if (rc != ESP_GATT_OK) {
|
||||
// fixes ESP_GATT_NO_RESOURCES error mostly
|
||||
log_e("esp_ble_gattc_app_register_error: rc=%d", rc);
|
||||
BLEDevice::removePeerDevice(m_appId, true);
|
||||
// not sure if this is needed here
|
||||
// esp_ble_gattc_app_unregister(m_gattc_if);
|
||||
// m_gattc_if = ESP_GATT_IF_NONE;
|
||||
return false;
|
||||
}
|
||||
|
||||
m_peerAddress = address;
|
||||
|
||||
@ -122,10 +134,17 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
|
||||
);
|
||||
if (errRc != ESP_OK) {
|
||||
log_e("esp_ble_gattc_open: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
BLEDevice::removePeerDevice(m_appId, true);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete.
|
||||
rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete.
|
||||
// check the status of the connection and cleanup in case of failure
|
||||
if (rc != ESP_GATT_OK) {
|
||||
BLEDevice::removePeerDevice(m_appId, true);
|
||||
esp_ble_gattc_app_unregister(m_gattc_if);
|
||||
m_gattc_if = ESP_GATT_IF_NONE;
|
||||
}
|
||||
log_v("<< connect(), rc=%d", rc==ESP_GATT_OK);
|
||||
return rc == ESP_GATT_OK;
|
||||
} // connect
|
||||
@ -157,6 +176,11 @@ void BLEClient::gattClientEventHandler(
|
||||
log_d("gattClientEventHandler [esp_gatt_if: %d] ... %s",
|
||||
gattc_if, BLEUtils::gattClientEventTypeToString(event).c_str());
|
||||
|
||||
// it is possible to receive events from other connections while waiting for registration
|
||||
if (m_gattc_if == ESP_GATT_IF_NONE && event != ESP_GATTC_REG_EVT) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Execute handler code based on the type of event received.
|
||||
switch(event) {
|
||||
|
||||
@ -178,17 +202,20 @@ void BLEClient::gattClientEventHandler(
|
||||
// - uint16_t conn_id
|
||||
// - esp_bd_addr_t remote_bda
|
||||
case ESP_GATTC_DISCONNECT_EVT: {
|
||||
if (evtParam->disconnect.conn_id != getConnId()) break;
|
||||
// If we receive a disconnect event, set the class flag that indicates that we are
|
||||
// no longer connected.
|
||||
bool m_wasConnected = m_isConnected;
|
||||
m_isConnected = false;
|
||||
if (m_pClientCallbacks != nullptr) {
|
||||
m_pClientCallbacks->onDisconnect(this);
|
||||
}
|
||||
esp_ble_gattc_app_unregister(m_gattc_if);
|
||||
m_gattc_if = ESP_GATT_IF_NONE;
|
||||
m_semaphoreOpenEvt.give(ESP_GATT_IF_NONE);
|
||||
m_semaphoreRssiCmplEvt.give();
|
||||
m_semaphoreSearchCmplEvt.give(1);
|
||||
BLEDevice::removePeerDevice(m_appId, true);
|
||||
if (m_wasConnected && m_pClientCallbacks != nullptr) {
|
||||
m_pClientCallbacks->onDisconnect(this);
|
||||
}
|
||||
break;
|
||||
} // ESP_GATTC_DISCONNECT_EVT
|
||||
|
||||
@ -202,11 +229,13 @@ void BLEClient::gattClientEventHandler(
|
||||
//
|
||||
case ESP_GATTC_OPEN_EVT: {
|
||||
m_conn_id = evtParam->open.conn_id;
|
||||
if (m_pClientCallbacks != nullptr) {
|
||||
m_pClientCallbacks->onConnect(this);
|
||||
}
|
||||
if (evtParam->open.status == ESP_GATT_OK) {
|
||||
m_isConnected = true; // Flag us as connected.
|
||||
if (m_pClientCallbacks != nullptr) {
|
||||
m_pClientCallbacks->onConnect(this);
|
||||
}
|
||||
} else {
|
||||
log_e("Failed to connect, status=%s", GeneralUtils::errorToString(evtParam->open.status));
|
||||
}
|
||||
m_semaphoreOpenEvt.give(evtParam->open.status);
|
||||
break;
|
||||
@ -222,11 +251,13 @@ void BLEClient::gattClientEventHandler(
|
||||
//
|
||||
case ESP_GATTC_REG_EVT: {
|
||||
m_gattc_if = gattc_if;
|
||||
m_semaphoreRegEvt.give();
|
||||
// pass on the registration status result, in case of failure
|
||||
m_semaphoreRegEvt.give(evtParam->reg.status);
|
||||
break;
|
||||
} // ESP_GATTC_REG_EVT
|
||||
|
||||
case ESP_GATTC_CFG_MTU_EVT:
|
||||
if (evtParam->cfg_mtu.conn_id != getConnId()) break;
|
||||
if(evtParam->cfg_mtu.status != ESP_GATT_OK) {
|
||||
log_e("Config mtu failed");
|
||||
}
|
||||
@ -234,7 +265,8 @@ void BLEClient::gattClientEventHandler(
|
||||
break;
|
||||
|
||||
case ESP_GATTC_CONNECT_EVT: {
|
||||
BLEDevice::updatePeerDevice(this, true, m_gattc_if);
|
||||
if (evtParam->connect.conn_id != getConnId()) break;
|
||||
BLEDevice::updatePeerDevice(this, true, m_appId);
|
||||
esp_err_t errRc = esp_ble_gattc_send_mtu_req(gattc_if, evtParam->connect.conn_id);
|
||||
if (errRc != ESP_OK) {
|
||||
log_e("esp_ble_gattc_send_mtu_req: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
@ -255,6 +287,7 @@ void BLEClient::gattClientEventHandler(
|
||||
// - uint16_t conn_id
|
||||
//
|
||||
case ESP_GATTC_SEARCH_CMPL_EVT: {
|
||||
if (evtParam->search_cmpl.conn_id != getConnId()) break;
|
||||
esp_ble_gattc_cb_param_t* p_data = (esp_ble_gattc_cb_param_t*)evtParam;
|
||||
if (p_data->search_cmpl.status != ESP_GATT_OK){
|
||||
log_e("search service failed, error status = %x", p_data->search_cmpl.status);
|
||||
@ -285,6 +318,7 @@ void BLEClient::gattClientEventHandler(
|
||||
// - esp_gatt_id_t srvc_id
|
||||
//
|
||||
case ESP_GATTC_SEARCH_RES_EVT: {
|
||||
if (evtParam->search_res.conn_id != getConnId()) break;
|
||||
BLEUUID uuid = BLEUUID(evtParam->search_res.srvc_id);
|
||||
BLERemoteService* pRemoteService = new BLERemoteService(
|
||||
evtParam->search_res.srvc_id,
|
||||
|
@ -3,6 +3,11 @@
|
||||
*
|
||||
* Created on: Mar 12, 2018
|
||||
* Author: pcbreflux
|
||||
* Edited on: Mar 20, 2020 by beegee-tokyo
|
||||
* Fix temperature value (8.8 fixed format)
|
||||
* Fix time stamp (0.1 second resolution)
|
||||
* Fixes based on EddystoneTLM frame specification https://github.com/google/eddystone/blob/master/eddystone-tlm/tlm-plain.md
|
||||
*
|
||||
*/
|
||||
#include "sdkconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED)
|
||||
@ -20,7 +25,7 @@ BLEEddystoneTLM::BLEEddystoneTLM() {
|
||||
m_eddystoneData.frameType = EDDYSTONE_TLM_FRAME_TYPE;
|
||||
m_eddystoneData.version = 0;
|
||||
m_eddystoneData.volt = 3300; // 3300mV = 3.3V
|
||||
m_eddystoneData.temp = (uint16_t) ((float) 23.00);
|
||||
m_eddystoneData.temp = (uint16_t) ((float) 23.00)/256;
|
||||
m_eddystoneData.advCount = 0;
|
||||
m_eddystoneData.tmil = 0;
|
||||
} // BLEEddystoneTLM
|
||||
@ -38,41 +43,50 @@ uint8_t BLEEddystoneTLM::getVersion() {
|
||||
} // getVersion
|
||||
|
||||
uint16_t BLEEddystoneTLM::getVolt() {
|
||||
return m_eddystoneData.volt;
|
||||
return ENDIAN_CHANGE_U16(m_eddystoneData.volt);
|
||||
} // getVolt
|
||||
|
||||
float BLEEddystoneTLM::getTemp() {
|
||||
return (float)m_eddystoneData.temp;
|
||||
return ENDIAN_CHANGE_U16(m_eddystoneData.temp) / 256.0f;
|
||||
} // getTemp
|
||||
|
||||
uint32_t BLEEddystoneTLM::getCount() {
|
||||
return m_eddystoneData.advCount;
|
||||
return ENDIAN_CHANGE_U32(m_eddystoneData.advCount);
|
||||
} // getCount
|
||||
|
||||
uint32_t BLEEddystoneTLM::getTime() {
|
||||
return m_eddystoneData.tmil;
|
||||
return (ENDIAN_CHANGE_U32(m_eddystoneData.tmil)) / 10;
|
||||
} // getTime
|
||||
|
||||
std::string BLEEddystoneTLM::toString() {
|
||||
std::string out = "";
|
||||
uint32_t rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil);
|
||||
char val[6];
|
||||
char val[12];
|
||||
|
||||
out += "Version " + m_eddystoneData.version;
|
||||
out += "Version "; // + std::string(m_eddystoneData.version);
|
||||
snprintf(val, sizeof(val), "%d", m_eddystoneData.version);
|
||||
out += val;
|
||||
out += "\n";
|
||||
out += "Battery Voltage " + ENDIAN_CHANGE_U16(m_eddystoneData.volt);
|
||||
out += "Battery Voltage "; // + ENDIAN_CHANGE_U16(m_eddystoneData.volt);
|
||||
snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U16(m_eddystoneData.volt));
|
||||
out += val;
|
||||
out += " mV\n";
|
||||
|
||||
out += "Temperature ";
|
||||
snprintf(val, sizeof(val), "%d", m_eddystoneData.temp);
|
||||
snprintf(val, sizeof(val), "%.2f", ENDIAN_CHANGE_U16(m_eddystoneData.temp) / 256.0f);
|
||||
out += val;
|
||||
out += ".0 °C\n";
|
||||
out += " C\n";
|
||||
|
||||
out += "Adv. Count ";
|
||||
snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U32(m_eddystoneData.advCount));
|
||||
out += val;
|
||||
out += "\n";
|
||||
|
||||
out += "Time in seconds ";
|
||||
snprintf(val, sizeof(val), "%d", rawsec/10);
|
||||
out += val;
|
||||
out += "\n";
|
||||
|
||||
out += "Time ";
|
||||
|
||||
snprintf(val, sizeof(val), "%04d", rawsec / 864000);
|
||||
|
@ -194,6 +194,7 @@ BLECharacteristic* BLEHIDDevice::protocolMode() {
|
||||
|
||||
void BLEHIDDevice::setBatteryLevel(uint8_t level) {
|
||||
m_batteryLevelCharacteristic->setValue(&level, 1);
|
||||
m_batteryLevelCharacteristic->notify();
|
||||
}
|
||||
/*
|
||||
* @brief Returns battery level characteristic
|
||||
|
@ -52,6 +52,7 @@ BLERemoteCharacteristic::BLERemoteCharacteristic(
|
||||
*/
|
||||
BLERemoteCharacteristic::~BLERemoteCharacteristic() {
|
||||
removeDescriptors(); // Release resources for any descriptor information we may have allocated.
|
||||
free(m_rawData);
|
||||
} // ~BLERemoteCharacteristic
|
||||
|
||||
|
||||
@ -237,6 +238,13 @@ void BLERemoteCharacteristic::gattClientEventHandler(esp_gattc_cb_event_t event,
|
||||
break;
|
||||
} // ESP_GATTC_WRITE_CHAR_EVT
|
||||
|
||||
case ESP_GATTC_READ_DESCR_EVT:
|
||||
case ESP_GATTC_WRITE_DESCR_EVT:
|
||||
for (auto &myPair : m_descriptorMap) {
|
||||
myPair.second->gattClientEventHandler(
|
||||
event, gattc_if, evtParam);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
@ -446,7 +454,7 @@ std::string BLERemoteCharacteristic::readValue() {
|
||||
* unregistering a notification.
|
||||
* @return N/A.
|
||||
*/
|
||||
void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback, bool notifications) {
|
||||
void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback, bool notifications, bool descriptorRequiresRegistration) {
|
||||
log_v(">> registerForNotify(): %s", toString().c_str());
|
||||
|
||||
m_notifyCallback = notifyCallback; // Save the notification callback.
|
||||
@ -467,7 +475,8 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback,
|
||||
uint8_t val[] = {0x01, 0x00};
|
||||
if(!notifications) val[0] = 0x02;
|
||||
BLERemoteDescriptor* desc = getDescriptor(BLEUUID((uint16_t)0x2902));
|
||||
desc->writeValue(val, 2);
|
||||
if (desc != nullptr && descriptorRequiresRegistration)
|
||||
desc->writeValue(val, 2, true);
|
||||
} // End Register
|
||||
else { // If we weren't passed a callback function, then this is an unregistration.
|
||||
esp_err_t errRc = ::esp_ble_gattc_unregister_for_notify(
|
||||
@ -482,7 +491,8 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback,
|
||||
|
||||
uint8_t val[] = {0x00, 0x00};
|
||||
BLERemoteDescriptor* desc = getDescriptor((uint16_t)0x2902);
|
||||
desc->writeValue(val, 2);
|
||||
if (desc != nullptr && descriptorRequiresRegistration)
|
||||
desc->writeValue(val, 2, true);
|
||||
} // End Unregister
|
||||
|
||||
m_semaphoreRegForNotifyEvt.wait("registerForNotify");
|
||||
@ -533,7 +543,7 @@ std::string BLERemoteCharacteristic::toString() {
|
||||
* @return N/A.
|
||||
*/
|
||||
void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) {
|
||||
writeValue((uint8_t*)newValue.c_str(), strlen(newValue.c_str()), response);
|
||||
writeValue((uint8_t*)newValue.data(), newValue.length(), response);
|
||||
} // writeValue
|
||||
|
||||
|
||||
|
@ -39,6 +39,7 @@ public:
|
||||
bool canWriteNoResponse();
|
||||
BLERemoteDescriptor* getDescriptor(BLEUUID uuid);
|
||||
std::map<std::string, BLERemoteDescriptor*>* getDescriptors();
|
||||
BLERemoteService* getRemoteService();
|
||||
uint16_t getHandle();
|
||||
BLEUUID getUUID();
|
||||
std::string readValue();
|
||||
@ -46,7 +47,7 @@ public:
|
||||
uint16_t readUInt16();
|
||||
uint32_t readUInt32();
|
||||
float readFloat();
|
||||
void registerForNotify(notify_callback _callback, bool notifications = true);
|
||||
void registerForNotify(notify_callback _callback, bool notifications = true, bool descriptorRequiresRegistration = true);
|
||||
void writeValue(uint8_t* data, size_t length, bool response = false);
|
||||
void writeValue(std::string newValue, bool response = false);
|
||||
void writeValue(uint8_t newValue, bool response = false);
|
||||
@ -63,7 +64,6 @@ private:
|
||||
// Private member functions
|
||||
void gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam);
|
||||
|
||||
BLERemoteService* getRemoteService();
|
||||
void removeDescriptors();
|
||||
void retrieveDescriptors();
|
||||
|
||||
|
@ -49,6 +49,23 @@ BLEUUID BLERemoteDescriptor::getUUID() {
|
||||
return m_uuid;
|
||||
} // getUUID
|
||||
|
||||
void BLERemoteDescriptor::gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam) {
|
||||
switch(event) {
|
||||
case ESP_GATTC_READ_DESCR_EVT:
|
||||
if (evtParam->read.handle != getHandle())
|
||||
break;
|
||||
m_semaphoreReadDescrEvt.give();
|
||||
break;
|
||||
|
||||
case ESP_GATTC_WRITE_DESCR_EVT:
|
||||
if (evtParam->write.handle != getHandle())
|
||||
break;
|
||||
m_semaphoreWriteDescrEvt.give();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string BLERemoteDescriptor::readValue() {
|
||||
log_v(">> readValue: %s", toString().c_str());
|
||||
@ -137,6 +154,8 @@ void BLERemoteDescriptor::writeValue(uint8_t* data, size_t length, bool response
|
||||
return;
|
||||
}
|
||||
|
||||
m_semaphoreWriteDescrEvt.take("writeValue");
|
||||
|
||||
esp_err_t errRc = ::esp_ble_gattc_write_char_descr(
|
||||
m_pRemoteCharacteristic->getRemoteService()->getClient()->getGattcIf(),
|
||||
m_pRemoteCharacteristic->getRemoteService()->getClient()->getConnId(),
|
||||
@ -149,6 +168,8 @@ void BLERemoteDescriptor::writeValue(uint8_t* data, size_t length, bool response
|
||||
if (errRc != ESP_OK) {
|
||||
log_e("esp_ble_gattc_write_char_descr: %d", errRc);
|
||||
}
|
||||
|
||||
m_semaphoreWriteDescrEvt.wait("writeValue");
|
||||
log_v("<< writeValue");
|
||||
} // writeValue
|
||||
|
||||
|
@ -35,7 +35,7 @@ public:
|
||||
void writeValue(std::string newValue, bool response = false);
|
||||
void writeValue(uint8_t newValue, bool response = false);
|
||||
void setAuth(esp_gatt_auth_req_t auth);
|
||||
|
||||
void gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam);
|
||||
|
||||
private:
|
||||
friend class BLERemoteCharacteristic;
|
||||
@ -49,6 +49,7 @@ private:
|
||||
std::string m_value; // Last received value of the descriptor.
|
||||
BLERemoteCharacteristic* m_pRemoteCharacteristic; // Reference to the Remote characteristic of which this descriptor is associated.
|
||||
FreeRTOS::Semaphore m_semaphoreReadDescrEvt = FreeRTOS::Semaphore("ReadDescrEvt");
|
||||
FreeRTOS::Semaphore m_semaphoreWriteDescrEvt = FreeRTOS::Semaphore("WriteDescrEvt");
|
||||
esp_gatt_auth_req_t m_auth;
|
||||
|
||||
|
||||
|
@ -244,7 +244,16 @@ std::map<uint16_t, BLERemoteCharacteristic*>* BLERemoteService::getCharacteristi
|
||||
* @brief This function is designed to get characteristics map when we have multiple characteristics with the same UUID
|
||||
*/
|
||||
void BLERemoteService::getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic*>* pCharacteristicMap) {
|
||||
pCharacteristicMap = &m_characteristicMapByHandle;
|
||||
log_v(">> getCharacteristics() for service: %s", getUUID().toString().c_str());
|
||||
(void)pCharacteristicMap;
|
||||
// If is possible that we have not read the characteristics associated with the service so do that
|
||||
// now. The request to retrieve the characteristics by calling "retrieveCharacteristics" is a blocking
|
||||
// call and does not return until all the characteristics are available.
|
||||
if (!m_haveCharacteristics) {
|
||||
retrieveCharacteristics();
|
||||
}
|
||||
log_v("<< getCharacteristics() for service: %s", getUUID().toString().c_str());
|
||||
*pCharacteristicMap = m_characteristicMapByHandle;
|
||||
} // Get the characteristics map.
|
||||
|
||||
/**
|
||||
@ -302,13 +311,10 @@ std::string BLERemoteService::getValue(BLEUUID characteristicUuid) {
|
||||
* @return N/A.
|
||||
*/
|
||||
void BLERemoteService::removeCharacteristics() {
|
||||
for (auto &myPair : m_characteristicMap) {
|
||||
delete myPair.second;
|
||||
//m_characteristicMap.erase(myPair.first); // Should be no need to delete as it will be deleted by the clear
|
||||
}
|
||||
m_characteristicMap.clear(); // Clear the map
|
||||
for (auto &myPair : m_characteristicMapByHandle) {
|
||||
delete myPair.second;
|
||||
// delete the characteristics only once
|
||||
}
|
||||
m_characteristicMapByHandle.clear(); // Clear the map
|
||||
} // removeCharacteristics
|
||||
|
@ -22,12 +22,15 @@
|
||||
* Constructor
|
||||
*/
|
||||
BLEScan::BLEScan() {
|
||||
memset(&m_scan_params, 0, sizeof(m_scan_params)); // Initialize all params
|
||||
m_scan_params.scan_type = BLE_SCAN_TYPE_PASSIVE; // Default is a passive scan.
|
||||
m_scan_params.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
|
||||
m_scan_params.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL;
|
||||
m_scan_params.scan_duplicate = BLE_SCAN_DUPLICATE_DISABLE;
|
||||
m_pAdvertisedDeviceCallbacks = nullptr;
|
||||
m_stopped = true;
|
||||
m_wantDuplicates = false;
|
||||
m_shouldParse = true;
|
||||
setInterval(100);
|
||||
setWindow(100);
|
||||
} // BLEScan
|
||||
@ -88,15 +91,18 @@ void BLEScan::handleGAPEvent(
|
||||
// ignore it.
|
||||
BLEAddress advertisedAddress(param->scan_rst.bda);
|
||||
bool found = false;
|
||||
bool shouldDelete = true;
|
||||
|
||||
if (m_scanResults.m_vectorAdvertisedDevices.count(advertisedAddress.toString()) != 0) {
|
||||
found = true;
|
||||
}
|
||||
if (!m_wantDuplicates) {
|
||||
if (m_scanResults.m_vectorAdvertisedDevices.count(advertisedAddress.toString()) != 0) {
|
||||
found = true;
|
||||
}
|
||||
|
||||
if (found && !m_wantDuplicates) { // If we found a previous entry AND we don't want duplicates, then we are done.
|
||||
log_d("Ignoring %s, already seen it.", advertisedAddress.toString().c_str());
|
||||
vTaskDelay(1); // <--- allow to switch task in case we scan infinity and dont have new devices to report, or we are blocked here
|
||||
break;
|
||||
if (found) { // If we found a previous entry AND we don't want duplicates, then we are done.
|
||||
log_d("Ignoring %s, already seen it.", advertisedAddress.toString().c_str());
|
||||
vTaskDelay(1); // <--- allow to switch task in case we scan infinity and dont have new devices to report, or we are blocked here
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// We now construct a model of the advertised device that we have just found for the first
|
||||
@ -107,19 +113,23 @@ void BLEScan::handleGAPEvent(
|
||||
advertisedDevice->setAddress(advertisedAddress);
|
||||
advertisedDevice->setRSSI(param->scan_rst.rssi);
|
||||
advertisedDevice->setAdFlag(param->scan_rst.flag);
|
||||
advertisedDevice->parseAdvertisement((uint8_t*)param->scan_rst.ble_adv, param->scan_rst.adv_data_len + param->scan_rst.scan_rsp_len);
|
||||
if (m_shouldParse) {
|
||||
advertisedDevice->parseAdvertisement((uint8_t*)param->scan_rst.ble_adv, param->scan_rst.adv_data_len + param->scan_rst.scan_rsp_len);
|
||||
} else {
|
||||
advertisedDevice->setPayload((uint8_t*)param->scan_rst.ble_adv, param->scan_rst.adv_data_len + param->scan_rst.scan_rsp_len);
|
||||
}
|
||||
advertisedDevice->setScan(this);
|
||||
advertisedDevice->setAddressType(param->scan_rst.ble_addr_type);
|
||||
|
||||
if (!found) { // If we have previously seen this device, don't record it again.
|
||||
m_scanResults.m_vectorAdvertisedDevices.insert(std::pair<std::string, BLEAdvertisedDevice*>(advertisedAddress.toString(), advertisedDevice));
|
||||
}
|
||||
|
||||
if (m_pAdvertisedDeviceCallbacks) {
|
||||
if (m_pAdvertisedDeviceCallbacks) { // if has callback, no need to record to vector
|
||||
m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice);
|
||||
} else if (!m_wantDuplicates && !found) { // if no callback and not want duplicate, and not already in vector, record it
|
||||
m_scanResults.m_vectorAdvertisedDevices.insert(std::pair<std::string, BLEAdvertisedDevice*>(advertisedAddress.toString(), advertisedDevice));
|
||||
shouldDelete = false;
|
||||
}
|
||||
if(found)
|
||||
if (shouldDelete) {
|
||||
delete advertisedDevice;
|
||||
}
|
||||
|
||||
break;
|
||||
} // ESP_GAP_SEARCH_INQ_RES_EVT
|
||||
@ -159,13 +169,14 @@ void BLEScan::setActiveScan(bool active) {
|
||||
* @brief Set the call backs to be invoked.
|
||||
* @param [in] pAdvertisedDeviceCallbacks Call backs to be invoked.
|
||||
* @param [in] wantDuplicates True if we wish to be called back with duplicates. Default is false.
|
||||
* @param [in] shouldParse True if we wish to parse advertised package or raw payload. Default is true.
|
||||
*/
|
||||
void BLEScan::setAdvertisedDeviceCallbacks(BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks, bool wantDuplicates) {
|
||||
void BLEScan::setAdvertisedDeviceCallbacks(BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks, bool wantDuplicates, bool shouldParse) {
|
||||
m_wantDuplicates = wantDuplicates;
|
||||
m_pAdvertisedDeviceCallbacks = pAdvertisedDeviceCallbacks;
|
||||
m_shouldParse = shouldParse;
|
||||
} // setAdvertisedDeviceCallbacks
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set the interval to scan.
|
||||
* @param [in] The interval in msecs.
|
||||
|
@ -51,7 +51,8 @@ public:
|
||||
void setActiveScan(bool active);
|
||||
void setAdvertisedDeviceCallbacks(
|
||||
BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks,
|
||||
bool wantDuplicates = false);
|
||||
bool wantDuplicates = false,
|
||||
bool shouldParse = true);
|
||||
void setInterval(uint16_t intervalMSecs);
|
||||
void setWindow(uint16_t windowMSecs);
|
||||
bool start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults), bool is_continue = false);
|
||||
@ -73,6 +74,7 @@ private:
|
||||
esp_ble_scan_params_t m_scan_params;
|
||||
BLEAdvertisedDeviceCallbacks* m_pAdvertisedDeviceCallbacks = nullptr;
|
||||
bool m_stopped = true;
|
||||
bool m_shouldParse = true;
|
||||
FreeRTOS::Semaphore m_semaphoreScanEnd = FreeRTOS::Semaphore("ScanEnd");
|
||||
BLEScanResults m_scanResults;
|
||||
bool m_wantDuplicates;
|
||||
|
@ -61,6 +61,17 @@ void BLESecurity::setKeySize(uint8_t key_size) {
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &m_keySize, sizeof(uint8_t));
|
||||
} //setKeySize
|
||||
|
||||
/**
|
||||
* Setup for static PIN connection, call it first and then call setAuthenticationMode eventually to change it
|
||||
*/
|
||||
void BLESecurity::setStaticPIN(uint32_t pin){
|
||||
uint32_t passkey = pin;
|
||||
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));
|
||||
setCapability(ESP_IO_CAP_OUT);
|
||||
setKeySize();
|
||||
setAuthenticationMode(ESP_LE_AUTH_REQ_SC_ONLY);
|
||||
setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Debug function to display what keys are exchanged by peers
|
||||
|
@ -21,6 +21,7 @@ public:
|
||||
void setInitEncryptionKey(uint8_t init_key);
|
||||
void setRespEncryptionKey(uint8_t resp_key);
|
||||
void setKeySize(uint8_t key_size = 16);
|
||||
void setStaticPIN(uint32_t pin);
|
||||
static char* esp_key_type_to_str(esp_ble_key_type_t key_type);
|
||||
|
||||
private:
|
||||
|
@ -193,8 +193,8 @@ bool FreeRTOS::Semaphore::take(std::string owner) {
|
||||
} else {
|
||||
rc = ::xSemaphoreTake(m_semaphore, portMAX_DELAY) == pdTRUE;
|
||||
}
|
||||
m_owner = owner;
|
||||
if (rc) {
|
||||
m_owner = owner;
|
||||
log_v("Semaphore taken: %s", toString().c_str());
|
||||
} else {
|
||||
log_e("Semaphore NOT taken: %s", toString().c_str());
|
||||
@ -218,8 +218,8 @@ bool FreeRTOS::Semaphore::take(uint32_t timeoutMs, std::string owner) {
|
||||
} else {
|
||||
rc = ::xSemaphoreTake(m_semaphore, timeoutMs / portTICK_PERIOD_MS) == pdTRUE;
|
||||
}
|
||||
m_owner = owner;
|
||||
if (rc) {
|
||||
m_owner = owner;
|
||||
log_v("Semaphore taken: %s", toString().c_str());
|
||||
} else {
|
||||
log_e("Semaphore NOT taken: %s", toString().c_str());
|
||||
|
@ -0,0 +1,75 @@
|
||||
//This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
//By Richard Li - 2020
|
||||
//
|
||||
//This example creates a bridge between Serial and Classical Bluetooth (SPP with authentication)
|
||||
//and also demonstrate that SerialBT have the same functionalities of a normal Serial
|
||||
|
||||
#include "BluetoothSerial.h"
|
||||
|
||||
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
|
||||
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
|
||||
#endif
|
||||
|
||||
BluetoothSerial SerialBT;
|
||||
boolean confirmRequestPending = true;
|
||||
|
||||
void BTConfirmRequestCallback(uint32_t numVal)
|
||||
{
|
||||
confirmRequestPending = true;
|
||||
Serial.println(numVal);
|
||||
}
|
||||
|
||||
void BTAuthCompleteCallback(boolean success)
|
||||
{
|
||||
confirmRequestPending = false;
|
||||
if (success)
|
||||
{
|
||||
Serial.println("Pairing success!!");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Pairing failed, rejected by user!!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
SerialBT.enableSSP();
|
||||
SerialBT.onConfirmRequest(BTConfirmRequestCallback);
|
||||
SerialBT.onAuthComplete(BTAuthCompleteCallback);
|
||||
SerialBT.begin("ESP32test"); //Bluetooth device name
|
||||
Serial.println("The device started, now you can pair it with bluetooth!");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (confirmRequestPending)
|
||||
{
|
||||
if (Serial.available())
|
||||
{
|
||||
int dat = Serial.read();
|
||||
if (dat == 'Y' || dat == 'y')
|
||||
{
|
||||
SerialBT.confirmReply(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
SerialBT.confirmReply(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Serial.available())
|
||||
{
|
||||
SerialBT.write(Serial.read());
|
||||
}
|
||||
if (SerialBT.available())
|
||||
{
|
||||
Serial.write(SerialBT.read());
|
||||
}
|
||||
delay(20);
|
||||
}
|
||||
}
|
@ -51,6 +51,9 @@ static EventGroupHandle_t _spp_event_group = NULL;
|
||||
static boolean secondConnectionAttempt;
|
||||
static esp_spp_cb_t * custom_spp_callback = NULL;
|
||||
static BluetoothSerialDataCb custom_data_callback = NULL;
|
||||
static esp_bd_addr_t current_bd_addr;
|
||||
static ConfirmRequestCb confirm_request_callback = NULL;
|
||||
static AuthCompleteCb auth_complete_callback = NULL;
|
||||
|
||||
#define INQ_LEN 0x10
|
||||
#define INQ_NUM_RSPS 20
|
||||
@ -398,8 +401,14 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
|
||||
case ESP_BT_GAP_AUTH_CMPL_EVT:
|
||||
if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
|
||||
log_v("authentication success: %s", param->auth_cmpl.device_name);
|
||||
if (auth_complete_callback) {
|
||||
auth_complete_callback(true);
|
||||
}
|
||||
} else {
|
||||
log_e("authentication failed, status:%d", param->auth_cmpl.stat);
|
||||
if (auth_complete_callback) {
|
||||
auth_complete_callback(false);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@ -421,7 +430,13 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
|
||||
|
||||
case ESP_BT_GAP_CFM_REQ_EVT:
|
||||
log_i("ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val);
|
||||
esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
|
||||
if (confirm_request_callback) {
|
||||
memcpy(current_bd_addr, param->cfm_req.bda, sizeof(esp_bd_addr_t));
|
||||
confirm_request_callback(param->cfm_req.num_val);
|
||||
}
|
||||
else {
|
||||
esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
|
||||
}
|
||||
break;
|
||||
|
||||
case ESP_BT_GAP_KEY_NOTIF_EVT:
|
||||
@ -500,7 +515,9 @@ static bool _init_bt(const char *deviceName)
|
||||
}
|
||||
}
|
||||
|
||||
if (_isMaster && esp_bt_gap_register_callback(esp_bt_gap_cb) != ESP_OK) {
|
||||
// Why only master need this? Slave need this during pairing as well
|
||||
// if (_isMaster && esp_bt_gap_register_callback(esp_bt_gap_cb) != ESP_OK) {
|
||||
if (esp_bt_gap_register_callback(esp_bt_gap_cb) != ESP_OK) {
|
||||
log_e("gap register failed");
|
||||
return false;
|
||||
}
|
||||
@ -672,6 +689,22 @@ void BluetoothSerial::end()
|
||||
_stop_bt();
|
||||
}
|
||||
|
||||
void BluetoothSerial::onConfirmRequest(ConfirmRequestCb cb)
|
||||
{
|
||||
confirm_request_callback = cb;
|
||||
}
|
||||
|
||||
void BluetoothSerial::onAuthComplete(AuthCompleteCb cb)
|
||||
{
|
||||
auth_complete_callback = cb;
|
||||
}
|
||||
|
||||
void BluetoothSerial::confirmReply(boolean confirm)
|
||||
{
|
||||
esp_bt_gap_ssp_confirm_reply(current_bd_addr, confirm);
|
||||
}
|
||||
|
||||
|
||||
esp_err_t BluetoothSerial::register_callback(esp_spp_cb_t * callback)
|
||||
{
|
||||
custom_spp_callback = callback;
|
||||
|
@ -25,6 +25,8 @@
|
||||
#include <functional>
|
||||
|
||||
typedef std::function<void(const uint8_t *buffer, size_t size)> BluetoothSerialDataCb;
|
||||
typedef std::function<void(uint32_t num_val)> ConfirmRequestCb;
|
||||
typedef std::function<void(boolean success)> AuthCompleteCb;
|
||||
|
||||
class BluetoothSerial: public Stream
|
||||
{
|
||||
@ -44,6 +46,10 @@ class BluetoothSerial: public Stream
|
||||
void end(void);
|
||||
void onData(BluetoothSerialDataCb cb);
|
||||
esp_err_t register_callback(esp_spp_cb_t * callback);
|
||||
|
||||
void onConfirmRequest(ConfirmRequestCb cb);
|
||||
void onAuthComplete(AuthCompleteCb cb);
|
||||
void confirmReply(boolean confirm);
|
||||
|
||||
void enableSSP();
|
||||
bool setPin(const char *pin);
|
||||
|
@ -2,7 +2,7 @@
|
||||
#include <DNSServer.h>
|
||||
|
||||
const byte DNS_PORT = 53;
|
||||
IPAddress apIP(192, 168, 1, 1);
|
||||
IPAddress apIP(8,8,4,4); // The default android DNS
|
||||
DNSServer dnsServer;
|
||||
WiFiServer server(80);
|
||||
|
||||
@ -12,11 +12,9 @@ String responseHTML = ""
|
||||
"be redirected here.</p></body></html>";
|
||||
|
||||
void setup() {
|
||||
WiFi.disconnect(); //added to start with the wifi off, avoid crashing
|
||||
WiFi.mode(WIFI_OFF); //added to start with the wifi off, avoid crashing
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP("ESP32-DNSServer");
|
||||
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
|
||||
WiFi.softAP("DNSServer CaptivePortal example");
|
||||
|
||||
// if DNSServer is started with "*" for domain name, it will reply with
|
||||
// provided IP to all DNS request
|
||||
|
@ -2,6 +2,12 @@
|
||||
#include <lwip/def.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
// #define DEBUG_ESP_DNS
|
||||
#ifdef DEBUG_ESP_PORT
|
||||
#define DEBUG_OUTPUT DEBUG_ESP_PORT
|
||||
#else
|
||||
#define DEBUG_OUTPUT Serial
|
||||
#endif
|
||||
|
||||
DNSServer::DNSServer()
|
||||
{
|
||||
@ -184,6 +190,11 @@ void DNSServer::replyWithIP()
|
||||
_udp.write((unsigned char*) &answerIPv4, 2 );
|
||||
_udp.write(_resolvedIP, sizeof(_resolvedIP)); // The IP address to return
|
||||
_udp.endPacket();
|
||||
|
||||
#ifdef DEBUG_ESP_DNS
|
||||
DEBUG_OUTPUT.printf("DNS responds: %s for %s\n",
|
||||
IPAddress(_resolvedIP).toString().c_str(), getDomainNameWithoutWwwPrefix().c_str() );
|
||||
#endif
|
||||
}
|
||||
|
||||
void DNSServer::replyWithCustomCode()
|
||||
|
@ -17,6 +17,7 @@ EEPROMClass AGE("eeprom2", 0x100);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
Serial.println("Testing EEPROMClass\n");
|
||||
if (!NAMES.begin(NAMES.length())) {
|
||||
Serial.println("Failed to initialise NAMES");
|
||||
@ -43,7 +44,7 @@ void setup() {
|
||||
uint32_t age = 47;
|
||||
|
||||
// Write: Variables ---> EEPROM stores
|
||||
NAMES.put(0, name);
|
||||
NAMES.writeString(0, name);
|
||||
HEIGHT.put(0, height);
|
||||
AGE.put(0, age);
|
||||
Serial.print("name: "); Serial.println(name);
|
||||
|
@ -1,14 +1,30 @@
|
||||
uint64_t chipid;
|
||||
/* The true ESP32 chip ID is essentially its MAC address.
|
||||
This sketch provides an alternate chip ID that matches
|
||||
the output of the ESP.getChipId() function on ESP8266
|
||||
(i.e. a 32-bit integer matching the last 3 bytes of
|
||||
the MAC address. This is less unique than the
|
||||
MAC address chip ID, but is helpful when you need
|
||||
an identifier that can be no more than a 32-bit integer
|
||||
(like for switch...case).
|
||||
|
||||
created 2020-06-07 by cweinhofer
|
||||
with help from Cicicok */
|
||||
|
||||
uint32_t chipId = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
chipid=ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes).
|
||||
Serial.printf("ESP32 Chip ID = %04X",(uint16_t)(chipid>>32));//print High 2 bytes
|
||||
Serial.printf("%08X\n",(uint32_t)chipid);//print Low 4bytes.
|
||||
for(int i=0; i<17; i=i+8) {
|
||||
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
|
||||
}
|
||||
|
||||
Serial.printf("ESP32 Chip model = %s Rev %d\n", ESP.getChipModel(), ESP.getChipRevision());
|
||||
Serial.printf("This chip has %d cores\n", ESP.getChipCores());
|
||||
Serial.print("Chip ID: "); Serial.println(chipId);
|
||||
|
||||
delay(3000);
|
||||
|
||||
}
|
||||
|
64
libraries/ESP32/examples/RMT/RMTCallback/RMTCallback.ino
Normal file
64
libraries/ESP32/examples/RMT/RMTCallback/RMTCallback.ino
Normal file
@ -0,0 +1,64 @@
|
||||
#include "Arduino.h"
|
||||
#include "esp32-hal.h"
|
||||
|
||||
extern "C" void receive_trampoline(uint32_t *data, size_t len, void * arg);
|
||||
|
||||
class MyProcessor {
|
||||
private:
|
||||
rmt_obj_t* rmt_recv = NULL;
|
||||
float realNanoTick;
|
||||
uint32_t buff; // rolling buffer of most recent 32 bits.
|
||||
int at = 0;
|
||||
|
||||
public:
|
||||
MyProcessor(uint8_t pin, float nanoTicks) {
|
||||
assert((rmt_recv = rmtInit(21, false, RMT_MEM_192)));
|
||||
|
||||
realNanoTick = rmtSetTick(rmt_recv, nanoTicks);
|
||||
};
|
||||
void begin() {
|
||||
rmtRead(rmt_recv, receive_trampoline, this);
|
||||
};
|
||||
|
||||
void process(rmt_data_t *data, size_t len) {
|
||||
for (int i = 0; len; len--) {
|
||||
if (data[i].duration0 == 0)
|
||||
break;
|
||||
buff = (buff << 1) | (data[i].level0 ? 1 : 0);
|
||||
i++;
|
||||
|
||||
if (data[i].duration1 == 0)
|
||||
break;
|
||||
buff = (buff << 1) | (data[i].level1 ? 1 : 0);
|
||||
i++;
|
||||
};
|
||||
};
|
||||
uint32_t val() {
|
||||
return buff;
|
||||
}
|
||||
};
|
||||
|
||||
void receive_trampoline(uint32_t *data, size_t len, void * arg)
|
||||
{
|
||||
MyProcessor * p = (MyProcessor *)arg;
|
||||
p->process((rmt_data_t*) data, len);
|
||||
}
|
||||
|
||||
// Attach 3 processors to GPIO 4, 5 and 10 with different tick/speeds.
|
||||
MyProcessor mp1 = MyProcessor(4, 1000);
|
||||
MyProcessor mp2 = MyProcessor(5, 1000);
|
||||
MyProcessor mp3 = MyProcessor(10, 500);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
mp1.begin();
|
||||
mp2.begin();
|
||||
mp3.begin();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.printf("GPIO 4: %08x 5: %08x 6: %08x\n", mp1.val(), mp2.val(), mp3.val());
|
||||
delay(500);
|
||||
}
|
@ -16,6 +16,7 @@ static EventGroupHandle_t events;
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
events = xEventGroupCreate();
|
||||
|
||||
if ((rmt_send = rmtInit(18, true, RMT_MEM_64)) == NULL)
|
||||
{
|
||||
|
@ -172,7 +172,7 @@ void parseRmt(rmt_data_t* items, size_t len, uint32_t* channels){
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void receive_data(uint32_t *data, size_t len)
|
||||
extern "C" void receive_data(uint32_t *data, size_t len, void * arg)
|
||||
{
|
||||
parseRmt((rmt_data_t*) data, len, channels);
|
||||
}
|
||||
@ -192,7 +192,7 @@ void setup()
|
||||
Serial.printf("real tick set to: %fns\n", realTick);
|
||||
|
||||
// Ask to start reading
|
||||
rmtRead(rmt_recv, receive_data);
|
||||
rmtRead(rmt_recv, receive_data, NULL);
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
@ -47,7 +47,7 @@ void setup(void)
|
||||
|
||||
// Set up mDNS responder:
|
||||
// - first argument is the domain name, in this example
|
||||
// the fully-qualified domain name is "esp8266.local"
|
||||
// the fully-qualified domain name is "esp32.local"
|
||||
// - second argument is the IP address to advertise
|
||||
// we send our IP address on the WiFi network
|
||||
if (!MDNS.begin("esp32")) {
|
||||
|
@ -130,7 +130,7 @@ void MDNSResponder::disableWorkstation(){
|
||||
}
|
||||
}
|
||||
|
||||
void MDNSResponder::addService(char *name, char *proto, uint16_t port){
|
||||
bool MDNSResponder::addService(char *name, char *proto, uint16_t port){
|
||||
char _name[strlen(name)+2];
|
||||
char _proto[strlen(proto)+2];
|
||||
if (name[0] == '_') {
|
||||
@ -146,7 +146,9 @@ void MDNSResponder::addService(char *name, char *proto, uint16_t port){
|
||||
|
||||
if(mdns_service_add(NULL, _name, _proto, port, NULL, 0)) {
|
||||
log_e("Failed adding service %s.%s.\n", name, proto);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *value){
|
||||
|
@ -65,12 +65,12 @@ public:
|
||||
setInstanceName(String(name));
|
||||
}
|
||||
|
||||
void addService(char *service, char *proto, uint16_t port);
|
||||
void addService(const char *service, const char *proto, uint16_t port){
|
||||
addService((char *)service, (char *)proto, port);
|
||||
bool addService(char *service, char *proto, uint16_t port);
|
||||
bool addService(const char *service, const char *proto, uint16_t port){
|
||||
return addService((char *)service, (char *)proto, port);
|
||||
}
|
||||
void addService(String service, String proto, uint16_t port){
|
||||
addService(service.c_str(), proto.c_str(), port);
|
||||
bool addService(String service, String proto, uint16_t port){
|
||||
return addService(service.c_str(), proto.c_str(), port);
|
||||
}
|
||||
|
||||
bool addServiceTxt(char *name, char *proto, char * key, char * value);
|
||||
@ -84,7 +84,7 @@ public:
|
||||
void enableArduino(uint16_t port=3232, bool auth=false);
|
||||
void disableArduino();
|
||||
|
||||
void enableWorkstation(wifi_interface_t interface=ESP_IF_WIFI_STA);
|
||||
void enableWorkstation(wifi_interface_t interface=WIFI_IF_STA);
|
||||
void disableWorkstation();
|
||||
|
||||
IPAddress queryHost(char *host, uint32_t timeout=2000);
|
||||
|
@ -135,6 +135,21 @@ size_t F_Fat::totalBytes()
|
||||
return tot_sect * sect_size;
|
||||
}
|
||||
|
||||
size_t F_Fat::usedBytes()
|
||||
{
|
||||
FATFS *fs;
|
||||
DWORD free_clust, used_sect, sect_size;
|
||||
|
||||
BYTE pdrv = ff_diskio_get_pdrv_wl(_wl_handle);
|
||||
char drv[3] = {(char)(48+pdrv), ':', 0};
|
||||
if ( f_getfree(drv, &free_clust, &fs) != FR_OK){
|
||||
return 0;
|
||||
}
|
||||
used_sect = (fs->n_fatent - 2 - free_clust) * fs->csize;
|
||||
sect_size = CONFIG_WL_SECTOR_SIZE;
|
||||
return used_sect * sect_size;
|
||||
}
|
||||
|
||||
size_t F_Fat::freeBytes()
|
||||
{
|
||||
|
||||
|
@ -31,6 +31,7 @@ public:
|
||||
bool begin(bool formatOnFail=false, const char * basePath="/ffat", uint8_t maxOpenFiles=10, const char * partitionLabel = (char*)FFAT_PARTITION_LABEL);
|
||||
bool format(bool full_wipe = FFAT_WIPE_QUICK, char* partitionLabel = (char*)FFAT_PARTITION_LABEL);
|
||||
size_t totalBytes();
|
||||
size_t usedBytes();
|
||||
size_t freeBytes();
|
||||
void end();
|
||||
bool exists(const char* path);
|
||||
|
@ -184,6 +184,11 @@ bool VFSImpl::rmdir(const char *path)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strcmp(_mountpoint, "/spiffs") == 0) {
|
||||
log_e("rmdir is unnecessary in SPIFFS");
|
||||
return false;
|
||||
}
|
||||
|
||||
VFSFileImpl f(this, path, "r");
|
||||
if(!f || !f.isDirectory()) {
|
||||
if(f) {
|
||||
@ -200,7 +205,7 @@ bool VFSImpl::rmdir(const char *path)
|
||||
return false;
|
||||
}
|
||||
sprintf(temp,"%s%s", _mountpoint, path);
|
||||
auto rc = unlink(temp);
|
||||
auto rc = ::rmdir(temp);
|
||||
free(temp);
|
||||
return rc == 0;
|
||||
}
|
||||
|
@ -73,11 +73,15 @@ public:
|
||||
|
||||
bool verify(WiFiClient& client, const char* host) override
|
||||
{
|
||||
WiFiClientSecure& wcs = static_cast<WiFiClientSecure&>(client);
|
||||
wcs.setCACert(_cacert);
|
||||
wcs.setCertificate(_clicert);
|
||||
wcs.setPrivateKey(_clikey);
|
||||
return true;
|
||||
WiFiClientSecure& wcs = static_cast<WiFiClientSecure&>(client);
|
||||
if (_cacert == nullptr) {
|
||||
wcs.setInsecure();
|
||||
} else {
|
||||
wcs.setCACert(_cacert);
|
||||
wcs.setCertificate(_clicert);
|
||||
wcs.setPrivateKey(_clikey);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
@ -265,13 +269,20 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
|
||||
|
||||
// get port
|
||||
index = host.indexOf(':');
|
||||
String the_host;
|
||||
if(index >= 0) {
|
||||
_host = host.substring(0, index); // hostname
|
||||
the_host = host.substring(0, index); // hostname
|
||||
host.remove(0, (index + 1)); // remove hostname + :
|
||||
_port = host.toInt(); // get port
|
||||
} else {
|
||||
_host = host;
|
||||
the_host = host;
|
||||
}
|
||||
if(_host != the_host && connected()){
|
||||
log_d("switching host from '%s' to '%s'. disconnecting first", _host.c_str(), the_host.c_str());
|
||||
_canReuse = false;
|
||||
disconnect(true);
|
||||
}
|
||||
_host = the_host;
|
||||
_uri = url;
|
||||
log_d("host: %s port: %d url: %s", _host.c_str(), _port, _uri.c_str());
|
||||
return true;
|
||||
@ -548,29 +559,106 @@ int HTTPClient::sendRequest(const char * type, String payload)
|
||||
*/
|
||||
int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size)
|
||||
{
|
||||
// connect to server
|
||||
if(!connect()) {
|
||||
return returnError(HTTPC_ERROR_CONNECTION_REFUSED);
|
||||
}
|
||||
|
||||
if(payload && size > 0) {
|
||||
addHeader(F("Content-Length"), String(size));
|
||||
}
|
||||
|
||||
// send Header
|
||||
if(!sendHeader(type)) {
|
||||
return returnError(HTTPC_ERROR_SEND_HEADER_FAILED);
|
||||
}
|
||||
|
||||
// send Payload if needed
|
||||
if(payload && size > 0) {
|
||||
if(_client->write(&payload[0], size) != size) {
|
||||
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
|
||||
int code;
|
||||
bool redirect = false;
|
||||
uint16_t redirectCount = 0;
|
||||
do {
|
||||
// wipe out any existing headers from previous request
|
||||
for(size_t i = 0; i < _headerKeysCount; i++) {
|
||||
if (_currentHeaders[i].value.length() > 0) {
|
||||
_currentHeaders[i].value.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log_d("request type: '%s' redirCount: %d\n", type, redirectCount);
|
||||
|
||||
// connect to server
|
||||
if(!connect()) {
|
||||
return returnError(HTTPC_ERROR_CONNECTION_REFUSED);
|
||||
}
|
||||
|
||||
if(payload && size > 0) {
|
||||
addHeader(F("Content-Length"), String(size));
|
||||
}
|
||||
|
||||
// send Header
|
||||
if(!sendHeader(type)) {
|
||||
return returnError(HTTPC_ERROR_SEND_HEADER_FAILED);
|
||||
}
|
||||
|
||||
// send Payload if needed
|
||||
if(payload && size > 0) {
|
||||
if(_client->write(&payload[0], size) != size) {
|
||||
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
code = handleHeaderResponse();
|
||||
log_d("sendRequest code=%d\n", code);
|
||||
|
||||
// Handle redirections as stated in RFC document:
|
||||
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
|
||||
//
|
||||
// Implementing HTTP_CODE_FOUND as redirection with GET method,
|
||||
// to follow most of existing user agent implementations.
|
||||
//
|
||||
redirect = false;
|
||||
if (
|
||||
_followRedirects != HTTPC_DISABLE_FOLLOW_REDIRECTS &&
|
||||
redirectCount < _redirectLimit &&
|
||||
_location.length() > 0
|
||||
) {
|
||||
switch (code) {
|
||||
// redirecting using the same method
|
||||
case HTTP_CODE_MOVED_PERMANENTLY:
|
||||
case HTTP_CODE_TEMPORARY_REDIRECT: {
|
||||
if (
|
||||
// allow to force redirections on other methods
|
||||
// (the RFC require user to accept the redirection)
|
||||
_followRedirects == HTTPC_FORCE_FOLLOW_REDIRECTS ||
|
||||
// allow GET and HEAD methods without force
|
||||
!strcmp(type, "GET") ||
|
||||
!strcmp(type, "HEAD")
|
||||
) {
|
||||
redirectCount += 1;
|
||||
log_d("following redirect (the same method): '%s' redirCount: %d\n", _location.c_str(), redirectCount);
|
||||
if (!setURL(_location)) {
|
||||
log_d("failed setting URL for redirection\n");
|
||||
// no redirection
|
||||
break;
|
||||
}
|
||||
// redirect using the same request method and payload, diffrent URL
|
||||
redirect = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// redirecting with method dropped to GET or HEAD
|
||||
// note: it does not need `HTTPC_FORCE_FOLLOW_REDIRECTS` for any method
|
||||
case HTTP_CODE_FOUND:
|
||||
case HTTP_CODE_SEE_OTHER: {
|
||||
redirectCount += 1;
|
||||
log_d("following redirect (dropped to GET/HEAD): '%s' redirCount: %d\n", _location.c_str(), redirectCount);
|
||||
if (!setURL(_location)) {
|
||||
log_d("failed setting URL for redirection\n");
|
||||
// no redirection
|
||||
break;
|
||||
}
|
||||
// redirect after changing method to GET/HEAD and dropping payload
|
||||
type = "GET";
|
||||
payload = nullptr;
|
||||
size = 0;
|
||||
redirect = true;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} while (redirect);
|
||||
// handle Server Response (Header)
|
||||
return returnError(handleHeaderResponse());
|
||||
return returnError(code);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -842,18 +930,19 @@ int HTTPClient::writeToStream(Stream * stream)
|
||||
*/
|
||||
String HTTPClient::getString(void)
|
||||
{
|
||||
StreamString sstring;
|
||||
|
||||
if(_size) {
|
||||
// try to reserve needed memmory
|
||||
if(!sstring.reserve((_size + 1))) {
|
||||
// _size can be -1 when Server sends no Content-Length header
|
||||
if(_size > 0 || _size == -1) {
|
||||
StreamString sstring;
|
||||
// try to reserve needed memory (noop if _size == -1)
|
||||
if(sstring.reserve((_size + 1))) {
|
||||
writeToStream(&sstring);
|
||||
return sstring;
|
||||
} else {
|
||||
log_d("not enough memory to reserve a string! need: %d", (_size + 1));
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
writeToStream(&sstring);
|
||||
return sstring;
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1012,7 +1101,13 @@ bool HTTPClient::connect(void)
|
||||
log_d("HTTPClient::begin was not called or returned error");
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef HTTPCLIENT_1_1_COMPATIBLE
|
||||
if (_tcpDeprecated && !_transportTraits->verify(*_client, _host.c_str())) {
|
||||
log_d("transport level verify failed");
|
||||
_client->stop();
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
if(!_client->connect(_host.c_str(), _port, _connectTimeout)) {
|
||||
log_d("failed connect to %s:%u", _host.c_str(), _port);
|
||||
return false;
|
||||
@ -1023,14 +1118,6 @@ bool HTTPClient::connect(void)
|
||||
|
||||
log_d(" connected to %s:%u", _host.c_str(), _port);
|
||||
|
||||
#ifdef HTTPCLIENT_1_1_COMPATIBLE
|
||||
if (_tcpDeprecated && !_transportTraits->verify(*_client, _host.c_str())) {
|
||||
log_d("transport level verify failed");
|
||||
_client->stop();
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
#ifdef ESP8266
|
||||
@ -1110,6 +1197,7 @@ int HTTPClient::handleHeaderResponse()
|
||||
|
||||
_transferEncoding = HTTPC_TE_IDENTITY;
|
||||
unsigned long lastDataTime = millis();
|
||||
bool firstLine = true;
|
||||
|
||||
while(connected()) {
|
||||
size_t len = _client->available();
|
||||
@ -1121,11 +1209,13 @@ int HTTPClient::handleHeaderResponse()
|
||||
|
||||
log_v("RX: '%s'", headerLine.c_str());
|
||||
|
||||
if(headerLine.startsWith("HTTP/1.")) {
|
||||
if(_canReuse) {
|
||||
if(firstLine) {
|
||||
firstLine = false;
|
||||
if(_canReuse && headerLine.startsWith("HTTP/1.")) {
|
||||
_canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0');
|
||||
}
|
||||
_returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
|
||||
int codePos = headerLine.indexOf(' ') + 1;
|
||||
_returnCode = headerLine.substring(codePos, headerLine.indexOf(' ', codePos)).toInt();
|
||||
} else if(headerLine.indexOf(':')) {
|
||||
String headerName = headerLine.substring(0, headerLine.indexOf(':'));
|
||||
String headerValue = headerLine.substring(headerLine.indexOf(':') + 1);
|
||||
@ -1145,6 +1235,10 @@ int HTTPClient::handleHeaderResponse()
|
||||
transferEncoding = headerValue;
|
||||
}
|
||||
|
||||
if (headerName.equalsIgnoreCase("Location")) {
|
||||
_location = headerValue;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < _headerKeysCount; i++) {
|
||||
if(_currentHeaders[i].key.equalsIgnoreCase(headerName)) {
|
||||
_currentHeaders[i].value = headerValue;
|
||||
@ -1231,9 +1325,9 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
|
||||
readBytes = buff_size;
|
||||
}
|
||||
|
||||
// stop if no more reading
|
||||
if (readBytes == 0)
|
||||
break;
|
||||
// stop if no more reading
|
||||
if (readBytes == 0)
|
||||
break;
|
||||
|
||||
// read data
|
||||
int bytesRead = _client->readBytes(buff, readBytes);
|
||||
@ -1322,3 +1416,52 @@ int HTTPClient::returnError(int error)
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
void HTTPClient::setFollowRedirects(followRedirects_t follow)
|
||||
{
|
||||
_followRedirects = follow;
|
||||
}
|
||||
|
||||
void HTTPClient::setRedirectLimit(uint16_t limit)
|
||||
{
|
||||
_redirectLimit = limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the URL to a new value. Handy for following redirects.
|
||||
* @param url
|
||||
*/
|
||||
bool HTTPClient::setURL(const String& url)
|
||||
{
|
||||
// if the new location is only a path then only update the URI
|
||||
if (url && url[0] == '/') {
|
||||
_uri = url;
|
||||
clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!url.startsWith(_protocol + ':')) {
|
||||
log_d("new URL not the same protocol, expected '%s', URL: '%s'\n", _protocol.c_str(), url.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
// check if the port is specified
|
||||
int indexPort = url.indexOf(':', 6); // find the first ':' excluding the one from the protocol
|
||||
int indexURI = url.indexOf('/', 7); // find where the URI starts to make sure the ':' is not part of it
|
||||
if (indexPort == -1 || indexPort > indexURI) {
|
||||
// the port is not specified
|
||||
_port = (_protocol == "https" ? 443 : 80);
|
||||
}
|
||||
|
||||
// disconnect but preserve _client.
|
||||
// Also have to keep the connection otherwise it will free some of the memory used by _client
|
||||
// and will blow up later when trying to do _client->available() or similar
|
||||
_canReuse = true;
|
||||
disconnect(true);
|
||||
return beginInternal(url, _protocol.c_str());
|
||||
}
|
||||
|
||||
const String &HTTPClient::getLocation(void)
|
||||
{
|
||||
return _location;
|
||||
}
|
||||
|
@ -119,6 +119,24 @@ typedef enum {
|
||||
HTTPC_TE_CHUNKED
|
||||
} transferEncoding_t;
|
||||
|
||||
/**
|
||||
* redirection follow mode.
|
||||
* + `HTTPC_DISABLE_FOLLOW_REDIRECTS` - no redirection will be followed.
|
||||
* + `HTTPC_STRICT_FOLLOW_REDIRECTS` - strict RFC2616, only requests using
|
||||
* GET or HEAD methods will be redirected (using the same method),
|
||||
* since the RFC requires end-user confirmation in other cases.
|
||||
* + `HTTPC_FORCE_FOLLOW_REDIRECTS` - all redirections will be followed,
|
||||
* regardless of a used method. New request will use the same method,
|
||||
* and they will include the same body data and the same headers.
|
||||
* In the sense of the RFC, it's just like every redirection is confirmed.
|
||||
*/
|
||||
typedef enum {
|
||||
HTTPC_DISABLE_FOLLOW_REDIRECTS,
|
||||
HTTPC_STRICT_FOLLOW_REDIRECTS,
|
||||
HTTPC_FORCE_FOLLOW_REDIRECTS
|
||||
} followRedirects_t;
|
||||
|
||||
|
||||
#ifdef HTTPCLIENT_1_1_COMPATIBLE
|
||||
class TransportTraits;
|
||||
typedef std::unique_ptr<TransportTraits> TransportTraitsPtr;
|
||||
@ -156,6 +174,11 @@ public:
|
||||
void setConnectTimeout(int32_t connectTimeout);
|
||||
void setTimeout(uint16_t timeout);
|
||||
|
||||
// Redirections
|
||||
void setFollowRedirects(followRedirects_t follow);
|
||||
void setRedirectLimit(uint16_t limit); // max redirects to follow for a single request
|
||||
|
||||
bool setURL(const String &url);
|
||||
void useHTTP10(bool usehttp10 = true);
|
||||
|
||||
/// request handling
|
||||
@ -182,6 +205,7 @@ public:
|
||||
|
||||
|
||||
int getSize(void);
|
||||
const String &getLocation(void);
|
||||
|
||||
WiFiClient& getStream(void);
|
||||
WiFiClient* getStreamPtr(void);
|
||||
@ -235,6 +259,9 @@ protected:
|
||||
int _returnCode = 0;
|
||||
int _size = -1;
|
||||
bool _canReuse = false;
|
||||
followRedirects_t _followRedirects = HTTPC_DISABLE_FOLLOW_REDIRECTS;
|
||||
uint16_t _redirectLimit = 10;
|
||||
String _location;
|
||||
transferEncoding_t _transferEncoding = HTTPC_TE_IDENTITY;
|
||||
};
|
||||
|
||||
|
@ -52,7 +52,7 @@ void loop() {
|
||||
|
||||
t_httpUpdate_return ret = httpUpdate.update(client, "http://server/file.bin");
|
||||
// Or:
|
||||
//t_httpUpdate_return ret = httpUpdate.update(client, "server", 80, "file.bin");
|
||||
//t_httpUpdate_return ret = httpUpdate.update(client, "server", 80, "/file.bin");
|
||||
|
||||
switch (ret) {
|
||||
case HTTP_UPDATE_FAILED:
|
||||
|
@ -108,7 +108,7 @@ void loop() {
|
||||
|
||||
t_httpUpdate_return ret = httpUpdate.update(client, "https://server/file.bin");
|
||||
// Or:
|
||||
//t_httpUpdate_return ret = httpUpdate.update(client, "server", 443, "file.bin");
|
||||
//t_httpUpdate_return ret = httpUpdate.update(client, "server", 443, "/file.bin");
|
||||
|
||||
|
||||
switch (ret) {
|
||||
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
To upload through terminal you can use: curl -F "image=@firmware.bin" esp32-webupdate.local/update
|
||||
*/
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <WebServer.h>
|
||||
#include <ESPmDNS.h>
|
||||
#include <HTTPUpdateServer.h>
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "your-ssid"
|
||||
#define STAPSK "your-password"
|
||||
#endif
|
||||
|
||||
const char* host = "esp32-webupdate";
|
||||
const char* ssid = STASSID;
|
||||
const char* password = STAPSK;
|
||||
|
||||
WebServer httpServer(80);
|
||||
HTTPUpdateServer httpUpdater;
|
||||
|
||||
void setup(void) {
|
||||
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
Serial.println("Booting Sketch...");
|
||||
WiFi.mode(WIFI_AP_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
||||
WiFi.begin(ssid, password);
|
||||
Serial.println("WiFi failed, retrying.");
|
||||
}
|
||||
|
||||
MDNS.begin(host);
|
||||
if (MDNS.begin("esp32")) {
|
||||
Serial.println("mDNS responder started");
|
||||
}
|
||||
|
||||
|
||||
httpUpdater.setup(&httpServer);
|
||||
httpServer.begin();
|
||||
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
Serial.printf("HTTPUpdateServer ready! Open http://%s.local/update in your browser\n", host);
|
||||
}
|
||||
|
||||
void loop(void) {
|
||||
httpServer.handleClient();
|
||||
}
|
20
libraries/HTTPUpdateServer/keywords.txt
Normal file
20
libraries/HTTPUpdateServer/keywords.txt
Normal file
@ -0,0 +1,20 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For HTTPUpdateServer
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
ESP32HTTPUpdateServer KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
begin KEYWORD2
|
||||
setup KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
9
libraries/HTTPUpdateServer/library.properties
Normal file
9
libraries/HTTPUpdateServer/library.properties
Normal file
@ -0,0 +1,9 @@
|
||||
name=HTTPUpdateServer
|
||||
version=1.0
|
||||
author=Hristo Kapanakov
|
||||
maintainer=
|
||||
sentence=Simple HTTP Update server based on the WebServer
|
||||
paragraph=The library accepts HTTP post requests to the /update url, and updates the ESP32 firmware.
|
||||
category=Communication
|
||||
url=
|
||||
architectures=esp32
|
166
libraries/HTTPUpdateServer/src/HTTPUpdateServer.h
Normal file
166
libraries/HTTPUpdateServer/src/HTTPUpdateServer.h
Normal file
@ -0,0 +1,166 @@
|
||||
#ifndef __HTTP_UPDATE_SERVER_H
|
||||
#define __HTTP_UPDATE_SERVER_H
|
||||
|
||||
#include<SPIFFS.h>
|
||||
#include <StreamString.h>
|
||||
#include <Update.h>
|
||||
#include <WebServer.h>
|
||||
|
||||
|
||||
static const char serverIndex[] PROGMEM =
|
||||
R"(<!DOCTYPE html>
|
||||
<html lang='en'>
|
||||
<head>
|
||||
<meta charset='utf-8'>
|
||||
<meta name='viewport' content='width=device-width,initial-scale=1'/>
|
||||
</head>
|
||||
<body>
|
||||
<form method='POST' action='' enctype='multipart/form-data'>
|
||||
Firmware:<br>
|
||||
<input type='file' accept='.bin,.bin.gz' name='firmware'>
|
||||
<input type='submit' value='Update Firmware'>
|
||||
</form>
|
||||
<form method='POST' action='' enctype='multipart/form-data'>
|
||||
FileSystem:<br>
|
||||
<input type='file' accept='.bin,.bin.gz,.image' name='filesystem'>
|
||||
<input type='submit' value='Update FileSystem'>
|
||||
</form>
|
||||
</body>
|
||||
</html>)";
|
||||
static const char successResponse[] PROGMEM =
|
||||
"<META http-equiv=\"refresh\" content=\"15;URL=/\">Update Success! Rebooting...";
|
||||
|
||||
class HTTPUpdateServer
|
||||
{
|
||||
public:
|
||||
HTTPUpdateServer(bool serial_debug=false) {
|
||||
_serial_output = serial_debug;
|
||||
_server = NULL;
|
||||
_username = emptyString;
|
||||
_password = emptyString;
|
||||
_authenticated = false;
|
||||
}
|
||||
|
||||
void setup(WebServer *server)
|
||||
{
|
||||
setup(server, emptyString, emptyString);
|
||||
}
|
||||
|
||||
void setup(WebServer *server, const String& path)
|
||||
{
|
||||
setup(server, path, emptyString, emptyString);
|
||||
}
|
||||
|
||||
void setup(WebServer *server, const String& username, const String& password)
|
||||
{
|
||||
setup(server, "/update", username, password);
|
||||
}
|
||||
|
||||
void setup(WebServer *server, const String& path, const String& username, const String& password)
|
||||
{
|
||||
|
||||
_server = server;
|
||||
_username = username;
|
||||
_password = password;
|
||||
|
||||
// handler for the /update form page
|
||||
_server->on(path.c_str(), HTTP_GET, [&]() {
|
||||
if (_username != emptyString && _password != emptyString && !_server->authenticate(_username.c_str(), _password.c_str()))
|
||||
return _server->requestAuthentication();
|
||||
_server->send_P(200, PSTR("text/html"), serverIndex);
|
||||
});
|
||||
|
||||
// handler for the /update form POST (once file upload finishes)
|
||||
_server->on(path.c_str(), HTTP_POST, [&]() {
|
||||
if (!_authenticated)
|
||||
return _server->requestAuthentication();
|
||||
if (Update.hasError()) {
|
||||
_server->send(200, F("text/html"), String(F("Update error: ")) + _updaterError);
|
||||
}
|
||||
else {
|
||||
_server->client().setNoDelay(true);
|
||||
_server->send_P(200, PSTR("text/html"), successResponse);
|
||||
delay(100);
|
||||
_server->client().stop();
|
||||
ESP.restart();
|
||||
}
|
||||
}, [&]() {
|
||||
// handler for the file upload, get's the sketch bytes, and writes
|
||||
// them through the Update object
|
||||
HTTPUpload& upload = _server->upload();
|
||||
|
||||
if (upload.status == UPLOAD_FILE_START) {
|
||||
_updaterError.clear();
|
||||
if (_serial_output)
|
||||
Serial.setDebugOutput(true);
|
||||
|
||||
_authenticated = (_username == emptyString || _password == emptyString || _server->authenticate(_username.c_str(), _password.c_str()));
|
||||
if (!_authenticated) {
|
||||
if (_serial_output)
|
||||
Serial.printf("Unauthenticated Update\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_serial_output)
|
||||
Serial.printf("Update: %s\n", upload.filename.c_str());
|
||||
if (upload.name == "filesystem") {
|
||||
if (!Update.begin(SPIFFS.totalBytes(), U_SPIFFS)) {//start with max available size
|
||||
if (_serial_output) Update.printError(Serial);
|
||||
}
|
||||
}
|
||||
else {
|
||||
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
|
||||
if (!Update.begin(maxSketchSpace, U_FLASH)) {//start with max available size
|
||||
_setUpdaterError();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (_authenticated && upload.status == UPLOAD_FILE_WRITE && !_updaterError.length()) {
|
||||
if (_serial_output) Serial.printf(".");
|
||||
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
|
||||
_setUpdaterError();
|
||||
}
|
||||
}
|
||||
else if (_authenticated && upload.status == UPLOAD_FILE_END && !_updaterError.length()) {
|
||||
if (Update.end(true)) { //true to set the size to the current progress
|
||||
if (_serial_output) Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
|
||||
}
|
||||
else {
|
||||
_setUpdaterError();
|
||||
}
|
||||
if (_serial_output) Serial.setDebugOutput(false);
|
||||
}
|
||||
else if (_authenticated && upload.status == UPLOAD_FILE_ABORTED) {
|
||||
Update.end();
|
||||
if (_serial_output) Serial.println("Update was aborted");
|
||||
}
|
||||
delay(0);
|
||||
});
|
||||
}
|
||||
|
||||
void updateCredentials(const String& username, const String& password)
|
||||
{
|
||||
_username = username;
|
||||
_password = password;
|
||||
}
|
||||
|
||||
protected:
|
||||
void _setUpdaterError()
|
||||
{
|
||||
if (_serial_output) Update.printError(Serial);
|
||||
StreamString str;
|
||||
Update.printError(str);
|
||||
_updaterError = str.c_str();
|
||||
}
|
||||
|
||||
private:
|
||||
bool _serial_output;
|
||||
WebServer *_server;
|
||||
String _username;
|
||||
String _password;
|
||||
bool _authenticated;
|
||||
String _updaterError;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
@ -14,8 +14,9 @@
|
||||
#include "Preferences.h"
|
||||
|
||||
#include "nvs.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
const char * nvs_errors[] = { "OTHER", "NOT_INITIALIZED", "NOT_FOUND", "TYPE_MISMATCH", "READ_ONLY", "NOT_ENOUGH_SPACE", "INVALID_NAME", "INVALID_HANDLE", "REMOVE_FAILED", "KEY_TOO_LONG", "PAGE_FULL", "INVALID_STATE", "INVALID_LENGHT"};
|
||||
const char * nvs_errors[] = { "OTHER", "NOT_INITIALIZED", "NOT_FOUND", "TYPE_MISMATCH", "READ_ONLY", "NOT_ENOUGH_SPACE", "INVALID_NAME", "INVALID_HANDLE", "REMOVE_FAILED", "KEY_TOO_LONG", "PAGE_FULL", "INVALID_STATE", "INVALID_LENGTH"};
|
||||
#define nvs_error(e) (((e)>ESP_ERR_NVS_BASE)?nvs_errors[(e)&~(ESP_ERR_NVS_BASE)]:nvs_errors[0])
|
||||
|
||||
Preferences::Preferences()
|
||||
@ -28,12 +29,22 @@ Preferences::~Preferences(){
|
||||
end();
|
||||
}
|
||||
|
||||
bool Preferences::begin(const char * name, bool readOnly){
|
||||
bool Preferences::begin(const char * name, bool readOnly, const char* partition_label){
|
||||
if(_started){
|
||||
return false;
|
||||
}
|
||||
_readOnly = readOnly;
|
||||
esp_err_t err = nvs_open(name, readOnly?NVS_READONLY:NVS_READWRITE, &_handle);
|
||||
esp_err_t err = ESP_OK;
|
||||
if (partition_label != NULL) {
|
||||
err = nvs_flash_init_partition(partition_label);
|
||||
if (err) {
|
||||
log_e("nvs_flash_init_partition failed: %s", nvs_error(err));
|
||||
return false;
|
||||
}
|
||||
err = nvs_open_from_partition(partition_label, name, readOnly ? NVS_READONLY : NVS_READWRITE, &_handle);
|
||||
} else {
|
||||
err = nvs_open(name, readOnly?NVS_READONLY:NVS_READWRITE, &_handle);
|
||||
}
|
||||
if(err){
|
||||
log_e("nvs_open failed: %s", nvs_error(err));
|
||||
return false;
|
||||
@ -280,6 +291,30 @@ size_t Preferences::putBytes(const char* key, const void* value, size_t len){
|
||||
return len;
|
||||
}
|
||||
|
||||
PreferenceType Preferences::getType(const char* key) {
|
||||
if(!_started || !key || strlen(key)>15){
|
||||
return PT_INVALID;
|
||||
}
|
||||
int8_t mt1; uint8_t mt2; int16_t mt3; uint16_t mt4;
|
||||
int32_t mt5; uint32_t mt6; int64_t mt7; uint64_t mt8;
|
||||
size_t len = 0;
|
||||
if(nvs_get_i8(_handle, key, &mt1) == ESP_OK) return PT_I8;
|
||||
if(nvs_get_u8(_handle, key, &mt2) == ESP_OK) return PT_U8;
|
||||
if(nvs_get_i16(_handle, key, &mt3) == ESP_OK) return PT_I16;
|
||||
if(nvs_get_u16(_handle, key, &mt4) == ESP_OK) return PT_U16;
|
||||
if(nvs_get_i32(_handle, key, &mt5) == ESP_OK) return PT_I32;
|
||||
if(nvs_get_u32(_handle, key, &mt6) == ESP_OK) return PT_U32;
|
||||
if(nvs_get_i64(_handle, key, &mt7) == ESP_OK) return PT_I64;
|
||||
if(nvs_get_u64(_handle, key, &mt8) == ESP_OK) return PT_U64;
|
||||
if(nvs_get_str(_handle, key, NULL, &len) == ESP_OK) return PT_STR;
|
||||
if(nvs_get_blob(_handle, key, NULL, &len) == ESP_OK) return PT_BLOB;
|
||||
return PT_INVALID;
|
||||
}
|
||||
|
||||
bool Preferences::isKey(const char* key) {
|
||||
return getType(key) != PT_INVALID;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get a key value
|
||||
* */
|
||||
|
@ -16,6 +16,10 @@
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
typedef enum {
|
||||
PT_I8, PT_U8, PT_I16, PT_U16, PT_I32, PT_U32, PT_I64, PT_U64, PT_STR, PT_BLOB, PT_INVALID
|
||||
} PreferenceType;
|
||||
|
||||
class Preferences {
|
||||
protected:
|
||||
uint32_t _handle;
|
||||
@ -25,7 +29,7 @@ class Preferences {
|
||||
Preferences();
|
||||
~Preferences();
|
||||
|
||||
bool begin(const char * name, bool readOnly=false);
|
||||
bool begin(const char * name, bool readOnly=false, const char* partition_label=NULL);
|
||||
void end();
|
||||
|
||||
bool clear();
|
||||
@ -48,6 +52,8 @@ class Preferences {
|
||||
size_t putString(const char* key, String value);
|
||||
size_t putBytes(const char* key, const void* value, size_t len);
|
||||
|
||||
bool isKey(const char* key);
|
||||
PreferenceType getType(const char* key);
|
||||
int8_t getChar(const char* key, int8_t defaultValue = 0);
|
||||
uint8_t getUChar(const char* key, uint8_t defaultValue = 0);
|
||||
int16_t getShort(const char* key, int16_t defaultValue = 0);
|
||||
@ -63,7 +69,7 @@ class Preferences {
|
||||
bool getBool(const char* key, bool defaultValue = false);
|
||||
size_t getString(const char* key, char* value, size_t maxLen);
|
||||
String getString(const char* key, String defaultValue = String());
|
||||
size_t getBytesLength(const char* key);
|
||||
size_t getBytesLength(const char* key);
|
||||
size_t getBytes(const char* key, void * buf, size_t maxLen);
|
||||
size_t freeEntries();
|
||||
};
|
||||
|
@ -607,8 +607,9 @@ DRESULT ff_sd_write(uint8_t pdrv, const uint8_t* buffer, DWORD sector, UINT coun
|
||||
|
||||
if (count > 1) {
|
||||
res = sdWriteSectors(pdrv, (const char*)buffer, sector, count) ? RES_OK : RES_ERROR;
|
||||
} else {
|
||||
res = sdWriteSector(pdrv, (const char*)buffer, sector) ? RES_OK : RES_ERROR;
|
||||
}
|
||||
res = sdWriteSector(pdrv, (const char*)buffer, sector) ? RES_OK : RES_ERROR;
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -648,6 +649,7 @@ uint8_t sdcard_uninit(uint8_t pdrv)
|
||||
if (pdrv >= FF_VOLUMES || card == NULL) {
|
||||
return 1;
|
||||
}
|
||||
sdTransaction(pdrv, GO_IDLE_STATE, 0, NULL);
|
||||
ff_diskio_register(pdrv, NULL);
|
||||
s_cards[pdrv] = NULL;
|
||||
esp_err_t err = ESP_OK;
|
||||
|
@ -85,7 +85,7 @@ void SPIClass::setHwCs(bool use)
|
||||
if(use && !_use_hw_ss) {
|
||||
spiAttachSS(_spi, 0, _ss);
|
||||
spiSSEnable(_spi);
|
||||
} else if(_use_hw_ss) {
|
||||
} else if(!use && _use_hw_ss) {
|
||||
spiSSDisable(_spi);
|
||||
spiDetachSS(_spi, _ss);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
SPI.h - SPI library for esp8266
|
||||
SPI.h - SPI library for esp32
|
||||
|
||||
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
@ -25,6 +25,8 @@
|
||||
#include "pins_arduino.h"
|
||||
#include "esp32-hal-spi.h"
|
||||
|
||||
#define SPI_HAS_TRANSACTION
|
||||
|
||||
class SPISettings
|
||||
{
|
||||
public:
|
||||
|
@ -43,21 +43,38 @@ bool SPIFFSImpl::exists(const char* path)
|
||||
return (f == true) && !f.isDirectory();
|
||||
}
|
||||
|
||||
SPIFFSFS::SPIFFSFS() : FS(FSImplPtr(new SPIFFSImpl()))
|
||||
SPIFFSFS::SPIFFSFS() : FS(FSImplPtr(new SPIFFSImpl())), partitionLabel_(NULL)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles)
|
||||
SPIFFSFS::~SPIFFSFS()
|
||||
{
|
||||
if(esp_spiffs_mounted(NULL)){
|
||||
if (partitionLabel_){
|
||||
free(partitionLabel_);
|
||||
partitionLabel_ = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles, const char * partitionLabel)
|
||||
{
|
||||
if (partitionLabel_){
|
||||
free(partitionLabel_);
|
||||
partitionLabel_ = NULL;
|
||||
}
|
||||
|
||||
if (partitionLabel){
|
||||
partitionLabel_ = strdup(partitionLabel);
|
||||
}
|
||||
|
||||
if(esp_spiffs_mounted(partitionLabel_)){
|
||||
log_w("SPIFFS Already Mounted!");
|
||||
return true;
|
||||
}
|
||||
|
||||
esp_vfs_spiffs_conf_t conf = {
|
||||
.base_path = basePath,
|
||||
.partition_label = NULL,
|
||||
.partition_label = partitionLabel_,
|
||||
.max_files = maxOpenFiles,
|
||||
.format_if_mount_failed = false
|
||||
};
|
||||
@ -78,8 +95,8 @@ bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFi
|
||||
|
||||
void SPIFFSFS::end()
|
||||
{
|
||||
if(esp_spiffs_mounted(NULL)){
|
||||
esp_err_t err = esp_vfs_spiffs_unregister(NULL);
|
||||
if(esp_spiffs_mounted(partitionLabel_)){
|
||||
esp_err_t err = esp_vfs_spiffs_unregister(partitionLabel_);
|
||||
if(err){
|
||||
log_e("Unmounting SPIFFS failed! Error: %d", err);
|
||||
return;
|
||||
@ -91,7 +108,7 @@ void SPIFFSFS::end()
|
||||
bool SPIFFSFS::format()
|
||||
{
|
||||
disableCore0WDT();
|
||||
esp_err_t err = esp_spiffs_format(NULL);
|
||||
esp_err_t err = esp_spiffs_format(partitionLabel_);
|
||||
enableCore0WDT();
|
||||
if(err){
|
||||
log_e("Formatting SPIFFS failed! Error: %d", err);
|
||||
@ -103,7 +120,7 @@ bool SPIFFSFS::format()
|
||||
size_t SPIFFSFS::totalBytes()
|
||||
{
|
||||
size_t total,used;
|
||||
if(esp_spiffs_info(NULL, &total, &used)){
|
||||
if(esp_spiffs_info(partitionLabel_, &total, &used)){
|
||||
return 0;
|
||||
}
|
||||
return total;
|
||||
@ -112,7 +129,7 @@ size_t SPIFFSFS::totalBytes()
|
||||
size_t SPIFFSFS::usedBytes()
|
||||
{
|
||||
size_t total,used;
|
||||
if(esp_spiffs_info(NULL, &total, &used)){
|
||||
if(esp_spiffs_info(partitionLabel_, &total, &used)){
|
||||
return 0;
|
||||
}
|
||||
return used;
|
||||
|
@ -23,11 +23,15 @@ class SPIFFSFS : public FS
|
||||
{
|
||||
public:
|
||||
SPIFFSFS();
|
||||
bool begin(bool formatOnFail=false, const char * basePath="/spiffs", uint8_t maxOpenFiles=10);
|
||||
~SPIFFSFS();
|
||||
bool begin(bool formatOnFail=false, const char * basePath="/spiffs", uint8_t maxOpenFiles=10, const char * partitionLabel=NULL);
|
||||
bool format();
|
||||
size_t totalBytes();
|
||||
size_t usedBytes();
|
||||
void end();
|
||||
|
||||
private:
|
||||
char * partitionLabel_;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,98 @@
|
||||
// This sketch provide the functionality of OTA Firmware Upgrade
|
||||
#include "WiFi.h"
|
||||
#include "HttpsOTAUpdate.h"
|
||||
// This sketch shows how to implement HTTPS firmware update Over The Air.
|
||||
// Please provide your WiFi credentials, https URL to the firmware image and the server certificate.
|
||||
|
||||
static const char *ssid = "your-ssid"; // your network SSID (name of wifi network)
|
||||
static const char *password = "your-password"; // your network password
|
||||
|
||||
static const char *url = "https://example.com/firmware.bin"; //state url of your firmware image
|
||||
|
||||
static const char *server_certificate = "-----BEGIN CERTIFICATE-----\n" \
|
||||
"MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" \
|
||||
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \
|
||||
"DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" \
|
||||
"SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" \
|
||||
"GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" \
|
||||
"AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" \
|
||||
"q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" \
|
||||
"SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" \
|
||||
"Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n" \
|
||||
"a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n" \
|
||||
"/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n" \
|
||||
"AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n" \
|
||||
"CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n" \
|
||||
"bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n" \
|
||||
"c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n" \
|
||||
"VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n" \
|
||||
"ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n" \
|
||||
"MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n" \
|
||||
"Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n" \
|
||||
"AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n" \
|
||||
"uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n" \
|
||||
"wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n" \
|
||||
"X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" \
|
||||
"PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" \
|
||||
"KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" \
|
||||
"-----END CERTIFICATE-----";
|
||||
|
||||
static HttpsOTAStatus_t otastatus;
|
||||
|
||||
void HttpEvent(HttpEvent_t *event)
|
||||
{
|
||||
switch(event->event_id) {
|
||||
case HTTP_EVENT_ERROR:
|
||||
Serial.println("Http Event Error");
|
||||
break;
|
||||
case HTTP_EVENT_ON_CONNECTED:
|
||||
Serial.println("Http Event On Connected");
|
||||
break;
|
||||
case HTTP_EVENT_HEADER_SENT:
|
||||
Serial.println("Http Event Header Sent");
|
||||
break;
|
||||
case HTTP_EVENT_ON_HEADER:
|
||||
Serial.printf("Http Event On Header, key=%s, value=%s\n", event->header_key, event->header_value);
|
||||
break;
|
||||
case HTTP_EVENT_ON_DATA:
|
||||
break;
|
||||
case HTTP_EVENT_ON_FINISH:
|
||||
Serial.println("Http Event On Finish");
|
||||
break;
|
||||
case HTTP_EVENT_DISCONNECTED:
|
||||
Serial.println("Http Event Disconnected");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void setup(){
|
||||
|
||||
Serial.begin(115200);
|
||||
Serial.print("Attempting to connect to SSID: ");
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
// attempt to connect to Wifi network:
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.print(".");
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
Serial.print("Connected to ");
|
||||
Serial.println(ssid);
|
||||
|
||||
HttpsOTA.onHttpEvent(HttpEvent);
|
||||
Serial.println("Starting OTA");
|
||||
HttpsOTA.begin(url, server_certificate);
|
||||
|
||||
Serial.println("Please Wait it takes some time ...");
|
||||
}
|
||||
|
||||
void loop(){
|
||||
otastatus = HttpsOTA.status();
|
||||
if(otastatus == HTTPS_OTA_SUCCESS) {
|
||||
Serial.println("Firmware written successfully. To reboot device, call API ESP.restart() or PUSH restart button on device");
|
||||
} else if(otastatus == HTTPS_OTA_FAIL) {
|
||||
Serial.println("Firmware Upgrade Fail");
|
||||
}
|
||||
delay(1000);
|
||||
}
|
32
libraries/Update/examples/HTTPS_OTA_Update/Readme.md
Normal file
32
libraries/Update/examples/HTTPS_OTA_Update/Readme.md
Normal file
@ -0,0 +1,32 @@
|
||||
# OTA Firmware Upgrade for Arduino
|
||||
This sketch allows Arduino user to perform Over The Air (OTA) firmware upgrade. It uses HTTPS.
|
||||
|
||||
# API introduced for OTA
|
||||
|
||||
## HttpsOTA.begin(const char * url, const char * server_certificate, bool skip_cert_common_name_check)
|
||||
|
||||
Main API which starts firmware upgrade
|
||||
|
||||
### Parameters
|
||||
* url : URL for the uploaded firmware image
|
||||
* server_certificate : Provide the ota server certificate for authentication via HTTPS
|
||||
* skip_cert_common_name_check : Skip any validation of server certificate CN field
|
||||
|
||||
The default value provided to skip_cert_common_name_check is true
|
||||
|
||||
## HttpsOTA.onHttpEvent(function)
|
||||
|
||||
This API exposes HTTP Events to the user
|
||||
|
||||
### Parameter
|
||||
Function passed has following signature
|
||||
void HttpEvent (HttpEvent_t * event);
|
||||
|
||||
# HttpsOTA.otaStatus()
|
||||
|
||||
It tracks the progress of OTA firmware upgrade.
|
||||
* HTTPS_OTA_IDLE : OTA upgrade have not started yet.
|
||||
* HTTPS_OTA_UPDATNG : OTA upgarde is in progress.
|
||||
* HTTPS_OTA_SUCCESS : OTA upgrade is successful.
|
||||
* HTTPS_OTA_FAIL : OTA upgrade failed.
|
||||
* HTTPS_OTA_ERR : Error occured while creating xEventGroup().
|
107
libraries/Update/src/HttpsOTAUpdate.cpp
Normal file
107
libraries/Update/src/HttpsOTAUpdate.cpp
Normal file
@ -0,0 +1,107 @@
|
||||
/* OTA task
|
||||
|
||||
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 <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include "esp32-hal-log.h"
|
||||
#include "esp_http_client.h"
|
||||
#include "esp_https_ota.h"
|
||||
|
||||
#include "HttpsOTAUpdate.h"
|
||||
#include "Esp.h"
|
||||
#define OTA_TASK_STACK_SIZE 9216
|
||||
|
||||
typedef void (*HttpEventCb)(HttpEvent_t*);
|
||||
|
||||
static esp_http_client_config_t config;
|
||||
static HttpEventCb cb;
|
||||
static EventGroupHandle_t ota_status = NULL;//check for ota status
|
||||
static EventBits_t set_bit;
|
||||
|
||||
const int OTA_IDLE_BIT = BIT0;
|
||||
const int OTA_UPDATING_BIT = BIT1;
|
||||
const int OTA_SUCCESS_BIT = BIT2;
|
||||
const int OTA_FAIL_BIT = BIT3;
|
||||
|
||||
esp_err_t http_event_handler(esp_http_client_event_t *event)
|
||||
{
|
||||
cb(event);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void https_ota_task(void *param)
|
||||
{
|
||||
if(ota_status) {
|
||||
xEventGroupSetBits(ota_status, OTA_UPDATING_BIT);
|
||||
xEventGroupClearBits(ota_status, OTA_IDLE_BIT);
|
||||
}
|
||||
esp_err_t ret = esp_https_ota((const esp_http_client_config_t *)param);
|
||||
if(ret == ESP_OK) {
|
||||
if(ota_status) {
|
||||
xEventGroupClearBits(ota_status, OTA_UPDATING_BIT);
|
||||
xEventGroupSetBits(ota_status, OTA_SUCCESS_BIT);
|
||||
}
|
||||
} else {
|
||||
if(ota_status) {
|
||||
xEventGroupClearBits(ota_status, OTA_UPDATING_BIT);
|
||||
xEventGroupSetBits(ota_status, OTA_FAIL_BIT);
|
||||
}
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
HttpsOTAStatus_t HttpsOTAUpdateClass::status()
|
||||
{
|
||||
if(ota_status) {
|
||||
set_bit = xEventGroupGetBits(ota_status);
|
||||
if(set_bit == OTA_IDLE_BIT) {
|
||||
return HTTPS_OTA_IDLE;
|
||||
}
|
||||
if(set_bit == OTA_UPDATING_BIT) {
|
||||
return HTTPS_OTA_UPDATING;
|
||||
}
|
||||
if(set_bit == OTA_SUCCESS_BIT) {
|
||||
return HTTPS_OTA_SUCCESS;
|
||||
}
|
||||
if(set_bit == OTA_FAIL_BIT) {
|
||||
return HTTPS_OTA_FAIL;
|
||||
}
|
||||
}
|
||||
return HTTPS_OTA_ERR;
|
||||
}
|
||||
|
||||
void HttpsOTAUpdateClass::onHttpEvent(HttpEventCb cbEvent)
|
||||
{
|
||||
cb = cbEvent;
|
||||
}
|
||||
|
||||
void HttpsOTAUpdateClass::begin(const char *url, const char *cert_pem, bool skip_cert_common_name_check)
|
||||
{
|
||||
config.url = url;
|
||||
config.cert_pem = cert_pem;
|
||||
config.skip_cert_common_name_check = skip_cert_common_name_check;
|
||||
config.event_handler = http_event_handler;
|
||||
|
||||
if(!ota_status) {
|
||||
ota_status = xEventGroupCreate();
|
||||
if(!ota_status) {
|
||||
log_e("OTA Event Group Create Failed");
|
||||
}
|
||||
xEventGroupSetBits(ota_status, OTA_IDLE_BIT);
|
||||
}
|
||||
|
||||
if (xTaskCreate(&https_ota_task, "https_ota_task", OTA_TASK_STACK_SIZE, &config, 5, NULL) != pdPASS) {
|
||||
log_e("Couldn't create ota task\n");
|
||||
}
|
||||
}
|
||||
|
||||
HttpsOTAUpdateClass HttpsOTA;
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user