Compare commits

...

140 Commits

Author SHA1 Message Date
6d256b6454 IDF release/v3.3 68b237fe5 with Disable IRAM optimisation for WiFi 2020-11-16 13:06:50 +02:00
f6bf0f7aa2 IDF release/v3.3 68b237fe5 2020-11-16 12:44:07 +02:00
a59eb5d51e Merge branch 'master' into idf-release/v3.3 2020-11-16 12:09:23 +02:00
adafd9d7c0 Merge pull request #4538 from loick111/feature/custom_variants_dir
The goal is to allow custom configuration for `variants_dir` to define our own board variant directly in the project.

With this functionnality, we are now allowed to define in our projects a custom board **AND** a custom variant.
https://docs.platformio.org/en/latest/platforms/creating_board.html#custom-embedded-boards

Here is an example of how to define a custom board with custom variant:
```
my_project
├── boards
│   └── custom_esp32dev.json
└── variants
    └── custom-esp32dev
        └── pins_arduino.h
```

custom_esp32dev.json
```json
{
  "build": {
    "arduino":{
      "ldscript": "esp32_out.ld"
    },
    "core": "esp32",
    "extra_flags": "-DARDUINO_ESP32_DEV",
    "f_cpu": "240000000L",
    "f_flash": "40000000L",
    "flash_mode": "dio",
    "mcu": "esp32",
    "variants_dir": "variants",
    "variant": "custom-esp32dev"
  },
  "connectivity": [
    "wifi",
    "bluetooth",
    "ethernet",
    "can"
  ],
  "debug": {
    "openocd_board": "esp-wroom-32.cfg"
  },
  "frameworks": [
    "arduino",
    "espidf"
  ],
  "name": "My Custom Espressif ESP32 Dev Module",
  "upload": {
    "flash_size": "4MB",
    "maximum_ram_size": 327680,
    "maximum_size": 4194304,
    "require_upload_port": true,
    "speed": 460800
  },
  "url": "https://en.wikipedia.org/wiki/ESP32",
  "vendor": "Espressif"
}
```
2020-11-15 20:43:43 +02:00
ac9fdeffe4 Allow custom variants directory 2020-11-15 16:20:02 +01:00
cee7b4237c Merge branch 'master' into idf-release/v3.3 2020-11-15 12:02:51 +02:00
954df2fc3e Disable IRAM optimization for WiFi 2020-11-15 11:56:01 +02:00
e41fb08b2a Update esptool to work on BigSur 2020-11-15 08:53:07 +02:00
d8b1fc81c0 Added usedBytes to match other filesystems (#4534) 2020-11-15 08:48:21 +02:00
378b6ac032 Fix issue in webserver with Chrome based browsers
https://github.com/espressif/arduino-esp32/issues/3652
2020-11-15 08:46:23 +02:00
cecef8e930 IDF release/v3.3 68b237fe5 2020-11-14 00:33:27 +00:00
a8e99baeab Merge branch 'master' into idf-release/v3.3 2020-11-12 21:29:57 +02:00
b6cc108d49 Update WiFiProv.cpp (#4519)
Do not pollute the global namespace with generic names like 'config' by declaring global variables 'static'.
2020-11-10 20:51:10 +02:00
8816bb5505 Added #define LED_BUILTIN to all pins_arduino.h that need it. (#4520) 2020-11-10 20:50:35 +02:00
3274602eb0 Notify the batteryLevel change (#4517) 2020-11-10 13:02:00 +02:00
534f0810a6 fix bitWrite macro (#4507)
Fixes https://github.com/espressif/arduino-esp32/issues/4466
2020-11-09 17:08:13 +02:00
28a8073069 Fix issue 4095 (#4503)
pgmspace.h missing 'pgm_get_far_address'
2020-11-08 04:55:59 +02:00
7494c4e76d IDF release/v3.3 44ec7972b 2020-11-08 00:36:32 +00:00
486a4c66c4 SDCARD: First sector always written twice if multiple sectors are updated 2020-11-06 22:20:20 +02:00
c1951670d1 IDF release/v3.3 66d3783c8 2020-11-06 18:15:23 +00:00
ad07d36932 Update README.md 2020-11-06 17:21:12 +02:00
c6a8da61f7 Allow faster reuse of socket, to be able to restart WifiServer. (#4306)
See #3960 for more details of the problem and the solution. I only implemented what was proposed in this ticket, as it solves my problem, which was the same as in this ticket. Credits for the code going to @etrinh ;-)

This also is a more consistence behaviour compared to esp8266, where it also is possible to restart the wifiserver immediately on the same port.
2020-11-06 14:16:50 +02:00
dd1a15478f add TimerCAM and CoreInk board (#4498) 2020-11-06 13:30:57 +02:00
bcb7012a32 Change variants folder T-Beam (#4496) 2020-11-06 12:42:18 +02:00
3968821834 HttpClient uses Serial.printf() (#4488)
changed to log_d()
2020-11-04 14:49:33 +02:00
90f869e772 Fix BUG: Parsing of first line fails (#4484)
..because a firstLine = false; is missing ;)
2020-11-04 02:24:01 +02:00
be4d3b6cb8 Try to fix issue with GIT 2.29.0 2020-11-03 22:22:35 +02:00
60606e5ad0 Update on-release.sh 2020-11-03 21:53:22 +02:00
22b427df0f IDF release/v3.3 (#3672)
ESP-IDF release/v3.3: 66d3783c8
esp-face: 420fc7e
esp32-camera: 0107093
2020-11-03 21:20:00 +02:00
6e5be78838 Update install-arduino-ide.sh 2020-11-03 21:12:05 +02:00
e2452c0dfc Added isKey method to Preferences (#4441)
Checks to see if a string is a key in the namespace. 

Fixes #4440
2020-11-03 17:03:04 +02:00
56a7ae8712 Trailing spaces (#3738)
* fix typo in WiFiMulti

* clean up trailing spaces

* clean up script file used in cleaning

Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
2020-11-02 22:11:26 +02:00
e4b008e712 Handle stream timeouts properly, for slow HTTP/HTTPS links (#3752)
This patch fixes update timeouts (error #6) on slow HTTP/HTTPS links.
2020-11-02 22:01:27 +02:00
76afaf2d6b Add more options to the AI thinker cam. (#4253)
* Add more options to the AI thinker cam.

The reason is power saving and compatibility of cheap chines clones with crappy flash chips.
2020-11-02 21:21:22 +02:00
dccb4e8608 improve & fix BLEScan when wantDuplicates (#3995)
* improve & fix BLEScan when too many BLE devices
- when wantDuplicates, no need to check duplicate and no more insert into vector
- delete advertisedDevice when not insert into vector, fix memory leak
- add showParse when you just want raw advertised data
2020-11-02 20:41:50 +02:00
c2346c37da Add initial support for arm64 toolchain. (#4117)
Closes #4111.
gcc: I was not able to find an arm64 build on espressif's website so I choosed to put the armhf version, even if this should works this is only ok as temporary, arm64 runs better on arm64 than armhf :)
esptool: being interpreted python its ok
mkspiff: need igrr/mkspiffs#74 to be merged and artifacts added in /package/package_esp32_index.template.json.
2020-11-02 20:39:31 +02:00
3491ca7845 Add virtual beginMulticast(...) stub to UDP class (#4061)
https://github.com/arduino/ArduinoCore-sam/pull/6/files

It is used here:
https://github.com/arduino-libraries/ArduinoMDNS/blob/master/MDNS.cpp#L150
2020-11-02 20:26:50 +02:00
0e341a6192 Add sdkconfig option CONFIG_ARDUINO_UDP_TASK_PRIORITY, which sets the task priority for the UDP vtask." (#4131) 2020-11-02 20:21:59 +02:00
76cd2e2375 Fix BLE connection handling (#4137)
Remove device from Peer list if connection fails.

Only call onConnect callback if connection was successful.

Only call onDisconnect callback if the connection was previously connected (ESP_GATTC_DISCONNECT_EVT is fired on a unsuccessful connection attempt also).

Resolves a number of issues with phantom events and callbacks being fired.
2020-11-02 20:20:40 +02:00
9f7ff009c6 Fix parameter to BLEDevice::updatePeerDevice (#4133)
::addPeerDevice and ::removePeerDevice are called with m_appId, so should ::updatePeerDevice as all use the same parameter for the underlying map's key.
2020-11-02 20:20:16 +02:00
704b71dabe Fix header parsing
fixes #4454

closes #4455
2020-11-02 20:10:22 +02:00
7c0572172c Fix for issue #4158: BLEAdvertising - Crash with stack trace originating in Bluedroid (#4182)
* Fix for issue #4158: Crash with stack trace originating in Bluedroid
Improved configuration of scan response data in 'BLEAdvertising' avoids the crash:
- Added member variable 'm_scanRespData' to configure scan response differently from advertising data
- Initialization of 'm_scanRespData' in BLEAdvertising constructor
- Use of 'm_scanRespData' within BLEAdvertising::start() to configure the scan response
- 'Flags' and 'Appearance' are cleared in the scan response data
- With this fix, device names of up to 29 characters can be used without causing a crash.
2020-11-02 19:39:20 +02:00
f57c36782f Add sendContent overload that takes a const char* and a length (#4276)
The web server currently lacks the ability to send a buffer. Only strings are supported.

This PR adds an overload to sendContent.
2020-11-02 19:17:02 +02:00
3054bdf5a5 HTTPUpdateServer library (#4244) 2020-11-02 19:16:23 +02:00
1014ba40af Update ISSUE_TEMPLATE.md (#4416)
Make options list into a table
2020-11-02 18:59:46 +02:00
d6b91872cb Fix for espressif#3460 issue (#4424)
Fixes: #3460

This code has been run in production for 1 month and it looks stable, no data dropped and it definitely fixes the issue described. I think that this can be merged to avoid using custom package referencing in PlatformIO that has been used in quite a few projects for now.

Co-authored-by: Ivan Golubic <ivan@mvt-solutions.com>
2020-11-02 18:59:03 +02:00
d6b383f84b Merge pull request #4429 from Bmooij/feature/Add_flash_helper_constructor_to_Uri
Add flash helper constructor to Uri
2020-11-02 18:56:08 +02:00
cadbad8850 Add partition label argument to Update and ArduinoOTA classThe UpdateClass in the Updater component has the ability to update data toa SPIFFS partition. It selects the first available partition using theESP-IDF esp_partition_find_first() function.That behaviour is problematic if one has multiple SPIFFS partitions.This change allows a user to pass the label argument (defaults to NULL)to UpdateClass::begin() so a specific SPIFFS partition can be updated.Additionally, ArduinoOTA can set this partition label using thenew method ArduinoOTAClass::setPartitionLabel which is optional.This change does not break compatibility. (#4442)
The UpdateClass in the Updater component has the ability to update data to
a SPIFFS partition. It selects the first available partition using the
ESP-IDF esp_partition_find_first() function.
That behaviour is problematic if one has multiple SPIFFS partitions.

This change allows a user to pass the label argument (defaults to NULL)
to UpdateClass::begin() so a specific SPIFFS partition can be updated.

Additionally, ArduinoOTA can set this partition label using the
new method ArduinoOTAClass::setPartitionLabel which is optional.

This change does not break compatibility.
2020-11-02 18:49:24 +02:00
3cbfa2ffef Add partition label argument to SPIFFS (#4443)
* Add partition label argument to SPIFFSSPIFFS currently assumes there is only ever one partition.This change allows a user to pass the label argument (defaults to NULL)to SPIFFS::begin() so a specific SPIFFS partition can be referenced.This change does not break compatibility.
2020-11-02 18:47:36 +02:00
360e04fa36 Fixing BLE GATT Characteristic notification and Characteristic Descriptor read (#4464)
* BLERemoteChar: fix descriptor 2902 write for characteristic notifications

When registering a notification on a characteristic, the 2902 descriptor
(CCCD) value is set to 1 (or 2 for indication).
According to the BLUETOOTH CORE SPECIFICATION Version 5.2, Revision Date
2019-12-31, section 4.12.3 "Write Characteristic Descriptors" (page 1588),
the characteristic descriptor write must expect a response.
Currently, the descriptor write is performed without expecting a reponse,
which prevents the notification to be functional with some BLE stacks.
This commit modify the write to expect the response.

Signed-off-by: Jimmy Durand Wesolowski <jimmy.durand.wesolowski@commsolid.com>

* BLERemoteChar: forward GATT client event to characteristic descriptors

This commits prevents a permanent wait when calling BLERemoteDescriptor
readValue function, on the m_semaphoreReadDescrEvt semaphore.

ESP32 BLE stack calls to remote characteristic
- notification,
- value read
- value write
and remote characteristic descriptor
- value read
are asynchronous.

When such a call is performed by this library, a semaphore is taken prior
to the BLE stack read or write operation, and waited on after it.

Releasing the semaphore is done by the characteristic event handling
function (gattClientEventHandler), when the appropriate event is received.

However, the characteristic descriptor events are discarded, and the
value read semaphore is never released.

This commits forwards the GATT client events from the remote
characteristic down to their remote characteristic descriptor, and
implements their event handling.

Adding a semaphore for the remote characteristic descriptor value write
will be done in a separate commit.

Signed-off-by: Jimmy Durand Wesolowski <jimmy.durand.wesolowski@commsolid.com>

* BLERemoteDescriptor: add semaphore to characteristic descriptor write

This adds a semaphore to characteristic descriptor value write, to mimic
the value read function, and to ensure completion of the operation before
we carry on.

Signed-off-by: Jimmy Durand Wesolowski <jimmy.durand.wesolowski@commsolid.com>

Co-authored-by: Jimmy Durand Wesolowski <jimmy.durand.wesolowski@commsolid.com>
2020-11-02 18:36:25 +02:00
57145ade6f Small description comment correction (#4471)
esp8266 --> esp32
2020-11-02 18:35:23 +02:00
f39024675c Don't convert to null-terminated string prior to writeValue (#4473)
fixes: #4472
2020-11-02 18:34:57 +02:00
f7fb00632e #4293 added missing '<tr>' (#4450)
Fixes issue #4293
2020-10-27 15:35:54 +02:00
7e40de226f Fixes #4435 - WiFiClient improperly treats zero data available for read as an error (#4448) 2020-10-27 12:01:41 +02:00
ae240a3902 Add flash helper constructor to Uri 2020-10-19 16:20:49 +02:00
1287c52933 Add missing "-mlongcalls" flag to PlatformIO build script (#4420)
Fixes possible issues with assembly files in external libraries
2020-10-16 21:58:05 +03:00
25bd585c25 Corrections of Stream.Find, FindUntil and added FindMulti - like AVR-Core Libraries (#3442)
* Corrections of Find, FindUntil and FindMulti

Find has some bug that is not working with Ethernet.find() so, I copied code from Stream.h and Stream.cpp in AVR-CORE library and now it's working perfectly.
I don't know where was the error, but an Ethernet.find compiled to MEGA2560 was working but not working when compiled to esp32, after corrections of code (copy of AVR-Core libraries) it's working perfect.
So probably has some error on original ESP32-Core library.

Below is part of code that was working with MEGA2560 and not with ESP32 libraries.
client.find never return TRUE with ESP32 original library and with AVR it's works.

boolean esp32_fw_update(EthernetClient &client, DecodedHeader &header, const String &field_filename, const String &field_crc) {

  char bound[header.boundary.length()+3];
  char term[]="\r\n";
  
  strcpy(bound,header.boundary.c_str());
  strcat(bound,term);
  while (client.find(bound)) { 
    String line=client.readStringUntil('\r');

* Update Stream.h

* Update Stream.cpp

Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
2020-10-14 15:20:40 +03:00
d79a1f3d10 Add an aditional (void *) arg to the RMT callback (much like Ticker() et.al.). (#3345)
* Add an aditional (void *) arg to the RMT callback - to allow more flexible handling of the callback (e.g. by passing a private struct or a class pointer). Same pattern as used by the Ticker() and many others. Example updated & new example with a trapoline added.

* Fix example for new API

* Fix lint warnings

* Add a second missed example.

* Correct timeout & improve socket error handling.
2020-10-14 14:41:50 +03:00
831f0ac29a Rename eep -> partitions.bin & hex -> bin (#4143)
This is in line with the
[arduino platform specification](https://arduino.github.io/arduino-cli/platform-specification/#recipes-for-extraction-of-executable-files-and-other-binary-data)
specifying that the file extension after recipe.objcopy is arbitrary and
that the AVR platform uses `eep` & `hex`, while the esp32 platform seems
to have file extensions `partition.bin` & `bin`
2020-10-14 14:34:14 +03:00
ee3bb16c77 Fix support for following redirects added by ee88c42c3b (#4240) (#4385) 2020-10-14 14:32:47 +03:00
d8dca9c73b bugfix (#4389) 2020-10-14 14:30:32 +03:00
c3f3497048 fixed some typos (#4395) 2020-10-14 14:27:49 +03:00
18c3345451 fix typos in WiFiSTA.cpp (#4396)
* fix typos

* made return value description a bit more helpful
2020-10-14 14:27:19 +03:00
b07f1c11fe Updated Readme.md (#4400)
* Updated Readme.md

Added links to latest release. Made it look a lot cleaner.

* Update README.md
2020-10-14 14:26:41 +03:00
2685a5dd7b Certificate isn't be free in case parse failure. (#4412)
I met problem while I was working with the WiFiClientSecure.
I tried to found the source of the problem, and I found it in the sll_client.cpp.
Please check my contribution.

I've open this problem in #4335 but received no response.
2020-10-14 14:25:26 +03:00
675a40b257 Fix Not by reference. But the value was updated. (#3279) 2020-10-03 03:41:03 +03:00
3570d48eb9 Added the DoIT ESPduino-32 board (#1520) 2020-10-03 03:06:40 +03:00
f76ec4f50b adds debugging to dnsserver (#1046) 2020-10-03 02:59:55 +03:00
fb6d5ad234 Add Inex OpenKB Board (#4002)
Please add OpenKB Board Product from INEX Co. Ltd.

https://inex.co.th/shop/openkb.html
2020-10-03 02:20:10 +03:00
219ff3005b Add missing slashes in HTTPUpdate examples (#4238)
I spent quite a while today figuring out how to get an OTA update over HTTPS on a custom port working. A part of my problem was not putting a slash before the .bin filename, since it wasn't there in the example. This produced invalid HTTP requests. Adding the slash would make it clear that it needs to be there.

Given that the URL in line 53 contains the same words "server" and "file.bin", one might assume that in line 55, the slash after the port number would get added automatically, however I have found out that without a slash you get an invalid request. Adding the slash removes any doubt.
2020-10-03 02:13:27 +03:00
ccab428e4d Added definitions for TTGO-LoRA32-V2.1.6 (#4205) 2020-10-03 02:06:11 +03:00
d2d24a14e0 add board WiFiduino32 (#4218) 2020-10-03 02:04:10 +03:00
af11921535 Add support for S.ODI_Ultra_v1.0 (#4372)
Add a new Dev Board S.ODI Ultra v1 in esp32 library
2020-10-03 01:59:53 +03:00
82112384ca Fix missing headers when compiling as IDF component with Cmake (#4377)
* HttpsOTAUpdate introduced new IDF component requirements.
These have been added to CMakeLists.txt to correct compilation errors when using Cmake.
2020-10-01 15:45:52 +03:00
99aa866477 Update MDNSResponder::addService to return a boolean (#4365)
I was playing with the mDNS service and noticed the method MDNSResponder::addService could return a Boolean with the way it is implemented just like other functions in this library.

This would be handy to know at a higher level weather or not the service was added correctly to the mDNS server of the ESP32.
2020-10-01 15:44:24 +03:00
82670b96f8 Fix for missed scan response data (BLEScan). (#4358)
This is a fix for missing scan responses after a first successfull scan.

While running the BLE_scan.ino sketch with wantDuplicates=false, i got
only one result with correct advertising and scan response length (31,26):

pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(), false);
pBLEScan->start(scanTime, false);
...
[W][BLEScan.cpp:109] handleGAPEvent(): bytes length: 31 + 26, addr type: 1

All following calls to start() just returned the advertising data without
scan response data:

pBLEScan->start(scanTime, false);
[W][BLEScan.cpp:109] handleGAPEvent(): bytes length: 31 + 0, addr type: 1

With "wantDuplicates=true" i got:

pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(), true);
pBLEScan->start(scanTime, false);
[W][BLEScan.cpp:109] handleGAPEvent(): bytes length: 31 + 26, addr type: 1
[W][BLEScan.cpp:109] handleGAPEvent(): bytes length: 31 + 26, addr type: 1
[W][BLEScan.cpp:73] handleGAPEvent(): ESP_GAP_SEARCH_INQ_CMPL_EVT
Devices found: 1
Scan done!
pBLEScan->start(scanTime, false);
[W][BLEScan.cpp:109] handleGAPEvent(): bytes length: 31 + 0, addr type: 1
[W][BLEScan.cpp:109] handleGAPEvent(): bytes length: 0 + 26, addr type: 1
[W][BLEScan.cpp:109] handleGAPEvent(): bytes length: 31 + 0, addr type: 1
[W][BLEScan.cpp:109] handleGAPEvent(): bytes length: 0 + 26, addr type: 1

Explicitly initializing m_scan_params.scan_duplicate of BLEScan solves
this issue (In my case the un-initialized value was
m_scan_params.scan_duplicate == 1073599044).

Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
2020-10-01 15:43:30 +03:00
9e7b13e46d WebServer.handleClient delay (#4350)
* If WebServer.handleClient is run in a tight loop, it will starve other processes.  So, if there is no connection, throw in a delay(1).  Fixes #4348

* Made a variable to control the delay behavior
2020-10-01 15:42:23 +03:00
2243081f85 add I2C_RX_FIFO_OVF_INT_ST handling to i2c_isr_handler_default (#4342)
Fixes crash on ESP32 when I2C FiFo overflows and interrupt function is unable to handle crash and throws this error:
[E][esp32-hal-i2c.c:1013] i2c_isr_handler_default(): unknown int=4

Co-authored-by: 0xDEADBEEF <0xde4dbeef@gmail.com>
2020-10-01 15:40:42 +03:00
1f4491f4ee Updated AzureIoT submodule. (#4322) 2020-10-01 15:38:56 +03:00
aa529eb5a0 Fix getString() freeze on empty responses (#4317) 2020-10-01 15:36:23 +03:00
a9cb7c6d6f mismatched parameter names. Fixes #4310 (#4313) 2020-10-01 15:35:40 +03:00
45d47e2be0 Fix duplicated pin constant (#4282)
G22 was defined twice, changing the first one to G21
2020-10-01 15:31:56 +03:00
99c94bb482 Set SD state to idle before unregister (reduces power) (#4272) 2020-10-01 15:31:16 +03:00
f98fc7ee9f fix hwSerial tx only flush (#4263) 2020-10-01 15:30:45 +03:00
0957776855 Reinit updater md5 related fields (#4260)
MD5 cleanup on begin
Typos
2020-10-01 15:29:58 +03:00
d93245d0f5 add m5stack-core2 board (#4255)
* add m5stack-core2 board
2020-10-01 15:28:40 +03:00
c917ed2504 shallow clone to make installation faster (#4246)
shallow clone (board and submodules) to make installation faster
2020-10-01 14:43:48 +03:00
ee88c42c3b Add support for following redirects in HTTPClient (#4240) 2020-10-01 14:41:54 +03:00
837cc3d271 Added SparkFun ESP32 Thing Plus board (#4224) 2020-10-01 14:39:39 +03:00
9b2ae12fb7 Added D pin numbers (#4220)
Source: https://wiki.dfrobot.com/FireBeetle_ESP32_IOT_Microcontroller(V3.0)__Supports_Wi-Fi_&_Bluetooth__SKU__DFR0478
2020-10-01 14:39:01 +03:00
86e221d087 Update HelloServer.ino (#4219)
Change name esp8266 for esp32 in welcome message
2020-10-01 14:38:26 +03:00
663effa00e Update Parsing.cpp (#4217)
* Update Parsing.cpp

When uploading TLS cert files the end of file "-----END CERTIFICATE-----" (or any kind of file with the sequence "CRLF--") is taken as posible end boundary. Then it is compared to the start boundary string. As it is expected, comparison turns to be false, and the whole end boundary string is put to _currentUpload->buf through _uploadWriteByte(). Here you have the problem: if you read boundary.length() bytes from HTTP request and you have some of the actual end boundary bytes in it, when you put all those bytes into _currentUpload->buf you are making a mistake. You will miss the actual end boundary string because some of those bytes were put in _currentUpload->buf.

* Update Parsing.cpp
2020-10-01 14:37:59 +03:00
6f237a8415 Fix ttgo twatch & tbeam board definition error (#4212) 2020-10-01 14:36:32 +03:00
c3c38a8eb5 Enable precompiled libraries (#4209)
The latest versions of Arduino IDE shifted the responsibility for precompiled libraries support to the core developers, which breaks precompiled library support in esp32 Arduino core. See https://github.com/arduino/ArduinoCore-avr/pull/52 for more details:

```
In this new version of the builder we are not doing any heuristics to find the right spot where the ldflags should be inserted (this was causing many bugs on its own); instead, we fully trust the core makers to add explicit support to precompiled libs.
```

This chage re-enables precompiled library support in the esp32 Arduino core.
2020-10-01 14:32:20 +03:00
8fcc914853 Added facility to invert the polarity of input UART bits. (#4200) 2020-10-01 13:58:48 +03:00
d03f8f1277 Update pins_arduino.h (#4190) 2020-10-01 13:54:01 +03:00
882b12c44e rmdir causes issues in SPIFFS. Fixes #4138, albeit not very cleanly (#4154)
SPIFFS causes crashes if you attempt to rmdir. Since there are no true directories in spiffs, this ought to be a noop. It looks like @me-no-dev worked around this by using unlink instead of rmdir, which works in fatfs and doesn't panic spiffs. This behavior is not universal. In order to get littlefs working, it would be good to get this back to conformity. Rather than digging deep into the upstream spiffs, I just check the mountpoint and noop if it is "/spiffs". So, if the user has changed the mountpoint, this will not work, but I think it's a pretty good tradeoff.
2020-10-01 13:52:24 +03:00
93d850f783 Fixed comment with correct FQDN (#4152) 2020-10-01 13:51:36 +03:00
4f48caca2c Set scan_duplicate in BLE scan params (#4126)
This value is uninitialised and as such can be a random (and invalid) value. It's needs to be set per the espressif documentation here:

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/esp_gap_ble.html#_CPPv4N21esp_ble_scan_params_t14scan_duplicateE

This PR sets it to DUPLICATE_DISABLE. Chosen as this is needed to ensure all scan data is populated in the scan callback, per this comment in the IDF:

https://github.com/espressif/esp-idf/blob/master/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c#L3591

"//if scan duplicate is enabled, the adv packet without scan response is allowed to report to higher layer"

We **don't** want it to report to the higher layer (ie BLEScan.cpp) **unless** it has the active scan response.

Seems to resolve #3770 #3677 and possibly others.
2020-10-01 13:45:45 +03:00
80e9e42c3b Fix issue #3522 (WiFi does not restart after stopped) (#4114)
This commit fixes issue https://github.com/espressif/arduino-esp32/issues/3522 where WiFi service fails to start after a WiFi.disconnect(true) or a WiFi.mode(WIFI_OFF).
2020-09-30 15:34:10 +03:00
494061af26 WebServer: Fix OOB write (#4088)
Successful exploitation could lead to arbitrary code execution.

The bug can be reproduced by running the following in a browser:
```
const formData = new FormData();
for (let i = 0;i < 33;++i) { formData.append("foo", i.toString()); }
await fetch("http://esp.local", { method: 'POST', body: formData });
```
2020-09-30 15:28:28 +03:00
2fd3d042b2 Fix #4046 Details below: (#4086)
Informed by the discussion in the bug and the code in 'that other branch'
the fix was clear.  Just set a flag if we start handling a write, and
use that flag to guard the long write complete call.
2020-09-30 15:27:35 +03:00
b551310c37 Minor change but could confuse some (#4084) 2020-09-30 15:26:38 +03:00
f30edd040e Update CaptivePortal.ino (#4080)
* Update CaptivePortal.ino

Illegal SSID used for SoftAP

* Fixed ordering problem.  Now actually works
2020-09-30 15:26:15 +03:00
b7c5e502e7 Add static pin support (#4078) 2020-09-30 15:25:42 +03:00
fa8a1c38d5 fix #4071 (#4072)
SPIFFS File object evaluates as true even if the file could not be opened.
2020-09-30 15:24:59 +03:00
d56267bd8c Update GetChipID.ino (#4070)
Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
2020-09-30 15:24:29 +03:00
1f6b0b35f8 Have BLECLient gattc event handlers verify conn_id (#4064)
Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
2020-09-30 15:21:59 +03:00
342b9cf2d8 Fix handling of registerForNotify in BLERemoteCharacteristic.cpp (#4063) 2020-09-30 15:20:28 +03:00
11d071b1c8 Fix to allow more than one certificate to be loaded (espressif#3248). (#4056)
Co-authored-by: Mark Hale <mark.hale@physics.org>
2020-09-30 15:19:41 +03:00
f48d9016fd Update T_Watch board properties and add revision selection (#4025)
Co-authored-by: lewis he <lewisxhe@outlook.com>
2020-09-30 15:18:17 +03:00
a55265f74b Fix clockCyclesPerMicrosecond Change from fixed value to current value (#3993) 2020-09-30 15:16:20 +03:00
c1a7198e7d Don't change owner if take() doesnt succeed (#3987) 2020-09-30 15:15:47 +03:00
4d4a1fde36 Added HealthyPi 4 Board Support (#3985)
Co-authored-by: Ashwin <ashwin@circuitects.com>
2020-09-30 15:14:39 +03:00
d219e56872 Update pins_arduino.h (#3908)
Update in accordance with Firebeetle's official firebeetle32 pins_arduino.h. Without it, digital pins weren't definable, and analogue pins didnt line up.

Taken from http://download.dfrobot.top/FireBeetle/DFRobot_FireBeetle-ESP32-0.0.9.zip
2020-09-30 15:07:26 +03:00
19ccc479c3 WIFI_PS_MAX_MODEM feature requested #3896 (#3900) 2020-09-30 15:06:58 +03:00
c18d50cb91 Use esp_partition_* functions in Updater.cpp (#3898)
Background

The current implementation of Update() uses the spi_flash_* api to write and read from flash. These functions ignore the partition->encrypted flag and always write raw data to flash even if the partition is marked as encrypted.

Changes in this PR

Update() now uses the esp_partition_* api.
Wrapper functions for esp_partition_* added to ESP.cpp. This was done to maintain a consistent approach to the way the spi_flash_* functions were used. I note though that not all of the esp-idf functions are used are wrapped, for example esp_ota_get_next_update_partition() so it may be that these should not be added?
The current implementation of Update() changes the first (magic) byte of firmware to 0xFF on write, and then when the firmware is completely written changes it back to ESP_IMAGE_HEADER_MAGIC. This works without erasing the sector because flash bits can be changed from 1->0 (but not 0->1). If the flash is encrypted then the actual data written to flash will not be all ones, so this approach will not work. In addition, encrypted flash must be written in 16 byte blocks. So, instead of changing the first byte the changed code stashes the first 16 bytes, and starts writing at the 17th byte, leaving the first 16 bytes as 0xFF. Then, in _enablePartition() the stashed bytes can be successfully written.
Benefits

Whilst it's not possible to use encrypted flash directly from either the Arduino IDE or PIO it's reasonably straightforward to compile and flash a bootloader with the necessary support from a simple esp-idf project and then use ArduinoOTA for subsequent updates. This PR enables the use of this workflow until such time as encrypted flash is supported, and is a first (small) step toward adding support.
Regardless of the above, the esp_partition_* api is recommended over the api_flash_* api.
Application code should mostly use these esp_partition_* API functions instead of lower level spi_flash_* API functions. Partition table API functions do bounds checking and calculate correct offsets in flash, based on data stored in a partition table.
2020-09-30 15:06:19 +03:00
80418fadcf Fixes UART detach. Fixes #3878 (#3894)
* Fixes UART detach.  Fixes #3878

* 0 is not a good holder value for pins!

* 0 is not a good holder value for pins!
2020-09-30 15:04:18 +03:00
8b6d020352 added new board Logsens V1p1 (#3880)
* Added new board variant for Imbrios LogSens V1.1

Imbrios LogSens V1.1 new board variant

* added new board Imbrios LogSens V1.1

Added new board details: Imbrios LogSens V1.1
2020-09-30 15:01:51 +03:00
5197916983 Fix BLEClient disconnect bug (#3876)
By default the disconnect is broadcasted to every clients. So if you call disconnect on one connected client, they'll all be disconnected if we don't filter the event by conn_id.
2020-09-30 15:01:02 +03:00
7e9d42da68 ESP.getChipModel() and ESP.getChipCores() (#3847)
* ESP.getChipModel() returns model of the chip

* ESP.getChipCores() returns the core count.

* Example gives chip model, revision and core count.

* Read efuse for chipmodel

Co-authored-by: Martijn Scheepers <ms@SDNengineering.nl>
2020-09-30 14:57:36 +03:00
e34e0b45de Fixed bug where mutex would not be unlocked (#3837)
Fixed bug where uartResizeRxBuffer() did not unlock mutex if creation of queue failed.
2020-09-30 14:56:41 +03:00
ef2b54547e Fix issue #3833, data parsing of Eddystone TLM data frame (#3836)
* Fix issue #3833, data parsing of Eddystone TLM data frame    
Add Beacon scanner example to show usage of BLEEddystoneTLM class and  BLEEddystoneURL class     
Add EddystoneTLM beacon example    
Add EddystoneURL beacon example

* Fix buffer size for .toString()
2020-09-30 14:55:58 +03:00
9856f0cc28 Update RMTLoopback.ino (#3823)
BUGFIX: avoids assertion in xEventGroupWaitBits()
(/home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/event_groups.c:350 (xEventGroupWaitBits)- assert failed!)
2020-09-30 14:52:22 +03:00
daa8c55667 add new board mpython (#3814) 2020-09-30 14:49:57 +03:00
af7ec4ead1 fix EEPROM class example (#3786) 2020-09-30 14:43:05 +03:00
9e65ed1af1 Process compiler.libraries.ldflags (#3783)
Add support for the v1.8.6 compiler.libraries.ldflags:

https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification

This fixes the issue reported here:

https://community.bosch-sensortec.com/t5/MEMS-sensors-forum/BSEC-1-4-7-4-compilation-error-on-ESP32-1-0-3-rc1/td-p/9120
2020-09-30 14:42:04 +03:00
7a92f89d12 Set TLS cert options before calling connect on client, so verify works (#3774)
When connecting using transportTraits, the CA and client certificates are ignored after the initial _client->connect() is called. This is because on connect, WiFiClientSecure will call start_ssl_client with _CA_Cert and other cert options set to null unless setCACert, setCertificate etc. are called before connect. Running _transportTraits->verify after connect therefore does exactly nothing. It's easy to verify that this is the case by passing a CACert to HTTPClient with verbose logging enabled - the logs will say "WARNING: Use certificates for a more secure communication!" which is only present when both PSK and CA are null. This change fixes the issue.
2020-09-30 14:41:03 +03:00
5871ca9ce9 UpdateClass::printError now accepts the Print object (#3763) 2020-09-30 14:39:25 +03:00
0dfa5babc2 fix typo in WiFiMulti (#3737) 2020-09-30 14:37:21 +03:00
e4b2ce4e81 DNS resolving timeout change to prevent stack overlapping (#3731)
Real DNS resolving timeout used by lwip library is 14[s] (7[s] for DNS1 + 7[s] for DNS2). Function WiFiGenericClass::hostByName() has timeout set to lower value (only 4[s]), so callback function may be called after this low timeout and it may overlappe stack memory used now by other function.
Fixes #3722
2020-09-30 14:35:53 +03:00
7af4490fcc Update SPI.h 2020-09-30 14:34:37 +03:00
4204869ec9 Extend Print class for 64bit integers. (#3688)
* Extend Print class for 64bit integers.

modulo 32bit and 64bit tuned for code size.

* Fix 32bit long used in long long printNumber.
2020-09-30 14:31:36 +03:00
ab23e8a656 Greatly reduces error rate (half, or 0 zero errors, depends on in/out ranges) for round-trip mapping at the same performance. (#3655)
(Based on "improved_map" from ESP8266's Servo.cpp)
2020-09-30 14:28:51 +03:00
5999b7ba46 removed double delete of characteristics (#3521) 2020-09-30 14:25:31 +03:00
7b613c1238 Added documentation regarding delay() resolution in esp-idf component (#3014) 2020-09-30 14:17:53 +03:00
cee2359e33 Update pins_arduino.h (#4211)
fix some mistake
2020-09-28 10:24:06 +03:00
37a7fb3d6a Update PlatformIO CI script (#4307) 2020-08-31 18:06:34 +03:00
9d547a8a44 Provisioning fixes for: 1.Space 2.Provisioned (#4291) 2020-08-25 11:03:06 +03:00
1fd5cd79c5 Add OTA update feature
This sketch provide functionality for OTA firmware upgrade.
2020-08-25 11:01:58 +03:00
410 changed files with 10045 additions and 1408 deletions

View File

@ -48,16 +48,22 @@ else
export ARDUINO_USR_PATH="$HOME/Arduino" export ARDUINO_USR_PATH="$HOME/Arduino"
fi 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 if [ ! -d "$ARDUINO_IDE_PATH" ]; then
echo "Installing Arduino IDE on $OS_NAME ..." 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 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' ..." echo "Extracting 'arduino.$ARCHIVE_FORMAT' ..."
tar xf "arduino.$ARCHIVE_FORMAT" > /dev/null tar xf "arduino.$ARCHIVE_FORMAT" > /dev/null
mv arduino-nightly "$ARDUINO_IDE_PATH" mv arduino-nightly "$ARDUINO_IDE_PATH"
else 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' ..." echo "Extracting 'arduino.$ARCHIVE_FORMAT' ..."
unzip "arduino.$ARCHIVE_FORMAT" > /dev/null unzip "arduino.$ARCHIVE_FORMAT" > /dev/null
if [ "$OS_IS_MACOS" == "1" ]; then if [ "$OS_IS_MACOS" == "1" ]; then

View File

@ -12,19 +12,14 @@ echo "Installing Platform ESP32 ..."
python -m platformio platform install https://github.com/platformio/platform-espressif32.git > /dev/null 2>&1 python -m platformio platform install https://github.com/platformio/platform-espressif32.git > /dev/null 2>&1
echo "Replacing the framework version ..." echo "Replacing the framework version ..."
if [[ "$OSTYPE" == "darwin"* ]]; then 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()"
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
if [ "$GITHUB_REPOSITORY" == "espressif/arduino-esp32" ]; then if [ "$GITHUB_REPOSITORY" == "espressif/arduino-esp32" ]; then
echo "Linking Core..." echo "Linking Core..."
ln -s $GITHUB_WORKSPACE "$PLATFORMIO_ESP32_PATH" ln -s $GITHUB_WORKSPACE "$PLATFORMIO_ESP32_PATH"
else else
echo "Cloning Core Repository ..." 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 fi
echo "PlatformIO for ESP32 has been installed" echo "PlatformIO for ESP32 has been installed"

View File

@ -330,7 +330,7 @@ fi
if [ ! -z "$COMMITS_SINCE_RELEASE" ] && [ "$COMMITS_SINCE_RELEASE" != "null" ]; then if [ ! -z "$COMMITS_SINCE_RELEASE" ] && [ "$COMMITS_SINCE_RELEASE" != "null" ]; then
echo "Getting commits since $COMMITS_SINCE_RELEASE ..." echo "Getting commits since $COMMITS_SINCE_RELEASE ..."
commitFile=$OUTPUT_DIR/commits.txt 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' releaseNotes+=$'\r\n##### Commits\r\n'
IFS=$'\n' IFS=$'\n'
for next in `cat $commitFile` for next in `cat $commitFile`

View File

@ -60,6 +60,7 @@ set(LIBRARY_SRCS
libraries/SPI/src/SPI.cpp libraries/SPI/src/SPI.cpp
libraries/Ticker/src/Ticker.cpp libraries/Ticker/src/Ticker.cpp
libraries/Update/src/Updater.cpp libraries/Update/src/Updater.cpp
libraries/Update/src/HttpsOTAUpdate.cpp
libraries/WebServer/src/WebServer.cpp libraries/WebServer/src/WebServer.cpp
libraries/WebServer/src/Parsing.cpp libraries/WebServer/src/Parsing.cpp
libraries/WebServer/src/detail/mimetable.cpp libraries/WebServer/src/detail/mimetable.cpp
@ -207,7 +208,7 @@ set(COMPONENT_ADD_INCLUDEDIRS
set(COMPONENT_PRIV_INCLUDEDIRS cores/esp32/libb64) set(COMPONENT_PRIV_INCLUDEDIRS cores/esp32/libb64)
set(COMPONENT_REQUIRES spi_flash mbedtls mdns ethernet esp_adc_cal wifi_provisioning) 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() register_component()

View File

@ -76,13 +76,18 @@ choice ARDUINO_UDP_RUNNING_CORE
endchoice 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 config ARDUINO_UDP_RUNNING_CORE
int int
default 0 if ARDUINO_UDP_RUN_CORE0 default 0 if ARDUINO_UDP_RUN_CORE0
default 1 if ARDUINO_UDP_RUN_CORE1 default 1 if ARDUINO_UDP_RUN_CORE1
default -1 if ARDUINO_UDP_RUN_NO_AFFINITY default -1 if ARDUINO_UDP_RUN_NO_AFFINITY
config DISABLE_HAL_LOCKS config DISABLE_HAL_LOCKS
bool "Disable mutex locks for HAL" bool "Disable mutex locks for HAL"
default "n" default "n"

View File

@ -11,9 +11,11 @@
- [ESP32Dev Board PINMAP](#esp32dev-board-pinmap) - [ESP32Dev Board PINMAP](#esp32dev-board-pinmap)
### Development Status ### Development Status
[Latest stable release ![Release Version](https://img.shields.io/github/release/espressif/arduino-esp32.svg?style=plastic) ![Release Date](https://img.shields.io/github/release-date/espressif/arduino-esp32.svg?style=plastic)](https://github.com/espressif/arduino-esp32/releases/latest/) ![Downloads](https://img.shields.io/github/downloads/espressif/arduino-esp32/latest/total.svg?style=plastic)
[Latest development release ![Development Version](https://img.shields.io/github/release/espressif/arduino-esp32/all.svg?style=plastic) ![Development Date](https://img.shields.io/github/release-date-pre/espressif/arduino-esp32.svg?style=plastic)](https://github.com/espressif/arduino-esp32/releases/latest/) ![Downloads](https://img.shields.io/github/downloads-pre/espressif/arduino-esp32/latest/total.svg?style=plastic) Latest Stable Release [![Release Version](https://img.shields.io/github/release/espressif/arduino-esp32.svg?style=plastic)](https://github.com/espressif/arduino-esp32/releases/latest/) [![Release Date](https://img.shields.io/github/release-date/espressif/arduino-esp32.svg?style=plastic)](https://github.com/espressif/arduino-esp32/releases/latest/) [![Downloads](https://img.shields.io/github/downloads/espressif/arduino-esp32/latest/total.svg?style=plastic)](https://github.com/espressif/arduino-esp32/releases/latest/)
Latest Development Release [![Release Version](https://img.shields.io/github/release/espressif/arduino-esp32/all.svg?style=plastic)](https://github.com/espressif/arduino-esp32/releases/latest/) [![Release Date](https://img.shields.io/github/release-date-pre/espressif/arduino-esp32.svg?style=plastic)](https://github.com/espressif/arduino-esp32/releases/latest/) [![Downloads](https://img.shields.io/github/downloads-pre/espressif/arduino-esp32/latest/total.svg?style=plastic)](https://github.com/espressif/arduino-esp32/releases/latest/)
### Installation Instructions ### Installation Instructions
- Using Arduino IDE Boards Manager (preferred) - Using Arduino IDE Boards Manager (preferred)

File diff suppressed because it is too large Load Diff

View File

@ -78,7 +78,7 @@
#define interrupts() sei() #define interrupts() sei()
#define noInterrupts() cli() #define noInterrupts() cli()
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L ) #define clockCyclesPerMicrosecond() ( (long int)getCpuFrequencyMhz() )
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() ) #define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() ) #define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
@ -88,7 +88,7 @@
#define bitRead(value, bit) (((value) >> (bit)) & 0x01) #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit))) #define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(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 // avr-libc defines _NOP() since 1.6.2
#ifndef _NOP #ifndef _NOP

View File

@ -218,6 +218,33 @@ uint8_t EspClass::getChipRevision(void)
return chip_info.revision; 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) const char * EspClass::getSdkVersion(void)
{ {
return esp_get_idf_version(); 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; 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) uint64_t EspClass::getEfuseMac(void)
{ {

View File

@ -21,6 +21,7 @@
#define ESP_H #define ESP_H
#include <Arduino.h> #include <Arduino.h>
#include <esp_partition.h>
/** /**
* AVR macros for WDT managment * AVR macros for WDT managment
@ -75,6 +76,8 @@ public:
uint32_t getMaxAllocPsram(); uint32_t getMaxAllocPsram();
uint8_t getChipRevision(); uint8_t getChipRevision();
const char * getChipModel();
uint8_t getChipCores();
uint32_t getCpuFreqMHz(){ return getCpuFrequencyMhz(); } uint32_t getCpuFreqMHz(){ return getCpuFrequencyMhz(); }
inline uint32_t getCycleCount() __attribute__((always_inline)); inline uint32_t getCycleCount() __attribute__((always_inline));
const char * getSdkVersion(); const char * getSdkVersion();
@ -97,6 +100,10 @@ public:
bool flashWrite(uint32_t offset, uint32_t *data, size_t size); bool flashWrite(uint32_t offset, uint32_t *data, size_t size);
bool flashRead(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(); uint64_t getEfuseMac();
}; };

View File

@ -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); _uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert);
_tx_pin = txPin;
_rx_pin = rxPin;
if(!baud) { if(!baud) {
uartStartDetectBaudrate(_uart); uartStartDetectBaudrate(_uart);
@ -70,6 +72,8 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
} else { } else {
log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible"); log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible");
_uart = NULL; _uart = NULL;
_tx_pin = 255;
_rx_pin = 255;
} }
} }
} }
@ -84,7 +88,8 @@ void HardwareSerial::end()
if(uartGetDebug() == _uart_nr) { if(uartGetDebug() == _uart_nr) {
uartSetDebug(0); uartSetDebug(0);
} }
uartEnd(_uart); log_v("pins %d %d",_tx_pin, _rx_pin);
uartEnd(_uart, _tx_pin, _rx_pin);
_uart = 0; _uart = 0;
} }
@ -179,3 +184,8 @@ HardwareSerial::operator bool() const
{ {
return true; return true;
} }
void HardwareSerial::setRxInvert(bool invert)
{
uartSetRxInvert(_uart, invert);
}

View File

@ -100,10 +100,14 @@ public:
size_t setRxBufferSize(size_t); size_t setRxBufferSize(size_t);
void setDebugOutput(bool); void setDebugOutput(bool);
void setRxInvert(bool);
protected: protected:
int _uart_nr; int _uart_nr;
uart_t* _uart; uart_t* _uart;
uint8_t _tx_pin;
uint8_t _rx_pin;
}; };
extern void serialEventRun(void) __attribute__((weak)); extern void serialEventRun(void) __attribute__((weak));

View File

@ -111,18 +111,12 @@ size_t Print::print(unsigned int n, int base)
size_t Print::print(long n, int base) size_t Print::print(long n, int base)
{ {
if(base == 0) { int t = 0;
return write(n); if (base == 10 && n < 0) {
} else if(base == 10) { t = print('-');
if(n < 0) { n = -n;
int t = print('-');
n = -n;
return printNumber(n, 10) + t;
}
return printNumber(n, 10);
} else {
return printNumber(n, base);
} }
return printNumber(static_cast<unsigned long>(n), base) + t;
} }
size_t Print::print(unsigned long n, int base) 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) size_t Print::print(double n, int digits)
{ {
return printFloat(n, digits); return printFloat(n, digits);
@ -226,6 +239,20 @@ size_t Print::println(unsigned long num, int base)
return n; 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 Print::println(double num, int digits)
{ {
size_t n = print(num, 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) 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]; char *str = &buf[sizeof(buf) - 1];
*str = '\0'; *str = '\0';
@ -262,11 +289,34 @@ size_t Print::printNumber(unsigned long n, uint8_t base)
} }
do { 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; n /= base;
char c = m - base * n; char c = m - base * n;
*--str = c < 10 ? c + '0' : c + 'A' - 10; *--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n); } while (n);
return write(str); return write(str);
} }

View File

@ -36,6 +36,7 @@ class Print
private: private:
int write_error; int write_error;
size_t printNumber(unsigned long, uint8_t); size_t printNumber(unsigned long, uint8_t);
size_t printNumber(unsigned long long, uint8_t);
size_t printFloat(double, uint8_t); size_t printFloat(double, uint8_t);
protected: protected:
void setWriteError(int err = 1) void setWriteError(int err = 1)
@ -81,6 +82,8 @@ public:
size_t print(unsigned int, int = DEC); size_t print(unsigned int, int = DEC);
size_t print(long, int = DEC); size_t print(long, int = DEC);
size_t print(unsigned 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(double, int = 2);
size_t print(const Printable&); size_t print(const Printable&);
size_t print(struct tm * timeinfo, const char * format = NULL); size_t print(struct tm * timeinfo, const char * format = NULL);
@ -94,6 +97,8 @@ public:
size_t println(unsigned int, int = DEC); size_t println(unsigned int, int = DEC);
size_t println(long, int = DEC); size_t println(long, int = DEC);
size_t println(unsigned 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(double, int = 2);
size_t println(const Printable&); size_t println(const Printable&);
size_t println(struct tm * timeinfo, const char * format = NULL); size_t println(struct tm * timeinfo, const char * format = NULL);

View File

@ -87,22 +87,22 @@ unsigned long Stream::getTimeout(void) {
} }
// find returns true if the target string is found // 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 // 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 // returns true if target string is found, false if timed out
bool Stream::find(const char *target, size_t length) 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 // 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 // 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 // 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) 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! if (terminator == NULL) {
size_t termIndex = 0; MultiTarget t[1] = {{target, targetLen, 0}};
int c; 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) { int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
return true; // return true if target is a null string // 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) { }
// unreachable
if(c != target[index]) { return -1;
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;
} }
// returns the first valid (long) integer value from the current position. // returns the first valid (long) integer value from the current position.

View File

@ -60,6 +60,7 @@ public:
void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
unsigned long getTimeout(void); unsigned long getTimeout(void);
bool find(const char *target); // reads data from the stream until the target string is found bool find(const char *target); // reads data from the stream until the target string is found
bool find(uint8_t *target) bool find(uint8_t *target)
{ {
@ -123,6 +124,17 @@ protected:
// this allows format characters (typically commas) in values to be ignored // this allows format characters (typically commas) in values to be ignored
float parseFloat(char skipChar); // as above but the given skipChar is 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 #endif

View File

@ -43,6 +43,7 @@ class UDP: public Stream
public: 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 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 virtual void stop() =0; // Finish with the UDP socket
// Sending UDP packets // Sending UDP packets

View File

@ -44,7 +44,7 @@ long random(long howbig)
uint32_t t = -howbig; uint32_t t = -howbig;
if (t >= howbig) { if (t >= howbig) {
t -= howbig; t -= howbig;
if (t >= howbig) if (t >= howbig)
t %= howbig; t %= howbig;
} }
while (l < t) { 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 map(long x, long in_min, long in_max, long out_min, long out_max) {
long divisor = (in_max - in_min); const long dividend = out_max - out_min;
if(divisor == 0){ const long divisor = in_max - in_min;
return -1; //AVR returns -1, SAM returns 0 const long delta = x - in_min;
}
return (x - in_min) * (out_max - out_min) / divisor + out_min; return (delta * dividend + (divisor / 2)) / divisor + out_min;
} }
unsigned int makeWord(unsigned int w) unsigned int makeWord(unsigned int w)

View File

@ -945,6 +945,14 @@ static void IRAM_ATTR i2c_isr_handler_default(void* arg)
activeInt &=~I2C_RXFIFO_FULL_INT_ST; 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 (activeInt & I2C_ACK_ERR_INT_ST_M) {//fatal error, abort i2c service
if (p_i2c->mode == I2C_MASTER) { if (p_i2c->mode == I2C_MASTER) {
i2c_update_error_byte_cnt(p_i2c); // calc which byte caused ack Error, check if address or data i2c_update_error_byte_cnt(p_i2c); // calc which byte caused ack Error, check if address or data

View File

@ -94,6 +94,7 @@ struct rmt_obj_s
transaction_state_t tx_state; transaction_state_t tx_state;
rmt_rx_data_cb_t cb; rmt_rx_data_cb_t cb;
bool data_alloc; 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] = { 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, NULL},
{ 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}, { 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}, { 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}, { 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}, { 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}, { 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}, { false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false, NULL},
}; };
/** /**
@ -324,6 +325,7 @@ bool rmtReadData(rmt_obj_t* rmt, uint32_t* data, size_t size)
return true; return true;
} }
bool rmtBeginReceive(rmt_obj_t* rmt) bool rmtBeginReceive(rmt_obj_t* rmt)
{ {
if (!rmt) { if (!rmt) {
@ -357,7 +359,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) { if (!rmt && !cb) {
return false; return false;
@ -365,6 +367,7 @@ bool rmtRead(rmt_obj_t* rmt, rmt_rx_data_cb_t cb)
int channel = rmt->channel; int channel = rmt->channel;
RMT_MUTEX_LOCK(channel); RMT_MUTEX_LOCK(channel);
rmt->arg = arg;
rmt->intr_mode = E_RX_INTR; rmt->intr_mode = E_RX_INTR;
rmt->tx_state = E_FIRST_HALF; rmt->tx_state = E_FIRST_HALF;
rmt->cb = cb; rmt->cb = cb;
@ -391,6 +394,19 @@ bool rmtRead(rmt_obj_t* rmt, rmt_rx_data_cb_t cb)
return true; 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) bool rmtReadAsync(rmt_obj_t* rmt, rmt_data_t* data, size_t size, void* eventFlag, bool waitForData, uint32_t timeout)
{ {
if (!rmt) { if (!rmt) {
@ -523,6 +539,8 @@ rmt_obj_t* rmtInit(int pin, bool tx_not_rx, rmt_reserve_memsize_t memsize)
rmt->tx_not_rx = tx_not_rx; rmt->tx_not_rx = tx_not_rx;
rmt->buffers =buffers; rmt->buffers =buffers;
rmt->channel = channel; rmt->channel = channel;
rmt->arg = NULL;
_initPin(pin, channel, tx_not_rx); _initPin(pin, channel, tx_not_rx);
// Initialize the registers in default mode: // Initialize the registers in default mode:
@ -544,6 +562,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_lv = 0; // signal level for idle
RMT.conf_ch[channel].conf1.idle_out_en = 1; // enable idle RMT.conf_ch[channel].conf1.idle_out_en = 1; // enable idle
RMT.conf_ch[channel].conf1.ref_always_on = 0; // base clock RMT.conf_ch[channel].conf1.ref_always_on = 0; // base clock
RMT.apb_conf.fifo_mask = 1; RMT.apb_conf.fifo_mask = 1;
if (tx_not_rx) { if (tx_not_rx) {
@ -659,7 +678,7 @@ static void IRAM_ATTR _rmt_isr(void* arg)
} }
if (g_rmt_objects[ch].cb) { if (g_rmt_objects[ch].cb) {
// actually received data ptr // 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 // restart the reception
RMT.conf_ch[ch].conf1.mem_owner = 1; RMT.conf_ch[ch].conf1.mem_owner = 1;

View File

@ -40,7 +40,7 @@ typedef enum {
typedef struct rmt_obj_s rmt_obj_t; 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 { typedef struct {
union { 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 * 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 */ /* Additional interface */

View File

@ -54,7 +54,7 @@ extern "C" {
struct spi_struct_t; struct spi_struct_t;
typedef struct spi_struct_t spi_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); void spiStopBus(spi_t * spi);
//Attach/Detach Signal Pins //Attach/Detach Signal Pins

View File

@ -124,21 +124,21 @@ void uartDisableInterrupt(uart_t* uart)
UART_MUTEX_UNLOCK(); UART_MUTEX_UNLOCK();
} }
void uartDetachRx(uart_t* uart) void uartDetachRx(uart_t* uart, uint8_t rxPin)
{ {
if(uart == NULL) { if(uart == NULL) {
return; return;
} }
pinMatrixInDetach(UART_RXD_IDX(uart->num), false, false); pinMatrixInDetach(rxPin, false, false);
uartDisableInterrupt(uart); uartDisableInterrupt(uart);
} }
void uartDetachTx(uart_t* uart) void uartDetachTx(uart_t* uart, uint8_t txPin)
{ {
if(uart == NULL) { if(uart == NULL) {
return; return;
} }
pinMatrixOutDetach(UART_TXD_IDX(uart->num), false, false); pinMatrixOutDetach(txPin, false, false);
} }
void uartAttachRx(uart_t* uart, uint8_t rxPin, bool inverted) 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; return uart;
} }
void uartEnd(uart_t* uart) void uartEnd(uart_t* uart, uint8_t txPin, uint8_t rxPin)
{ {
if(uart == NULL) { if(uart == NULL) {
return; return;
@ -243,8 +243,8 @@ void uartEnd(uart_t* uart)
UART_MUTEX_UNLOCK(); UART_MUTEX_UNLOCK();
uartDetachRx(uart); uartDetachRx(uart, rxPin);
uartDetachTx(uart); uartDetachTx(uart, txPin);
} }
size_t uartResizeRxBuffer(uart_t * uart, size_t new_size) { 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); vQueueDelete(uart->queue);
uart->queue = xQueueCreate(new_size, sizeof(uint8_t)); uart->queue = xQueueCreate(new_size, sizeof(uint8_t));
if(uart->queue == NULL) { if(uart->queue == NULL) {
UART_MUTEX_UNLOCK();
return NULL; return NULL;
} }
} }
@ -265,6 +266,17 @@ size_t uartResizeRxBuffer(uart_t * uart, size_t new_size) {
return 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) uint32_t uartAvailable(uart_t* uart)
{ {
if(uart == NULL || uart->queue == NULL) { 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) void uartFlush(uart_t* uart)
{ {
uartFlushTxOnly(uart,false); uartFlushTxOnly(uart,true);
} }
void uartFlushTxOnly(uart_t* uart, bool txOnly) void uartFlushTxOnly(uart_t* uart, bool txOnly)

View File

@ -52,7 +52,7 @@ struct uart_struct_t;
typedef struct uart_struct_t uart_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); 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 uartAvailable(uart_t* uart);
uint32_t uartAvailableForWrite(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); size_t uartResizeRxBuffer(uart_t* uart, size_t new_size);
void uartSetRxInvert(uart_t* uart, bool invert);
void uartSetDebug(uart_t* uart); void uartSetDebug(uart_t* uart);
int uartGetDebug(); int uartGetDebug();

View File

@ -53,6 +53,8 @@ typedef unsigned long prog_uint32_t;
*(void * const *)(_addr); \ *(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_byte_near(addr) pgm_read_byte(addr)
#define pgm_read_word_near(addr) pgm_read_word(addr) #define pgm_read_word_near(addr) pgm_read_word(addr)
#define pgm_read_dword_near(addr) pgm_read_dword(addr) #define pgm_read_dword_near(addr) pgm_read_dword(addr)

View File

@ -16,7 +16,7 @@
Boston, MA 02111-1307 USA Boston, MA 02111-1307 USA
$Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
*/ */
#include "esp32-hal.h" #include "esp32-hal.h"
#include "wiring_private.h" #include "wiring_private.h"

View File

@ -1,26 +1,28 @@
Make your question, not a Statement, inclusive. Include all pertinent information: Make your question, not a Statement, inclusive. Include all pertinent information:
What you are trying to do. What you are trying to do
Describe your system( Hardware, computer, O/S, core version, environment) Describe your system (Hardware, computer, O/S, core version, environment)
Describe what is failing Describe what is failing
Show the shortest possible code that will duplicate the error Show the shortest possible code that will duplicate the error
Show the EXACT error message(it doesn't work is not enough) 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. 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***: 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 ----------------------------- ----------------------------- Remove above -----------------------------
### Hardware: ### Hardware:
Board: ?ESP32 Dev Module? ?node32? ?ttgo_lora? |||||||
Core Installation/update date: ?11/jul/2017? |:---|---|---|---|---|---|
IDE name: ?Arduino IDE? ?Platform.io? ?IDF component? |<B>Board</B>|ESP32 Dev Module|node32|ttgo_lora|ESP32-S2-Saola|Custom w/ ESP32-S2-WROVER 16MB|
Flash Frequency: ?40Mhz? |<B>Version/Date</B>|1.0.4|2.0.0|0badbeef|11/jul/2017|today's master|
PSRAM enabled: ?no? |<B>IDE name</B>|Arduino IDE|Atom + Platform.io|IDF component|VSCode|
Upload Speed: ?115200? |<B>Flash Frequency</B>|40Mhz|80Mhz|
Computer OS: ?Windows 10? ?Mac OSX? ?Ubuntu? |<B>PSRAM enabled</B>|yes|no|
|<B>Upload Speed</B>|115200|
|<B>Computer OS</B>|Windows 10|Mac OSX|Ubuntu|
### Description: ### Description:
Describe your problem here Describe your problem here

View File

@ -4,7 +4,7 @@
- Stable release link: `https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json` - 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` - 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). - 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. - Start Arduino and open Preferences window.

View File

@ -7,9 +7,9 @@ Installation instructions for Mac OS
```bash ```bash
mkdir -p ~/Documents/Arduino/hardware/espressif && \ mkdir -p ~/Documents/Arduino/hardware/espressif && \
cd ~/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 && \ cd esp32 && \
git submodule update --init --recursive && \ git submodule update --init --recursive --depth 1 && \
cd tools && \ cd tools && \
python get.py python get.py
``` ```

View File

@ -72,6 +72,11 @@ If you are writing code that does not require Arduino to compile and you want yo
#endif #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 ## 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. 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.

View File

@ -14,7 +14,7 @@ WebServer server(80);
* Login page * Login page
*/ */
const char* loginIndex = const char* loginIndex =
"<form name='loginForm'>" "<form name='loginForm'>"
"<table width='20%' bgcolor='A09F9F' align='center'>" "<table width='20%' bgcolor='A09F9F' align='center'>"
"<tr>" "<tr>"
@ -25,8 +25,9 @@ const char* loginIndex =
"<br>" "<br>"
"<br>" "<br>"
"</tr>" "</tr>"
"<td>Username:</td>" "<tr>"
"<td><input type='text' size=25 name='userid'><br></td>" "<td>Username:</td>"
"<td><input type='text' size=25 name='userid'><br></td>"
"</tr>" "</tr>"
"<br>" "<br>"
"<br>" "<br>"
@ -54,12 +55,12 @@ const char* loginIndex =
"}" "}"
"}" "}"
"</script>"; "</script>";
/* /*
* Server Index Page * 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>" "<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'>" "<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
"<input type='file' name='update'>" "<input type='file' name='update'>"
@ -88,7 +89,7 @@ const char* serverIndex =
"return xhr;" "return xhr;"
"}," "},"
"success:function(d, s) {" "success:function(d, s) {"
"console.log('success!')" "console.log('success!')"
"}," "},"
"error: function (a, b, c) {" "error: function (a, b, c) {"
"}" "}"

View File

@ -88,6 +88,17 @@ ArduinoOTAClass& ArduinoOTAClass::setPasswordHash(const char * password) {
return *this; 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){ ArduinoOTAClass& ArduinoOTAClass::setRebootOnSuccess(bool reboot){
_rebootOnSuccess = reboot; _rebootOnSuccess = reboot;
return *this; return *this;
@ -235,7 +246,8 @@ void ArduinoOTAClass::_onRx(){
} }
void ArduinoOTAClass::_runUpdate() { 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()); log_e("Begin ERROR: %s", Update.errorString());
@ -358,7 +370,7 @@ void ArduinoOTAClass::end() {
void ArduinoOTAClass::handle() { void ArduinoOTAClass::handle() {
if (!_initialized) { if (!_initialized) {
return; return;
} }
if (_state == OTA_RUNUPDATE) { if (_state == OTA_RUNUPDATE) {
_runUpdate(); _runUpdate();

View File

@ -44,6 +44,10 @@ class ArduinoOTAClass
//Sets the password as above but in the form MD5(password). Default NULL //Sets the password as above but in the form MD5(password). Default NULL
ArduinoOTAClass& setPasswordHash(const char *password); 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 //Sets if the device should be rebooted after successful update. Default true
ArduinoOTAClass& setRebootOnSuccess(bool reboot); ArduinoOTAClass& setRebootOnSuccess(bool reboot);
@ -80,6 +84,7 @@ class ArduinoOTAClass
int _port; int _port;
String _password; String _password;
String _hostname; String _hostname;
String _partition_label;
String _nonce; String _nonce;
WiFiUDP _udp_ota; WiFiUDP _udp_ota;
bool _initialized; bool _initialized;

View File

@ -150,7 +150,7 @@ static bool _udp_task_start(){
} }
} }
if(!_udp_task_handle){ 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){ if(!_udp_task_handle){
return false; return false;
} }

View 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);
}

View File

@ -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

View File

@ -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()
{
}

View File

@ -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

View File

@ -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()
{
}

View File

@ -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

View File

@ -388,6 +388,15 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload, size_t total_len)
} // !finished } // !finished
} // parseAdvertisement } // 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. * @brief Set the address of the advertised device.

View File

@ -64,6 +64,7 @@ private:
friend class BLEScan; friend class BLEScan;
void parseAdvertisement(uint8_t* payload, size_t total_len=62); 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 setAddress(BLEAddress address);
void setAdFlag(uint8_t adFlag); void setAdFlag(uint8_t adFlag);
void setAdvertizementResult(uint8_t* payload); void setAdvertizementResult(uint8_t* payload);

View File

@ -28,7 +28,9 @@
* @brief Construct a default advertising object. * @brief Construct a default advertising object.
* *
*/ */
BLEAdvertising::BLEAdvertising() { BLEAdvertising::BLEAdvertising()
: m_scanRespData{}
{
m_advData.set_scan_rsp = false; m_advData.set_scan_rsp = false;
m_advData.include_name = true; m_advData.include_name = true;
m_advData.include_txpower = true; m_advData.include_txpower = true;
@ -215,10 +217,15 @@ void BLEAdvertising::start() {
} }
if (!m_customScanResponseData && m_scanResp) { if (!m_customScanResponseData && m_scanResp) {
m_advData.set_scan_rsp = true; // Set the configuration for scan response.
m_advData.include_name = m_scanResp; memcpy(&m_scanRespData, &m_advData, sizeof(esp_ble_adv_data_t)); // Copy the content of m_advData.
m_advData.include_txpower = m_scanResp; m_scanRespData.set_scan_rsp = true; // Define this struct as scan response data
errRc = ::esp_ble_gap_config_adv_data(&m_advData); 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) { if (errRc != ESP_OK) {
log_e("<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); log_e("<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return; return;

View File

@ -30,7 +30,7 @@ public:
void setPartialServices(BLEUUID uuid); void setPartialServices(BLEUUID uuid);
void setServiceData(BLEUUID uuid, std::string data); void setServiceData(BLEUUID uuid, std::string data);
void setShortName(std::string name); 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. std::string getPayload(); // Retrieve the current advert payload.
private: private:
@ -68,12 +68,13 @@ public:
private: private:
esp_ble_adv_data_t m_advData; 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; esp_ble_adv_params_t m_advParams;
std::vector<BLEUUID> m_serviceUUIDs; std::vector<BLEUUID> m_serviceUUIDs;
bool m_customAdvData = false; // Are we using custom advertising data? bool m_customAdvData = false; // Are we using custom advertising data?
bool m_customScanResponseData = false; // Are we using custom scan response data? bool m_customScanResponseData = false; // Are we using custom scan response data?
FreeRTOS::Semaphore m_semaphoreSetAdv = FreeRTOS::Semaphore("startAdvert"); FreeRTOS::Semaphore m_semaphoreSetAdv = FreeRTOS::Semaphore("startAdvert");
bool m_scanResp = true; bool m_scanResp = true;
}; };
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View File

@ -219,19 +219,22 @@ void BLECharacteristic::handleGATTServerEvent(
// - uint8_t exec_write_flag - Either ESP_GATT_PREP_WRITE_EXEC or ESP_GATT_PREP_WRITE_CANCEL // - uint8_t exec_write_flag - Either ESP_GATT_PREP_WRITE_EXEC or ESP_GATT_PREP_WRITE_CANCEL
// //
case ESP_GATTS_EXEC_WRITE_EVT: { case ESP_GATTS_EXEC_WRITE_EVT: {
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) { if(m_writeEvt){
m_value.commit(); m_writeEvt = false;
m_pCallbacks->onWrite(this); // Invoke the onWrite callback handler. if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
} else { m_value.commit();
m_value.cancel(); 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, esp_err_t errRc = ::esp_ble_gatts_send_response(
param->write.trans_id, ESP_GATT_OK, nullptr); gatts_if,
if (errRc != ESP_OK) { param->write.conn_id,
log_e("esp_ble_gatts_send_response: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); 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; break;
} // ESP_GATTS_EXEC_WRITE_EVT } // ESP_GATTS_EXEC_WRITE_EVT
@ -277,6 +280,7 @@ void BLECharacteristic::handleGATTServerEvent(
if (param->write.handle == m_handle) { if (param->write.handle == m_handle) {
if (param->write.is_prep) { if (param->write.is_prep) {
m_value.addPart(param->write.value, param->write.len); m_value.addPart(param->write.value, param->write.len);
m_writeEvt = true;
} else { } else {
setValue(param->write.value, param->write.len); setValue(param->write.value, param->write.len);
} }

View File

@ -107,6 +107,7 @@ private:
BLEService* m_pService; BLEService* m_pService;
BLEValue m_value; BLEValue m_value;
esp_gatt_perm_t m_permissions = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE; 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( void handleGATTServerEvent(
esp_gatts_cb_event_t event, esp_gatts_cb_event_t event,

View File

@ -105,6 +105,7 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
esp_err_t errRc = ::esp_ble_gattc_app_register(m_appId); esp_err_t errRc = ::esp_ble_gattc_app_register(m_appId);
if (errRc != ESP_OK) { if (errRc != ESP_OK) {
log_e("esp_ble_gattc_app_register: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); log_e("esp_ble_gattc_app_register: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
BLEDevice::removePeerDevice(m_appId, true);
return false; return false;
} }
@ -122,6 +123,7 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
); );
if (errRc != ESP_OK) { if (errRc != ESP_OK) {
log_e("esp_ble_gattc_open: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); log_e("esp_ble_gattc_open: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
BLEDevice::removePeerDevice(m_appId, true);
return false; return false;
} }
@ -178,12 +180,13 @@ void BLEClient::gattClientEventHandler(
// - uint16_t conn_id // - uint16_t conn_id
// - esp_bd_addr_t remote_bda // - esp_bd_addr_t remote_bda
case ESP_GATTC_DISCONNECT_EVT: { 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 // If we receive a disconnect event, set the class flag that indicates that we are
// no longer connected. // no longer connected.
m_isConnected = false; if (m_isConnected && m_pClientCallbacks != nullptr) {
if (m_pClientCallbacks != nullptr) {
m_pClientCallbacks->onDisconnect(this); m_pClientCallbacks->onDisconnect(this);
} }
m_isConnected = false;
esp_ble_gattc_app_unregister(m_gattc_if); esp_ble_gattc_app_unregister(m_gattc_if);
m_semaphoreOpenEvt.give(ESP_GATT_IF_NONE); m_semaphoreOpenEvt.give(ESP_GATT_IF_NONE);
m_semaphoreRssiCmplEvt.give(); m_semaphoreRssiCmplEvt.give();
@ -202,11 +205,13 @@ void BLEClient::gattClientEventHandler(
// //
case ESP_GATTC_OPEN_EVT: { case ESP_GATTC_OPEN_EVT: {
m_conn_id = evtParam->open.conn_id; m_conn_id = evtParam->open.conn_id;
if (m_pClientCallbacks != nullptr) {
m_pClientCallbacks->onConnect(this);
}
if (evtParam->open.status == ESP_GATT_OK) { if (evtParam->open.status == ESP_GATT_OK) {
m_isConnected = true; // Flag us as connected. 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); m_semaphoreOpenEvt.give(evtParam->open.status);
break; break;
@ -227,6 +232,7 @@ void BLEClient::gattClientEventHandler(
} // ESP_GATTC_REG_EVT } // ESP_GATTC_REG_EVT
case ESP_GATTC_CFG_MTU_EVT: case ESP_GATTC_CFG_MTU_EVT:
if (evtParam->cfg_mtu.conn_id != getConnId()) break;
if(evtParam->cfg_mtu.status != ESP_GATT_OK) { if(evtParam->cfg_mtu.status != ESP_GATT_OK) {
log_e("Config mtu failed"); log_e("Config mtu failed");
} }
@ -234,7 +240,8 @@ void BLEClient::gattClientEventHandler(
break; break;
case ESP_GATTC_CONNECT_EVT: { 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); esp_err_t errRc = esp_ble_gattc_send_mtu_req(gattc_if, evtParam->connect.conn_id);
if (errRc != ESP_OK) { if (errRc != ESP_OK) {
log_e("esp_ble_gattc_send_mtu_req: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); log_e("esp_ble_gattc_send_mtu_req: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
@ -255,6 +262,7 @@ void BLEClient::gattClientEventHandler(
// - uint16_t conn_id // - uint16_t conn_id
// //
case ESP_GATTC_SEARCH_CMPL_EVT: { 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; esp_ble_gattc_cb_param_t* p_data = (esp_ble_gattc_cb_param_t*)evtParam;
if (p_data->search_cmpl.status != ESP_GATT_OK){ if (p_data->search_cmpl.status != ESP_GATT_OK){
log_e("search service failed, error status = %x", p_data->search_cmpl.status); log_e("search service failed, error status = %x", p_data->search_cmpl.status);
@ -285,6 +293,7 @@ void BLEClient::gattClientEventHandler(
// - esp_gatt_id_t srvc_id // - esp_gatt_id_t srvc_id
// //
case ESP_GATTC_SEARCH_RES_EVT: { case ESP_GATTC_SEARCH_RES_EVT: {
if (evtParam->search_res.conn_id != getConnId()) break;
BLEUUID uuid = BLEUUID(evtParam->search_res.srvc_id); BLEUUID uuid = BLEUUID(evtParam->search_res.srvc_id);
BLERemoteService* pRemoteService = new BLERemoteService( BLERemoteService* pRemoteService = new BLERemoteService(
evtParam->search_res.srvc_id, evtParam->search_res.srvc_id,

View File

@ -3,6 +3,11 @@
* *
* Created on: Mar 12, 2018 * Created on: Mar 12, 2018
* Author: pcbreflux * 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" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
@ -20,7 +25,7 @@ BLEEddystoneTLM::BLEEddystoneTLM() {
m_eddystoneData.frameType = EDDYSTONE_TLM_FRAME_TYPE; m_eddystoneData.frameType = EDDYSTONE_TLM_FRAME_TYPE;
m_eddystoneData.version = 0; m_eddystoneData.version = 0;
m_eddystoneData.volt = 3300; // 3300mV = 3.3V 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.advCount = 0;
m_eddystoneData.tmil = 0; m_eddystoneData.tmil = 0;
} // BLEEddystoneTLM } // BLEEddystoneTLM
@ -38,41 +43,50 @@ uint8_t BLEEddystoneTLM::getVersion() {
} // getVersion } // getVersion
uint16_t BLEEddystoneTLM::getVolt() { uint16_t BLEEddystoneTLM::getVolt() {
return m_eddystoneData.volt; return ENDIAN_CHANGE_U16(m_eddystoneData.volt);
} // getVolt } // getVolt
float BLEEddystoneTLM::getTemp() { float BLEEddystoneTLM::getTemp() {
return (float)m_eddystoneData.temp; return ENDIAN_CHANGE_U16(m_eddystoneData.temp) / 256.0f;
} // getTemp } // getTemp
uint32_t BLEEddystoneTLM::getCount() { uint32_t BLEEddystoneTLM::getCount() {
return m_eddystoneData.advCount; return ENDIAN_CHANGE_U32(m_eddystoneData.advCount);
} // getCount } // getCount
uint32_t BLEEddystoneTLM::getTime() { uint32_t BLEEddystoneTLM::getTime() {
return m_eddystoneData.tmil; return (ENDIAN_CHANGE_U32(m_eddystoneData.tmil)) / 10;
} // getTime } // getTime
std::string BLEEddystoneTLM::toString() { std::string BLEEddystoneTLM::toString() {
std::string out = ""; std::string out = "";
uint32_t rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil); 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 += "\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 += " mV\n";
out += "Temperature "; 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 += val;
out += ".0 °C\n"; out += " C\n";
out += "Adv. Count "; out += "Adv. Count ";
snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U32(m_eddystoneData.advCount)); snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U32(m_eddystoneData.advCount));
out += val; out += val;
out += "\n"; out += "\n";
out += "Time in seconds ";
snprintf(val, sizeof(val), "%d", rawsec/10);
out += val;
out += "\n";
out += "Time "; out += "Time ";
snprintf(val, sizeof(val), "%04d", rawsec / 864000); snprintf(val, sizeof(val), "%04d", rawsec / 864000);

View File

@ -194,6 +194,7 @@ BLECharacteristic* BLEHIDDevice::protocolMode() {
void BLEHIDDevice::setBatteryLevel(uint8_t level) { void BLEHIDDevice::setBatteryLevel(uint8_t level) {
m_batteryLevelCharacteristic->setValue(&level, 1); m_batteryLevelCharacteristic->setValue(&level, 1);
m_batteryLevelCharacteristic->notify();
} }
/* /*
* @brief Returns battery level characteristic * @brief Returns battery level characteristic

View File

@ -237,6 +237,13 @@ void BLERemoteCharacteristic::gattClientEventHandler(esp_gattc_cb_event_t event,
break; break;
} // ESP_GATTC_WRITE_CHAR_EVT } // 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: default:
break; break;
@ -467,7 +474,8 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback,
uint8_t val[] = {0x01, 0x00}; uint8_t val[] = {0x01, 0x00};
if(!notifications) val[0] = 0x02; if(!notifications) val[0] = 0x02;
BLERemoteDescriptor* desc = getDescriptor(BLEUUID((uint16_t)0x2902)); BLERemoteDescriptor* desc = getDescriptor(BLEUUID((uint16_t)0x2902));
desc->writeValue(val, 2); if (desc != nullptr)
desc->writeValue(val, 2, true);
} // End Register } // End Register
else { // If we weren't passed a callback function, then this is an unregistration. else { // If we weren't passed a callback function, then this is an unregistration.
esp_err_t errRc = ::esp_ble_gattc_unregister_for_notify( esp_err_t errRc = ::esp_ble_gattc_unregister_for_notify(
@ -482,7 +490,8 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback,
uint8_t val[] = {0x00, 0x00}; uint8_t val[] = {0x00, 0x00};
BLERemoteDescriptor* desc = getDescriptor((uint16_t)0x2902); BLERemoteDescriptor* desc = getDescriptor((uint16_t)0x2902);
desc->writeValue(val, 2); if (desc != nullptr)
desc->writeValue(val, 2, true);
} // End Unregister } // End Unregister
m_semaphoreRegForNotifyEvt.wait("registerForNotify"); m_semaphoreRegForNotifyEvt.wait("registerForNotify");
@ -533,7 +542,7 @@ std::string BLERemoteCharacteristic::toString() {
* @return N/A. * @return N/A.
*/ */
void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) { 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 } // writeValue

View File

@ -49,6 +49,23 @@ BLEUUID BLERemoteDescriptor::getUUID() {
return m_uuid; return m_uuid;
} // getUUID } // 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() { std::string BLERemoteDescriptor::readValue() {
log_v(">> readValue: %s", toString().c_str()); log_v(">> readValue: %s", toString().c_str());
@ -137,6 +154,8 @@ void BLERemoteDescriptor::writeValue(uint8_t* data, size_t length, bool response
return; return;
} }
m_semaphoreWriteDescrEvt.take("writeValue");
esp_err_t errRc = ::esp_ble_gattc_write_char_descr( esp_err_t errRc = ::esp_ble_gattc_write_char_descr(
m_pRemoteCharacteristic->getRemoteService()->getClient()->getGattcIf(), m_pRemoteCharacteristic->getRemoteService()->getClient()->getGattcIf(),
m_pRemoteCharacteristic->getRemoteService()->getClient()->getConnId(), m_pRemoteCharacteristic->getRemoteService()->getClient()->getConnId(),
@ -149,6 +168,8 @@ void BLERemoteDescriptor::writeValue(uint8_t* data, size_t length, bool response
if (errRc != ESP_OK) { if (errRc != ESP_OK) {
log_e("esp_ble_gattc_write_char_descr: %d", errRc); log_e("esp_ble_gattc_write_char_descr: %d", errRc);
} }
m_semaphoreWriteDescrEvt.wait("writeValue");
log_v("<< writeValue"); log_v("<< writeValue");
} // writeValue } // writeValue

View File

@ -35,7 +35,7 @@ public:
void writeValue(std::string newValue, bool response = false); void writeValue(std::string newValue, bool response = false);
void writeValue(uint8_t newValue, bool response = false); void writeValue(uint8_t newValue, bool response = false);
void setAuth(esp_gatt_auth_req_t auth); 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: private:
friend class BLERemoteCharacteristic; friend class BLERemoteCharacteristic;
@ -49,6 +49,7 @@ private:
std::string m_value; // Last received value of the descriptor. std::string m_value; // Last received value of the descriptor.
BLERemoteCharacteristic* m_pRemoteCharacteristic; // Reference to the Remote characteristic of which this descriptor is associated. BLERemoteCharacteristic* m_pRemoteCharacteristic; // Reference to the Remote characteristic of which this descriptor is associated.
FreeRTOS::Semaphore m_semaphoreReadDescrEvt = FreeRTOS::Semaphore("ReadDescrEvt"); FreeRTOS::Semaphore m_semaphoreReadDescrEvt = FreeRTOS::Semaphore("ReadDescrEvt");
FreeRTOS::Semaphore m_semaphoreWriteDescrEvt = FreeRTOS::Semaphore("WriteDescrEvt");
esp_gatt_auth_req_t m_auth; esp_gatt_auth_req_t m_auth;

View File

@ -244,7 +244,15 @@ 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 * @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) { void BLERemoteService::getCharacteristics(std::map<uint16_t, BLERemoteCharacteristic*>* pCharacteristicMap) {
pCharacteristicMap = &m_characteristicMapByHandle; log_v(">> getCharacteristics() for service: %s", getUUID().toString().c_str());
// 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. } // Get the characteristics map.
/** /**
@ -302,13 +310,10 @@ std::string BLERemoteService::getValue(BLEUUID characteristicUuid) {
* @return N/A. * @return N/A.
*/ */
void BLERemoteService::removeCharacteristics() { 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 m_characteristicMap.clear(); // Clear the map
for (auto &myPair : m_characteristicMapByHandle) { for (auto &myPair : m_characteristicMapByHandle) {
delete myPair.second; delete myPair.second;
// delete the characteristics only once
} }
m_characteristicMapByHandle.clear(); // Clear the map m_characteristicMapByHandle.clear(); // Clear the map
} // removeCharacteristics } // removeCharacteristics

View File

@ -22,12 +22,15 @@
* Constructor * Constructor
*/ */
BLEScan::BLEScan() { 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.scan_type = BLE_SCAN_TYPE_PASSIVE; // Default is a passive scan.
m_scan_params.own_addr_type = BLE_ADDR_TYPE_PUBLIC; 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_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL;
m_scan_params.scan_duplicate = BLE_SCAN_DUPLICATE_DISABLE;
m_pAdvertisedDeviceCallbacks = nullptr; m_pAdvertisedDeviceCallbacks = nullptr;
m_stopped = true; m_stopped = true;
m_wantDuplicates = false; m_wantDuplicates = false;
m_shouldParse = true;
setInterval(100); setInterval(100);
setWindow(100); setWindow(100);
} // BLEScan } // BLEScan
@ -88,15 +91,18 @@ void BLEScan::handleGAPEvent(
// ignore it. // ignore it.
BLEAddress advertisedAddress(param->scan_rst.bda); BLEAddress advertisedAddress(param->scan_rst.bda);
bool found = false; bool found = false;
bool shouldDelete = true;
if (m_scanResults.m_vectorAdvertisedDevices.count(advertisedAddress.toString()) != 0) { if (!m_wantDuplicates) {
found = true; 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. 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()); 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 vTaskDelay(1); // <--- allow to switch task in case we scan infinity and dont have new devices to report, or we are blocked here
break; break;
}
} }
// We now construct a model of the advertised device that we have just found for the first // 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->setAddress(advertisedAddress);
advertisedDevice->setRSSI(param->scan_rst.rssi); advertisedDevice->setRSSI(param->scan_rst.rssi);
advertisedDevice->setAdFlag(param->scan_rst.flag); 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->setScan(this);
advertisedDevice->setAddressType(param->scan_rst.ble_addr_type); advertisedDevice->setAddressType(param->scan_rst.ble_addr_type);
if (!found) { // If we have previously seen this device, don't record it again. if (m_pAdvertisedDeviceCallbacks) { // if has callback, no need to record to vector
m_scanResults.m_vectorAdvertisedDevices.insert(std::pair<std::string, BLEAdvertisedDevice*>(advertisedAddress.toString(), advertisedDevice));
}
if (m_pAdvertisedDeviceCallbacks) {
m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice); 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; delete advertisedDevice;
}
break; break;
} // ESP_GAP_SEARCH_INQ_RES_EVT } // ESP_GAP_SEARCH_INQ_RES_EVT
@ -159,13 +169,14 @@ void BLEScan::setActiveScan(bool active) {
* @brief Set the call backs to be invoked. * @brief Set the call backs to be invoked.
* @param [in] pAdvertisedDeviceCallbacks 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] 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_wantDuplicates = wantDuplicates;
m_pAdvertisedDeviceCallbacks = pAdvertisedDeviceCallbacks; m_pAdvertisedDeviceCallbacks = pAdvertisedDeviceCallbacks;
m_shouldParse = shouldParse;
} // setAdvertisedDeviceCallbacks } // setAdvertisedDeviceCallbacks
/** /**
* @brief Set the interval to scan. * @brief Set the interval to scan.
* @param [in] The interval in msecs. * @param [in] The interval in msecs.

View File

@ -51,7 +51,8 @@ public:
void setActiveScan(bool active); void setActiveScan(bool active);
void setAdvertisedDeviceCallbacks( void setAdvertisedDeviceCallbacks(
BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks, BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks,
bool wantDuplicates = false); bool wantDuplicates = false,
bool shouldParse = true);
void setInterval(uint16_t intervalMSecs); void setInterval(uint16_t intervalMSecs);
void setWindow(uint16_t windowMSecs); void setWindow(uint16_t windowMSecs);
bool start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults), bool is_continue = false); 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; esp_ble_scan_params_t m_scan_params;
BLEAdvertisedDeviceCallbacks* m_pAdvertisedDeviceCallbacks = nullptr; BLEAdvertisedDeviceCallbacks* m_pAdvertisedDeviceCallbacks = nullptr;
bool m_stopped = true; bool m_stopped = true;
bool m_shouldParse = true;
FreeRTOS::Semaphore m_semaphoreScanEnd = FreeRTOS::Semaphore("ScanEnd"); FreeRTOS::Semaphore m_semaphoreScanEnd = FreeRTOS::Semaphore("ScanEnd");
BLEScanResults m_scanResults; BLEScanResults m_scanResults;
bool m_wantDuplicates; bool m_wantDuplicates;

View File

@ -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)); esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &m_keySize, sizeof(uint8_t));
} //setKeySize } //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 * @brief Debug function to display what keys are exchanged by peers

View File

@ -21,6 +21,7 @@ public:
void setInitEncryptionKey(uint8_t init_key); void setInitEncryptionKey(uint8_t init_key);
void setRespEncryptionKey(uint8_t resp_key); void setRespEncryptionKey(uint8_t resp_key);
void setKeySize(uint8_t key_size = 16); 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); static char* esp_key_type_to_str(esp_ble_key_type_t key_type);
private: private:

View File

@ -193,8 +193,8 @@ bool FreeRTOS::Semaphore::take(std::string owner) {
} else { } else {
rc = ::xSemaphoreTake(m_semaphore, portMAX_DELAY) == pdTRUE; rc = ::xSemaphoreTake(m_semaphore, portMAX_DELAY) == pdTRUE;
} }
m_owner = owner;
if (rc) { if (rc) {
m_owner = owner;
log_v("Semaphore taken: %s", toString().c_str()); log_v("Semaphore taken: %s", toString().c_str());
} else { } else {
log_e("Semaphore NOT taken: %s", toString().c_str()); log_e("Semaphore NOT taken: %s", toString().c_str());
@ -218,8 +218,8 @@ bool FreeRTOS::Semaphore::take(uint32_t timeoutMs, std::string owner) {
} else { } else {
rc = ::xSemaphoreTake(m_semaphore, timeoutMs / portTICK_PERIOD_MS) == pdTRUE; rc = ::xSemaphoreTake(m_semaphore, timeoutMs / portTICK_PERIOD_MS) == pdTRUE;
} }
m_owner = owner;
if (rc) { if (rc) {
m_owner = owner;
log_v("Semaphore taken: %s", toString().c_str()); log_v("Semaphore taken: %s", toString().c_str());
} else { } else {
log_e("Semaphore NOT taken: %s", toString().c_str()); log_e("Semaphore NOT taken: %s", toString().c_str());

View File

@ -2,7 +2,7 @@
#include <DNSServer.h> #include <DNSServer.h>
const byte DNS_PORT = 53; const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 1, 1); IPAddress apIP(8,8,4,4); // The default android DNS
DNSServer dnsServer; DNSServer dnsServer;
WiFiServer server(80); WiFiServer server(80);
@ -12,11 +12,9 @@ String responseHTML = ""
"be redirected here.</p></body></html>"; "be redirected here.</p></body></html>";
void setup() { 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.mode(WIFI_AP);
WiFi.softAP("ESP32-DNSServer");
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); 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 // if DNSServer is started with "*" for domain name, it will reply with
// provided IP to all DNS request // provided IP to all DNS request

View File

@ -2,6 +2,12 @@
#include <lwip/def.h> #include <lwip/def.h>
#include <Arduino.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() DNSServer::DNSServer()
{ {
@ -184,6 +190,11 @@ void DNSServer::replyWithIP()
_udp.write((unsigned char*) &answerIPv4, 2 ); _udp.write((unsigned char*) &answerIPv4, 2 );
_udp.write(_resolvedIP, sizeof(_resolvedIP)); // The IP address to return _udp.write(_resolvedIP, sizeof(_resolvedIP)); // The IP address to return
_udp.endPacket(); _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() void DNSServer::replyWithCustomCode()

View File

@ -17,6 +17,7 @@ EEPROMClass AGE("eeprom2", 0x100);
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
delay(1000);
Serial.println("Testing EEPROMClass\n"); Serial.println("Testing EEPROMClass\n");
if (!NAMES.begin(NAMES.length())) { if (!NAMES.begin(NAMES.length())) {
Serial.println("Failed to initialise NAMES"); Serial.println("Failed to initialise NAMES");
@ -43,7 +44,7 @@ void setup() {
uint32_t age = 47; uint32_t age = 47;
// Write: Variables ---> EEPROM stores // Write: Variables ---> EEPROM stores
NAMES.put(0, name); NAMES.writeString(0, name);
HEIGHT.put(0, height); HEIGHT.put(0, height);
AGE.put(0, age); AGE.put(0, age);
Serial.print("name: "); Serial.println(name); Serial.print("name: "); Serial.println(name);

View File

@ -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() { void setup() {
Serial.begin(115200); Serial.begin(115200);
} }
void loop() { void loop() {
chipid=ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes). for(int i=0; i<17; i=i+8) {
Serial.printf("ESP32 Chip ID = %04X",(uint16_t)(chipid>>32));//print High 2 bytes chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
Serial.printf("%08X\n",(uint32_t)chipid);//print Low 4bytes. }
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); delay(3000);
} }

View 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);
}

View File

@ -16,6 +16,7 @@ static EventGroupHandle_t events;
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
events = xEventGroupCreate();
if ((rmt_send = rmtInit(18, true, RMT_MEM_64)) == NULL) if ((rmt_send = rmtInit(18, true, RMT_MEM_64)) == NULL)
{ {

View File

@ -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); parseRmt((rmt_data_t*) data, len, channels);
} }
@ -192,7 +192,7 @@ void setup()
Serial.printf("real tick set to: %fns\n", realTick); Serial.printf("real tick set to: %fns\n", realTick);
// Ask to start reading // Ask to start reading
rmtRead(rmt_recv, receive_data); rmtRead(rmt_recv, receive_data, NULL);
} }
void loop() void loop()

View File

@ -47,7 +47,7 @@ void setup(void)
// Set up mDNS responder: // Set up mDNS responder:
// - first argument is the domain name, in this example // - 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 // - second argument is the IP address to advertise
// we send our IP address on the WiFi network // we send our IP address on the WiFi network
if (!MDNS.begin("esp32")) { if (!MDNS.begin("esp32")) {

View File

@ -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 _name[strlen(name)+2];
char _proto[strlen(proto)+2]; char _proto[strlen(proto)+2];
if (name[0] == '_') { 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)) { if(mdns_service_add(NULL, _name, _proto, port, NULL, 0)) {
log_e("Failed adding service %s.%s.\n", name, proto); 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){ bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *value){

View File

@ -65,12 +65,12 @@ public:
setInstanceName(String(name)); setInstanceName(String(name));
} }
void addService(char *service, char *proto, uint16_t port); bool addService(char *service, char *proto, uint16_t port);
void addService(const char *service, const char *proto, uint16_t port){ bool addService(const char *service, const char *proto, uint16_t port){
addService((char *)service, (char *)proto, port); return addService((char *)service, (char *)proto, port);
} }
void addService(String service, String proto, uint16_t port){ bool addService(String service, String proto, uint16_t port){
addService(service.c_str(), proto.c_str(), port); return addService(service.c_str(), proto.c_str(), port);
} }
bool addServiceTxt(char *name, char *proto, char * key, char * value); bool addServiceTxt(char *name, char *proto, char * key, char * value);

View File

@ -135,6 +135,21 @@ size_t F_Fat::totalBytes()
return tot_sect * sect_size; 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() size_t F_Fat::freeBytes()
{ {

View File

@ -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 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); bool format(bool full_wipe = FFAT_WIPE_QUICK, char* partitionLabel = (char*)FFAT_PARTITION_LABEL);
size_t totalBytes(); size_t totalBytes();
size_t usedBytes();
size_t freeBytes(); size_t freeBytes();
void end(); void end();
bool exists(const char* path); bool exists(const char* path);

View File

@ -184,6 +184,11 @@ bool VFSImpl::rmdir(const char *path)
return false; return false;
} }
if (strcmp(_mountpoint, "/spiffs") == 0) {
log_e("rmdir is unnecessary in SPIFFS");
return false;
}
VFSFileImpl f(this, path, "r"); VFSFileImpl f(this, path, "r");
if(!f || !f.isDirectory()) { if(!f || !f.isDirectory()) {
if(f) { if(f) {
@ -200,7 +205,7 @@ bool VFSImpl::rmdir(const char *path)
return false; return false;
} }
sprintf(temp,"%s%s", _mountpoint, path); sprintf(temp,"%s%s", _mountpoint, path);
auto rc = unlink(temp); auto rc = ::rmdir(temp);
free(temp); free(temp);
return rc == 0; return rc == 0;
} }

View File

@ -548,29 +548,106 @@ int HTTPClient::sendRequest(const char * type, String payload)
*/ */
int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size) int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size)
{ {
// connect to server int code;
if(!connect()) { bool redirect = false;
return returnError(HTTPC_ERROR_CONNECTION_REFUSED); uint16_t redirectCount = 0;
} do {
// wipe out any existing headers from previous request
if(payload && size > 0) { for(size_t i = 0; i < _headerKeysCount; i++) {
addHeader(F("Content-Length"), String(size)); if (_currentHeaders[i].value.length() > 0) {
} _currentHeaders[i].value.clear();
}
// 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);
} }
}
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) // handle Server Response (Header)
return returnError(handleHeaderResponse()); return returnError(code);
} }
/** /**
@ -842,18 +919,19 @@ int HTTPClient::writeToStream(Stream * stream)
*/ */
String HTTPClient::getString(void) String HTTPClient::getString(void)
{ {
StreamString sstring; // _size can be -1 when Server sends no Content-Length header
if(_size > 0 || _size == -1) {
if(_size) { StreamString sstring;
// try to reserve needed memmory // try to reserve needed memory (noop if _size == -1)
if(!sstring.reserve((_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)); log_d("not enough memory to reserve a string! need: %d", (_size + 1));
return "";
} }
} }
writeToStream(&sstring); return "";
return sstring;
} }
/** /**
@ -1012,7 +1090,13 @@ bool HTTPClient::connect(void)
log_d("HTTPClient::begin was not called or returned error"); log_d("HTTPClient::begin was not called or returned error");
return false; 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)) { if(!_client->connect(_host.c_str(), _port, _connectTimeout)) {
log_d("failed connect to %s:%u", _host.c_str(), _port); log_d("failed connect to %s:%u", _host.c_str(), _port);
return false; return false;
@ -1023,14 +1107,6 @@ bool HTTPClient::connect(void)
log_d(" connected to %s:%u", _host.c_str(), _port); 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 #ifdef ESP8266
@ -1110,6 +1186,7 @@ int HTTPClient::handleHeaderResponse()
_transferEncoding = HTTPC_TE_IDENTITY; _transferEncoding = HTTPC_TE_IDENTITY;
unsigned long lastDataTime = millis(); unsigned long lastDataTime = millis();
bool firstLine = true;
while(connected()) { while(connected()) {
size_t len = _client->available(); size_t len = _client->available();
@ -1121,11 +1198,13 @@ int HTTPClient::handleHeaderResponse()
log_v("RX: '%s'", headerLine.c_str()); log_v("RX: '%s'", headerLine.c_str());
if(headerLine.startsWith("HTTP/1.")) { if(firstLine) {
if(_canReuse) { firstLine = false;
if(_canReuse && headerLine.startsWith("HTTP/1.")) {
_canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0'); _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(':')) { } else if(headerLine.indexOf(':')) {
String headerName = headerLine.substring(0, headerLine.indexOf(':')); String headerName = headerLine.substring(0, headerLine.indexOf(':'));
String headerValue = headerLine.substring(headerLine.indexOf(':') + 1); String headerValue = headerLine.substring(headerLine.indexOf(':') + 1);
@ -1145,6 +1224,10 @@ int HTTPClient::handleHeaderResponse()
transferEncoding = headerValue; transferEncoding = headerValue;
} }
if (headerName.equalsIgnoreCase("Location")) {
_location = headerValue;
}
for(size_t i = 0; i < _headerKeysCount; i++) { for(size_t i = 0; i < _headerKeysCount; i++) {
if(_currentHeaders[i].key.equalsIgnoreCase(headerName)) { if(_currentHeaders[i].key.equalsIgnoreCase(headerName)) {
_currentHeaders[i].value = headerValue; _currentHeaders[i].value = headerValue;
@ -1322,3 +1405,52 @@ int HTTPClient::returnError(int error)
} }
return 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;
}

View File

@ -119,6 +119,24 @@ typedef enum {
HTTPC_TE_CHUNKED HTTPC_TE_CHUNKED
} transferEncoding_t; } 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 #ifdef HTTPCLIENT_1_1_COMPATIBLE
class TransportTraits; class TransportTraits;
typedef std::unique_ptr<TransportTraits> TransportTraitsPtr; typedef std::unique_ptr<TransportTraits> TransportTraitsPtr;
@ -156,6 +174,11 @@ public:
void setConnectTimeout(int32_t connectTimeout); void setConnectTimeout(int32_t connectTimeout);
void setTimeout(uint16_t timeout); 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); void useHTTP10(bool usehttp10 = true);
/// request handling /// request handling
@ -182,6 +205,7 @@ public:
int getSize(void); int getSize(void);
const String &getLocation(void);
WiFiClient& getStream(void); WiFiClient& getStream(void);
WiFiClient* getStreamPtr(void); WiFiClient* getStreamPtr(void);
@ -235,6 +259,9 @@ protected:
int _returnCode = 0; int _returnCode = 0;
int _size = -1; int _size = -1;
bool _canReuse = false; bool _canReuse = false;
followRedirects_t _followRedirects = HTTPC_DISABLE_FOLLOW_REDIRECTS;
uint16_t _redirectLimit = 10;
String _location;
transferEncoding_t _transferEncoding = HTTPC_TE_IDENTITY; transferEncoding_t _transferEncoding = HTTPC_TE_IDENTITY;
}; };

View File

@ -52,7 +52,7 @@ void loop() {
t_httpUpdate_return ret = httpUpdate.update(client, "http://server/file.bin"); t_httpUpdate_return ret = httpUpdate.update(client, "http://server/file.bin");
// Or: // 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) { switch (ret) {
case HTTP_UPDATE_FAILED: case HTTP_UPDATE_FAILED:

View File

@ -108,7 +108,7 @@ void loop() {
t_httpUpdate_return ret = httpUpdate.update(client, "https://server/file.bin"); t_httpUpdate_return ret = httpUpdate.update(client, "https://server/file.bin");
// Or: // 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) { switch (ret) {

View File

@ -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();
}

View File

@ -0,0 +1,20 @@
#######################################
# Syntax Coloring Map For HTTPUpdateServer
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
ESP32HTTPUpdateServer KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
begin KEYWORD2
setup KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################

View 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

View 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

View File

@ -15,7 +15,7 @@
#include "nvs.h" #include "nvs.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]) #define nvs_error(e) (((e)>ESP_ERR_NVS_BASE)?nvs_errors[(e)&~(ESP_ERR_NVS_BASE)]:nvs_errors[0])
Preferences::Preferences() Preferences::Preferences()
@ -280,6 +280,30 @@ size_t Preferences::putBytes(const char* key, const void* value, size_t len){
return 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 * Get a key value
* */ * */

View File

@ -16,6 +16,10 @@
#include "Arduino.h" #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 { class Preferences {
protected: protected:
uint32_t _handle; uint32_t _handle;
@ -48,6 +52,8 @@ class Preferences {
size_t putString(const char* key, String value); size_t putString(const char* key, String value);
size_t putBytes(const char* key, const void* value, size_t len); 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); int8_t getChar(const char* key, int8_t defaultValue = 0);
uint8_t getUChar(const char* key, uint8_t defaultValue = 0); uint8_t getUChar(const char* key, uint8_t defaultValue = 0);
int16_t getShort(const char* key, int16_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); bool getBool(const char* key, bool defaultValue = false);
size_t getString(const char* key, char* value, size_t maxLen); size_t getString(const char* key, char* value, size_t maxLen);
String getString(const char* key, String defaultValue = String()); 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 getBytes(const char* key, void * buf, size_t maxLen);
size_t freeEntries(); size_t freeEntries();
}; };

View File

@ -607,8 +607,9 @@ DRESULT ff_sd_write(uint8_t pdrv, const uint8_t* buffer, DWORD sector, UINT coun
if (count > 1) { if (count > 1) {
res = sdWriteSectors(pdrv, (const char*)buffer, sector, count) ? RES_OK : RES_ERROR; 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; return res;
} }
@ -648,6 +649,7 @@ uint8_t sdcard_uninit(uint8_t pdrv)
if (pdrv >= FF_VOLUMES || card == NULL) { if (pdrv >= FF_VOLUMES || card == NULL) {
return 1; return 1;
} }
sdTransaction(pdrv, GO_IDLE_STATE, 0, NULL);
ff_diskio_register(pdrv, NULL); ff_diskio_register(pdrv, NULL);
s_cards[pdrv] = NULL; s_cards[pdrv] = NULL;
esp_err_t err = ESP_OK; esp_err_t err = ESP_OK;

View File

@ -1,5 +1,5 @@
/* /*
SPI.h - SPI library for esp8266 SPI.h - SPI library for esp32
Copyright (c) 2015 Hristo Gochkov. All rights reserved. Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment. This file is part of the esp8266 core for Arduino environment.
@ -25,6 +25,8 @@
#include "pins_arduino.h" #include "pins_arduino.h"
#include "esp32-hal-spi.h" #include "esp32-hal-spi.h"
#define SPI_HAS_TRANSACTION
class SPISettings class SPISettings
{ {
public: public:

View File

@ -43,21 +43,38 @@ bool SPIFFSImpl::exists(const char* path)
return (f == true) && !f.isDirectory(); 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!"); log_w("SPIFFS Already Mounted!");
return true; return true;
} }
esp_vfs_spiffs_conf_t conf = { esp_vfs_spiffs_conf_t conf = {
.base_path = basePath, .base_path = basePath,
.partition_label = NULL, .partition_label = partitionLabel_,
.max_files = maxOpenFiles, .max_files = maxOpenFiles,
.format_if_mount_failed = false .format_if_mount_failed = false
}; };
@ -78,8 +95,8 @@ bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFi
void SPIFFSFS::end() void SPIFFSFS::end()
{ {
if(esp_spiffs_mounted(NULL)){ if(esp_spiffs_mounted(partitionLabel_)){
esp_err_t err = esp_vfs_spiffs_unregister(NULL); esp_err_t err = esp_vfs_spiffs_unregister(partitionLabel_);
if(err){ if(err){
log_e("Unmounting SPIFFS failed! Error: %d", err); log_e("Unmounting SPIFFS failed! Error: %d", err);
return; return;
@ -91,7 +108,7 @@ void SPIFFSFS::end()
bool SPIFFSFS::format() bool SPIFFSFS::format()
{ {
disableCore0WDT(); disableCore0WDT();
esp_err_t err = esp_spiffs_format(NULL); esp_err_t err = esp_spiffs_format(partitionLabel_);
enableCore0WDT(); enableCore0WDT();
if(err){ if(err){
log_e("Formatting SPIFFS failed! Error: %d", err); log_e("Formatting SPIFFS failed! Error: %d", err);
@ -103,7 +120,7 @@ bool SPIFFSFS::format()
size_t SPIFFSFS::totalBytes() size_t SPIFFSFS::totalBytes()
{ {
size_t total,used; size_t total,used;
if(esp_spiffs_info(NULL, &total, &used)){ if(esp_spiffs_info(partitionLabel_, &total, &used)){
return 0; return 0;
} }
return total; return total;
@ -112,7 +129,7 @@ size_t SPIFFSFS::totalBytes()
size_t SPIFFSFS::usedBytes() size_t SPIFFSFS::usedBytes()
{ {
size_t total,used; size_t total,used;
if(esp_spiffs_info(NULL, &total, &used)){ if(esp_spiffs_info(partitionLabel_, &total, &used)){
return 0; return 0;
} }
return used; return used;

View File

@ -23,11 +23,15 @@ class SPIFFSFS : public FS
{ {
public: public:
SPIFFSFS(); 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(); bool format();
size_t totalBytes(); size_t totalBytes();
size_t usedBytes(); size_t usedBytes();
void end(); void end();
private:
char * partitionLabel_;
}; };
} }

View File

@ -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);
}

View 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().

View 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;

View File

@ -0,0 +1,21 @@
#include "esp_http_client.h"
#define HttpEvent_t esp_http_client_event_t
typedef enum
{
HTTPS_OTA_IDLE,
HTTPS_OTA_UPDATING,
HTTPS_OTA_SUCCESS,
HTTPS_OTA_FAIL,
HTTPS_OTA_ERR
}HttpsOTAStatus_t;
class HttpsOTAUpdateClass {
public:
void begin(const char *url, const char *cert_pem, bool skip_cert_common_name_check = true);
void onHttpEvent(void (*http_event_cb_t)(HttpEvent_t *));
HttpsOTAStatus_t status();
};
extern HttpsOTAUpdateClass HttpsOTA;

View File

@ -26,6 +26,8 @@
#define U_SPIFFS 100 #define U_SPIFFS 100
#define U_AUTH 200 #define U_AUTH 200
#define ENCRYPTED_BLOCK_SIZE 16
class UpdateClass { class UpdateClass {
public: public:
typedef std::function<void(size_t, size_t)> THandlerFunction_Progress; typedef std::function<void(size_t, size_t)> THandlerFunction_Progress;
@ -41,7 +43,7 @@ class UpdateClass {
Call this to check the space needed for the update Call this to check the space needed for the update
Will return false if there is not enough space Will return false if there is not enough space
*/ */
bool begin(size_t size=UPDATE_SIZE_UNKNOWN, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW); bool begin(size_t size=UPDATE_SIZE_UNKNOWN, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW, const char *label = NULL);
/* /*
Writes a buffer to the flash and increments the address Writes a buffer to the flash and increments the address
@ -78,7 +80,7 @@ class UpdateClass {
/* /*
Prints the last error to an output stream Prints the last error to an output stream
*/ */
void printError(Stream &out); void printError(Print &out);
const char * errorString(); const char * errorString();
@ -88,12 +90,12 @@ class UpdateClass {
bool setMD5(const char * expected_md5); bool setMD5(const char * expected_md5);
/* /*
returns the MD5 String of the sucessfully ended firmware returns the MD5 String of the successfully ended firmware
*/ */
String md5String(void){ return _md5.toString(); } String md5String(void){ return _md5.toString(); }
/* /*
populated the result with the md5 bytes of the sucessfully ended firmware populated the result with the md5 bytes of the successfully ended firmware
*/ */
void md5(uint8_t * result){ return _md5.getBytes(result); } void md5(uint8_t * result){ return _md5.getBytes(result); }
@ -163,10 +165,12 @@ class UpdateClass {
bool _writeBuffer(); bool _writeBuffer();
bool _verifyHeader(uint8_t data); bool _verifyHeader(uint8_t data);
bool _verifyEnd(); bool _verifyEnd();
bool _enablePartition(const esp_partition_t* partition);
uint8_t _error; uint8_t _error;
uint8_t *_buffer; uint8_t *_buffer;
uint8_t *_skipBuffer;
size_t _bufferLen; size_t _bufferLen;
size_t _size; size_t _size;
THandlerFunction_Progress _progress_callback; THandlerFunction_Progress _progress_callback;

View File

@ -36,11 +36,11 @@ static const char * _err2str(uint8_t _error){
} }
static bool _partitionIsBootable(const esp_partition_t* partition){ static bool _partitionIsBootable(const esp_partition_t* partition){
uint8_t buf[4]; uint8_t buf[ENCRYPTED_BLOCK_SIZE];
if(!partition){ if(!partition){
return false; return false;
} }
if(!ESP.flashRead(partition->address, (uint32_t*)buf, 4)) { if(!ESP.partitionRead(partition, 0, (uint32_t*)buf, ENCRYPTED_BLOCK_SIZE)) {
return false; return false;
} }
@ -50,17 +50,11 @@ static bool _partitionIsBootable(const esp_partition_t* partition){
return true; return true;
} }
static bool _enablePartition(const esp_partition_t* partition){ bool UpdateClass::_enablePartition(const esp_partition_t* partition){
uint8_t buf[4];
if(!partition){ if(!partition){
return false; return false;
} }
if(!ESP.flashRead(partition->address, (uint32_t*)buf, 4)) { return ESP.partitionWrite(partition, 0, (uint32_t*) _skipBuffer, ENCRYPTED_BLOCK_SIZE);
return false;
}
buf[0] = ESP_IMAGE_HEADER_MAGIC;
return ESP.flashWrite(partition->address, (uint32_t*)buf, 4);
} }
UpdateClass::UpdateClass() UpdateClass::UpdateClass()
@ -110,7 +104,7 @@ bool UpdateClass::rollBack(){
return _partitionIsBootable(partition) && !esp_ota_set_boot_partition(partition); return _partitionIsBootable(partition) && !esp_ota_set_boot_partition(partition);
} }
bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) { bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, const char *label) {
if(_size > 0){ if(_size > 0){
log_w("already running"); log_w("already running");
return false; return false;
@ -121,6 +115,8 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
_reset(); _reset();
_error = 0; _error = 0;
_target_md5 = emptyString;
_md5 = MD5Builder();
if(size == 0) { if(size == 0) {
_error = UPDATE_ERROR_SIZE; _error = UPDATE_ERROR_SIZE;
@ -136,7 +132,7 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
log_d("OTA Partition: %s", _partition->label); log_d("OTA Partition: %s", _partition->label);
} }
else if (command == U_SPIFFS) { else if (command == U_SPIFFS) {
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, NULL); _partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, label);
if(!_partition){ if(!_partition){
_error = UPDATE_ERROR_NO_PARTITION; _error = UPDATE_ERROR_NO_PARTITION;
return false; return false;
@ -179,24 +175,33 @@ void UpdateClass::abort(){
bool UpdateClass::_writeBuffer(){ bool UpdateClass::_writeBuffer(){
//first bytes of new firmware //first bytes of new firmware
uint8_t skip = 0;
if(!_progress && _command == U_FLASH){ if(!_progress && _command == U_FLASH){
//check magic //check magic
if(_buffer[0] != ESP_IMAGE_HEADER_MAGIC){ if(_buffer[0] != ESP_IMAGE_HEADER_MAGIC){
_abort(UPDATE_ERROR_MAGIC_BYTE); _abort(UPDATE_ERROR_MAGIC_BYTE);
return false; return false;
} }
//remove magic byte from the firmware now and write it upon success
//this ensures that partially written firmware will not be bootable //Stash the first 16 bytes of data and set the offset so they are
_buffer[0] = 0xFF; //not written at this point so that partially written firmware
//will not be bootable
skip = ENCRYPTED_BLOCK_SIZE;
_skipBuffer = (uint8_t*)malloc(skip);
if(!_skipBuffer){
log_e("malloc failed");
return false;
}
memcpy(_skipBuffer, _buffer, skip);
} }
if (!_progress && _progress_callback) { if (!_progress && _progress_callback) {
_progress_callback(0, _size); _progress_callback(0, _size);
} }
if(!ESP.flashEraseSector((_partition->address + _progress)/SPI_FLASH_SEC_SIZE)){ if(!ESP.partitionEraseRange(_partition, _progress, SPI_FLASH_SEC_SIZE)){
_abort(UPDATE_ERROR_ERASE); _abort(UPDATE_ERROR_ERASE);
return false; return false;
} }
if (!ESP.flashWrite(_partition->address + _progress, (uint32_t*)_buffer, _bufferLen)) { if (!ESP.partitionWrite(_partition, _progress + skip, (uint32_t*)_buffer + skip/sizeof(uint32_t), _bufferLen - skip)) {
_abort(UPDATE_ERROR_WRITE); _abort(UPDATE_ERROR_WRITE);
return false; return false;
} }
@ -318,6 +323,8 @@ size_t UpdateClass::write(uint8_t *data, size_t len) {
size_t UpdateClass::writeStream(Stream &data) { size_t UpdateClass::writeStream(Stream &data) {
size_t written = 0; size_t written = 0;
size_t toRead = 0; size_t toRead = 0;
int timeout_failures = 0;
if(hasError() || !isRunning()) if(hasError() || !isRunning())
return 0; return 0;
@ -339,15 +346,24 @@ size_t UpdateClass::writeStream(Stream &data) {
bytesToRead = remaining(); bytesToRead = remaining();
} }
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead); /*
if(toRead == 0) { //Timeout Init read&timeout counters and try to read, if read failed, increase counter,
delay(100); wait 100ms and try to read again. If counter > 300 (30 sec), give up/abort
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead); */
if(toRead == 0) { //Timeout toRead = 0;
_abort(UPDATE_ERROR_STREAM); timeout_failures = 0;
return written; while(!toRead) {
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead);
if(toRead == 0) {
timeout_failures++;
if (timeout_failures >= 300) {
_abort(UPDATE_ERROR_STREAM);
return written;
}
delay(100);
} }
} }
if(_ledPin != -1) { if(_ledPin != -1) {
digitalWrite(_ledPin, !_ledOn); // Switch LED off digitalWrite(_ledPin, !_ledOn); // Switch LED off
} }
@ -359,7 +375,7 @@ size_t UpdateClass::writeStream(Stream &data) {
return written; return written;
} }
void UpdateClass::printError(Stream &out){ void UpdateClass::printError(Print &out){
out.println(_err2str(_error)); out.println(_err2str(_error));
} }

View File

@ -12,7 +12,7 @@ const int led = 13;
void handleRoot() { void handleRoot() {
digitalWrite(led, 1); digitalWrite(led, 1);
server.send(200, "text/plain", "hello from esp8266!"); server.send(200, "text/plain", "hello from esp32!");
digitalWrite(led, 0); digitalWrite(led, 0);
} }

View File

@ -32,7 +32,7 @@ void setup(void) {
Serial.println("MDNS responder started"); Serial.println("MDNS responder started");
} }
server.on("/", []() { server.on(F("/"), []() {
server.send(200, "text/plain", "hello from esp32!"); server.send(200, "text/plain", "hello from esp32!");
}); });

View File

@ -356,9 +356,9 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
client.readStringUntil('\n'); client.readStringUntil('\n');
//start reading the form //start reading the form
if (line == ("--"+boundary)){ if (line == ("--"+boundary)){
if(_postArgs) delete[] _postArgs; if(_postArgs) delete[] _postArgs;
_postArgs = new RequestArgument[WEBSERVER_MAX_POST_ARGS]; _postArgs = new RequestArgument[WEBSERVER_MAX_POST_ARGS];
_postArgsLen = 0; _postArgsLen = 0;
while(1){ while(1){
String argName; String argName;
String argValue; String argValue;
@ -413,6 +413,9 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
if (line == ("--"+boundary+"--")){ if (line == ("--"+boundary+"--")){
log_v("Done Parsing POST"); log_v("Done Parsing POST");
break; break;
} else if (_postArgsLen >= WEBSERVER_MAX_POST_ARGS) {
log_e("Too many PostArgs (max: %d) in request.", WEBSERVER_MAX_POST_ARGS);
return false;
} }
} else { } else {
_currentUpload.reset(new HTTPUpload()); _currentUpload.reset(new HTTPUpload());
@ -458,7 +461,23 @@ readfile:
} }
uint8_t endBuf[boundary.length()]; uint8_t endBuf[boundary.length()];
client.readBytes(endBuf, boundary.length()); uint32_t i = 0;
while(i < boundary.length()){
argByte = _uploadReadByte(client);
if(argByte < 0) return _parseFormUploadAborted();
if ((char)argByte == 0x0D){
_uploadWriteByte(0x0D);
_uploadWriteByte(0x0A);
_uploadWriteByte((uint8_t)('-'));
_uploadWriteByte((uint8_t)('-'));
uint32_t j = 0;
while(j < i){
_uploadWriteByte(endBuf[j++]);
}
goto readfile;
}
endBuf[i++] = (uint8_t)argByte;
}
if (strstr((const char*)endBuf, boundary.c_str()) != NULL){ if (strstr((const char*)endBuf, boundary.c_str()) != NULL){
if(_currentHandler && _currentHandler->canUpload(_currentUri)) if(_currentHandler && _currentHandler->canUpload(_currentUri))

View File

@ -12,6 +12,7 @@ class Uri {
public: public:
Uri(const char *uri) : _uri(uri) {} Uri(const char *uri) : _uri(uri) {}
Uri(const String &uri) : _uri(uri) {} Uri(const String &uri) : _uri(uri) {}
Uri(const __FlashStringHelper *uri) : _uri(String(uri)) {}
virtual ~Uri() {} virtual ~Uri() {}
virtual Uri* clone() const { virtual Uri* clone() const {

View File

@ -45,6 +45,7 @@ WebServer::WebServer(IPAddress addr, int port)
, _currentVersion(0) , _currentVersion(0)
, _currentStatus(HC_NONE) , _currentStatus(HC_NONE)
, _statusChange(0) , _statusChange(0)
, _nullDelay(true)
, _currentHandler(nullptr) , _currentHandler(nullptr)
, _firstHandler(nullptr) , _firstHandler(nullptr)
, _lastHandler(nullptr) , _lastHandler(nullptr)
@ -66,6 +67,7 @@ WebServer::WebServer(int port)
, _currentVersion(0) , _currentVersion(0)
, _currentStatus(HC_NONE) , _currentStatus(HC_NONE)
, _statusChange(0) , _statusChange(0)
, _nullDelay(true)
, _currentHandler(nullptr) , _currentHandler(nullptr)
, _firstHandler(nullptr) , _firstHandler(nullptr)
, _lastHandler(nullptr) , _lastHandler(nullptr)
@ -280,6 +282,9 @@ void WebServer::handleClient() {
if (_currentStatus == HC_NONE) { if (_currentStatus == HC_NONE) {
WiFiClient client = _server.available(); WiFiClient client = _server.available();
if (!client) { if (!client) {
if (_nullDelay) {
delay(1);
}
return; return;
} }
@ -308,11 +313,12 @@ void WebServer::handleClient() {
_contentLength = CONTENT_LENGTH_NOT_SET; _contentLength = CONTENT_LENGTH_NOT_SET;
_handleRequest(); _handleRequest();
if (_currentClient.connected()) { // Fix for issue with Chrome based browsers: https://github.com/espressif/arduino-esp32/issues/3652
_currentStatus = HC_WAIT_CLOSE; // if (_currentClient.connected()) {
_statusChange = millis(); // _currentStatus = HC_WAIT_CLOSE;
keepCurrentClient = true; // _statusChange = millis();
} // keepCurrentClient = true;
// }
} }
} else { // !_currentClient.available() } else { // !_currentClient.available()
if (millis() - _statusChange <= HTTP_MAX_DATA_WAIT) { if (millis() - _statusChange <= HTTP_MAX_DATA_WAIT) {
@ -370,6 +376,10 @@ void WebServer::setContentLength(const size_t contentLength) {
_contentLength = contentLength; _contentLength = contentLength;
} }
void WebServer::enableDelay(boolean value) {
_nullDelay = value;
}
void WebServer::enableCORS(boolean value) { void WebServer::enableCORS(boolean value) {
_corsEnabled = value; _corsEnabled = value;
} }
@ -454,20 +464,23 @@ void WebServer::send(int code, const String& content_type, const String& content
} }
void WebServer::sendContent(const String& content) { void WebServer::sendContent(const String& content) {
sendContent(content.c_str(), content.length());
}
void WebServer::sendContent(const char* content, size_t contentLength) {
const char * footer = "\r\n"; const char * footer = "\r\n";
size_t len = content.length();
if(_chunked) { if(_chunked) {
char * chunkSize = (char *)malloc(11); char * chunkSize = (char *)malloc(11);
if(chunkSize){ if(chunkSize){
sprintf(chunkSize, "%x%s", len, footer); sprintf(chunkSize, "%x%s", contentLength, footer);
_currentClientWrite(chunkSize, strlen(chunkSize)); _currentClientWrite(chunkSize, strlen(chunkSize));
free(chunkSize); free(chunkSize);
} }
} }
_currentClientWrite(content.c_str(), len); _currentClientWrite(content, contentLength);
if(_chunked){ if(_chunked){
_currentClient.write(footer, 2); _currentClient.write(footer, 2);
if (len == 0) { if (contentLength == 0) {
_chunked = false; _chunked = false;
} }
} }

View File

@ -123,12 +123,14 @@ public:
void send_P(int code, PGM_P content_type, PGM_P content); void send_P(int code, PGM_P content_type, PGM_P content);
void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength); void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength);
void enableDelay(boolean value);
void enableCORS(boolean value = true); void enableCORS(boolean value = true);
void enableCrossOrigin(boolean value = true); void enableCrossOrigin(boolean value = true);
void setContentLength(const size_t contentLength); void setContentLength(const size_t contentLength);
void sendHeader(const String& name, const String& value, bool first = false); void sendHeader(const String& name, const String& value, bool first = false);
void sendContent(const String& content); void sendContent(const String& content);
void sendContent(const char* content, size_t contentLength);
void sendContent_P(PGM_P content); void sendContent_P(PGM_P content);
void sendContent_P(PGM_P content, size_t size); void sendContent_P(PGM_P content, size_t size);
@ -176,6 +178,7 @@ protected:
uint8_t _currentVersion; uint8_t _currentVersion;
HTTPClientStatus _currentStatus; HTTPClientStatus _currentStatus;
unsigned long _statusChange; unsigned long _statusChange;
boolean _nullDelay;
RequestHandler* _currentHandler; RequestHandler* _currentHandler;
RequestHandler* _firstHandler; RequestHandler* _firstHandler;

View File

@ -113,7 +113,7 @@ public:
} }
File f = _fs.open(path, "r"); File f = _fs.open(path, "r");
if (!f) if (!f || !f.available())
return false; return false;
if (_cache_header.length() != 0) if (_cache_header.length() != 0)

View File

@ -6,7 +6,7 @@ based WPS entry to get your ESP connected to your WiFi router.
Hardware Requirements Hardware Requirements
======================== ========================
ESP32 and a Router having atleast one WPS functionality ESP32 and a Router having WPS functionality
This code is under Public Domain License. This code is under Public Domain License.
@ -63,7 +63,7 @@ void WiFiEvent(WiFiEvent_t event, system_event_info_t info){
WiFi.reconnect(); WiFi.reconnect();
break; break;
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS: case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
Serial.println("WPS Successfull, stopping WPS and connecting to: " + String(WiFi.SSID())); Serial.println("WPS Successful, stopping WPS and connecting to: " + String(WiFi.SSID()));
esp_wifi_wps_disable(); esp_wifi_wps_disable();
delay(10); delay(10);
WiFi.begin(); WiFi.begin();
@ -75,7 +75,7 @@ void WiFiEvent(WiFiEvent_t event, system_event_info_t info){
esp_wifi_wps_start(0); esp_wifi_wps_start(0);
break; break;
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT: case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
Serial.println("WPS Timedout, retrying"); Serial.println("WPS Timeout, retrying");
esp_wifi_wps_disable(); esp_wifi_wps_disable();
esp_wifi_wps_enable(&config); esp_wifi_wps_enable(&config);
esp_wifi_wps_start(0); esp_wifi_wps_start(0);
@ -106,4 +106,4 @@ void setup(){
void loop(){ void loop(){
//nothing to do here //nothing to do here
} }

Some files were not shown because too many files have changed in this diff Show More