Compare commits

..

132 Commits

Author SHA1 Message Date
aa030e044c Temporary stop git log to build prerelease 2018-11-28 00:05:36 +01:00
95d417cd93 Update build-release.sh
force correct dir
2018-11-27 23:59:12 +01:00
df4eeb3005 Update build-release.sh
fix debug
2018-11-27 23:53:29 +01:00
a360064768 Update build-release.sh
Add more debug
2018-11-27 23:50:30 +01:00
bfde8daf75 Update build-release.sh
print out the command
2018-11-27 23:43:34 +01:00
e583a0e879 Travis: Set proper dir before checking git 2018-11-27 23:35:23 +01:00
7e9afe8c5e Add response headers with sketch and flash sizes, and a SHA256 (#2116) 2018-11-27 21:27:38 +01:00
dcb007a485 do not skip main.cpp when checking CMakeLists 2018-11-27 18:50:09 +01:00
bec6f87235 Update CMakeLists.txt 2018-11-26 23:54:13 +01:00
4ae64c541e Fixed missing return value (#2090) 2018-11-26 23:26:08 +01:00
46257c03b3 handshake in ssl_client.cpp (#2044)
* issue #2041

* handshake timeout

* seconds to milliseconds
2018-11-26 23:25:08 +01:00
0640964879 Solve issue #2092 by initializing * _client to nullptr (#2097) 2018-11-26 23:23:19 +01:00
e609c78f20 Initialize detectedBaudRate to prevent compilation errors (#2101) 2018-11-26 23:22:37 +01:00
04963009ee Update IDF to a0468b2 (#2108)
* Update IDF to a0468b2

* add missing ld file

* Fix PIO builds and change coex policy
2018-11-26 23:22:11 +01:00
Luc
c3ec91f968 Allow to add custom callback in BT Serial (#2081)
This allow to catch the events when connected /disconnected,, etc...
This also allow to get parameters of events like the remote address of connected devices, etc...
Small change but lot of flexibility
2018-11-19 19:30:28 +01:00
a30005949a adding D-duino-32 board/pins (#2030)
* adding d-duino-32 board/pins, fixes #930

* remove OLED_RST
2018-11-19 19:29:57 +01:00
Luc
acefd4bf2e Fix Partition Calculation for min SPIFFS (#2072)
I have kept the original APPS size on purpose as 40KB SPIFFS is useless IMHO
2018-11-19 17:10:10 +01:00
c700e5694f Some fixes to nghttp2 to provide basic functionality. Added the missing main header. Removed the asio headers which require Boost libraries. Moved http_parser into the expected location. (#2068) 2018-11-19 17:09:38 +01:00
0d564d7b1d Limit the number of simultaneously connected devices to BluetoothSerial to only 1 (#2061) 2018-11-19 17:08:15 +01:00
44ca2ee976 Fix uart TX flushing (#2029)
wait for FSM to return idle
2018-11-19 17:04:05 +01:00
af79e18ecb Added ESP:: functions for sketch size (#2028)
* Added ESP:: functions for sketch size

* Fixed free space name to match ESP8266
2018-11-19 17:03:36 +01:00
273196d7e6 HTTPClientEnterprise example (#2023) 2018-11-19 17:02:47 +01:00
5d2460c74a Working example for HTTPS over Eduroam network - WifiClient secure library (#2022)
* Working example for HTTPS over Eduroam network

* Update WiFiClientSecureEnterprise.ino
2018-11-19 17:02:24 +01:00
259ff80d60 use libbase64 macro to calculate base64 length (#2007) 2018-11-19 17:01:38 +01:00
Bob
3902aa4019 Adding path arguments to WebServer (#1994) 2018-11-19 17:00:52 +01:00
f9d1b24c01 Update (#1992)
Added Olimex board ESP32-PoE. pins_arduino header file based on the ESP32-EVB with changed SS pin and removed BOARD_HAS_1BIT_SDMMC macro.
2018-11-19 17:00:20 +01:00
c7fa251d78 updated WifiClientEnterprise example (#1988)
* updated WifiClientEnterprise example

* Update WiFiClientEnterprise.ino
2018-11-19 16:59:56 +01:00
7b811f9b3a leave possible endless loop (#1986) 2018-11-19 16:59:14 +01:00
1fdc660641 [Feature] Boards added: Pycom LoPy & LoPy4 (#1984)
* Create pins_arduino.h

* Update boards.txt

Pycom LoPy + LoPy4 boards added

* Create pins_arduino.h

* Update pins_arduino.h

bugfix antenna switch GPIO port 16 -> 21

* Update pins_arduino.h

* Update pins_arduino.h

* Update pins_arduino.h

* Update pins_arduino.h

* Update pins_arduino.h

* Update boards.txt
2018-11-19 16:58:45 +01:00
a53c9de09d Updated table of tested locations - Enterprise wifi networks - WifiClientEnterprise (#1980)
* Updated table of tested locations

* updated
2018-11-19 16:58:07 +01:00
b58a3509b8 Feature/http update (#1979)
* Added HTTPUpdate class for downloading sketches from a server

* Added HTTPUpdate class for downloading sketches from a server

* Added HTTPUpdate to CMakeLists.txt

* Change ESP8266 class references to ESP32 for httpUpdate.ino example

* Change ESP8266 class references to ESP32 for httpUpdate.ino example. setLedPin() commented out because not all boards support LED_BUITLIN

* Added check to handle mixup of old and present api properly

* Correct HTTPClient::setTimeout() to convert milliseconds to seconds. Correct WiFiClient::setTimeout() to call Stream::setTimeout() with seconds converted back to milliseconds. Remove inproper checks for _insecure.

* Added small comment because it looked like the Travis build did not finish
2018-11-19 16:57:38 +01:00
01d22c8807 Feature/http client (#1973)
* Pass client parameter into two new begin() functions. Set other begin() functions deprecated. Updated library version to 1.2

* Added working HTTPS example on a public url with a certificate

* Remove two unnecessary tests in ::disconnect()

* Add a scoping block to BasicHttpsClient.ino to assure HTTPClient is destroyed before WiFiClientSecure

* Added check to handle mixup of old and present api properly

* Correct HTTPClient::setTimeout() to convert milliseconds to seconds. Correct WiFiClient::setTimeout() to call Stream::setTimeout() with seconds converted back to milliseconds. Remove inproper checks for _insecure.

* Added small comment because it looked like the Travis build did not finish
2018-11-19 16:57:23 +01:00
b70737d276 Fix partition tables to reflect that M5Stack Fire has 16MB flash and 4MB PSRAM (#1969) 2018-11-19 16:53:13 +01:00
233d31bed2 Added baudrate detection to esp32-hal-uart and HardwareSerial (#1961)
* Added baudrate detection to esp32-hal-uart and HardwareSerial

* Solved compiler warning for uartResizeRxBuffer()

* Add unit to header variable name (timeout_ms)

* Reverting accidentally changed files to master

* Add small delay after baudrate detection
2018-11-19 16:51:55 +01:00
65c861ad4c Added loadCert methods to WiFiClientSecure (#1959) 2018-11-19 16:50:08 +01:00
f6a71da378 Update pins_arduino.h (#1949)
Pin definitions T8/T9 & DAC1/DAC2 Back To Front (Issue #1831)
2018-11-19 16:47:07 +01:00
b8c9819e7f Update pins_arduino.h (#1948)
Pin definitions T8/T9 & DAC1/DAC2 Back To Front
2018-11-19 16:46:53 +01:00
1bc1e8c602 Update WString.cpp (#1936) 2018-11-19 16:45:09 +01:00
2132d9f809 Update WString.h (#1935) 2018-11-19 16:44:53 +01:00
14ff311479 make WiFi.softAP() more robust (#1925)
* make WiFi.softAP() more robust

* WiFi.softAP() revert fallback to WIFI_AUTH_OPEN
2018-11-19 16:43:59 +01:00
a3ed511884 WiFi: improve WiFiEvent list in WiFiClientEvents example (#1917)
fixes #1875
2018-11-19 16:42:33 +01:00
9e2888392e Added a message for all event types. (#1916) 2018-11-19 16:41:56 +01:00
a43682596f Deep-sleep example-sketches reported wrong wakeup-reason (#1911)
Incorrect values used, use the proper defines from header-files instead.
2018-11-19 16:41:07 +01:00
825b6ea79e Fixed previous error (#1908) 2018-11-19 16:40:44 +01:00
deaf339bde Wire endTransmission() fix for issue #1725 (#1888)
* removed uint8_t Wire.endTransmission(uint8_t sendStop)

Having both endTransmission(bool) and endTransmission(uint8_t) creates problems.
There is no need for endTransmission(uint8_t)
endTransmission(1) and endTransmission(0) works with endTransmission(bool).
Removing endTransmission(uint8_t) allows the ESP32 code to be compatible with
all the other Arduino cores by allowing endTransmission(1) and endTranmission(0)
to work as it does on all the other cores.

* Wire library version bump for endTransmission() update
2018-11-19 16:40:14 +01:00
f12df4c719 allow component projects to compile with CONFIG_DISABLE_HAL_LOCKS (#1880) 2018-11-19 16:39:42 +01:00
85032b226c Do not break UDP if pbuf is null 2018-09-26 23:29:51 +02:00
e5ea089a7f Reduce resource requirements, Share Interrupt (#1877)
#1869 exposed a resource exhaustion issue. The current HAL layer for I2C support is designed to use a shared interrupt, But, during debugging to solve the interrupt overloading condition identified in #1588, and the generation of pr #1717, the interrupt allocation parameters were changed.  This change was unnecessary, the code will work successfully with shared interrupts.  So, there is no need to assign a private interrupt for each I2C peripheral.
2018-09-21 08:40:01 +02:00
96822d783f Update IDF to 3.2-3276a13 and esptool.py to 2.5.0 (#1878)
* TX Flow Control and Code cleanup

* Use semaphore instead of delay

TX functionality is done.

* Use single buffer and empty queue on exit

* Fix compile issues because of LwIP code relocation

* Add temporary header to fix Azure not compiling

* Fix AsyncUDP early init

* AsyncUDP Multicast fixes

* Add source mac address and rework multicast

* Allow redefinition of default pins for Serials 1 and 2

* Update IDF to 3276a13

* Update esptool.py to 2.5.0

* Fix sketches

* Fix log level in BluetoothSetial
2018-09-21 08:39:36 +02:00
4e96bffe0e Initial version of rmt driver (#1525)
* rmt driver initial version

* supporting conti mode plus interrupts

* using conitnous mode for sending more data

* working continous mode

* rmt driver cleanup after conti mode

* initial version of rmt driver

* adding a simple example

* adding channel and block locks

* modified of rmt interface for simpler/easier usage

* adding header sentinels, split interface to common and additional settings

* Fixes per code review + support for rx callback mode

* renamed internal structures and enums, fixed formatting

* cmake support for rmt

* refactored tx-conti interrupts to function to make it more readable

* added Tx and Rx examples

* added license headers

* minor updates per review

* used struct access, renamed defines, corrected diagram
2018-09-17 23:19:27 +02:00
ea61563c69 Functional interrupt (#1728)
* Initial

* Implementation

* Add to CMakelist.txt

* Add example

* Add IRAM_ATTR
2018-09-17 23:13:58 +02:00
5be3078b76 InterruptArg should take voidFuncPtrArg as argument (#1776) 2018-09-17 23:10:13 +02:00
7206b2f397 FAT on SPI Flash Library (#1809)
* First commit of FFat library

* Fixed reboot loops if no fat present. Added CMakeLists

* Functionalize the partition checks

* Cleanup, especially in format

* Dont format if mounted.  More wording cleanup

* 16M ffat should only be on 16M board

* Fix some casting issues that trip up the compiler when building as ESP-IDF component
2018-09-17 23:06:04 +02:00
3028ec42c7 Add BananaPi-BIT Development Board Support (#1810)
* Add BPI-BIT boards connfig

* Add BPI-BIT v1.3 boards
2018-09-17 22:32:14 +02:00
145904fb9c Add wESP32 support (#1821)
* Add wESP32 support

* Add default Ethernet config
2018-09-17 22:28:24 +02:00
cb8d72fdc7 Fix WifiClientEnterprise - STA mode set (#1782)
Few testers let me know, they were unable with connection to Eduroam network under PEAP+MsCHAPv2 methods.
They were trying many modifications. Solution is very absurd. 
Change board to STA mode manually. 
One tester from Italy was able to connect to Eduroam network under TTLS + MsCHAPv2 with that sketch too!

Sketch was tested and worked almost for all testers with that problem.
2018-09-17 21:43:16 +02:00
1e4bf14a3e cores: replace max, min, round macros with imports from std (#1783)
fixes #1734
2018-09-17 21:33:01 +02:00
f9f995b283 Flush serial in DeepSleep example to allow print before sleep (#1791)
* Add delay into example to allow print before sleep

* Changed to Serial.flush()
2018-09-17 21:31:24 +02:00
c8fe873965 add WiFiAccessPoint example (#1833) 2018-09-17 21:24:23 +02:00
18d260ec6e Fix for TFT_eSPI and Adafruit libs (#1837)
see #1800
2018-09-17 21:21:51 +02:00
3a8ac27a86 layout fix (#1845)
(removed quotes from last line, and fixed indentation, no text changes)
2018-09-17 21:20:57 +02:00
a6a9a518a7 _uart_isr use wr_addr != rd_addr as test for internal queue not empty (#1849) 2018-09-17 21:19:51 +02:00
02ee799f35 Add T-Beam Board Support (#1852) 2018-09-17 21:19:02 +02:00
ce61074802 Add functionality allowing rxBuffer of HardwareSerial to be changed in size via HardwareSerial::setRxBufferSize. (#1855) 2018-09-17 21:16:18 +02:00
339618f8ef Updating Boart.txt ESP32Thing in PartitionScheme and DebugLevel (#1860) 2018-09-17 21:12:15 +02:00
6e4e4c96ee Updated ISSUE_TEMPLATE to try to get better postings (#1730)
* Updated ISSUE_TEMPLATE to try to get better postings

* Added line for PSRAM enabled

* More complete info request as per @stickbreaker
2018-09-17 21:11:41 +02:00
a0f0bd930c Fix BTserial memory leaks (#1801)
- Delete queue at end
- Close BT connection before end
- DeInit SPP
2018-08-27 12:06:23 +02:00
80c110ece7 Add more methods to access memory properties 2018-08-18 17:10:35 +02:00
65511b23d3 Add separate method to get free PSRAM and report only internal in getFreeHeap 2018-08-18 16:34:41 +02:00
14d6f6e7e6 Correct pins for actual hardware (#1768)
The original pins_arduino.h did not correspond to the actual hardware.
2018-08-18 08:51:22 +02:00
9db207afbe Improve bus recovery (#1767)
If the esp32 is reset during a i2c read cycle the slave device may be in control of the SDA line.  

If the SDA line is held low, the esp32 cannot issue a START or STOP to recover the bus. 

The previous code did not correctly configure the SCL output pin, and it cycled SCL 9 times with SDA Low.  Since the slave device was in a READ cycle, it just continued outputting the bits of the current byte.  When the ACK/NAK bit space occurred, The low output value of SDA was interpreted as ACK so the slave device continued with the next byte.  It never terminated the READ cycle. 

This new code will correctly recover from an interrupted READ
2018-08-18 08:50:59 +02:00
a989853d4a Update mac.md (#1760)
add a troubleshooting hint
2018-08-18 08:50:14 +02:00
172802b18e Remove duplicate ota_data flashing under IDF
Fixes: https://github.com/espressif/arduino-esp32/issues/1724
2018-08-16 13:34:47 +02:00
fff1783046 Switch to isolated build flags per framework (#1748) 2018-08-14 12:01:07 +02:00
cb53ec4891 Informations about WifiClientEnterprise.ino sketch (#1737)
* informations about sketch

* Update README.md
2018-08-14 12:00:31 +02:00
7d3a67ada0 Update Arduino/hardware path (#1727) 2018-08-14 11:53:34 +02:00
d057e544e0 Added a freeEntries method to Preferences library (#1722) 2018-08-14 11:52:01 +02:00
b05430cfd9 Wire ReSTART fix, with others (#1717)
* ReSTART fix, Sequencing fix

pr #1665 introduce a problem with ReSTART, when solving this problem I found an interaction between the TxFifo refill, RxFifo empty and CMD[] fill.  during certain sequences a dataqueue command would be skipped, this skipping resulted in a mismatch between the contents of the TxFifo and the i2c command sequence.  The problem manifested as an ACK error. 
In addition to this required bug fix I propose:
* `Wire.begin()` be changed from a `void` to a `bool` this will allow the reset functionality of `Wire.begin()` to be reported.  Currently `Wire.begin()` attempts to reset the i2c Peripheral, but cannot report success/failure.
* `Wire.busy()` be added. this `bool` function returns the hardware status of the bus. This status can be use in multi-master environments for application level interleaving of commands, also in single master environment, it can be used to detect a 'hung' bus.  With the functional change to `Wire.begin()` this allows app level recover of a hung bus.
* `Wire.lastError()` value updated for all errors, previously when interleaving `Wire.endTransmission(false)` and `Wire.readTransmission(false)`, the 128 byte `Wire.write()` buffer was exhausted without generating and error(very exotic). I discovered this error when I created a sequence of directed reads to a EEPROM. Each directed read used 2 bytes of the 128 byte `write()` buffer, so after 64 consecutive ReSTART writes with ReSTART reads, `Wire()`  had no room to record the directed address bytes.  It generated just a NAK check without setting the EEPROMs internal register address.  The succeeding ReSTART read succeeded at incorrect address.
* Changes to the HAL layer:
** added `i2cGetStatus()` which returns the i2c peripheral status word, used to detect bus_busy currently
** added `i2cDebug()` programmatic control of debug buffer output
** changed `i2cAddQueue()` to allow data_only queue element this will allow a i2c transaction to use multiple data pointers.
** removed direct access to DumpInts(), DumpI2c() from app, use i2cDebug() to set trigger points 
 
*

* Update esp32-hal-i2c.c

* Update Wire.cpp

* ReSTART, Sequencing

pr #1665 introduce a problem with ReSTART, when solving this problem I found an interaction between the TxFifo refill, RxFifo empty and CMD[] fill.  during certain sequences a dataqueue command would be skipped, this skipping resulted in a mismatch between the contents of the TxFifo and the i2c command sequence.  The problem manifested as an ACK error. 
In addition to this required bug fix I propose:
* `Wire.begin()` be changed from a `void` to a `bool` this will allow the reset functionality of `Wire.begin()` to be reported.  Currently `Wire.begin()` attempts to reset the i2c Peripheral, but cannot report success/failure.
* `Wire.busy()` be added. this `bool` function returns the hardware status of the bus. This status can be use in multi-master environments for application level interleaving of commands, also in single master environment, it can be used to detect a 'hung' bus.  With the functional change to `Wire.begin()` this allows app level recover of a hung bus.
* `Wire.lastError()` value updated for all errors, previously when interleaving `Wire.endTransmission(false)` and `Wire.readTransmission(false)`, the 128 byte `Wire.write()` buffer was exhausted without generating and error(very exotic). I discovered this error when I created a sequence of directed reads to a EEPROM. Each directed read used 2 bytes of the 128 byte `write()` buffer, so after 64 consecutive ReSTART writes with ReSTART reads, `Wire()`  had no room to record the directed address bytes.  It generated just a NAK check without setting the EEPROMs internal register address.  The succeeding ReSTART read succeeded at incorrect address.
* Changes to the HAL layer:
** added `i2cGetStatus()` which returns the i2c peripheral status word, used to detect bus_busy currently
** added `i2cDebug()` programmatic control of debug buffer output
** changed `i2cAddQueue()` to allow data_only queue element this will allow a i2c transaction to use multiple data pointers.
** removed direct access to DumpInts(), DumpI2c() from app, use i2cDebug() to set trigger points 
 
*

* Forgot DebugFlags Return

@andriyadi found this, total brain fade on my part.
2018-08-14 11:51:15 +02:00
e346f20aa9 Fix WiFiMulti Logs 2018-07-30 09:42:44 -03:00
bdc45e39ef [OTA] Fix "Error response from device" if OK response comes to early (#1695)
Because TCP is stream-based, an earlier read can 'take away' the "OK" response
from the device, so that a later read doesn't get the message.
2018-07-30 10:22:01 +03:00
a7ddf39521 Cleanup on README and boards_manager (#1693)
* Added instructions for installation with boards manager (stolen directly from esp8266)

* changed to production link instead of dev

* Added to main README. Made mods to images as requested.

* Added links for development package

* Moved version images to README.md

* Just a little change for cleaner look

* Cleaned up README.md and boards_manager.md to make installation easier.
2018-07-30 10:21:03 +03:00
abb8ea99d5 Fix WiFiMulti Logs (#1690) 2018-07-30 10:18:48 +03:00
30b3eebabc * merge only annotated tag messages into release notes (#1683) 2018-07-28 10:28:04 +03:00
3222e6490a add LOLIN D32 & D32 PRO Board support (#1688) 2018-07-28 10:27:26 +03:00
2fba81223e Added instructions for installation with boards manager (#1630)
* Added instructions for installation with boards manager (stolen directly from esp8266)

* changed to production link instead of dev

* Added to main README. Made mods to images as requested.

* Added links for development package
2018-07-26 08:10:46 +02:00
e51f7a5b87 fix compilation errors in idf 2018-07-25 22:39:32 +02:00
7d2560cbbf roll back the while loop in _parseForm 2018-07-25 20:59:56 +02:00
17065dfd3a Added a define to format the spiffs in SPIFFS_Test.ino (#1662)
* Added a define to format the spiffs in SPIFFS_Test.ino

* Uncommented the define

* Matched define names
2018-07-25 18:44:37 +02:00
2f5b3c0c56 Functions _uploadReadByte and _parseForm were modified in order to (#1677)
speed up uploading data. Now there is no need to call time consuming
client.connected() method.
2018-07-25 17:37:15 +02:00
1fe3ee87b6 Feature/selective compilation (#1671)
* Selective compilation

* Optimized component.mk

* Autoconnect WiFi now forces WiFi
2018-07-25 13:02:37 +02:00
328523f5e3 Memory leak (#1672)
When a package of size 0 arrives, "buf" is created, but never released. (Sorry, that was my mistake in the last patch)
2018-07-25 12:56:41 +02:00
Bin
7761ebd9f2 Add M5Stack-FIRE board (#1647)
* Add M5Stack-FIRE board

* updated m5stack-fire boards.txt

* remove pins 16 and 17
2018-07-25 12:56:04 +02:00
f9a382ab9f fix some compilation error and warnings in i2c 2018-07-24 22:06:50 +02:00
d854dc1bf6 Create WiFiClientEnterprise.ino (#1640)
Sketch for ESP32 boards that allow them to connect to WPA/WPA2 Enterprise Networks.
2018-07-24 19:57:57 +02:00
f1f8d7e306 Packet with zero data length (#1659)
If you receive a package with a data length of zero, parsePacket returns 0, but rx_buffer will exist. So if another parsePacket with no read access returns to zeros, there is still data that can be read. This example would not work: https://www.arduino.cc/en/Reference/EthernetUDPParsePacket

Also I added a check if rx_buffer exit when you try to flush it.
2018-07-24 19:52:42 +02:00
da798c7db0 Add TTGO LoRa32 with OLED Version 1.x Board (#1663)
* Add ttgo-lora32-v1 folder to variants folder

* Add ttgo-lora32-v1 info to boards.txt

* Add defs to pins_arduino.h for I2C OLED and SPI LoRa radio pinouts as per @stickbreaker
2018-07-24 19:49:38 +02:00
8d7fb58672 Fix for spurious interrupts during I2C communications (#1665)
This version no longer needs an interrupt for each byte transferred. It only needs interrupts for START, STOP, FIFO empty/Full or error conditions.  This dramatically reduces the interrupt overhead.  I think the prior version was causing an interrupt overload condition where the ISR was not able to process every interrupt as they happened.
2018-07-24 19:43:45 +02:00
2fda054bea [OTA Timeout] Added ability set OTA timeout in the OTA client (#1669) 2018-07-24 19:40:18 +02:00
Luc
e157ec06a7 expose post args during upload (#1650) 2018-07-17 10:58:03 +02:00
05d72f963d fix WiFi STA going into loop in some cases of disconnect 2018-07-16 22:50:52 +02:00
b14f82b65f Release notes formatting update (#1634) 2018-07-16 20:47:30 +02:00
c830511f01 EEPROM library: Move #include of Arduino.h to header file (#1641)
EEPROM.h uses data types which are declared through Arduino.h but that file does not contain an #include directive for Arduino.h. This does not cause any problems when the EEPROM library is #included from a .ino file because the Arduino IDE automatically adds an #include directive for Arduino.h but this is not the case for .cpp files. If a .cpp file has an #include directive for EEPROM.h that does not follow an #include directive for Arduino.h then compilation fails:

E:\arduino\hardware\espressif\esp32\libraries\EEPROM/EEPROM.h:91:5: error: 'float_t' does not name a type

     float_t readFloat(int address);

     ^

E:\arduino\hardware\espressif\esp32\libraries\EEPROM/EEPROM.h:92:5: error: 'double_t' does not name a type

     double_t readDouble(int address);

     ^

E:\arduino\hardware\espressif\esp32\libraries\EEPROM/EEPROM.h:95:5: error: 'String' does not name a type

     String readString(int address);

     ^

E:\arduino\hardware\espressif\esp32\libraries\EEPROM/EEPROM.h:110:36: error: 'float_t' has not been declared

     size_t writeFloat(int address, float_t value);

                                    ^

E:\arduino\hardware\espressif\esp32\libraries\EEPROM/EEPROM.h:111:37: error: 'double_t' has not been declared

     size_t writeDouble(int address, double_t value);

                                     ^

E:\arduino\hardware\espressif\esp32\libraries\EEPROM/EEPROM.h:114:37: error: 'String' has not been declared

     size_t writeString(int address, String value);
2018-07-16 20:44:36 +02:00
cbd4dc53a6 Add ALKS board variant (#1643)
* Initial support for ALKS variant
2018-07-16 20:43:35 +02:00
44f5a4dbc8 Fix Client returning disconnected because of VFS errors 2018-07-16 20:08:27 +02:00
e63aa40650 Enable PSRAM builds when used as IDF component 2018-07-15 10:20:01 +02:00
28a410dd50 Spurious Interrupts Temporary fix 20180711 (#1625)
the 'eject' ERROR is and indication of an interrupt triggering without an source.  I am working to eliminate these serviceable interrupt.  This update increase stability on a HelTek Wifi Lora 32 board. with a SSD1306 OLED.  This update fixes a glaring error in the interrupt allocation code, the Interrupt mask was wrong.  I also dynamically adjust the FiFo thresholds based on Bus clockrate. The change to FiFo thresholds has reduced the number for 'eject' events.  I also change 'eject' from and ERROR to DEBUG.  An 'eject' event does not compromise i2c transmissions. It happens after a transaction has completed. 

Chuck.
2018-07-12 15:18:26 +02:00
ddfeae90d0 Fix AsyncUDP server exception 2018-07-11 20:46:54 +02:00
ff90778173 Fix HTTP client returning disconnected when there is still data avalable 2018-07-10 21:06:20 +02:00
4e9d1ee237 Fix connected to be wrongly reported in WiFiClient 2018-07-10 21:05:44 +02:00
c1a94b5326 Fix wrong event description 2018-07-10 16:13:34 +02:00
3e160587f3 Fix WiFi Client not properly reporting connected state 2018-07-10 15:01:40 +02:00
79010b6498 Add XinaBox CW02 to supported boards (#1620)
* Create pins_arduino.h

* Update boards.txt

* Update pins_arduino.h
2018-07-10 13:43:21 +02:00
9925772db0 * update 'version' in platform.txt (#1619) 2018-07-10 11:26:19 +02:00
c77aed4ac4 Allow using argument with attachInterrupt (#1535)
* Allow using argument with attachInterrupt

* formatting

replace tabs with spaces

* fix bug more then 1 interrupt

* leftover

* add example

* make attachInterruptArg public

* update example

* leftover
2018-07-07 11:26:58 +02:00
901a341949 BluetoothSerial: set COD to be compatible with macOS (#1304) (#1556) 2018-07-07 11:25:10 +02:00
9f6d0d2958 EEPROM.readstring was returning an extra character (#1609) 2018-07-07 10:19:44 +02:00
9efecc1be0 Implement RX buffer for WiFi client to speed up small reads 2018-07-05 22:31:58 +02:00
b0c6991bcf Small adjustments to the web server 2018-07-05 22:31:19 +02:00
8afdd71b3a Add missing event string to WiFi events 2018-07-05 22:30:54 +02:00
871dd183d8 Print to debug the IP address obtained by DHCP 2018-07-05 16:04:07 +02:00
05111bbde7 fix case where library source subfolders are not compiled when used as component 2018-07-05 14:27:17 +02:00
bad53905e8 Prevent exceptions in WiFi if not yet started 2018-07-05 12:28:15 +02:00
95b87545e7 Make Stream functions if File to not wait for timeout
Fixes: https://github.com/espressif/arduino-esp32/issues/1597
2018-07-05 11:51:58 +02:00
9f8f05735b * relnotes handling update (#1592)
* script exec fails on curl error
2018-07-04 15:46:46 +02:00
a835bb26c4 Add missing flag to fix psram issues
Thanks @igrr
2018-07-04 12:11:25 +02:00
5e46c9bae6 Add Wrover Support and Option to enable SPIRAM 2018-07-03 23:03:50 +02:00
659c8ad528 Fix TelnetToSerial sketch 2018-07-03 21:51:24 +02:00
2fe965259a "fix" SPI 2018-07-03 21:23:04 +02:00
0161e28614 Added OTAWebUpdater Doc (#1583) 2018-07-03 20:43:18 +02:00
4e5cbdaa7f Add declarations for all Serial, SPI and Wire buses 2018-07-03 20:41:03 +02:00
12ca9e8b52 Port UART reset fix from ESP-IDF (#1408) 2018-07-03 17:54:08 +02:00
8b01b9e187 Change esptool version notation 2018-07-03 14:50:14 +02:00
1338 changed files with 150245 additions and 62426 deletions

View File

@ -16,12 +16,15 @@ set(CORE_SRCS
cores/esp32/esp32-hal-timer.c
cores/esp32/esp32-hal-touch.c
cores/esp32/esp32-hal-uart.c
cores/esp32/esp32-hal-rmt.c
cores/esp32/Esp.cpp
cores/esp32/FunctionalInterrupt.cpp
cores/esp32/HardwareSerial.cpp
cores/esp32/IPAddress.cpp
cores/esp32/IPv6Address.cpp
cores/esp32/libb64/cdecode.c
cores/esp32/libb64/cencode.c
cores/esp32/main.cpp
cores/esp32/MD5Builder.cpp
cores/esp32/Print.cpp
cores/esp32/stdlib_noniso.c
@ -40,9 +43,11 @@ set(LIBRARY_SRCS
libraries/DNSServer/src/DNSServer.cpp
libraries/EEPROM/EEPROM.cpp
libraries/ESPmDNS/src/ESPmDNS.cpp
libraries/FFat/src/FFat.cpp
libraries/FS/src/FS.cpp
libraries/FS/src/vfs_api.cpp
libraries/HTTPClient/src/HTTPClient.cpp
libraries/HTTPUpdate/src/HTTPUpdate.cpp
libraries/NetBIOS/src/NetBIOS.cpp
libraries/Preferences/src/Preferences.cpp
libraries/SD_MMC/src/SD_MMC.cpp
@ -175,8 +180,10 @@ set(COMPONENT_ADD_INCLUDEDIRS
libraries/DNSServer/src
libraries/ESP32/src
libraries/ESPmDNS/src
libraries/FFat/src
libraries/FS/src
libraries/HTTPClient/src
libraries/HTTPUpdate/src
libraries/NetBIOS/src
libraries/Preferences/src
libraries/SD_MMC/src
@ -195,6 +202,6 @@ set(COMPONENT_ADD_INCLUDEDIRS
set(COMPONENT_PRIV_INCLUDEDIRS cores/esp32/libb64)
set(COMPONENT_REQUIRES spi_flash mbedtls mdns ethernet)
set(COMPONENT_PRIV_REQUIRES fatfs nvs_flash app_update spiffs bootloader_support openssl)
set(COMPONENT_PRIV_REQUIRES fatfs nvs_flash app_update spiffs bootloader_support openssl bt)
register_component()

View File

@ -106,8 +106,147 @@ config AUTOCONNECT_WIFI
bool "Autoconnect WiFi on boot"
default "n"
depends on AUTOSTART_ARDUINO
select ARDUINO_SELECTIVE_WiFi
help
If enabled, WiFi will connect to the last used SSID (if station was enabled),
else connection will be started only after calling WiFi.begin(ssid, password)
config ARDUINO_SELECTIVE_COMPILATION
bool "Include only specific Arduino libraries"
default n
config ARDUINO_SELECTIVE_ArduinoOTA
bool "Enable ArduinoOTA"
depends on ARDUINO_SELECTIVE_COMPILATION
select ARDUINO_SELECTIVE_WiFi
select ARDUINO_SELECTIVE_ESPmDNS
default y
config ARDUINO_SELECTIVE_AsyncUDP
bool "Enable AsyncUDP"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
config ARDUINO_SELECTIVE_AzureIoT
bool "Enable AzureIoT"
depends on ARDUINO_SELECTIVE_COMPILATION
select ARDUINO_SELECTIVE_HTTPClient
default y
config ARDUINO_SELECTIVE_BLE
bool "Enable BLE"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
config ARDUINO_SELECTIVE_BluetoothSerial
bool "Enable BluetoothSerial"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
config ARDUINO_SELECTIVE_DNSServer
bool "Enable DNSServer"
depends on ARDUINO_SELECTIVE_COMPILATION
select ARDUINO_SELECTIVE_WiFi
default y
config ARDUINO_SELECTIVE_EEPROM
bool "Enable EEPROM"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
config ARDUINO_SELECTIVE_ESP32
bool "Enable ESP32"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
config ARDUINO_SELECTIVE_ESPmDNS
bool "Enable ESPmDNS"
depends on ARDUINO_SELECTIVE_COMPILATION
select ARDUINO_SELECTIVE_WiFi
default y
config ARDUINO_SELECTIVE_FS
bool "Enable FS"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
config ARDUINO_SELECTIVE_HTTPClient
bool "Enable HTTPClient"
depends on ARDUINO_SELECTIVE_COMPILATION
select ARDUINO_SELECTIVE_WiFi
select ARDUINO_SELECTIVE_WiFiClientSecure
default y
config ARDUINO_SELECTIVE_NetBIOS
bool "Enable NetBIOS"
depends on ARDUINO_SELECTIVE_COMPILATION
select ARDUINO_SELECTIVE_WiFi
default y
config ARDUINO_SELECTIVE_Preferences
bool "Enable Preferences"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
config ARDUINO_SELECTIVE_SD
bool "Enable SD"
depends on ARDUINO_SELECTIVE_COMPILATION
select ARDUINO_SELECTIVE_FS
default y
config ARDUINO_SELECTIVE_SD_MMC
bool "Enable SD_MMC"
depends on ARDUINO_SELECTIVE_COMPILATION
select ARDUINO_SELECTIVE_FS
default y
config ARDUINO_SELECTIVE_SimpleBLE
bool "Enable SimpleBLE"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
config ARDUINO_SELECTIVE_SPI
bool "Enable SPI"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
config ARDUINO_SELECTIVE_SPIFFS
bool "Enable SPIFFS"
depends on ARDUINO_SELECTIVE_COMPILATION
select ARDUINO_SELECTIVE_FS
default y
config ARDUINO_SELECTIVE_Ticker
bool "Enable Ticker"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
config ARDUINO_SELECTIVE_Update
bool "Enable Update"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
config ARDUINO_SELECTIVE_WebServer
bool "Enable WebServer"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
select ARDUINO_SELECTIVE_FS
config ARDUINO_SELECTIVE_WiFi
bool "Enable WiFi"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
config ARDUINO_SELECTIVE_WiFiClientSecure
bool "Enable WiFiClientSecure"
depends on ARDUINO_SELECTIVE_COMPILATION
select ARDUINO_SELECTIVE_WiFi
default y
config ARDUINO_SELECTIVE_Wire
bool "Enable Wire"
depends on ARDUINO_SELECTIVE_COMPILATION
default y
endmenu

View File

@ -1,17 +1,7 @@
BOOT_APP_BIN_OFFSET := 0xe000
BOOT_APP_BIN_ROOT := $(call dequote,$(COMPONENT_PATH))
BOOT_APP_BIN_PATH := $(call dequote,$(abspath $(BOOT_APP_BIN_ROOT)/$(subst $(quote),,tools/partitions/boot_app0.bin)))
ifndef CONFIG_PARTITION_TABLE_CUSTOM
PARTITION_TABLE_CSV_PATH = $(call dequote,$(abspath $(BOOT_APP_BIN_ROOT)/$(subst $(quote),,tools/partitions/$(CONFIG_ARDUHAL_PARTITION_SCHEME).csv)))
endif
BOOT_APP_BIN_FLASH_CMD = $(ESPTOOLPY_SERIAL) write_flash $(BOOT_APP_BIN_OFFSET) $(BOOT_APP_BIN_PATH)
ESPTOOL_ALL_FLASH_ARGS += $(BOOT_APP_BIN_OFFSET) $(BOOT_APP_BIN_PATH)
CPPFLAGS += -DARDUINO=10800 -DESP32=1 -DARDUINO_ARCH_ESP32=1
boot-app0:
@echo "Rebooting to APP0"
$(BOOT_APP_BIN_FLASH_CMD)
CPPFLAGS += -DARDUINO=10800 -DESP32=1 -DARDUINO_ARCH_ESP32=1 -DBOARD_HAS_PSRAM

View File

@ -1,6 +1,4 @@
# Arduino core for ESP32 WiFi chip
[![Build Status](https://travis-ci.org/espressif/arduino-esp32.svg?branch=master)](https://travis-ci.org/espressif/arduino-esp32)
# Arduino core for ESP32 WiFi chip [![Build Status](https://travis-ci.org/espressif/arduino-esp32.svg?branch=master)](https://travis-ci.org/espressif/arduino-esp32)
### Need help or have a question? Join the chat at [![https://gitter.im/espressif/arduino-esp32](https://badges.gitter.im/espressif/arduino-esp32.svg)](https://gitter.im/espressif/arduino-esp32?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
@ -12,14 +10,19 @@
- [ESP32Dev Board PINMAP](#esp32dev-board-pinmap)
## 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/)
[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/)
Most of the framework is implemented. Most noticable is the missing analogWrite. While analogWrite is on it's way, there are a few other options that you can use:
- 16 channels [LEDC](cores/esp32/esp32-hal-ledc.h) which is PWM
- 8 channels [SigmaDelta](cores/esp32/esp32-hal-sigmadelta.h) which uses SigmaDelta modulation
- 2 channels [DAC](cores/esp32/esp32-hal-dac.h) which gives real analog output
## Installation Instructions
- Using Arduino IDE
- Using Arduino IDE Boards Manager (preferred)
+ [Instructions for Boards Manager](docs/arduino-ide/boards_manager.md)
- Using Arduino IDE with the development repository
+ [Instructions for Windows](docs/arduino-ide/windows.md)
+ [Instructions for Mac](docs/arduino-ide/mac.md)
+ [Instructions for Debian/Ubuntu Linux](docs/arduino-ide/debian_ubuntu.md)
@ -28,6 +31,7 @@ Most of the framework is implemented. Most noticable is the missing analogWrite.
- [Using PlatformIO](docs/platformio.md)
- [Building with make](docs/make.md)
- [Using as ESP-IDF component](docs/esp-idf_component.md)
- [Using OTAWebUpdater](docs/OTAWebUpdate/OTAWebUpdate.md)
#### Decoding exceptions

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,6 @@
ARDUINO_CORE_LIBS := $(patsubst $(COMPONENT_PATH)/%,%,$(sort $(dir $(wildcard $(COMPONENT_PATH)/libraries/*/*/))))
ARDUINO_LIBRARIES_LIST := $(patsubst $(COMPONENT_PATH)/libraries/%,%,$(wildcard $(COMPONENT_PATH)/libraries/*))
ARDUINO_SINGLE_LIBRARY_FILES = $(patsubst $(COMPONENT_PATH)/%,%,$(sort $(dir $(wildcard $(COMPONENT_PATH)/libraries/$(MODULE)/*/)) $(dir $(wildcard $(COMPONENT_PATH)/libraries/$(MODULE)/*/*/)) $(dir $(wildcard $(COMPONENT_PATH)/libraries/$(MODULE)/*/*/*/)) $(dir $(wildcard $(COMPONENT_PATH)/libraries/$(MODULE)/*/*/*/*/)) $(dir $(wildcard $(COMPONENT_PATH)/libraries/$(MODULE)/*/*/*/*/*/))))
ARDUINO_CORE_LIBS := $(foreach MODULE,$(ARDUINO_LIBRARIES_LIST),$(if $(CONFIG_ARDUINO_SELECTIVE_COMPILATION),$(if $(CONFIG_ARDUINO_SELECTIVE_$(MODULE)),$(ARDUINO_SINGLE_LIBRARY_FILES)),$(ARDUINO_SINGLE_LIBRARY_FILES)))
COMPONENT_ADD_INCLUDEDIRS := cores/esp32 variants/esp32 $(ARDUINO_CORE_LIBS)
COMPONENT_PRIV_INCLUDEDIRS := cores/esp32/libb64

View File

@ -75,7 +75,6 @@
#define abs(x) ((x)>0?(x):-(x))
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
#define radians(deg) ((deg)*DEG_TO_RAD)
#define degrees(rad) ((rad)*RAD_TO_DEG)
#define sq(x) ((x)*(x))
@ -146,6 +145,9 @@ void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
#ifdef __cplusplus
}
#include <algorithm>
#include <cmath>
#include "WCharacter.h"
#include "WString.h"
#include "Stream.h"
@ -158,6 +160,12 @@ void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
#include "HardwareSerial.h"
#include "Esp.h"
using std::isinf;
using std::isnan;
using std::max;
using std::min;
using ::round;
uint16_t makeWord(uint16_t w);
uint16_t makeWord(byte h, byte l);
@ -176,12 +184,6 @@ extern "C" void configTzTime(const char* tz,
long random(long);
#endif /* __cplusplus */
#ifndef _GLIBCXX_VECTOR
// arduino is not compatible with std::vector
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#endif
#define _min(a,b) ((a)<(b)?(a):(b))
#define _max(a,b) ((a)>(b)?(a):(b))

View File

@ -25,20 +25,11 @@
#include <memory>
#include <soc/soc.h>
#include <soc/efuse_reg.h>
/* Main header of binary image */
typedef struct {
uint8_t magic;
uint8_t segment_count;
uint8_t spi_mode; /* flash read mode (esp_image_spi_mode_t as uint8_t) */
uint8_t spi_speed: 4; /* flash frequency (esp_image_spi_freq_t as uint8_t) */
uint8_t spi_size: 4; /* flash chip size (esp_image_flash_size_t as uint8_t) */
uint32_t entry_addr;
uint8_t encrypt_flag; /* encrypt flag */
uint8_t extra_header[15]; /* ESP32 additional header, unused by second bootloader */
} esp_image_header_t;
#define ESP_IMAGE_HEADER_MAGIC 0xE9
#include <esp_partition.h>
#include <esp_ota_ops.h>
extern "C" {
#include <esp_image_format.h>
}
/**
* User-defined Literals
@ -112,9 +103,73 @@ void EspClass::restart(void)
esp_restart();
}
uint32_t EspClass::getHeapSize(void)
{
multi_heap_info_t info;
heap_caps_get_info(&info, MALLOC_CAP_INTERNAL);
return info.total_free_bytes + info.total_allocated_bytes;
}
uint32_t EspClass::getFreeHeap(void)
{
return esp_get_free_heap_size();
return heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
}
uint32_t EspClass::getMinFreeHeap(void)
{
return heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
}
uint32_t EspClass::getMaxAllocHeap(void)
{
return heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
}
uint32_t EspClass::getPsramSize(void)
{
multi_heap_info_t info;
heap_caps_get_info(&info, MALLOC_CAP_SPIRAM);
return info.total_free_bytes + info.total_allocated_bytes;
}
uint32_t EspClass::getFreePsram(void)
{
return heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
}
uint32_t EspClass::getMinFreePsram(void)
{
return heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM);
}
uint32_t EspClass::getMaxAllocPsram(void)
{
return heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM);
}
static uint32_t sketchSize(sketchSize_t response) {
esp_image_metadata_t data;
const esp_partition_t *running = esp_ota_get_running_partition();
if (!running) return 0;
const esp_partition_pos_t running_pos = {
.offset = running->address,
.size = running->size,
};
data.start_addr = running_pos.offset;
esp_image_load(ESP_IMAGE_VERIFY, &running_pos, &data);
if (response) {
return running_pos.size - data.image_len;
} else {
return data.image_len;
}
}
uint32_t EspClass::getSketchSize () {
return sketchSize(SKETCH_SIZE_TOTAL);
}
uint32_t EspClass::getFreeSketchSpace () {
return sketchSize(SKETCH_SIZE_FREE);
}
uint8_t EspClass::getChipRevision(void)

View File

@ -50,13 +50,30 @@ typedef enum {
FM_UNKNOWN = 0xff
} FlashMode_t;
typedef enum {
SKETCH_SIZE_TOTAL = 0,
SKETCH_SIZE_FREE = 1
} sketchSize_t;
class EspClass
{
public:
EspClass() {}
~EspClass() {}
void restart();
uint32_t getFreeHeap();
//Internal RAM
uint32_t getHeapSize(); //total heap size
uint32_t getFreeHeap(); //available heap
uint32_t getMinFreeHeap(); //lowest level of free heap since boot
uint32_t getMaxAllocHeap(); //largest block of heap that can be allocated at once
//SPI RAM
uint32_t getPsramSize();
uint32_t getFreePsram();
uint32_t getMinFreePsram();
uint32_t getMaxAllocPsram();
uint8_t getChipRevision();
uint8_t getCpuFreqMHz(){ return CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ; }
uint32_t getCycleCount();
@ -72,6 +89,9 @@ public:
uint32_t magicFlashChipSpeed(uint8_t byte);
FlashMode_t magicFlashChipMode(uint8_t byte);
uint32_t getSketchSize();
uint32_t getFreeSketchSpace();
bool flashEraseSector(uint32_t sector);
bool flashWrite(uint32_t offset, uint32_t *data, size_t size);
bool flashRead(uint32_t offset, uint32_t *data, size_t size);

View File

@ -0,0 +1,44 @@
/*
* FunctionalInterrupt.cpp
*
* Created on: 8 jul. 2018
* Author: Herman
*/
#include "FunctionalInterrupt.h"
#include "Arduino.h"
typedef void (*voidFuncPtr)(void);
typedef void (*voidFuncPtrArg)(void*);
extern "C"
{
extern void __attachInterruptFunctionalArg(uint8_t pin, voidFuncPtrArg userFunc, void * arg, int intr_type, bool functional);
}
void IRAM_ATTR interruptFunctional(void* arg)
{
InterruptArgStructure* localArg = (InterruptArgStructure*)arg;
if (localArg->interruptFunction)
{
localArg->interruptFunction();
}
}
void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode)
{
// use the local interrupt routine which takes the ArgStructure as argument
__attachInterruptFunctionalArg (pin, (voidFuncPtrArg)interruptFunctional, new InterruptArgStructure{intRoutine}, mode, true);
}
extern "C"
{
void cleanupFunctional(void* arg)
{
delete (InterruptArgStructure*)arg;
}
}

View File

@ -0,0 +1,20 @@
/*
* FunctionalInterrupt.h
*
* Created on: 8 jul. 2018
* Author: Herman
*/
#ifndef CORE_CORE_FUNCTIONALINTERRUPT_H_
#define CORE_CORE_FUNCTIONALINTERRUPT_H_
#include <functional>
struct InterruptArgStructure {
std::function<void(void)> interruptFunction;
};
void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode);
#endif /* CORE_CORE_FUNCTIONALINTERRUPT_H_ */

View File

@ -3,15 +3,34 @@
#include <string.h>
#include <inttypes.h>
#include "pins_arduino.h"
#include "HardwareSerial.h"
#ifndef RX1
#define RX1 9
#endif
#ifndef TX1
#define TX1 10
#endif
#ifndef RX2
#define RX2 16
#endif
#ifndef TX2
#define TX2 17
#endif
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
HardwareSerial Serial(0);
HardwareSerial Serial1(1);
HardwareSerial Serial2(2);
#endif
HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _uart(NULL) {}
void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert)
void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert, unsigned long timeout_ms)
{
if(0 > _uart_nr || _uart_nr > 2) {
log_e("Serial number is invalid, please use 0, 1 or 2");
@ -25,14 +44,33 @@ void HardwareSerial::begin(unsigned long baud, uint32_t config, int8_t rxPin, in
txPin = 1;
}
if(_uart_nr == 1 && rxPin < 0 && txPin < 0) {
rxPin = 9;
txPin = 10;
rxPin = RX1;
txPin = TX1;
}
if(_uart_nr == 2 && rxPin < 0 && txPin < 0) {
rxPin = 16;
txPin = 17;
rxPin = RX2;
txPin = TX2;
}
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert);
if(!baud) {
time_t startMillis = millis();
unsigned long detectedBaudRate = 0;
while(millis() - startMillis < timeout_ms && !(detectedBaudRate = uartDetectBaudrate(_uart))) {
yield();
}
end();
if(detectedBaudRate) {
delay(100); // Give some time...
_uart = uartBegin(_uart_nr, detectedBaudRate, config, rxPin, txPin, 256, invert);
} else {
log_e("Could not detect baudrate. Serial data at the port must be present within the timeout for detection to be possible");
_uart = NULL;
}
}
_uart = uartBegin(_uart_nr, baud, config, rxPin, txPin, 256, invert);
}
void HardwareSerial::end()
@ -44,6 +82,10 @@ void HardwareSerial::end()
_uart = 0;
}
size_t HardwareSerial::setRxBufferSize(size_t new_size) {
return uartResizeRxBuffer(_uart, new_size);
}
void HardwareSerial::setDebugOutput(bool en)
{
if(_uart == 0) {

View File

@ -22,6 +22,24 @@
Modified 18 December 2014 by Ivan Grokhotkov (esp8266 platform support)
Modified 31 March 2015 by Markus Sattler (rewrite the code for UART0 + UART1 support in ESP8266)
Modified 25 April 2015 by Thomas Flayols (add configuration different from 8N1 in ESP8266)
Modified 13 October 2018 by Jeroen Döll (add baudrate detection)
Baudrate detection example usage (detection on Serial1):
void setup() {
Serial.begin(115200);
delay(100);
Serial.println();
Serial1.begin(0, SERIAL_8N1, -1, -1, true, 11000UL); // Passing 0 for baudrate to detect it, the last parameter is a timeout in ms
unsigned long detectedBaudRate = Serial1.baudRate();
if(detectedBaudRate) {
Serial.printf("Detected baudrate is %lu\n", detectedBaudRate);
} else {
Serial.println("No baudrate detected, Serial1 will not work!");
}
}
Pay attention: the baudrate returned by baudRate() may be rounded, eg 115200 returns 115201
*/
#ifndef HardwareSerial_h
@ -37,7 +55,7 @@ class HardwareSerial: public Stream
public:
HardwareSerial(int uart_nr);
void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false);
void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL);
void end();
int available(void);
int availableForWrite(void);
@ -70,6 +88,7 @@ public:
uint32_t baudRate();
operator bool() const;
size_t setRxBufferSize(size_t);
void setDebugOutput(bool);
protected:
@ -79,6 +98,8 @@ protected:
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL)
extern HardwareSerial Serial;
extern HardwareSerial Serial1;
extern HardwareSerial Serial2;
#endif
#endif

View File

@ -882,6 +882,12 @@ float String::toFloat(void) const
return 0;
}
double String::toDouble(void) const {
if (buffer) {
return atof(buffer);
}
return 0;
}
unsigned char String::equalsConstantTime(const String &s2) const {
// To avoid possible time-based attacks present function

View File

@ -260,6 +260,7 @@ public:
// parsing/conversion
long toInt(void) const;
float toFloat(void) const;
double toDouble(void) const;
protected:
char *buffer; // the actual char array

View File

@ -0,0 +1 @@
#include "lwip/apps/sntp.h"

View File

@ -37,8 +37,7 @@ extern "C" {
*/
String base64::encode(uint8_t * data, size_t length)
{
// base64 needs more size then the source data
size_t size = ((length * 1.6f) + 1);
size_t size = base64_encode_expected_len(length) + 1;
char * buffer = (char *) malloc(size);
if(buffer) {
base64_encodestate _state;

View File

@ -70,7 +70,13 @@ const DRAM_ATTR esp32_gpioMux_t esp32_gpioMux[GPIO_PIN_COUNT]={
};
typedef void (*voidFuncPtr)(void);
static voidFuncPtr __pinInterruptHandlers[GPIO_PIN_COUNT] = {0,};
typedef void (*voidFuncPtrArg)(void*);
typedef struct {
voidFuncPtr fn;
void* arg;
bool functional;
} InterruptHandle_t;
static InterruptHandle_t __pinInterruptHandlers[GPIO_PIN_COUNT] = {0,};
#include "driver/rtc_io.h"
@ -193,7 +199,7 @@ extern int IRAM_ATTR __digitalRead(uint8_t pin)
static intr_handle_t gpio_intr_handle = NULL;
static void IRAM_ATTR __onPinInterrupt(void *arg)
static void IRAM_ATTR __onPinInterrupt()
{
uint32_t gpio_intr_status_l=0;
uint32_t gpio_intr_status_h=0;
@ -207,8 +213,12 @@ static void IRAM_ATTR __onPinInterrupt(void *arg)
if(gpio_intr_status_l) {
do {
if(gpio_intr_status_l & ((uint32_t)1 << pin)) {
if(__pinInterruptHandlers[pin]) {
__pinInterruptHandlers[pin]();
if(__pinInterruptHandlers[pin].fn) {
if(__pinInterruptHandlers[pin].arg){
((voidFuncPtrArg)__pinInterruptHandlers[pin].fn)(__pinInterruptHandlers[pin].arg);
} else {
__pinInterruptHandlers[pin].fn();
}
}
}
} while(++pin<32);
@ -217,23 +227,38 @@ static void IRAM_ATTR __onPinInterrupt(void *arg)
pin=32;
do {
if(gpio_intr_status_h & ((uint32_t)1 << (pin - 32))) {
if(__pinInterruptHandlers[pin]) {
__pinInterruptHandlers[pin]();
if(__pinInterruptHandlers[pin].fn) {
if(__pinInterruptHandlers[pin].arg){
((voidFuncPtrArg)__pinInterruptHandlers[pin].fn)(__pinInterruptHandlers[pin].arg);
} else {
__pinInterruptHandlers[pin].fn();
}
}
}
} while(++pin<GPIO_PIN_COUNT);
}
}
extern void __attachInterrupt(uint8_t pin, voidFuncPtr userFunc, int intr_type)
extern void cleanupFunctional(void* arg);
extern void __attachInterruptFunctionalArg(uint8_t pin, voidFuncPtrArg userFunc, void * arg, int intr_type, bool functional)
{
static bool interrupt_initialized = false;
if(!interrupt_initialized) {
interrupt_initialized = true;
esp_intr_alloc(ETS_GPIO_INTR_SOURCE, (int)ESP_INTR_FLAG_IRAM, __onPinInterrupt, NULL, &gpio_intr_handle);
}
__pinInterruptHandlers[pin] = userFunc;
// if new attach without detach remove old info
if (__pinInterruptHandlers[pin].functional && __pinInterruptHandlers[pin].arg)
{
cleanupFunctional(__pinInterruptHandlers[pin].arg);
}
__pinInterruptHandlers[pin].fn = (voidFuncPtr)userFunc;
__pinInterruptHandlers[pin].arg = arg;
__pinInterruptHandlers[pin].functional = functional;
esp_intr_disable(gpio_intr_handle);
if(esp_intr_get_cpu(gpio_intr_handle)) { //APP_CPU
GPIO.pin[pin].int_ena = 1;
@ -244,10 +269,26 @@ extern void __attachInterrupt(uint8_t pin, voidFuncPtr userFunc, int intr_type)
esp_intr_enable(gpio_intr_handle);
}
extern void __attachInterruptArg(uint8_t pin, voidFuncPtrArg userFunc, void * arg, int intr_type)
{
__attachInterruptFunctionalArg(pin, userFunc, arg, intr_type, false);
}
extern void __attachInterrupt(uint8_t pin, voidFuncPtr userFunc, int intr_type) {
__attachInterruptFunctionalArg(pin, (voidFuncPtrArg)userFunc, NULL, intr_type, false);
}
extern void __detachInterrupt(uint8_t pin)
{
esp_intr_disable(gpio_intr_handle);
__pinInterruptHandlers[pin] = NULL;
if (__pinInterruptHandlers[pin].functional && __pinInterruptHandlers[pin].arg)
{
cleanupFunctional(__pinInterruptHandlers[pin].arg);
}
__pinInterruptHandlers[pin].fn = NULL;
__pinInterruptHandlers[pin].arg = NULL;
__pinInterruptHandlers[pin].arg = false;
GPIO.pin[pin].int_ena = 0;
GPIO.pin[pin].int_type = 0;
esp_intr_enable(gpio_intr_handle);
@ -258,5 +299,6 @@ extern void pinMode(uint8_t pin, uint8_t mode) __attribute__ ((weak, alias("__pi
extern void digitalWrite(uint8_t pin, uint8_t val) __attribute__ ((weak, alias("__digitalWrite")));
extern int digitalRead(uint8_t pin) __attribute__ ((weak, alias("__digitalRead")));
extern void attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode) __attribute__ ((weak, alias("__attachInterrupt")));
extern void attachInterruptArg(uint8_t pin, voidFuncPtrArg handler, void * arg, int mode) __attribute__ ((weak, alias("__attachInterruptArg")));
extern void detachInterrupt(uint8_t pin) __attribute__ ((weak, alias("__detachInterrupt")));

View File

@ -79,6 +79,7 @@ void digitalWrite(uint8_t pin, uint8_t val);
int digitalRead(uint8_t pin);
void attachInterrupt(uint8_t pin, void (*)(void), int mode);
void attachInterruptArg(uint8_t pin, void (*)(void*), void * arg, int mode);
void detachInterrupt(uint8_t pin);
#ifdef __cplusplus

File diff suppressed because it is too large Load Diff

View File

@ -48,6 +48,7 @@ i2c_err_t i2cRead(i2c_t * i2c, uint16_t address, uint8_t* buff, uint16_t size, b
i2c_err_t i2cFlush(i2c_t *i2c);
i2c_err_t i2cSetFrequency(i2c_t * i2c, uint32_t clk_speed);
uint32_t i2cGetFrequency(i2c_t * i2c);
uint32_t i2cGetStatus(i2c_t * i2c); // Status register of peripheral
//Functions below should be used only if well understood
//Might be deprecated and removed in future
@ -62,8 +63,17 @@ i2c_err_t i2cAddQueueWrite(i2c_t *i2c, uint16_t i2cDeviceAddr, uint8_t *dataPtr,
i2c_err_t i2cAddQueueRead(i2c_t *i2c, uint16_t i2cDeviceAddr, uint8_t *dataPtr, uint16_t dataLen, bool SendStop, EventGroupHandle_t event);
//stickbreaker debug support
void i2cDumpInts(uint8_t num);
void i2cDumpI2c(i2c_t *i2c);
uint32_t i2cDebug(i2c_t *, uint32_t setBits, uint32_t resetBits);
// Debug actions have 3 currently defined locus
// 0xXX------ : at entry of ProcQueue
// 0x--XX---- : at exit of ProcQueue
// 0x------XX : at entry of Flush
//
// bit 0 causes DumpI2c to execute
// bit 1 causes DumpInts to execute
// bit 2 causes DumpCmdqueue to execute
// bit 3 causes DumpStatus to execute
// bit 4 causes DumpFifo to execute
#ifdef __cplusplus
}

View File

@ -79,38 +79,50 @@ int log_printf(const char *fmt, ...);
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
#define log_v(format, ...) log_printf(ARDUHAL_LOG_FORMAT(V, format), ##__VA_ARGS__)
#define isr_log_v(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(V, format), ##__VA_ARGS__)
#else
#define log_v(format, ...)
#define isr_log_v(format, ...)
#endif
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
#define log_d(format, ...) log_printf(ARDUHAL_LOG_FORMAT(D, format), ##__VA_ARGS__)
#define isr_log_d(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(D, format), ##__VA_ARGS__)
#else
#define log_d(format, ...)
#define isr_log_d(format, ...)
#endif
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO
#define log_i(format, ...) log_printf(ARDUHAL_LOG_FORMAT(I, format), ##__VA_ARGS__)
#define isr_log_i(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(I, format), ##__VA_ARGS__)
#else
#define log_i(format, ...)
#define isr_log_i(format, ...)
#endif
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_WARN
#define log_w(format, ...) log_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__)
#define isr_log_w(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(W, format), ##__VA_ARGS__)
#else
#define log_w(format, ...)
#define isr_log_w(format, ...)
#endif
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_ERROR
#define log_e(format, ...) log_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
#define isr_log_e(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
#else
#define log_e(format, ...)
#define isr_log_e(format, ...)
#endif
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_NONE
#define log_n(format, ...) log_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
#define isr_log_n(format, ...) ets_printf(ARDUHAL_LOG_FORMAT(E, format), ##__VA_ARGS__)
#else
#define log_n(format, ...)
#define isr_log_n(format, ...)
#endif
#include "esp_log.h"
@ -121,12 +133,22 @@ int log_printf(const char *fmt, ...);
#undef ESP_LOGI
#undef ESP_LOGD
#undef ESP_LOGV
#undef ESP_EARLY_LOGE
#undef ESP_EARLY_LOGW
#undef ESP_EARLY_LOGI
#undef ESP_EARLY_LOGD
#undef ESP_EARLY_LOGV
#define ESP_LOGE(tag, ...) log_e(__VA_ARGS__)
#define ESP_LOGW(tag, ...) log_w(__VA_ARGS__)
#define ESP_LOGI(tag, ...) log_i(__VA_ARGS__)
#define ESP_LOGD(tag, ...) log_d(__VA_ARGS__)
#define ESP_LOGV(tag, ...) log_v(__VA_ARGS__)
#define ESP_EARLY_LOGE(tag, ...) isr_log_e(__VA_ARGS__)
#define ESP_EARLY_LOGW(tag, ...) isr_log_w(__VA_ARGS__)
#define ESP_EARLY_LOGI(tag, ...) isr_log_i(__VA_ARGS__)
#define ESP_EARLY_LOGD(tag, ...) isr_log_d(__VA_ARGS__)
#define ESP_EARLY_LOGV(tag, ...) isr_log_v(__VA_ARGS__)
#endif
#ifdef __cplusplus

View File

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "esp32-hal.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
@ -24,6 +23,7 @@
#include "esp_timer.h"
#include "esp_bt.h"
#include <sys/time.h>
#include "esp32-hal.h"
//Undocumented!!! Get chip temperature in Farenheit
//Source: https://github.com/pcbreflux/espressif/blob/master/esp32/arduino/sketchbook/ESP32_int_temp_sensor/ESP32_int_temp_sensor.ino

View File

@ -1,6 +1,5 @@
#include "esp32-hal.h"
#include "sdkconfig.h"
#if CONFIG_SPIRAM_SUPPORT
#include "esp_spiram.h"

View File

@ -15,6 +15,10 @@
#ifndef _ESP32_HAL_PSRAM_H_
#define _ESP32_HAL_PSRAM_H_
#ifdef __cplusplus
extern "C" {
#endif
bool psramInit();
bool psramFound();
@ -22,4 +26,8 @@ void *ps_malloc(size_t size);
void *ps_calloc(size_t n, size_t size);
void *ps_realloc(void *ptr, size_t size);
#ifdef __cplusplus
}
#endif
#endif /* _ESP32_HAL_PSRAM_H_ */

821
cores/esp32/esp32-hal-rmt.c Normal file
View File

@ -0,0 +1,821 @@
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/semphr.h"
#include "esp32-hal.h"
#include "esp8266-compat.h"
#include "soc/gpio_reg.h"
#include "soc/gpio_reg.h"
#include "esp32-hal-rmt.h"
#include "driver/periph_ctrl.h"
#include "soc/rmt_struct.h"
#include "esp_intr_alloc.h"
/**
* Internal macros
*/
#define MAX_CHANNELS 8
#define MAX_DATA_PER_CHANNEL 64
#define MAX_DATA_PER_ITTERATION 62
#define _ABS(a) (a>0?a:-a)
#define _LIMIT(a,b) (a>b?b:a)
#define __INT_TX_END (1)
#define __INT_RX_END (2)
#define __INT_ERROR (4)
#define __INT_THR_EVNT (1<<24)
#define _INT_TX_END(channel) (__INT_TX_END<<(channel*3))
#define _INT_RX_END(channel) (__INT_RX_END<<(channel*3))
#define _INT_ERROR(channel) (__INT_ERROR<<(channel*3))
#define _INT_THR_EVNT(channel) ((__INT_THR_EVNT)<<(channel))
#if CONFIG_DISABLE_HAL_LOCKS
# define RMT_MUTEX_LOCK(channel)
# define RMT_MUTEX_UNLOCK(channel)
#else
# define RMT_MUTEX_LOCK(channel) do {} while (xSemaphoreTake(g_rmt_objlocks[channel], portMAX_DELAY) != pdPASS)
# define RMT_MUTEX_UNLOCK(channel) xSemaphoreGive(g_rmt_objlocks[channel])
#endif /* CONFIG_DISABLE_HAL_LOCKS */
#define _RMT_INTERNAL_DEBUG
#ifdef _RMT_INTERNAL_DEBUG
# define DEBUG_INTERRUPT_START(pin) digitalWrite(pin, 1);
# define DEBUG_INTERRUPT_END(pin) digitalWrite(pin, 0);
#else
# define DEBUG_INTERRUPT_START(pin)
# define DEBUG_INTERRUPT_END(pin)
#endif /* _RMT_INTERNAL_DEBUG */
/**
* Typedefs for internal stuctures, enums
*/
typedef enum {
E_NO_INTR = 0,
E_TX_INTR = 1,
E_TXTHR_INTR = 2,
E_RX_INTR = 4,
} intr_mode_t;
typedef enum {
E_INACTIVE = 0,
E_FIRST_HALF = 1,
E_LAST_DATA = 2,
E_END_TRANS = 4,
E_SET_CONTI = 8,
} transaction_state_t;
struct rmt_obj_s
{
bool allocated;
EventGroupHandle_t events;
int pin;
int channel;
bool tx_not_rx;
int buffers;
int data_size;
uint32_t* data_ptr;
intr_mode_t intr_mode;
transaction_state_t tx_state;
rmt_rx_data_cb_t cb;
bool data_alloc;
};
/**
* Internal variables for channel descriptors
*/
static xSemaphoreHandle g_rmt_objlocks[MAX_CHANNELS] = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
};
static rmt_obj_t g_rmt_objects[MAX_CHANNELS] = {
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
{ false, NULL, 0, 0, 0, 0, 0, NULL, E_NO_INTR, E_INACTIVE, NULL, false},
};
/**
* Internal variables for driver data
*/
static intr_handle_t intr_handle;
static bool periph_enabled = false;
static xSemaphoreHandle g_rmt_block_lock = NULL;
/**
* Internal method (private) declarations
*/
static void _initPin(int pin, int channel, bool tx_not_rx);
static bool _rmtSendOnce(rmt_obj_t* rmt, rmt_data_t* data, size_t size);
static void IRAM_ATTR _rmt_isr(void* arg);
static rmt_obj_t* _rmtAllocate(int pin, int from, int size);
static void _initPin(int pin, int channel, bool tx_not_rx);
static int IRAM_ATTR _rmt_get_mem_len(uint8_t channel);
static void IRAM_ATTR _rmt_tx_mem_first(uint8_t ch);
static void IRAM_ATTR _rmt_tx_mem_second(uint8_t ch);
/**
* Public method definitions
*/
bool rmtSetCarrier(rmt_obj_t* rmt, bool carrier_en, bool carrier_level, uint32_t low, uint32_t high)
{
if (!rmt || low > 0xFFFF || high > 0xFFFF) {
return false;
}
size_t channel = rmt->channel;
RMT_MUTEX_LOCK(channel);
RMT.carrier_duty_ch[channel].low = low;
RMT.carrier_duty_ch[channel].low = high;
RMT.conf_ch[channel].conf0.carrier_en = carrier_en;
RMT.conf_ch[channel].conf0.carrier_out_lv = carrier_level;
RMT_MUTEX_UNLOCK(channel);
return true;
}
bool rmtSetFilter(rmt_obj_t* rmt, bool filter_en, uint32_t filter_level)
{
if (!rmt || filter_level > 0xFF) {
return false;
}
size_t channel = rmt->channel;
RMT_MUTEX_LOCK(channel);
RMT.conf_ch[channel].conf1.rx_filter_thres = filter_level;
RMT.conf_ch[channel].conf1.rx_filter_en = filter_en;
RMT_MUTEX_UNLOCK(channel);
return true;
}
bool rmtSetRxThreshold(rmt_obj_t* rmt, uint32_t value)
{
if (!rmt || value > 0xFFFF) {
return false;
}
size_t channel = rmt->channel;
RMT_MUTEX_LOCK(channel);
RMT.conf_ch[channel].conf0.idle_thres = value;
RMT_MUTEX_UNLOCK(channel);
return true;
}
bool rmtDeinit(rmt_obj_t *rmt)
{
if (!rmt) {
return false;
}
// sanity check
if (rmt != &(g_rmt_objects[rmt->channel])) {
return false;
}
size_t from = rmt->channel;
size_t to = rmt->buffers + rmt->channel;
size_t i;
#if !CONFIG_DISABLE_HAL_LOCKS
if(g_rmt_objlocks[from] != NULL) {
vSemaphoreDelete(g_rmt_objlocks[from]);
}
#endif
if (g_rmt_objects[from].data_alloc) {
free(g_rmt_objects[from].data_ptr);
}
for (i = from; i < to; i++) {
g_rmt_objects[i].allocated = false;
}
g_rmt_objects[from].channel = 0;
g_rmt_objects[from].buffers = 0;
return true;
}
bool rmtWrite(rmt_obj_t* rmt, rmt_data_t* data, size_t size)
{
if (!rmt) {
return false;
}
int channel = rmt->channel;
int allocated_size = MAX_DATA_PER_CHANNEL * rmt->buffers;
if (size > allocated_size) {
int half_tx_nr = MAX_DATA_PER_ITTERATION/2;
RMT_MUTEX_LOCK(channel);
// setup interrupt handler if not yet installed for half and full tx
if (!intr_handle) {
esp_intr_alloc(ETS_RMT_INTR_SOURCE, (int)ESP_INTR_FLAG_IRAM, _rmt_isr, NULL, &intr_handle);
}
rmt->data_size = size - MAX_DATA_PER_ITTERATION;
rmt->data_ptr = ((uint32_t*)data) + MAX_DATA_PER_ITTERATION;
rmt->intr_mode = E_TX_INTR | E_TXTHR_INTR;
rmt->tx_state = E_SET_CONTI | E_FIRST_HALF;
// init the tx limit for interruption
RMT.tx_lim_ch[channel].limit = half_tx_nr+2;
// reset memory pointer
RMT.conf_ch[channel].conf1.apb_mem_rst = 1;
RMT.conf_ch[channel].conf1.apb_mem_rst = 0;
RMT.conf_ch[channel].conf1.mem_rd_rst = 1;
RMT.conf_ch[channel].conf1.mem_rd_rst = 0;
RMT.conf_ch[channel].conf1.mem_wr_rst = 1;
RMT.conf_ch[channel].conf1.mem_wr_rst = 0;
// set the tx end mark
RMTMEM.chan[channel].data32[MAX_DATA_PER_ITTERATION].val = 0;
// clear and enable both Tx completed and half tx event
RMT.int_clr.val = _INT_TX_END(channel);
RMT.int_clr.val = _INT_THR_EVNT(channel);
RMT.int_clr.val = _INT_ERROR(channel);
RMT.int_ena.val |= _INT_TX_END(channel);
RMT.int_ena.val |= _INT_THR_EVNT(channel);
RMT.int_ena.val |= _INT_ERROR(channel);
RMT_MUTEX_UNLOCK(channel);
// start the transation
return _rmtSendOnce(rmt, data, MAX_DATA_PER_ITTERATION);
} else {
// use one-go mode if data fits one buffer
return _rmtSendOnce(rmt, data, size);
}
}
bool rmtReadData(rmt_obj_t* rmt, uint32_t* data, size_t size)
{
if (!rmt) {
return false;
}
int channel = rmt->channel;
if (g_rmt_objects[channel].buffers < size/MAX_DATA_PER_CHANNEL) {
return false;
}
size_t i;
volatile uint32_t* rmt_mem_ptr = &(RMTMEM.chan[channel].data32[0].val);
for (i=0; i<size; i++) {
data[i] = *rmt_mem_ptr++;
}
return true;
}
bool rmtBeginReceive(rmt_obj_t* rmt)
{
if (!rmt) {
return false;
}
int channel = rmt->channel;
RMT.int_clr.val = _INT_ERROR(channel);
RMT.int_ena.val |= _INT_ERROR(channel);
RMT.conf_ch[channel].conf1.mem_owner = 1;
RMT.conf_ch[channel].conf1.mem_wr_rst = 1;
RMT.conf_ch[channel].conf1.rx_en = 1;
return true;
}
bool rmtReceiveCompleted(rmt_obj_t* rmt)
{
if (!rmt) {
return false;
}
int channel = rmt->channel;
if (RMT.int_raw.val&_INT_RX_END(channel)) {
// RX end flag
RMT.int_clr.val = _INT_RX_END(channel);
return true;
} else {
return false;
}
}
bool rmtRead(rmt_obj_t* rmt, rmt_rx_data_cb_t cb)
{
if (!rmt && !cb) {
return false;
}
int channel = rmt->channel;
RMT_MUTEX_LOCK(channel);
rmt->intr_mode = E_RX_INTR;
rmt->tx_state = E_FIRST_HALF;
rmt->cb = cb;
// allocate internally two buffers which would alternate
if (!rmt->data_alloc) {
rmt->data_ptr = (uint32_t*)malloc(2*MAX_DATA_PER_CHANNEL*(rmt->buffers)*sizeof(uint32_t));
rmt->data_size = MAX_DATA_PER_CHANNEL*rmt->buffers;
rmt->data_alloc = true;
}
RMT.conf_ch[channel].conf1.mem_owner = 1;
RMT.int_clr.val = _INT_RX_END(channel);
RMT.int_clr.val = _INT_ERROR(channel);
RMT.int_ena.val |= _INT_RX_END(channel);
RMT.int_ena.val |= _INT_ERROR(channel);
RMT.conf_ch[channel].conf1.mem_wr_rst = 1;
RMT.conf_ch[channel].conf1.rx_en = 1;
RMT_MUTEX_UNLOCK(channel);
return true;
}
bool rmtReadAsync(rmt_obj_t* rmt, rmt_data_t* data, size_t size, void* eventFlag, bool waitForData, uint32_t timeout)
{
if (!rmt) {
return false;
}
int channel = rmt->channel;
if (g_rmt_objects[channel].buffers < size/MAX_DATA_PER_CHANNEL) {
return false;
}
if (eventFlag) {
xEventGroupClearBits(eventFlag, RMT_FLAGS_ALL);
rmt->events = eventFlag;
}
if (data && size>0) {
rmt->data_ptr = (uint32_t*)data;
rmt->data_size = size;
}
RMT_MUTEX_LOCK(channel);
rmt->intr_mode = E_RX_INTR;
RMT.conf_ch[channel].conf1.mem_owner = 1;
RMT.int_clr.val = _INT_RX_END(channel);
RMT.int_clr.val = _INT_ERROR(channel);
RMT.int_ena.val |= _INT_RX_END(channel);
RMT.int_ena.val |= _INT_ERROR(channel);
RMT.conf_ch[channel].conf1.mem_wr_rst = 1;
RMT.conf_ch[channel].conf1.rx_en = 1;
RMT_MUTEX_UNLOCK(channel);
// wait for data if requested so
if (waitForData && eventFlag) {
uint32_t flags = xEventGroupWaitBits(eventFlag, RMT_FLAGS_ALL,
pdTRUE /* clear on exit */, pdFALSE /* wait for all bits */, timeout);
if (flags & RMT_FLAG_ERROR) {
return false;
}
}
return true;
}
float rmtSetTick(rmt_obj_t* rmt, float tick)
{
if (!rmt) {
return false;
}
/*
divider field span from 1 (smallest), 2, 3, ... , 0xFF, 0x00 (highest)
* rmt tick from 1/80M -> 12.5ns (1x) div_cnt = 0x01
3.2 us (256x) div_cnt = 0x00
* rmt tick for 1 MHz -> 1us (1x) div_cnt = 0x01
256us (256x) div_cnt = 0x00
*/
int apb_div = _LIMIT(tick/12.5, 256);
int ref_div = _LIMIT(tick/1000, 256);
float apb_tick = 12.5 * apb_div;
float ref_tick = 1000.0 * ref_div;
size_t channel = rmt->channel;
if (_ABS(apb_tick - tick) < _ABS(ref_tick - tick)) {
RMT.conf_ch[channel].conf0.div_cnt = apb_div & 0xFF;
RMT.conf_ch[channel].conf1.ref_always_on = 1;
return apb_tick;
} else {
RMT.conf_ch[channel].conf0.div_cnt = ref_div & 0xFF;
RMT.conf_ch[channel].conf1.ref_always_on = 0;
return ref_tick;
}
}
rmt_obj_t* rmtInit(int pin, bool tx_not_rx, rmt_reserve_memsize_t memsize)
{
int buffers = memsize;
rmt_obj_t* rmt;
size_t i;
size_t j;
// create common block mutex for protecting allocs from multiple threads
if (!g_rmt_block_lock) {
g_rmt_block_lock = xSemaphoreCreateMutex();
}
// lock
while (xSemaphoreTake(g_rmt_block_lock, portMAX_DELAY) != pdPASS) {}
for (i=0; i<MAX_CHANNELS; i++) {
for (j=0; j<buffers && i+j < MAX_CHANNELS; j++) {
// if the space is ocupied break and continue on other channel
if (g_rmt_objects[i+j].allocated) {
i += j; // continue searching from latter channel
break;
}
}
if (j == buffers) {
// found a space in channel descriptors
break;
}
}
if (i == MAX_CHANNELS || i+j >= MAX_CHANNELS || j != buffers) {
xSemaphoreGive(g_rmt_block_lock);
return NULL;
}
rmt = _rmtAllocate(pin, i, buffers);
xSemaphoreGive(g_rmt_block_lock);
size_t channel = i;
#if !CONFIG_DISABLE_HAL_LOCKS
if(g_rmt_objlocks[channel] == NULL) {
g_rmt_objlocks[channel] = xSemaphoreCreateMutex();
if(g_rmt_objlocks[channel] == NULL) {
return NULL;
}
}
#endif
RMT_MUTEX_LOCK(channel);
rmt->pin = pin;
rmt->tx_not_rx = tx_not_rx;
rmt->buffers =buffers;
rmt->channel = channel;
_initPin(pin, channel, tx_not_rx);
// Initialize the registers in default mode:
// - no carrier, filter
// - timebase tick of 1us
// - idle threshold set to 0x8000 (max pulse width + 1)
RMT.conf_ch[channel].conf0.div_cnt = 1;
RMT.conf_ch[channel].conf0.mem_size = buffers;
RMT.conf_ch[channel].conf0.carrier_en = 0;
RMT.conf_ch[channel].conf0.carrier_out_lv = 0;
RMT.conf_ch[channel].conf0.mem_pd = 0;
RMT.conf_ch[channel].conf0.idle_thres = 0x80;
RMT.conf_ch[channel].conf1.rx_en = 0;
RMT.conf_ch[channel].conf1.tx_conti_mode = 0;
RMT.conf_ch[channel].conf1.ref_cnt_rst = 0;
RMT.conf_ch[channel].conf1.rx_filter_en = 0;
RMT.conf_ch[channel].conf1.rx_filter_thres = 0;
RMT.conf_ch[channel].conf1.idle_out_lv = 0; // signal level for idle
RMT.conf_ch[channel].conf1.idle_out_en = 1; // enable idle
RMT.conf_ch[channel].conf1.ref_always_on = 0; // base clock
RMT.apb_conf.fifo_mask = 1;
if (tx_not_rx) {
// RMT.conf_ch[channel].conf1.rx_en = 0;
RMT.conf_ch[channel].conf1.mem_owner = 0;
RMT.conf_ch[channel].conf1.mem_rd_rst = 1;
} else {
// RMT.conf_ch[channel].conf1.rx_en = 1;
RMT.conf_ch[channel].conf1.mem_owner = 1;
RMT.conf_ch[channel].conf1.mem_wr_rst = 1;
}
// install interrupt if at least one channel is active
if (!intr_handle) {
esp_intr_alloc(ETS_RMT_INTR_SOURCE, (int)ESP_INTR_FLAG_IRAM, _rmt_isr, NULL, &intr_handle);
}
RMT_MUTEX_UNLOCK(channel);
return rmt;
}
/**
* Private methods definitions
*/
bool _rmtSendOnce(rmt_obj_t* rmt, rmt_data_t* data, size_t size)
{
if (!rmt) {
return false;
}
int channel = rmt->channel;
RMT.apb_conf.fifo_mask = 1;
if (data && size>0) {
size_t i;
volatile uint32_t* rmt_mem_ptr = &(RMTMEM.chan[channel].data32[0].val);
for (i = 0; i < size; i++) {
*rmt_mem_ptr++ = data[i].val;
}
// tx end mark
RMTMEM.chan[channel].data32[size].val = 0;
}
RMT_MUTEX_LOCK(channel);
RMT.conf_ch[channel].conf1.mem_rd_rst = 1;
RMT.conf_ch[channel].conf1.tx_start = 1;
RMT_MUTEX_UNLOCK(channel);
return true;
}
static rmt_obj_t* _rmtAllocate(int pin, int from, int size)
{
size_t i;
// setup how many buffers shall we use
g_rmt_objects[from].buffers = size;
for (i=0; i<size; i++) {
// mark the block of channels as used
g_rmt_objects[i+from].allocated = true;
}
return &(g_rmt_objects[from]);
}
static void _initPin(int pin, int channel, bool tx_not_rx)
{
if (!periph_enabled) {
periph_enabled = true;
periph_module_enable( PERIPH_RMT_MODULE );
}
if (tx_not_rx) {
pinMode(pin, OUTPUT);
pinMatrixOutAttach(pin, RMT_SIG_OUT0_IDX + channel, 0, 0);
} else {
pinMode(pin, INPUT);
pinMatrixInAttach(pin, RMT_SIG_IN0_IDX + channel, 0);
}
}
static void IRAM_ATTR _rmt_isr(void* arg)
{
int intr_val = RMT.int_st.val;
size_t ch;
for (ch = 0; ch < MAX_CHANNELS; ch++) {
if (intr_val & _INT_RX_END(ch)) {
// clear the flag
RMT.int_clr.val = _INT_RX_END(ch);
RMT.int_ena.val &= ~_INT_RX_END(ch);
if ((g_rmt_objects[ch].intr_mode) & E_RX_INTR) {
if (g_rmt_objects[ch].events) {
xEventGroupSetBits(g_rmt_objects[ch].events, RMT_FLAG_RX_DONE);
}
if (g_rmt_objects[ch].data_ptr && g_rmt_objects[ch].data_size > 0) {
size_t i;
uint32_t * data = g_rmt_objects[ch].data_ptr;
// in case of callback, provide switching between memories
if (g_rmt_objects[ch].cb) {
if (g_rmt_objects[ch].tx_state & E_FIRST_HALF) {
g_rmt_objects[ch].tx_state &= ~E_FIRST_HALF;
} else {
g_rmt_objects[ch].tx_state |= E_FIRST_HALF;
data += MAX_DATA_PER_CHANNEL*(g_rmt_objects[ch].buffers);
}
}
for (i = 0; i < g_rmt_objects[ch].data_size; i++ ) {
*data++ = RMTMEM.chan[ch].data32[i].val;
}
if (g_rmt_objects[ch].cb) {
// actually received data ptr
uint32_t * data = g_rmt_objects[ch].data_ptr;
(g_rmt_objects[ch].cb)(data, _rmt_get_mem_len(ch));
// restart the reception
RMT.conf_ch[ch].conf1.mem_owner = 1;
RMT.conf_ch[ch].conf1.mem_wr_rst = 1;
RMT.conf_ch[ch].conf1.rx_en = 1;
RMT.int_ena.val |= _INT_RX_END(ch);
} else {
// if not callback provide, expect only one Rx
g_rmt_objects[ch].intr_mode &= ~E_RX_INTR;
}
}
} else {
// Report error and disable Rx interrupt
log_e("Unexpected Rx interrupt!\n"); // TODO: eplace messages with log_X
RMT.int_ena.val &= ~_INT_RX_END(ch);
}
}
if (intr_val & _INT_ERROR(ch)) {
digitalWrite(2, 1);
// clear the flag
RMT.int_clr.val = _INT_ERROR(ch);
RMT.int_ena.val &= ~_INT_ERROR(ch);
// report error
log_e("RMT Error %d!\n", ch);
if (g_rmt_objects[ch].events) {
xEventGroupSetBits(g_rmt_objects[ch].events, RMT_FLAG_ERROR);
}
// reset memory
RMT.conf_ch[ch].conf1.mem_rd_rst = 1;
RMT.conf_ch[ch].conf1.mem_rd_rst = 0;
RMT.conf_ch[ch].conf1.mem_wr_rst = 1;
RMT.conf_ch[ch].conf1.mem_wr_rst = 0;
}
if (intr_val & _INT_TX_END(ch)) {
RMT.int_clr.val = _INT_TX_END(ch);
_rmt_tx_mem_second(ch);
}
if (intr_val & _INT_THR_EVNT(ch)) {
// clear the flag
RMT.int_clr.val = _INT_THR_EVNT(ch);
// initial setup of continuous mode
if (g_rmt_objects[ch].tx_state & E_SET_CONTI) {
RMT.conf_ch[ch].conf1.tx_conti_mode = 1;
g_rmt_objects[ch].intr_mode &= ~E_SET_CONTI;
}
_rmt_tx_mem_first(ch);
}
}
}
static void IRAM_ATTR _rmt_tx_mem_second(uint8_t ch)
{
DEBUG_INTERRUPT_START(4)
uint32_t* data = g_rmt_objects[ch].data_ptr;
int half_tx_nr = MAX_DATA_PER_ITTERATION/2;
int i;
RMT.tx_lim_ch[ch].limit = half_tx_nr+2;
RMT.int_clr.val = _INT_THR_EVNT(ch);
RMT.int_ena.val |= _INT_THR_EVNT(ch);
g_rmt_objects[ch].tx_state |= E_FIRST_HALF;
if (data) {
int remaining_size = g_rmt_objects[ch].data_size;
// will the remaining data occupy the entire halfbuffer
if (remaining_size > half_tx_nr) {
for (i = 0; i < half_tx_nr; i++) {
RMTMEM.chan[ch].data32[half_tx_nr+i].val = data[i];
}
g_rmt_objects[ch].data_size -= half_tx_nr;
g_rmt_objects[ch].data_ptr += half_tx_nr;
} else {
for (i = 0; i < half_tx_nr; i++) {
if (i < remaining_size) {
RMTMEM.chan[ch].data32[half_tx_nr+i].val = data[i];
} else {
RMTMEM.chan[ch].data32[half_tx_nr+i].val = 0x000F000F;
}
}
g_rmt_objects[ch].data_ptr = NULL;
}
} else if ((!(g_rmt_objects[ch].tx_state & E_LAST_DATA)) &&
(!(g_rmt_objects[ch].tx_state & E_END_TRANS))) {
for (i = 0; i < half_tx_nr; i++) {
RMTMEM.chan[ch].data32[half_tx_nr+i].val = 0x000F000F;
}
RMTMEM.chan[ch].data32[half_tx_nr+i].val = 0;
g_rmt_objects[ch].tx_state |= E_LAST_DATA;
RMT.conf_ch[ch].conf1.tx_conti_mode = 0;
} else {
log_d("RMT Tx finished %d!\n", ch);
RMT.conf_ch[ch].conf1.tx_conti_mode = 0;
RMT.int_ena.val &= ~_INT_TX_END(ch);
RMT.int_ena.val &= ~_INT_THR_EVNT(ch);
g_rmt_objects[ch].intr_mode = E_NO_INTR;
g_rmt_objects[ch].tx_state = E_INACTIVE;
}
DEBUG_INTERRUPT_END(4);
}
static void IRAM_ATTR _rmt_tx_mem_first(uint8_t ch)
{
DEBUG_INTERRUPT_START(2);
uint32_t* data = g_rmt_objects[ch].data_ptr;
int half_tx_nr = MAX_DATA_PER_ITTERATION/2;
int i;
RMT.int_ena.val &= ~_INT_THR_EVNT(ch);
RMT.tx_lim_ch[ch].limit = 0;
if (data) {
int remaining_size = g_rmt_objects[ch].data_size;
// will the remaining data occupy the entire halfbuffer
if (remaining_size > half_tx_nr) {
RMTMEM.chan[ch].data32[0].val = data[0] - 1;
for (i = 1; i < half_tx_nr; i++) {
RMTMEM.chan[ch].data32[i].val = data[i];
}
g_rmt_objects[ch].tx_state &= ~E_FIRST_HALF;
// turn off the treshold interrupt
RMT.int_ena.val &= ~_INT_THR_EVNT(ch);
RMT.tx_lim_ch[ch].limit = 0;
g_rmt_objects[ch].data_size -= half_tx_nr;
g_rmt_objects[ch].data_ptr += half_tx_nr;
} else {
RMTMEM.chan[ch].data32[0].val = data[0] - 1;
for (i = 1; i < half_tx_nr; i++) {
if (i < remaining_size) {
RMTMEM.chan[ch].data32[i].val = data[i];
} else {
RMTMEM.chan[ch].data32[i].val = 0x000F000F;
}
}
g_rmt_objects[ch].tx_state &= ~E_FIRST_HALF;
g_rmt_objects[ch].data_ptr = NULL;
}
} else {
for (i = 0; i < half_tx_nr; i++) {
RMTMEM.chan[ch].data32[i].val = 0x000F000F;
}
RMTMEM.chan[ch].data32[i].val = 0;
g_rmt_objects[ch].tx_state &= ~E_FIRST_HALF;
RMT.tx_lim_ch[ch].limit = 0;
g_rmt_objects[ch].tx_state |= E_LAST_DATA;
RMT.conf_ch[ch].conf1.tx_conti_mode = 0;
}
DEBUG_INTERRUPT_END(2);
}
static int IRAM_ATTR _rmt_get_mem_len(uint8_t channel)
{
int block_num = RMT.conf_ch[channel].conf0.mem_size;
int item_block_len = block_num * 64;
volatile rmt_item32_t* data = RMTMEM.chan[channel].data32;
int idx;
for(idx = 0; idx < item_block_len; idx++) {
if(data[idx].duration0 == 0) {
return idx;
} else if(data[idx].duration1 == 0) {
return idx + 1;
}
}
return idx;
}

137
cores/esp32/esp32-hal-rmt.h Normal file
View File

@ -0,0 +1,137 @@
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef MAIN_ESP32_HAL_RMT_H_
#define MAIN_ESP32_HAL_RMT_H_
#ifdef __cplusplus
extern "C" {
#endif
// notification flags
#define RMT_FLAG_TX_DONE (1)
#define RMT_FLAG_RX_DONE (2)
#define RMT_FLAG_ERROR (4)
#define RMT_FLAGS_ALL (RMT_FLAG_TX_DONE | RMT_FLAG_RX_DONE | RMT_FLAG_ERROR)
struct rmt_obj_s;
typedef enum {
RMT_MEM_64 = 1,
RMT_MEM_128 = 2,
RMT_MEM_192 = 3,
RMT_MEM_256 = 4,
RMT_MEM_320 = 5,
RMT_MEM_384 = 6,
RMT_MEM_448 = 7,
RMT_MEM_512 = 8,
} rmt_reserve_memsize_t;
typedef struct rmt_obj_s rmt_obj_t;
typedef void (*rmt_rx_data_cb_t)(uint32_t *data, size_t len);
typedef struct {
union {
struct {
uint32_t duration0 :15;
uint32_t level0 :1;
uint32_t duration1 :15;
uint32_t level1 :1;
};
uint32_t val;
};
} rmt_data_t;
/**
* Initialize the object
*
*/
rmt_obj_t* rmtInit(int pin, bool tx_not_rx, rmt_reserve_memsize_t memsize);
/**
* Sets the clock/divider of timebase the nearest tick to the supplied value in nanoseconds
* return the real actual tick value in ns
*/
float rmtSetTick(rmt_obj_t* rmt, float tick);
/**
* Sending data in one-go mode or continual mode
* (more data being send while updating buffers in interrupts)
*
*/
bool rmtWrite(rmt_obj_t* rmt, rmt_data_t* data, size_t size);
/**
* Initiates async receive, event flag indicates data received
*
*/
bool rmtReadAsync(rmt_obj_t* rmt, rmt_data_t* data, size_t size, void* eventFlag, bool waitForData, uint32_t timeout);
/**
* Initiates async receive with automatic buffering
* and callback with data from ISR
*
*/
bool rmtRead(rmt_obj_t* rmt, rmt_rx_data_cb_t cb);
/* Additional interface */
/**
* Start reception
*
*/
bool rmtBeginReceive(rmt_obj_t* rmt);
/**
* Checks if reception completes
*
*/
bool rmtReceiveCompleted(rmt_obj_t* rmt);
/**
* Reads the data for particular channel
*
*/
bool rmtReadData(rmt_obj_t* rmt, uint32_t* data, size_t size);
/**
* Setting threshold for Rx completed
*/
bool rmtSetRxThreshold(rmt_obj_t* rmt, uint32_t value);
/**
* Setting carrier
*/
bool rmtSetCarrier(rmt_obj_t* rmt, bool carrier_en, bool carrier_level, uint32_t low, uint32_t high);
/**
* Setting input filter
*/
bool rmtSetFilter(rmt_obj_t* rmt, bool filter_en, uint32_t filter_level);
// TODO:
// * uninstall interrupt when all channels are deinit
// * send only-conti mode with circular-buffer
// * put sanity checks to some macro or inlines
// * doxy comments
// * error reporting
#ifdef __cplusplus
}
#endif
#endif /* MAIN_ESP32_HAL_RMT_H_ */

View File

@ -13,7 +13,7 @@
// limitations under the License.
#include "esp32-hal.h"
#include "apps/sntp/sntp.h"
#include "lwip/apps/sntp.h"
static void setTimeZone(long offset, int daylight)
{

View File

@ -80,7 +80,7 @@ static void IRAM_ATTR _uart_isr(void *arg)
uart->dev->int_clr.rxfifo_full = 1;
uart->dev->int_clr.frm_err = 1;
uart->dev->int_clr.rxfifo_tout = 1;
while(uart->dev->status.rxfifo_cnt) {
while(uart->dev->status.rxfifo_cnt || (uart->dev->mem_rx_status.wr_addr != uart->dev->mem_rx_status.rd_addr)) {
c = uart->dev->fifo.rw_byte;
if(uart->queue != NULL && !xQueueIsQueueFullFromISR(uart->queue)) {
xQueueSendFromISR(uart->queue, &c, &xHigherPriorityTaskWoken);
@ -240,6 +240,26 @@ void uartEnd(uart_t* uart)
uartDetachTx(uart);
}
size_t uartResizeRxBuffer(uart_t * uart, size_t new_size) {
if(uart == NULL) {
return 0;
}
UART_MUTEX_LOCK();
if(uart->queue != NULL) {
uint8_t c;
while(xQueueReceive(uart->queue, &c, 0));
vQueueDelete(uart->queue);
uart->queue = xQueueCreate(new_size, sizeof(uint8_t));
if(uart->queue == NULL) {
return NULL;
}
}
UART_MUTEX_UNLOCK();
return new_size;
}
uint32_t uartAvailable(uart_t* uart)
{
if(uart == NULL || uart->queue == NULL) {
@ -313,13 +333,16 @@ void uartFlush(uart_t* uart)
}
UART_MUTEX_LOCK();
while(uart->dev->status.txfifo_cnt);
while(uart->dev->status.txfifo_cnt || uart->dev->status.st_utx_out);
uart->dev->conf0.txfifo_rst = 1;
uart->dev->conf0.txfifo_rst = 0;
//Due to hardware issue, we can not use fifo_rst to reset uart fifo.
//See description about UART_TXFIFO_RST and UART_RXFIFO_RST in <<esp32_technical_reference_manual>> v2.6 or later.
// we read the data out and make `fifo_len == 0 && rd_addr == wr_addr`.
while(uart->dev->status.rxfifo_cnt != 0 || (uart->dev->mem_rx_status.wr_addr != uart->dev->mem_rx_status.rd_addr)) {
READ_PERI_REG(UART_FIFO_REG(uart->num));
}
uart->dev->conf0.rxfifo_rst = 1;
uart->dev->conf0.rxfifo_rst = 0;
UART_MUTEX_UNLOCK();
}
@ -432,3 +455,65 @@ int log_printf(const char *format, ...)
}
return len;
}
/*
* if enough pulses are detected return the minimum high pulse duration + minimum low pulse duration divided by two.
* This equals one bit period. If flag is true the function return inmediately, otherwise it waits for enough pulses.
*/
unsigned long uartBaudrateDetect(uart_t *uart, bool flg)
{
while(uart->dev->rxd_cnt.edge_cnt < 30) { // UART_PULSE_NUM(uart_num)
if(flg) return 0;
ets_delay_us(1000);
}
UART_MUTEX_LOCK();
unsigned long ret = ((uart->dev->lowpulse.min_cnt + uart->dev->highpulse.min_cnt) >> 1) + 12;
UART_MUTEX_UNLOCK();
return ret;
}
/*
* To start detection of baud rate with the uart the auto_baud.en bit needs to be cleared and set. The bit period is
* detected calling uartBadrateDetect(). The raw baudrate is computed using the UART_CLK_FREQ. The raw baudrate is
* rounded to the closed real baudrate.
*/
unsigned long
uartDetectBaudrate(uart_t *uart)
{
static bool uartStateDetectingBaudrate = false;
if(!uartStateDetectingBaudrate) {
uart->dev->auto_baud.glitch_filt = 0x08;
uart->dev->auto_baud.en = 0;
uart->dev->auto_baud.en = 1;
uartStateDetectingBaudrate = true;
}
unsigned long divisor = uartBaudrateDetect(uart, true);
if (!divisor) {
return 0;
}
uart->dev->auto_baud.en = 0;
uartStateDetectingBaudrate = false; // Initialize for the next round
unsigned long baudrate = UART_CLK_FREQ / divisor;
static const unsigned long default_rates[] = {300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400};
size_t i;
for (i = 1; i < sizeof(default_rates) / sizeof(default_rates[0]) - 1; i++) // find the nearest real baudrate
{
if (baudrate <= default_rates[i])
{
if (baudrate - default_rates[i - 1] < default_rates[i] - baudrate) {
i--;
}
break;
}
}
return default_rates[i];
}

View File

@ -67,9 +67,13 @@ void uartFlush(uart_t* uart);
void uartSetBaudRate(uart_t* uart, uint32_t baud_rate);
uint32_t uartGetBaudRate(uart_t* uart);
size_t uartResizeRxBuffer(uart_t* uart, size_t new_size);
void uartSetDebug(uart_t* uart);
int uartGetDebug();
unsigned long uartDetectBaudrate(uart_t *uart);
#ifdef __cplusplus
}
#endif

View File

@ -33,6 +33,7 @@ extern "C" {
#include <string.h>
#include <math.h>
#include "sdkconfig.h"
#include "esp_system.h"
#ifndef F_CPU
#define F_CPU (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ * 1000000U)
@ -56,11 +57,17 @@ void yield(void);
#include "esp32-hal-spi.h"
#include "esp32-hal-i2c.h"
#include "esp32-hal-ledc.h"
#include "esp32-hal-rmt.h"
#include "esp32-hal-sigmadelta.h"
#include "esp32-hal-timer.h"
#include "esp32-hal-bt.h"
#include "esp32-hal-psram.h"
#include "esp_system.h"
#ifndef BOARD_HAS_PSRAM
#ifdef CONFIG_SPIRAM_SUPPORT
#undef CONFIG_SPIRAM_SUPPORT
#endif
#endif
//returns chip temperature in Celsius
float temperatureRead();

View File

@ -7,13 +7,10 @@ For details, see http://sourceforge.net/projects/libb64
#include "cencode.h"
const int CHARS_PER_LINE = 72;
void base64_init_encodestate(base64_encodestate* state_in)
{
state_in->step = step_A;
state_in->result = 0;
state_in->stepcount = 0;
}
char base64_encode_value(char value_in)
@ -68,12 +65,6 @@ int base64_encode_block(const char* plaintext_in, int length_in, char* code_out,
*codechar++ = base64_encode_value(result);
result = (fragment & 0x03f) >> 0;
*codechar++ = base64_encode_value(result);
++(state_in->stepcount);
if (state_in->stepcount == CHARS_PER_LINE/4) {
*codechar++ = '\n';
state_in->stepcount = 0;
}
}
}
/* control should not reach here */

View File

@ -1,24 +1,32 @@
Please fill the info fields, it helps to get you faster support ;)
Make your question, not a Statement, inclusive. Include all pertinent information:
If you have a Guru Meditation Error, please decode it:
What you are trying to do.
Describe your system( Hardware, computer, O/S, core version, environment)
Describe what is failing
Show the shortest possible code that will duplicate the error
Show the EXACT error message(it doesn't work is not enough)
Then if someone is interested and knowledgeable you might get a answer. All of this work on your part shows us that you have worked to solve YOUR problem. The more complete your issue posting is, the more likely someone will volunteer their time to help you.
If you have a Guru Meditation Error or Backtrace, ***please decode it***:
https://github.com/me-no-dev/EspExceptionDecoder
----------------------------- Remove above -----------------------------
### Hardware:
Board: ?ESP32 Dev Module?
Board: ?ESP32 Dev Module? ?node32? ?ttgo_lora?
Core Installation/update date: ?11/jul/2017?
IDE name: ?Arduino IDE? ?Platform.io? ?IDF component?
Flash Frequency: ?40Mhz?
PSRAM enabled: ?no?
Upload Speed: ?115200?
Computer OS: ?Windows 10? ?Mac OSX? ?Ubuntu?
### Description:
Describe your problem here
### Sketch:
### Sketch: (leave the backquotes for [code formatting](https://help.github.com/articles/creating-and-highlighting-code-blocks/))
```cpp
//Change the code below by your sketch

View File

@ -0,0 +1,59 @@
# Over the Air through Web browser
OTAWebUpdate is done with a web browser that can be useful in the following typical scenarios:
- Once the application developed and loading directly from Arduino IDE is inconvenient or not possible
- after deployment if user is unable to expose Firmware for OTA from external update server
- provide updates after deployment to small quantity of modules when setting an update server is not practicable
## Requirements
- The ESP and the computer must be connected to the same network
## Implementation
The sample implementation has been done using:
- example sketch OTAWebUpdater.ino
- ESP32 (Dev Module)
You can use another module also if it meets Flash chip size of the sketch
1-Before you begin, please make sure that you have the following software installed:
- Arduino IDE
- Host software depending on O/S you use:
- Avahi http://avahi.org/ for Linux
- Bonjour http://www.apple.com/support/bonjour/ for Windows
- Mac OSX and iOS - support is already built in / no any extra s/w is required
Prepare the sketch and configuration for initial upload with a serial port
- Start Arduino IDE and load sketch OTAWebUpdater.ino available under File > Examples > OTAWebUpdater.ino
- Update ssid and pass in the sketch so the module can join your Wi-Fi network
- Open File > Preferences, look for “Show verbose output during:” and check out “compilation” option
![verbrose](esp32verbose.PNG)
- Upload sketch (Ctrl+U)
- Now open web browser and enter the url, i.e. http://esp32.local. Once entered, browser should display a form
![login](esp32login.PNG)
> username= admin
> password= admin
**Note**-*If entering “http://ESP32.local” does not work, try replacing “ESP32” with modules IP address.This workaround is useful in case the host software installed does not work*.
Now click on Login button and browser will display a upload form
![upload](esp32upload.PNG)
For Uploading the New Firmware you need to provide the Binary File of your Code.
Exporting Binary file of the Firmware (Code)
- Open up the Arduino IDE
- Open up the Code, for Exporting up Binary file
- Now go to Sketch > export compiled Binary
![export](exportTobinary.PNG)
- Binary file is exported to the same Directory where your code is present
Once you are comfortable with this procedure go ahead and modify OTAWebUpdater.ino sketch to print some additional messages, compile it, Export new binary file and upload it using web browser to see entered changes on a Serial Monitor

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

View File

@ -0,0 +1,13 @@
Installation instructions using Arduino IDE Boards Manager
==========================================================
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 and 64 bit).
- 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.
- Enter ```https://dl.espressif.com/dl/package_esp32_index.json``` into *Additional Board Manager URLs* field. You can add multiple URLs, separating them with commas.
- Open Boards Manager from Tools > Board menu and install *esp32* platform (and don't forget to select your ESP32 board from Tools > Board menu after installation).
Stable release link: `https://dl.espressif.com/dl/package_esp32_index.json`
Development release link: `https://dl.espressif.com/dl/package_esp32_dev_index.json`

View File

@ -22,14 +22,15 @@ Installation instructions for Debian / Ubuntu OS
- If you have Arduino.app installed to /Applications/, modify the installation as follows, beginning at `mkdir -p ~/Arduino...`:
- If you have Arduino installed to ~/, modify the installation as follows, beginning at `mkdir -p ~/Arduino/hardware`:
```bash
cd /Applications/Arduino_*/Contents/java/hardware/
```bash
cd ~/Arduino/hardware
mkdir -p espressif && \
cd espressif && \
git clone https://github.com/espressif/arduino-esp32.git esp32 && \
cd esp32 && \
git submodule update --init --recursive && \
cd tools && \
python2 get.py```
python2 get.py
```

View File

@ -11,7 +11,7 @@ Installation instructions for Mac OS
cd esp32 && \
git submodule update --init --recursive && \
cd tools && \
python get.py
python get.py
```
Where `~/Documents/Arduino` represents your sketch book location as per "Arduino" > "Preferences" > "Sketchbook location" (in the IDE once started). Adjust the command above accordingly if necessary!
 
@ -20,6 +20,8 @@ Installation instructions for Mac OS
```xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun```
```xcode-select --install```
- Try `python3` instead of `python` if you get the error: `IOError: [Errno socket error] [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590)` when running `python get.py`
- Restart Arduino IDE

View File

@ -4,93 +4,101 @@
#include <ESPmDNS.h>
#include <Update.h>
const char* host = "ESP32";
const char* host = "esp32";
const char* ssid = "xxx";
const char* password = "xxxx";
WebServer server(80);
const char* loginIndex = "<form name='loginForm'>"
"<table width='20%' bgcolor='A09F9F' align='center'>"
"<tr>"
"<td colspan=2><center><font size=4><b>ESP32 Login Page</b></font></center>"
"<br>"
/*
* Login page
*/
"</td>"
"<br>"
"<br>"
"</tr>"
"<td>Username:</td>"
"<td><input type='text' size=25 name='userid'><br></td>"
"</tr>"
"<br>"
"<br>"
"<tr>"
"<td>Password:</td>"
"<td><input type='Password' size=25 name='pwd'><br></td>"
"<br>"
"<br>"
"</tr>"
const char* loginIndex =
"<form name='loginForm'>"
"<table width='20%' bgcolor='A09F9F' align='center'>"
"<tr>"
"<td colspan=2>"
"<center><font size=4><b>ESP32 Login Page</b></font></center>"
"<br>"
"</td>"
"<br>"
"<br>"
"</tr>"
"<td>Username:</td>"
"<td><input type='text' size=25 name='userid'><br></td>"
"</tr>"
"<br>"
"<br>"
"<tr>"
"<td>Password:</td>"
"<td><input type='Password' size=25 name='pwd'><br></td>"
"<br>"
"<br>"
"</tr>"
"<tr>"
"<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
"</tr>"
"</table>"
"</form>"
"<script>"
"function check(form)"
"{"
"if(form.userid.value=='admin' && form.pwd.value=='admin')"
"{"
"window.open('/serverIndex')"
"}"
"else"
"{"
" alert('Error Password or Username')/*displays error message*/"
"}"
"}"
"</script>";
/*
* Server Index Page
*/
const char* serverIndex =
"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
"<input type='file' name='update'>"
"<input type='submit' value='Update'>"
"</form>"
"<div id='prg'>progress: 0%</div>"
"<script>"
"$('form').submit(function(e){"
"e.preventDefault();"
"var form = $('#upload_form')[0];"
"var data = new FormData(form);"
" $.ajax({"
"url: '/update',"
"type: 'POST',"
"data: data,"
"contentType: false,"
"processData:false,"
"xhr: function() {"
"var xhr = new window.XMLHttpRequest();"
"xhr.upload.addEventListener('progress', function(evt) {"
"if (evt.lengthComputable) {"
"var per = evt.loaded / evt.total;"
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
"}"
"}, false);"
"return xhr;"
"},"
"success:function(d, s) {"
"console.log('success!')"
"},"
"error: function (a, b, c) {"
"}"
"});"
"});"
"</script>";
"<tr>"
"<td><input type='submit' onclick='check(this.form)' value='Login'></td>"
"</tr>"
"</table>"
"</form>"
"<script>"
"function check(form)"
"{"
"if(form.userid.value=='admin' && form.pwd.value=='admin')"
"{"
"window.open('/serverIndex')"
"}"
"else"
"{"
" alert('Error Password or Username')/*displays error message*/"
"}"
"}"
"</script>";
const char* serverIndex = "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
"<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
"<input type='file' name='update'>"
"<input type='submit' value='Update'>"
"</form>"
"<div id='prg'>progress: 0%</div>"
"<script>"
"$('form').submit(function(e){"
"e.preventDefault();"
"var form = $('#upload_form')[0];"
"var data = new FormData(form);"
" $.ajax({"
"url: '/update',"
"type: 'POST',"
"data: data,"
"contentType: false,"
"processData:false,"
"xhr: function() {"
"var xhr = new window.XMLHttpRequest();"
"xhr.upload.addEventListener('progress', function(evt) {"
"if (evt.lengthComputable) {"
"var per = evt.loaded / evt.total;"
"$('#prg').html('progress: ' + Math.round(per*100) + '%');"
"}"
"}, false);"
"return xhr;"
"},"
"success:function(d, s) {"
"console.log('success!')"
"},"
"error: function (a, b, c) {"
"}"
"});"
"});"
"</script>";
/*
* setup function
*/
void setup(void) {
Serial.begin(115200);
@ -110,7 +118,7 @@ void setup(void) {
Serial.println(WiFi.localIP());
/*use mdns for host name resolution*/
if (!MDNS.begin(host)) { //http://esp32.local
if (!MDNS.begin(host)) { //http://esp32.local
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);

View File

@ -9,7 +9,7 @@
#include "Update.h"
//#define OTA_DEBUG Serial
// #define OTA_DEBUG Serial
ArduinoOTAClass::ArduinoOTAClass()
: _port(0)
@ -20,6 +20,7 @@ ArduinoOTAClass::ArduinoOTAClass()
, _size(0)
, _cmd(0)
, _ota_port(0)
, _ota_timeout(1000)
, _start_callback(NULL)
, _end_callback(NULL)
, _error_callback(NULL)
@ -260,8 +261,9 @@ void ArduinoOTAClass::_runUpdate() {
}
uint32_t written = 0, total = 0, tried = 0;
while (!Update.isFinished() && client.connected()) {
size_t waited = 1000;
size_t waited = _ota_timeout;
size_t available = client.available();
while (!available && waited){
delay(1);
@ -387,6 +389,10 @@ int ArduinoOTAClass::getCommand() {
return _cmd;
}
void ArduinoOTAClass::setTimeout(int timeoutInMillis) {
_ota_timeout = timeoutInMillis;
}
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_ARDUINOOTA)
ArduinoOTAClass ArduinoOTA;
#endif
#endif

View File

@ -7,7 +7,6 @@
#define INT_BUFFER_SIZE 16
typedef enum {
OTA_IDLE,
OTA_WAITAUTH,
@ -25,9 +24,9 @@ typedef enum {
class ArduinoOTAClass
{
public:
typedef std::function<void(void)> THandlerFunction;
typedef std::function<void(ota_error_t)> THandlerFunction_Error;
typedef std::function<void(unsigned int, unsigned int)> THandlerFunction_Progress;
typedef std::function<void(void)> THandlerFunction;
typedef std::function<void(ota_error_t)> THandlerFunction_Error;
typedef std::function<void(unsigned int, unsigned int)> THandlerFunction_Progress;
ArduinoOTAClass();
~ArduinoOTAClass();
@ -75,6 +74,8 @@ class ArduinoOTAClass
//Gets update command type after OTA has started. Either U_FLASH or U_SPIFFS
int getCommand();
void setTimeout(int timeoutInMillis);
private:
int _port;
String _password;
@ -88,6 +89,7 @@ class ArduinoOTAClass
int _size;
int _cmd;
int _ota_port;
int _ota_timeout;
IPAddress _ota_ip;
String _md5;
@ -106,4 +108,4 @@ class ArduinoOTAClass
extern ArduinoOTAClass ArduinoOTA;
#endif
#endif /* __ARDUINO_OTA_H */
#endif /* __ARDUINO_OTA_H */

View File

@ -8,6 +8,7 @@ extern "C" {
#include "lwip/igmp.h"
#include "lwip/ip_addr.h"
#include "lwip/mld6.h"
#include "lwip/prot/ethernet.h"
#include <esp_err.h>
#include <esp_wifi.h>
}
@ -15,7 +16,7 @@ extern "C" {
#include "lwip/priv/tcpip_priv.h"
typedef struct {
struct tcpip_api_call call;
struct tcpip_api_call_data call;
udp_pcb * pcb;
const ip_addr_t *addr;
uint16_t port;
@ -24,7 +25,7 @@ typedef struct {
err_t err;
} udp_api_call_t;
static err_t _udp_connect_api(struct tcpip_api_call *api_call_msg){
static err_t _udp_connect_api(struct tcpip_api_call_data *api_call_msg){
udp_api_call_t * msg = (udp_api_call_t *)api_call_msg;
msg->err = udp_connect(msg->pcb, msg->addr, msg->port);
return msg->err;
@ -35,11 +36,11 @@ static err_t _udp_connect(struct udp_pcb *pcb, const ip_addr_t *addr, u16_t port
msg.pcb = pcb;
msg.addr = addr;
msg.port = port;
tcpip_api_call(_udp_connect_api, (struct tcpip_api_call*)&msg);
tcpip_api_call(_udp_connect_api, (struct tcpip_api_call_data*)&msg);
return msg.err;
}
static err_t _udp_disconnect_api(struct tcpip_api_call *api_call_msg){
static err_t _udp_disconnect_api(struct tcpip_api_call_data *api_call_msg){
udp_api_call_t * msg = (udp_api_call_t *)api_call_msg;
msg->err = 0;
udp_disconnect(msg->pcb);
@ -49,10 +50,10 @@ static err_t _udp_disconnect_api(struct tcpip_api_call *api_call_msg){
static void _udp_disconnect(struct udp_pcb *pcb){
udp_api_call_t msg;
msg.pcb = pcb;
tcpip_api_call(_udp_disconnect_api, (struct tcpip_api_call*)&msg);
tcpip_api_call(_udp_disconnect_api, (struct tcpip_api_call_data*)&msg);
}
static err_t _udp_remove_api(struct tcpip_api_call *api_call_msg){
static err_t _udp_remove_api(struct tcpip_api_call_data *api_call_msg){
udp_api_call_t * msg = (udp_api_call_t *)api_call_msg;
msg->err = 0;
udp_remove(msg->pcb);
@ -62,10 +63,10 @@ static err_t _udp_remove_api(struct tcpip_api_call *api_call_msg){
static void _udp_remove(struct udp_pcb *pcb){
udp_api_call_t msg;
msg.pcb = pcb;
tcpip_api_call(_udp_remove_api, (struct tcpip_api_call*)&msg);
tcpip_api_call(_udp_remove_api, (struct tcpip_api_call_data*)&msg);
}
static err_t _udp_bind_api(struct tcpip_api_call *api_call_msg){
static err_t _udp_bind_api(struct tcpip_api_call_data *api_call_msg){
udp_api_call_t * msg = (udp_api_call_t *)api_call_msg;
msg->err = udp_bind(msg->pcb, msg->addr, msg->port);
return msg->err;
@ -76,11 +77,11 @@ static err_t _udp_bind(struct udp_pcb *pcb, const ip_addr_t *addr, u16_t port){
msg.pcb = pcb;
msg.addr = addr;
msg.port = port;
tcpip_api_call(_udp_bind_api, (struct tcpip_api_call*)&msg);
tcpip_api_call(_udp_bind_api, (struct tcpip_api_call_data*)&msg);
return msg.err;
}
static err_t _udp_sendto_api(struct tcpip_api_call *api_call_msg){
static err_t _udp_sendto_api(struct tcpip_api_call_data *api_call_msg){
udp_api_call_t * msg = (udp_api_call_t *)api_call_msg;
msg->err = udp_sendto(msg->pcb, msg->pb, msg->addr, msg->port);
return msg->err;
@ -92,11 +93,11 @@ static err_t _udp_sendto(struct udp_pcb *pcb, struct pbuf *pb, const ip_addr_t *
msg.addr = addr;
msg.port = port;
msg.pb = pb;
tcpip_api_call(_udp_sendto_api, (struct tcpip_api_call*)&msg);
tcpip_api_call(_udp_sendto_api, (struct tcpip_api_call_data*)&msg);
return msg.err;
}
static err_t _udp_sendto_if_api(struct tcpip_api_call *api_call_msg){
static err_t _udp_sendto_if_api(struct tcpip_api_call_data *api_call_msg){
udp_api_call_t * msg = (udp_api_call_t *)api_call_msg;
msg->err = udp_sendto_if(msg->pcb, msg->pb, msg->addr, msg->port, msg->netif);
return msg->err;
@ -109,7 +110,7 @@ static err_t _udp_sendto_if(struct udp_pcb *pcb, struct pbuf *pb, const ip_addr_
msg.port = port;
msg.pb = pb;
msg.netif = netif;
tcpip_api_call(_udp_sendto_if_api, (struct tcpip_api_call*)&msg);
tcpip_api_call(_udp_sendto_if_api, (struct tcpip_api_call_data*)&msg);
return msg.err;
}
@ -131,7 +132,7 @@ static void _udp_task(void *pvParameters){
if(xQueueReceive(_udp_queue, &e, portMAX_DELAY) == pdTRUE){
if(!e->pb){
free((void*)(e));
break;
continue;
}
AsyncUDP::_s_recv(e->arg, e->pcb, e->pb, e->addr, e->port, e->netif);
free((void*)(e));
@ -286,23 +287,29 @@ AsyncUDPPacket::AsyncUDPPacket(AsyncUDP *udp, pbuf *pb, const ip_addr_t *raddr,
_len = pb->len;
_index = 0;
pbuf_ref(_pb);
//memcpy(&_remoteIp, raddr, sizeof(ip_addr_t));
_remoteIp.type = raddr->type;
_localIp.type = _remoteIp.type;
eth_hdr* eth = NULL;
udp_hdr* udphdr = reinterpret_cast<udp_hdr*>(_data - UDP_HLEN);
_localPort = ntohs(udphdr->dest);
_remotePort = ntohs(udphdr->src);
if (_remoteIp.type == IPADDR_TYPE_V4) {
eth = (eth_hdr *)(((uint8_t *)(pb->payload)) - UDP_HLEN - IP_HLEN - SIZEOF_ETH_HDR);
struct ip_hdr * iphdr = (struct ip_hdr *)(((uint8_t *)(pb->payload)) - UDP_HLEN - IP_HLEN);
_localIp.u_addr.ip4.addr = iphdr->dest.addr;
_remoteIp.u_addr.ip4.addr = iphdr->src.addr;
} else {
eth = (eth_hdr *)(((uint8_t *)(pb->payload)) - UDP_HLEN - IP6_HLEN - SIZEOF_ETH_HDR);
struct ip6_hdr * ip6hdr = (struct ip6_hdr *)(((uint8_t *)(pb->payload)) - UDP_HLEN - IP6_HLEN);
memcpy(&_localIp.u_addr.ip6.addr, (uint8_t *)ip6hdr->dest.addr, 16);
memcpy(&_remoteIp.u_addr.ip6.addr, (uint8_t *)ip6hdr->src.addr, 16);
}
memcpy(_remoteMac, eth->src.addr, 6);
struct netif * netif = NULL;
void * nif = NULL;
@ -413,6 +420,11 @@ uint16_t AsyncUDPPacket::remotePort()
return _remotePort;
}
void AsyncUDPPacket::remoteMac(uint8_t * mac)
{
memcpy(mac, _remoteMac, 6);
}
bool AsyncUDPPacket::isIPv6()
{
return _localIp.type == IPADDR_TYPE_V6;
@ -450,16 +462,24 @@ size_t AsyncUDPPacket::send(AsyncUDPMessage &message)
return write(message.data(), message.length());
}
AsyncUDP::AsyncUDP()
{
bool AsyncUDP::_init(){
if(_pcb){
return true;
}
_pcb = udp_new();
_connected = false;
_handler = NULL;
if(!_pcb){
return;
return false;
}
//_lock = xSemaphoreCreateMutex();
udp_recv(_pcb, &_udp_recv, (void *) this);
return true;
}
AsyncUDP::AsyncUDP()
{
_pcb = NULL;
_connected = false;
_handler = NULL;
}
AsyncUDP::~AsyncUDP()
@ -481,8 +501,7 @@ void AsyncUDP::close()
_udp_disconnect(_pcb);
}
_connected = false;
_pcb->multicast_ip.type = IPADDR_TYPE_V4;
_pcb->multicast_ip.u_addr.ip4.addr = 0;
//todo: unjoin multicast group
}
UDP_MUTEX_UNLOCK();
}
@ -493,7 +512,7 @@ bool AsyncUDP::connect(const ip_addr_t *addr, uint16_t port)
log_e("failed to start task");
return false;
}
if(_pcb == NULL) {
if(!_init()) {
return false;
}
close();
@ -514,7 +533,7 @@ bool AsyncUDP::listen(const ip_addr_t *addr, uint16_t port)
log_e("failed to start task");
return false;
}
if(_pcb == NULL) {
if(!_init()) {
return false;
}
close();
@ -532,57 +551,53 @@ bool AsyncUDP::listen(const ip_addr_t *addr, uint16_t port)
return true;
}
static esp_err_t joinMulticastGroup(const ip_addr_t *addr, bool join, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX)
{
struct netif * netif = NULL;
if(tcpip_if < TCPIP_ADAPTER_IF_MAX){
void * nif = NULL;
esp_err_t err = tcpip_adapter_get_netif(tcpip_if, &nif);
if (err) {
return ESP_ERR_INVALID_ARG;
}
netif = (struct netif *)nif;
}
if (addr->type == IPADDR_TYPE_V4) {
if(join){
if (igmp_joingroup_netif(netif, (const ip4_addr *)&(addr->u_addr.ip4))) {
return ESP_ERR_INVALID_STATE;
}
} else {
if (igmp_leavegroup_netif(netif, (const ip4_addr *)&(addr->u_addr.ip4))) {
return ESP_ERR_INVALID_STATE;
}
}
} else {
if(join){
if (mld6_joingroup_netif(netif, &(addr->u_addr.ip6))) {
return ESP_ERR_INVALID_STATE;
}
} else {
if (mld6_leavegroup_netif(netif, &(addr->u_addr.ip6))) {
return ESP_ERR_INVALID_STATE;
}
}
}
return ESP_OK;
}
bool AsyncUDP::listenMulticast(const ip_addr_t *addr, uint16_t port, uint8_t ttl, tcpip_adapter_if_t tcpip_if)
{
if(!ip_addr_ismulticast(addr)) {
return false;
}
ip_addr_t multicast_if_addr;
uint8_t mode;
if(esp_wifi_get_mode((wifi_mode_t*)&mode)){
mode = WIFI_MODE_NULL;
}
if(addr->type == IPADDR_TYPE_V6){
multicast_if_addr.type = IPADDR_TYPE_V6;
if((tcpip_if == TCPIP_ADAPTER_IF_STA && (mode & WIFI_MODE_STA))
|| (tcpip_if == TCPIP_ADAPTER_IF_AP && (mode & WIFI_MODE_AP))
|| (tcpip_if == TCPIP_ADAPTER_IF_ETH)) {
if(tcpip_adapter_get_ip6_linklocal(tcpip_if, &multicast_if_addr.u_addr.ip6)){
return false;
}
} else {
return false;
}
if (mld6_joingroup(&(multicast_if_addr.u_addr.ip6), &(addr->u_addr.ip6))) {
return false;
}
} else if(addr->type == IPADDR_TYPE_V4){
tcpip_adapter_ip_info_t ifIpInfo;
if((tcpip_if == TCPIP_ADAPTER_IF_STA && (mode & WIFI_MODE_STA))
|| (tcpip_if == TCPIP_ADAPTER_IF_AP && (mode & WIFI_MODE_AP))
|| (tcpip_if == TCPIP_ADAPTER_IF_ETH)) {
if(tcpip_adapter_get_ip_info(tcpip_if, &ifIpInfo)){
return false;
}
} else {
return false;
}
multicast_if_addr.type = IPADDR_TYPE_V4;
multicast_if_addr.u_addr.ip4.addr = ifIpInfo.ip.addr;
if (igmp_joingroup((const ip4_addr *)&multicast_if_addr.u_addr.ip4, (const ip4_addr *)&addr->u_addr.ip4)!= ERR_OK) {
return false;
}
} else {
if (joinMulticastGroup(addr, true, tcpip_if)!= ERR_OK) {
return false;
}
if(!listen(&multicast_if_addr, port)) {
if(!listen(NULL, port)) {
return false;
}
@ -590,9 +605,7 @@ bool AsyncUDP::listenMulticast(const ip_addr_t *addr, uint16_t port, uint8_t ttl
_pcb->mcast_ttl = ttl;
_pcb->remote_port = port;
ip_addr_copy(_pcb->remote_ip, *addr);
if(addr->type == IPADDR_TYPE_V4){
ip_addr_copy(_pcb->multicast_ip, multicast_if_addr);
}
//ip_addr_copy(_pcb->remote_ip, ip_addr_any_type);
UDP_MUTEX_UNLOCK();
return true;
@ -602,7 +615,7 @@ size_t AsyncUDP::writeTo(const uint8_t * data, size_t len, const ip_addr_t * add
{
if(!_pcb) {
UDP_MUTEX_LOCK();
_pcb = udp_new_ip_type(addr->type);
_pcb = udp_new();
UDP_MUTEX_UNLOCK();
if(_pcb == NULL) {
return 0;
@ -617,7 +630,7 @@ size_t AsyncUDP::writeTo(const uint8_t * data, size_t len, const ip_addr_t * add
uint8_t* dst = reinterpret_cast<uint8_t*>(pbt->payload);
memcpy(dst, data, len);
UDP_MUTEX_LOCK();
if(tcpip_if != TCPIP_ADAPTER_IF_MAX){
if(tcpip_if < TCPIP_ADAPTER_IF_MAX){
void * nif = NULL;
tcpip_adapter_get_netif((tcpip_adapter_if_t)tcpip_if, &nif);
if(!nif){

View File

@ -53,6 +53,7 @@ protected:
uint16_t _localPort;
ip_addr_t _remoteIp;
uint16_t _remotePort;
uint8_t _remoteMac[6];
uint8_t *_data;
size_t _len;
size_t _index;
@ -74,6 +75,7 @@ public:
IPAddress remoteIP();
IPv6Address remoteIPv6();
uint16_t remotePort();
void remoteMac(uint8_t * mac);
size_t send(AsyncUDPMessage &message);
@ -95,6 +97,7 @@ protected:
bool _connected;
AuPacketHandlerFunction _handler;
bool _init();
void _recv(udp_pcb *upcb, pbuf *pb, const ip_addr_t *addr, uint16_t port, struct netif * netif);
public:
@ -109,9 +112,9 @@ public:
bool listen(const IPv6Address addr, uint16_t port);
bool listen(uint16_t port);
bool listenMulticast(const ip_addr_t *addr, uint16_t port, uint8_t ttl=1, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_STA);
bool listenMulticast(const IPAddress addr, uint16_t port, uint8_t ttl=1, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_STA);
bool listenMulticast(const IPv6Address addr, uint16_t port, uint8_t ttl=1, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_STA);
bool listenMulticast(const ip_addr_t *addr, uint16_t port, uint8_t ttl=1, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX);
bool listenMulticast(const IPAddress addr, uint16_t port, uint8_t ttl=1, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX);
bool listenMulticast(const IPv6Address addr, uint16_t port, uint8_t ttl=1, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_MAX);
bool connect(const ip_addr_t *addr, uint16_t port);
bool connect(const IPAddress addr, uint16_t port);

View File

@ -40,11 +40,117 @@
#include "esp32-hal-log.h"
#endif
const char * _spp_server_name = "ESP32_SPP_SERVER";
const char * _spp_server_name = "ESP32SPP";
#define QUEUE_SIZE 256
#define RX_QUEUE_SIZE 512
#define TX_QUEUE_SIZE 32
static uint32_t _spp_client = 0;
static xQueueHandle _spp_queue = NULL;
static xQueueHandle _spp_rx_queue = NULL;
static xQueueHandle _spp_tx_queue = NULL;
static SemaphoreHandle_t _spp_tx_done = NULL;
static TaskHandle_t _spp_task_handle = NULL;
static EventGroupHandle_t _spp_event_group = NULL;
static boolean secondConnectionAttempt;
static esp_spp_cb_t * custom_spp_callback = NULL;
#define SPP_RUNNING 0x01
#define SPP_CONNECTED 0x02
#define SPP_CONGESTED 0x04
typedef struct {
size_t len;
uint8_t data[];
} spp_packet_t;
static esp_err_t _spp_queue_packet(uint8_t *data, size_t len){
if(!data || !len){
log_w("No data provided");
return ESP_OK;
}
spp_packet_t * packet = (spp_packet_t*)malloc(sizeof(spp_packet_t) + len);
if(!packet){
log_e("SPP TX Packet Malloc Failed!");
return ESP_FAIL;
}
packet->len = len;
memcpy(packet->data, data, len);
if (xQueueSend(_spp_tx_queue, &packet, portMAX_DELAY) != pdPASS) {
log_e("SPP TX Queue Send Failed!");
free(packet);
return ESP_FAIL;
}
return ESP_OK;
}
const uint16_t SPP_TX_MAX = 330;
static uint8_t _spp_tx_buffer[SPP_TX_MAX];
static uint16_t _spp_tx_buffer_len = 0;
static bool _spp_send_buffer(){
if((xEventGroupWaitBits(_spp_event_group, SPP_CONGESTED, pdFALSE, pdTRUE, portMAX_DELAY) & SPP_CONGESTED)){
esp_err_t err = esp_spp_write(_spp_client, _spp_tx_buffer_len, _spp_tx_buffer);
if(err != ESP_OK){
log_e("SPP Write Failed! [0x%X]", err);
return false;
}
_spp_tx_buffer_len = 0;
if(xSemaphoreTake(_spp_tx_done, portMAX_DELAY) != pdTRUE){
log_e("SPP Ack Failed!");
return false;
}
return true;
}
return false;
}
static void _spp_tx_task(void * arg){
spp_packet_t *packet = NULL;
size_t len = 0, to_send = 0;
uint8_t * data = NULL;
for (;;) {
if(_spp_tx_queue && xQueueReceive(_spp_tx_queue, &packet, portMAX_DELAY) == pdTRUE && packet){
if(packet->len <= (SPP_TX_MAX - _spp_tx_buffer_len)){
memcpy(_spp_tx_buffer+_spp_tx_buffer_len, packet->data, packet->len);
_spp_tx_buffer_len+=packet->len;
free(packet);
packet = NULL;
if(SPP_TX_MAX == _spp_tx_buffer_len || uxQueueMessagesWaiting(_spp_tx_queue) == 0){
_spp_send_buffer();
}
} else {
len = packet->len;
data = packet->data;
to_send = SPP_TX_MAX - _spp_tx_buffer_len;
memcpy(_spp_tx_buffer+_spp_tx_buffer_len, data, to_send);
_spp_tx_buffer_len = SPP_TX_MAX;
data += to_send;
len -= to_send;
_spp_send_buffer();
while(len >= SPP_TX_MAX){
memcpy(_spp_tx_buffer, data, SPP_TX_MAX);
_spp_tx_buffer_len = SPP_TX_MAX;
data += SPP_TX_MAX;
len -= SPP_TX_MAX;
_spp_send_buffer();
}
if(len){
memcpy(_spp_tx_buffer, data, len);
_spp_tx_buffer_len += len;
free(packet);
packet = NULL;
if(uxQueueMessagesWaiting(_spp_tx_queue) == 0){
_spp_send_buffer();
}
}
}
} else {
log_e("Something went horribly wrong");
}
}
vTaskDelete(NULL);
_spp_task_handle = NULL;
}
static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{
@ -54,87 +160,164 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
log_i("ESP_SPP_INIT_EVT");
esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
esp_spp_start_srv(ESP_SPP_SEC_NONE, ESP_SPP_ROLE_SLAVE, 0, _spp_server_name);
xEventGroupSetBits(_spp_event_group, SPP_RUNNING);
break;
case ESP_SPP_SRV_OPEN_EVT://Server connection open
if (!_spp_client){
_spp_client = param->open.handle;
} else {
secondConnectionAttempt = true;
esp_spp_disconnect(param->open.handle);
}
xEventGroupSetBits(_spp_event_group, SPP_CONNECTED);
log_i("ESP_SPP_SRV_OPEN_EVT");
break;
case ESP_SPP_CLOSE_EVT://Client connection closed
if(secondConnectionAttempt) {
secondConnectionAttempt = false;
} else {
_spp_client = 0;
}
xEventGroupClearBits(_spp_event_group, SPP_CONNECTED);
log_i("ESP_SPP_CLOSE_EVT");
break;
case ESP_SPP_CONG_EVT://connection congestion status changed
if(param->cong.cong){
xEventGroupClearBits(_spp_event_group, SPP_CONGESTED);
} else {
xEventGroupSetBits(_spp_event_group, SPP_CONGESTED);
}
log_v("ESP_SPP_CONG_EVT: %s", param->cong.cong?"CONGESTED":"FREE");
break;
case ESP_SPP_WRITE_EVT://write operation completed
if(param->write.cong){
xEventGroupClearBits(_spp_event_group, SPP_CONGESTED);
}
xSemaphoreGive(_spp_tx_done);//we can try to send another packet
log_v("ESP_SPP_WRITE_EVT: %u %s", param->write.len, param->write.cong?"CONGESTED":"FREE");
break;
case ESP_SPP_DATA_IND_EVT://connection received data
log_v("ESP_SPP_DATA_IND_EVT len=%d handle=%d", param->data_ind.len, param->data_ind.handle);
//esp_log_buffer_hex("",param->data_ind.data,param->data_ind.len); //for low level debug
//ets_printf("r:%u\n", param->data_ind.len);
if (_spp_rx_queue != NULL){
for (int i = 0; i < param->data_ind.len; i++){
if(xQueueSend(_spp_rx_queue, param->data_ind.data + i, (TickType_t)0) != pdTRUE){
log_e("RX Full! Discarding %u bytes", param->data_ind.len - i);
break;
}
}
}
break;
//should maybe delete those.
case ESP_SPP_DISCOVERY_COMP_EVT://discovery complete
log_i("ESP_SPP_DISCOVERY_COMP_EVT");
break;
case ESP_SPP_OPEN_EVT://Client connection open
log_i("ESP_SPP_OPEN_EVT");
break;
case ESP_SPP_CLOSE_EVT://Client connection closed
_spp_client = 0;
log_i("ESP_SPP_CLOSE_EVT");
break;
case ESP_SPP_START_EVT://server started
log_i("ESP_SPP_START_EVT");
break;
case ESP_SPP_CL_INIT_EVT://client initiated a connection
log_i("ESP_SPP_CL_INIT_EVT");
break;
case ESP_SPP_DATA_IND_EVT://connection received data
log_v("ESP_SPP_DATA_IND_EVT len=%d handle=%d", param->data_ind.len, param->data_ind.handle);
//esp_log_buffer_hex("",param->data_ind.data,param->data_ind.len); //for low level debug
if (_spp_queue != NULL){
for (int i = 0; i < param->data_ind.len; i++)
xQueueSend(_spp_queue, param->data_ind.data + i, (TickType_t)0);
} else {
log_e("SerialQueueBT ERROR");
}
break;
case ESP_SPP_CONG_EVT://connection congestion status changed
log_i("ESP_SPP_CONG_EVT");
break;
case ESP_SPP_WRITE_EVT://write operation completed
log_v("ESP_SPP_WRITE_EVT");
break;
case ESP_SPP_SRV_OPEN_EVT://Server connection open
_spp_client = param->open.handle;
log_i("ESP_SPP_SRV_OPEN_EVT");
break;
default:
break;
}
if(custom_spp_callback)(*custom_spp_callback)(event, param);
}
static bool _init_bt(const char *deviceName)
{
if(!_spp_event_group){
_spp_event_group = xEventGroupCreate();
if(!_spp_event_group){
log_e("SPP Event Group Create Failed!");
return false;
}
xEventGroupClearBits(_spp_event_group, 0xFFFFFF);
xEventGroupSetBits(_spp_event_group, SPP_CONGESTED);
}
if (_spp_rx_queue == NULL){
_spp_rx_queue = xQueueCreate(RX_QUEUE_SIZE, sizeof(uint8_t)); //initialize the queue
if (_spp_rx_queue == NULL){
log_e("RX Queue Create Failed");
return false;
}
}
if (_spp_tx_queue == NULL){
_spp_tx_queue = xQueueCreate(TX_QUEUE_SIZE, sizeof(spp_packet_t*)); //initialize the queue
if (_spp_tx_queue == NULL){
log_e("TX Queue Create Failed");
return false;
}
}
if(_spp_tx_done == NULL){
_spp_tx_done = xSemaphoreCreateBinary();
if (_spp_tx_done == NULL){
log_e("TX Semaphore Create Failed");
return false;
}
xSemaphoreTake(_spp_tx_done, 0);
}
if(!_spp_task_handle){
xTaskCreate(_spp_tx_task, "spp_tx", 4096, NULL, 2, &_spp_task_handle);
if(!_spp_task_handle){
log_e("Network Event Task Start Failed!");
return false;
}
}
if (!btStarted() && !btStart()){
log_e("%s initialize controller failed\n", __func__);
log_e("initialize controller failed");
return false;
}
esp_bluedroid_status_t bt_state = esp_bluedroid_get_status();
if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED){
if (esp_bluedroid_init()) {
log_e("%s initialize bluedroid failed\n", __func__);
log_e("initialize bluedroid failed");
return false;
}
}
if (bt_state != ESP_BLUEDROID_STATUS_ENABLED){
if (esp_bluedroid_enable()) {
log_e("%s enable bluedroid failed\n", __func__);
log_e("enable bluedroid failed");
return false;
}
}
if (esp_spp_register_callback(esp_spp_cb) != ESP_OK){
log_e("%s spp register failed\n", __func__);
log_e("spp register failed");
return false;
}
if (esp_spp_init(ESP_SPP_MODE_CB) != ESP_OK){
log_e("%s spp init failed\n", __func__);
log_e("spp init failed");
return false;
}
_spp_queue = xQueueCreate(QUEUE_SIZE, sizeof(uint8_t)); //initialize the queue
if (_spp_queue == NULL){
log_e("%s Queue creation error\n", __func__);
esp_bt_dev_set_device_name(deviceName);
// the default BTA_DM_COD_LOUDSPEAKER does not work with the macOS BT stack
esp_bt_cod_t cod;
cod.major = 0b00001;
cod.minor = 0b000100;
cod.service = 0b00000010110;
if (esp_bt_gap_set_cod(cod, ESP_BT_INIT_COD) != ESP_OK) {
log_e("set cod failed");
return false;
}
esp_bt_dev_set_device_name(deviceName);
return true;
}
@ -142,10 +325,39 @@ static bool _init_bt(const char *deviceName)
static bool _stop_bt()
{
if (btStarted()){
if(_spp_client)
esp_spp_disconnect(_spp_client);
esp_spp_deinit();
esp_bluedroid_disable();
esp_bluedroid_deinit();
btStop();
}
_spp_client = 0;
if(_spp_task_handle){
vTaskDelete(_spp_task_handle);
_spp_task_handle = NULL;
}
if(_spp_event_group){
vEventGroupDelete(_spp_event_group);
_spp_event_group = NULL;
}
if(_spp_rx_queue){
vQueueDelete(_spp_rx_queue);
//ToDo: clear RX queue when in packet mode
_spp_rx_queue = NULL;
}
if(_spp_tx_queue){
spp_packet_t *packet = NULL;
while(xQueueReceive(_spp_tx_queue, &packet, 0) == pdTRUE){
free(packet);
}
vQueueDelete(_spp_tx_queue);
_spp_tx_queue = NULL;
}
if (_spp_tx_done) {
vSemaphoreDelete(_spp_tx_done);
_spp_tx_done = NULL;
}
return true;
}
@ -174,60 +386,39 @@ bool BluetoothSerial::begin(String localName)
int BluetoothSerial::available(void)
{
if (!_spp_client || _spp_queue == NULL){
if (_spp_rx_queue == NULL){
return 0;
}
return uxQueueMessagesWaiting(_spp_queue);
return uxQueueMessagesWaiting(_spp_rx_queue);
}
int BluetoothSerial::peek(void)
{
if (available()){
if (!_spp_client || _spp_queue == NULL){
return 0;
}
uint8_t c;
if (xQueuePeek(_spp_queue, &c, 0)){
return c;
}
uint8_t c;
if (_spp_rx_queue && xQueuePeek(_spp_rx_queue, &c, 0)){
return c;
}
return -1;
}
bool BluetoothSerial::hasClient(void)
{
if (_spp_client)
return true;
return false;
return _spp_client > 0;
}
int BluetoothSerial::read(void)
{
if (available()){
if (!_spp_client || _spp_queue == NULL){
return 0;
}
uint8_t c;
if (xQueueReceive(_spp_queue, &c, 0)){
return c;
}
uint8_t c = 0;
if (_spp_rx_queue && xQueueReceive(_spp_rx_queue, &c, 0)){
return c;
}
return 0;
return -1;
}
size_t BluetoothSerial::write(uint8_t c)
{
if (!_spp_client){
return 0;
}
uint8_t buffer[1];
buffer[0] = c;
esp_err_t err = esp_spp_write(_spp_client, 1, buffer);
return (err == ESP_OK) ? 1 : 0;
return write(&c, 1);
}
size_t BluetoothSerial::write(const uint8_t *buffer, size_t size)
@ -235,18 +426,12 @@ size_t BluetoothSerial::write(const uint8_t *buffer, size_t size)
if (!_spp_client){
return 0;
}
esp_err_t err = esp_spp_write(_spp_client, size, (uint8_t *)buffer);
return (err == ESP_OK) ? size : 0;
return (_spp_queue_packet((uint8_t *)buffer, size) == ESP_OK) ? size : 0;
}
void BluetoothSerial::flush()
{
if (_spp_client){
int qsize = available();
uint8_t buffer[qsize];
esp_spp_write(_spp_client, qsize, buffer);
}
while(read() >= 0){}
}
void BluetoothSerial::end()
@ -254,4 +439,10 @@ void BluetoothSerial::end()
_stop_bt();
}
esp_err_t BluetoothSerial::register_callback(esp_spp_cb_t * callback)
{
custom_spp_callback = callback;
return ESP_OK;
}
#endif

View File

@ -21,6 +21,7 @@
#include "Arduino.h"
#include "Stream.h"
#include <esp_spp_api.h>
class BluetoothSerial: public Stream
{
@ -38,6 +39,7 @@ class BluetoothSerial: public Stream
size_t write(const uint8_t *buffer, size_t size);
void flush();
void end(void);
esp_err_t register_callback(esp_spp_cb_t * callback);
private:
String local_name;

View File

@ -24,7 +24,6 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Arduino.h"
#include "EEPROM.h"
#include <esp_log.h>
@ -296,9 +295,9 @@ String EEPROMClass::readString (int address)
if (address + len > _size)
return String(0);
char value[len + 1];
char value[len];
memcpy((uint8_t*) value, _data + address, len);
value[len + 1] = 0;
value[len] = 0;
return String(value);
}

View File

@ -29,6 +29,7 @@
#ifndef EEPROM_FLASH_PARTITION_NAME
#define EEPROM_FLASH_PARTITION_NAME "eeprom"
#endif
#include <Arduino.h>
extern "C" {
#include <stddef.h>

View File

@ -36,12 +36,12 @@ void print_wakeup_reason(){
switch(wakeup_reason)
{
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case 3 : Serial.println("Wakeup caused by timer"); break;
case 4 : Serial.println("Wakeup caused by touchpad"); break;
case 5 : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.println("Wakeup was not caused by deep sleep"); break;
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}

View File

@ -35,12 +35,12 @@ void print_wakeup_reason(){
switch(wakeup_reason)
{
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case 3 : Serial.println("Wakeup caused by timer"); break;
case 4 : Serial.println("Wakeup caused by touchpad"); break;
case 5 : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.println("Wakeup was not caused by deep sleep"); break;
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
@ -84,6 +84,7 @@ void setup(){
reset occurs.
*/
Serial.println("Going to sleep now");
Serial.flush();
esp_deep_sleep_start();
Serial.println("This will never be printed");
}

View File

@ -26,12 +26,12 @@ void print_wakeup_reason(){
switch(wakeup_reason)
{
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case 3 : Serial.println("Wakeup caused by timer"); break;
case 4 : Serial.println("Wakeup caused by touchpad"); break;
case 5 : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.println("Wakeup was not caused by deep sleep"); break;
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}

View File

@ -0,0 +1,47 @@
#include <Arduino.h>
#include <FunctionalInterrupt.h>
#define BUTTON1 16
#define BUTTON2 17
class Button
{
public:
Button(uint8_t reqPin) : PIN(reqPin){
pinMode(PIN, INPUT_PULLUP);
attachInterrupt(PIN, std::bind(&Button::isr,this), FALLING);
};
~Button() {
detachInterrupt(PIN);
}
void IRAM_ATTR isr() {
numberKeyPresses += 1;
pressed = true;
}
void checkPressed() {
if (pressed) {
Serial.printf("Button on pin %u has been pressed %u times\n", PIN, numberKeyPresses);
pressed = false;
}
}
private:
const uint8_t PIN;
volatile uint32_t numberKeyPresses;
volatile bool pressed;
};
Button button1(BUTTON1);
Button button2(BUTTON2);
void setup() {
Serial.begin(115200);
}
void loop() {
button1.checkPressed();
button2.checkPressed();
}

View File

@ -0,0 +1,45 @@
#include <Arduino.h>
struct Button {
const uint8_t PIN;
uint32_t numberKeyPresses;
bool pressed;
};
Button button1 = {23, 0, false};
Button button2 = {18, 0, false};
void IRAM_ATTR isr(void* arg) {
Button* s = static_cast<Button*>(arg);
s->numberKeyPresses += 1;
s->pressed = true;
}
void IRAM_ATTR isr() {
button2.numberKeyPresses += 1;
button2.pressed = true;
}
void setup() {
Serial.begin(115200);
pinMode(button1.PIN, INPUT_PULLUP);
attachInterruptArg(button1.PIN, isr, &button1, FALLING);
pinMode(button2.PIN, INPUT_PULLUP);
attachInterrupt(button2.PIN, isr, FALLING);
}
void loop() {
if (button1.pressed) {
Serial.printf("Button 1 has been pressed %u times\n", button1.numberKeyPresses);
button1.pressed = false;
}
if (button2.pressed) {
Serial.printf("Button 2 has been pressed %u times\n", button2.numberKeyPresses);
button2.pressed = false;
}
static uint32_t lastMillis = 0;
if (millis() - lastMillis > 10000) {
lastMillis = millis();
detachInterrupt(button1.PIN);
}
}

View File

@ -0,0 +1,61 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "Arduino.h"
#include "esp32-hal.h"
rmt_data_t my_data[256];
rmt_data_t data[256];
rmt_obj_t* rmt_send = NULL;
rmt_obj_t* rmt_recv = NULL;
static EventGroupHandle_t events;
void setup()
{
Serial.begin(115200);
if ((rmt_send = rmtInit(18, true, RMT_MEM_64)) == NULL)
{
Serial.println("init sender failed\n");
}
if ((rmt_recv = rmtInit(21, false, RMT_MEM_192)) == NULL)
{
Serial.println("init receiver failed\n");
}
float realTick = rmtSetTick(rmt_send, 100);
printf("real tick set to: %fns\n", realTick);
}
void loop()
{
// Init data
int i;
for (i=0; i<255; i++) {
data[i].val = 0x80010001 + ((i%13)<<16) + 13-(i%13);
}
data[255].val = 0;
// Start receiving
rmtReadAsync(rmt_recv, my_data, 100, events, false, 0);
// Send in continous mode
rmtWrite(rmt_send, data, 100);
// Wait for data
xEventGroupWaitBits(events, RMT_FLAG_RX_DONE, 1, 1, portMAX_DELAY);
// Printout the received data plus the original values
for (i=0; i<60; i++)
{
Serial.printf("%08x=%08x ", my_data[i], data[i] );
if (!((i+1)%4)) Serial.println("\n");
}
Serial.println("\n");
delay(2000);
}

View File

@ -0,0 +1,204 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "Arduino.h"
#include "esp32-hal.h"
//
// Note: This example uses a FrSKY device communication
// using XJT D12 protocol
//
// ; 0 bit = 6us low/10us high
// ; 1 bit = 14us low/10us high
// ;
// ; --------+ +----------+ +----------+
// ; | | | | |
// ; | 0 | | 1 | |
// ; | | | | |
// ; | | | | |
// ; +-------+ +-----------------+ +---------
// ;
// ; | 6us 10us | 14us 10us |
// ; |-------|----------|-----------------|----------|--------
// ; | 16us | 24us |
// Typedef of received frame
//
// ; 0x00 - Sync, 0x7E (sync header ID)
// ; 0x01 - Rx ID, 0x?? (receiver ID number, 0x00-0x??)
// ; 0x02 - Flags 1, 0x?? (used for failsafe and binding)
// ; 0x03 - Flags 2, 0x00 (reserved)
// ; 0x04-0x06, Channels 1/9 and 2/10
// ; 0x07-0x09, Channels 3/11 and 4/12
// ; 0x0A-0x0C, Channels 5/13 and 6/14
// ; 0x0D-0x0F, Channels 7/15 and 8/16
// ; 0x10 - 0x00, always zero
// ; 0x11 - CRC-16 High
// ; 0x12 - CRC-16 Low
// ; 0x13 - Tail, 0x7E (tail ID)
typedef union {
struct {
uint8_t head;//0x7E
uint8_t rxid;//Receiver Number
uint8_t flags;//Range:0x20, Bind:0x01
uint8_t reserved0;//0x00
union {
struct {
uint8_t ch0_l;
uint8_t ch0_h:4;
uint8_t ch1_l:4;
uint8_t ch1_h;
};
uint8_t bytes[3];
} channels[4];
uint8_t reserved1;//0x00
uint8_t crc_h;
uint8_t crc_l;
uint8_t tail;//0x7E
};
uint8_t buffer[20];
} xjt_packet_t;
#define XJT_VALID(i) (i->level0 && !i->level1 && i->duration0 >= 8 && i->duration0 <= 11)
rmt_obj_t* rmt_recv = NULL;
static uint32_t *s_channels;
static uint32_t channels[16];
static uint8_t xjt_flags = 0x0;
static uint8_t xjt_rxid = 0x0;
static bool xjtReceiveBit(size_t index, bool bit){
static xjt_packet_t xjt;
static uint8_t xjt_bit_index = 8;
static uint8_t xht_byte_index = 0;
static uint8_t xht_ones = 0;
if(!index){
xjt_bit_index = 8;
xht_byte_index = 0;
xht_ones = 0;
}
if(xht_byte_index > 19){
//fail!
return false;
}
if(bit){
xht_ones++;
if(xht_ones > 5 && xht_byte_index && xht_byte_index < 19){
//fail!
return false;
}
//add bit
xjt.buffer[xht_byte_index] |= (1 << --xjt_bit_index);
} else if(xht_ones == 5 && xht_byte_index && xht_byte_index < 19){
xht_ones = 0;
//skip bit
return true;
} else {
xht_ones = 0;
//add bit
xjt.buffer[xht_byte_index] &= ~(1 << --xjt_bit_index);
}
if ((!xjt_bit_index) || (xjt_bit_index==1 && xht_byte_index==19) ) {
xjt_bit_index = 8;
if(!xht_byte_index && xjt.buffer[0] != 0x7E){
//fail!
return false;
}
xht_byte_index++;
if(xht_byte_index == 20){
//done
if(xjt.buffer[19] != 0x7E){
//fail!
return false;
}
//check crc?
xjt_flags = xjt.flags;
xjt_rxid = xjt.rxid;
for(int i=0; i<4; i++){
uint16_t ch0 = xjt.channels[i].ch0_l | ((uint16_t)(xjt.channels[i].ch0_h & 0x7) << 8);
ch0 = ((ch0 * 2) + 2452) / 3;
uint16_t ch1 = xjt.channels[i].ch1_l | ((uint16_t)(xjt.channels[i].ch1_h & 0x7F) << 4);
ch1 = ((ch1 * 2) + 2452) / 3;
uint8_t c0n = i*2;
if(xjt.channels[i].ch0_h & 0x8){
c0n += 8;
}
uint8_t c1n = i*2+1;
if(xjt.channels[i].ch1_h & 0x80){
c1n += 8;
}
s_channels[c0n] = ch0;
s_channels[c1n] = ch1;
}
}
}
return true;
}
void parseRmt(rmt_data_t* items, size_t len, uint32_t* channels){
size_t chan = 0;
bool valid = true;
rmt_data_t* it = NULL;
if (!channels) {
log_e("Please provide data block for storing channel info");
return;
}
s_channels = channels;
it = &items[0];
for(size_t i = 0; i<len; i++){
if(!valid){
break;
}
it = &items[i];
if(XJT_VALID(it)){
if(it->duration1 >= 5 && it->duration1 <= 8){
valid = xjtReceiveBit(i, false);
} else if(it->duration1 >= 13 && it->duration1 <= 16){
valid = xjtReceiveBit(i, true);
} else {
valid = false;
}
} else if(!it->duration1 && !it->level1 && it->duration0 >= 5 && it->duration0 <= 8) {
valid = xjtReceiveBit(i, false);
}
}
}
extern "C" void receive_data(uint32_t *data, size_t len)
{
parseRmt((rmt_data_t*) data, len, channels);
}
void setup()
{
Serial.begin(115200);
// Initialize the channel to capture up to 192 items
if ((rmt_recv = rmtInit(21, false, RMT_MEM_192)) == NULL)
{
Serial.println("init receiver failed\n");
}
// Setup 1us tick
float realTick = rmtSetTick(rmt_recv, 1000);
Serial.printf("real tick set to: %fns\n", realTick);
// Ask to start reading
rmtRead(rmt_recv, receive_data);
}
void loop()
{
// printout some of the channels
Serial.printf("%04x %04x %04x %04x\n", channels[0], channels[1], channels[2], channels[3]);
delay(500);
}

View File

@ -0,0 +1,89 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "Arduino.h"
#include "esp32-hal.h"
#define NR_OF_LEDS 8*4
#define NR_OF_ALL_BITS 24*NR_OF_LEDS
//
// Note: This example uses Neopixel LED board, 32 LEDs chained one
// after another, each RGB LED has its 24 bit value
// for color configuration (8b for each color)
//
// Bits encoded as pulses as follows:
//
// "0":
// +-------+ +--
// | | |
// | | |
// | | |
// ---| |--------------|
// + + +
// | 0.4us | 0.85 0us |
//
// "1":
// +-------------+ +--
// | | |
// | | |
// | | |
// | | |
// ---+ +-------+
// | 0.8us | 0.4us |
rmt_data_t led_data[NR_OF_ALL_BITS];
rmt_obj_t* rmt_send = NULL;
void setup()
{
Serial.begin(115200);
if ((rmt_send = rmtInit(18, true, RMT_MEM_64)) == NULL)
{
Serial.println("init sender failed\n");
}
float realTick = rmtSetTick(rmt_send, 100);
Serial.printf("real tick set to: %fns\n", realTick);
}
int color[] = { 0x55, 0x11, 0x77 }; // RGB value
int led_index = 0;
void loop()
{
// Init data with only one led ON
int led, col, bit;
int i=0;
for (led=0; led<NR_OF_LEDS; led++) {
for (col=0; col<3; col++ ) {
for (bit=0; bit<8; bit++){
if ( (color[col] & (1<<(8-bit))) && (led == led_index) ) {
led_data[i].level0 = 1;
led_data[i].duration0 = 8;
led_data[i].level1 = 0;
led_data[i].duration1 = 4;
} else {
led_data[i].level0 = 1;
led_data[i].duration0 = 4;
led_data[i].level1 = 0;
led_data[i].duration1 = 8;
}
i++;
}
}
}
// make the led travel in the pannel
if ((++led_index)>=NR_OF_LEDS) {
led_index = 0;
}
// Send the data
rmtWrite(rmt_send, led_data, NR_OF_ALL_BITS);
delay(100);
}

View File

@ -6,7 +6,7 @@ hw_timer_t *timer = NULL;
void IRAM_ATTR resetModule() {
ets_printf("reboot\n");
esp_restart_noos();
esp_restart();
}
void setup() {

View File

@ -0,0 +1,181 @@
#include "FS.h"
#include "FFat.h"
// You only need to format FFat the first time you run a test
#define FORMAT_FFAT true
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\r\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("- failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println(" - not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print("\tSIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if(!file || file.isDirectory()){
Serial.println("- failed to open file for reading");
return;
}
Serial.println("- read from file:");
while(file.available()){
Serial.write(file.read());
}
}
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\r\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("- failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("- file written");
} else {
Serial.println("- frite failed");
}
}
void appendFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Appending to file: %s\r\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("- failed to open file for appending");
return;
}
if(file.print(message)){
Serial.println("- message appended");
} else {
Serial.println("- append failed");
}
}
void renameFile(fs::FS &fs, const char * path1, const char * path2){
Serial.printf("Renaming file %s to %s\r\n", path1, path2);
if (fs.rename(path1, path2)) {
Serial.println("- file renamed");
} else {
Serial.println("- rename failed");
}
}
void deleteFile(fs::FS &fs, const char * path){
Serial.printf("Deleting file: %s\r\n", path);
if(fs.remove(path)){
Serial.println("- file deleted");
} else {
Serial.println("- delete failed");
}
}
void testFileIO(fs::FS &fs, const char * path){
Serial.printf("Testing file I/O with %s\r\n", path);
static uint8_t buf[512];
size_t len = 0;
File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("- failed to open file for writing");
return;
}
size_t i;
Serial.print("- writing" );
uint32_t start = millis();
for(i=0; i<2048; i++){
if ((i & 0x001F) == 0x001F){
Serial.print(".");
}
file.write(buf, 512);
}
Serial.println("");
uint32_t end = millis() - start;
Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end);
file.close();
file = fs.open(path);
start = millis();
end = start;
i = 0;
if(file && !file.isDirectory()){
len = file.size();
size_t flen = len;
start = millis();
Serial.print("- reading" );
while(len){
size_t toRead = len;
if(toRead > 512){
toRead = 512;
}
file.read(buf, toRead);
if ((i++ & 0x001F) == 0x001F){
Serial.print(".");
}
len -= toRead;
}
Serial.println("");
end = millis() - start;
Serial.printf("- %u bytes read in %u ms\r\n", flen, end);
file.close();
} else {
Serial.println("- failed to open file for reading");
}
}
void setup(){
Serial.begin(115200);
Serial.setDebugOutput(true);
if (FORMAT_FFAT) FFat.format();
if(!FFat.begin()){
Serial.println("FFat Mount Failed");
return;
}
Serial.printf("Total space: %10lu\n", FFat.totalBytes());
Serial.printf("Free space: %10lu\n", FFat.freeBytes());
listDir(FFat, "/", 0);
writeFile(FFat, "/hello.txt", "Hello ");
appendFile(FFat, "/hello.txt", "World!\r\n");
readFile(FFat, "/hello.txt");
renameFile(FFat, "/hello.txt", "/foo.txt");
readFile(FFat, "/foo.txt");
deleteFile(FFat, "/foo.txt");
testFileIO(FFat, "/test.txt");
Serial.printf("Free space: %10lu\n", FFat.freeBytes());
deleteFile(FFat, "/test.txt");
Serial.println( "Test complete" );
}
void loop(){
}

View File

@ -0,0 +1,9 @@
name=FFat
version=1.0
author=Hristo Gochkov, Ivan Grokhtkov, Larry Bernstone
maintainer=Hristo Gochkov <hristo@espressif.com>
sentence=ESP32 FAT on Flash File System
paragraph=
category=Data Storage
url=
architectures=esp32

144
libraries/FFat/src/FFat.cpp Normal file
View File

@ -0,0 +1,144 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "vfs_api.h"
extern "C" {
#include "esp_vfs_fat.h"
#include "diskio.h"
#include "diskio_wl.h"
#include "vfs_fat_internal.h"
}
#include "FFat.h"
using namespace fs;
F_Fat::F_Fat(FSImplPtr impl)
: FS(impl)
{}
const esp_partition_t *check_ffat_partition(const char* label)
{
const esp_partition_t* ck_part = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, label);
if (!ck_part) {
log_e("No FAT partition found with label %s", label);
return NULL;
}
return ck_part;
}
bool F_Fat::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles, const char * partitionLabel)
{
if(_wl_handle){
log_w("Already Mounted!");
return true;
}
if (!check_ffat_partition(partitionLabel)) return false;
esp_vfs_fat_mount_config_t conf = {
.format_if_mount_failed = formatOnFail,
.max_files = maxOpenFiles
};
esp_err_t err = esp_vfs_fat_spiflash_mount(basePath, partitionLabel, &conf, &_wl_handle);
if(err){
log_e("Mounting FFat partition failed! Error: %d", err);
return false;
}
_impl->mountpoint(basePath);
return true;
}
void F_Fat::end()
{
if(_wl_handle){
esp_err_t err = esp_vfs_fat_spiflash_unmount(_impl->mountpoint(), _wl_handle);
if(err){
log_e("Unmounting FFat partition failed! Error: %d", err);
return;
}
_wl_handle = NULL;
_impl->mountpoint(NULL);
}
}
bool F_Fat::format(bool full_wipe, char* partitionLabel)
{
esp_err_t result;
if(_wl_handle){
log_w("Already Mounted!");
return false;
}
wl_handle_t temp_handle;
// Attempt to mount to see if there is already data
const esp_partition_t *ffat_partition = check_ffat_partition(partitionLabel);
if (!ffat_partition) return false;
result = wl_mount(ffat_partition, &temp_handle);
if (result == ESP_OK) {
// Wipe disk- quick just wipes the FAT. Full zeroes the whole disk
uint32_t wipe_size = full_wipe ? wl_size(temp_handle) : 16384;
wl_erase_range(temp_handle, 0, wipe_size);
wl_unmount(temp_handle);
}
// Now do a mount with format_if_fail (which it will)
esp_vfs_fat_mount_config_t conf = {
.format_if_mount_failed = true,
.max_files = 1
};
result = esp_vfs_fat_spiflash_mount("/format_ffat", partitionLabel, &conf, &temp_handle);
esp_vfs_fat_spiflash_unmount("/format_ffat", temp_handle);
return result;
}
size_t F_Fat::totalBytes()
{
FATFS *fs;
DWORD free_clust, tot_sect, sect_size;
BYTE pdrv = ff_diskio_get_pdrv_wl(_wl_handle);
char drv[3] = {(char)(48+pdrv), ':', 0};
FRESULT res = f_getfree(drv, &free_clust, &fs);
tot_sect = (fs->n_fatent - 2) * fs->csize;
sect_size = CONFIG_WL_SECTOR_SIZE;
return tot_sect * sect_size;
}
size_t F_Fat::freeBytes()
{
FATFS *fs;
DWORD free_clust, free_sect, sect_size;
BYTE pdrv = ff_diskio_get_pdrv_wl(_wl_handle);
char drv[3] = {(char)(48+pdrv), ':', 0};
FRESULT res = f_getfree(drv, &free_clust, &fs);
free_sect = free_clust * fs->csize;
sect_size = CONFIG_WL_SECTOR_SIZE;
return free_sect * sect_size;
}
bool F_Fat::exists(const char* path)
{
File f = open(path, "r");
return (f == true) && !f.isDirectory();
}
bool F_Fat::exists(const String& path)
{
return exists(path.c_str());
}
F_Fat FFat = F_Fat(FSImplPtr(new VFSImpl()));

47
libraries/FFat/src/FFat.h Normal file
View File

@ -0,0 +1,47 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _FFAT_H_
#define _FFAT_H_
#include "FS.h"
#include "wear_levelling.h"
#define FFAT_WIPE_QUICK 0
#define FFAT_WIPE_FULL 1
#define FFAT_PARTITION_LABEL "ffat"
namespace fs
{
class F_Fat : public FS
{
public:
F_Fat(FSImplPtr impl);
bool begin(bool formatOnFail=false, const char * basePath="/ffat", uint8_t maxOpenFiles=10, const char * partitionLabel = (char*)FFAT_PARTITION_LABEL);
bool format(bool full_wipe = FFAT_WIPE_QUICK, char* partitionLabel = (char*)FFAT_PARTITION_LABEL);
size_t totalBytes();
size_t freeBytes();
void end();
bool exists(const char* path);
bool exists(const String& path);
private:
wl_handle_t _wl_handle;
};
}
extern fs::F_Fat FFat;
#endif /* _FFAT_H_ */

View File

@ -47,7 +47,9 @@ enum SeekMode {
class File : public Stream
{
public:
File(FileImplPtr p = FileImplPtr()) : _p(p) {}
File(FileImplPtr p = FileImplPtr()) : _p(p) {
_timeout = 0;
}
size_t write(uint8_t) override;
size_t write(const uint8_t *buf, size_t size) override;

View File

@ -0,0 +1,147 @@
/**
BasicHTTPSClient.ino
Created on: 14.10.2018
*/
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
// This is GandiStandardSSLCA2.pem, the root Certificate Authority that signed
// the server certifcate for the demo server https://jigsaw.w3.org in this
// example. This certificate is valid until Sep 11 23:59:59 2024 GMT
const char* rootCACertificate = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIF6TCCA9GgAwIBAgIQBeTcO5Q4qzuFl8umoZhQ4zANBgkqhkiG9w0BAQwFADCB\n" \
"iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl\n" \
"cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV\n" \
"BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTQw\n" \
"OTEyMDAwMDAwWhcNMjQwOTExMjM1OTU5WjBfMQswCQYDVQQGEwJGUjEOMAwGA1UE\n" \
"CBMFUGFyaXMxDjAMBgNVBAcTBVBhcmlzMQ4wDAYDVQQKEwVHYW5kaTEgMB4GA1UE\n" \
"AxMXR2FuZGkgU3RhbmRhcmQgU1NMIENBIDIwggEiMA0GCSqGSIb3DQEBAQUAA4IB\n" \
"DwAwggEKAoIBAQCUBC2meZV0/9UAPPWu2JSxKXzAjwsLibmCg5duNyj1ohrP0pIL\n" \
"m6jTh5RzhBCf3DXLwi2SrCG5yzv8QMHBgyHwv/j2nPqcghDA0I5O5Q1MsJFckLSk\n" \
"QFEW2uSEEi0FXKEfFxkkUap66uEHG4aNAXLy59SDIzme4OFMH2sio7QQZrDtgpbX\n" \
"bmq08j+1QvzdirWrui0dOnWbMdw+naxb00ENbLAb9Tr1eeohovj0M1JLJC0epJmx\n" \
"bUi8uBL+cnB89/sCdfSN3tbawKAyGlLfOGsuRTg/PwSWAP2h9KK71RfWJ3wbWFmV\n" \
"XooS/ZyrgT5SKEhRhWvzkbKGPym1bgNi7tYFAgMBAAGjggF1MIIBcTAfBgNVHSME\n" \
"GDAWgBRTeb9aqitKz1SA4dibwJ3ysgNmyzAdBgNVHQ4EFgQUs5Cn2MmvTs1hPJ98\n" \
"rV1/Qf1pMOowDgYDVR0PAQH/BAQDAgGGMBIGA1UdEwEB/wQIMAYBAf8CAQAwHQYD\n" \
"VR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMCIGA1UdIAQbMBkwDQYLKwYBBAGy\n" \
"MQECAhowCAYGZ4EMAQIBMFAGA1UdHwRJMEcwRaBDoEGGP2h0dHA6Ly9jcmwudXNl\n" \
"cnRydXN0LmNvbS9VU0VSVHJ1c3RSU0FDZXJ0aWZpY2F0aW9uQXV0aG9yaXR5LmNy\n" \
"bDB2BggrBgEFBQcBAQRqMGgwPwYIKwYBBQUHMAKGM2h0dHA6Ly9jcnQudXNlcnRy\n" \
"dXN0LmNvbS9VU0VSVHJ1c3RSU0FBZGRUcnVzdENBLmNydDAlBggrBgEFBQcwAYYZ\n" \
"aHR0cDovL29jc3AudXNlcnRydXN0LmNvbTANBgkqhkiG9w0BAQwFAAOCAgEAWGf9\n" \
"crJq13xhlhl+2UNG0SZ9yFP6ZrBrLafTqlb3OojQO3LJUP33WbKqaPWMcwO7lWUX\n" \
"zi8c3ZgTopHJ7qFAbjyY1lzzsiI8Le4bpOHeICQW8owRc5E69vrOJAKHypPstLbI\n" \
"FhfFcvwnQPYT/pOmnVHvPCvYd1ebjGU6NSU2t7WKY28HJ5OxYI2A25bUeo8tqxyI\n" \
"yW5+1mUfr13KFj8oRtygNeX56eXVlogMT8a3d2dIhCe2H7Bo26y/d7CQuKLJHDJd\n" \
"ArolQ4FCR7vY4Y8MDEZf7kYzawMUgtN+zY+vkNaOJH1AQrRqahfGlZfh8jjNp+20\n" \
"J0CT33KpuMZmYzc4ZCIwojvxuch7yPspOqsactIGEk72gtQjbz7Dk+XYtsDe3CMW\n" \
"1hMwt6CaDixVBgBwAc/qOR2A24j3pSC4W/0xJmmPLQphgzpHphNULB7j7UTKvGof\n" \
"KA5R2d4On3XNDgOVyvnFqSot/kGkoUeuDcL5OWYzSlvhhChZbH2UF3bkRYKtcCD9\n" \
"0m9jqNf6oDP6N8v3smWe2lBvP+Sn845dWDKXcCMu5/3EFZucJ48y7RetWIExKREa\n" \
"m9T8bJUox04FB6b9HbwZ4ui3uRGKLXASUoWNjDNKD/yZkuBjcNqllEdjB+dYxzFf\n" \
"BT02Vf6Dsuimrdfp5gJ0iHRc2jTbkNJtUQoj1iM=\n" \
"-----END CERTIFICATE-----\n";
// Not sure if WiFiClientSecure checks the validity date of the certificate.
// Setting clock just to be sure...
void setClock() {
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
Serial.print(F("Waiting for NTP time sync: "));
time_t nowSecs = time(nullptr);
while (nowSecs < 8 * 3600 * 2) {
delay(500);
Serial.print(F("."));
yield();
nowSecs = time(nullptr);
}
Serial.println();
struct tm timeinfo;
gmtime_r(&nowSecs, &timeinfo);
Serial.print(F("Current time: "));
Serial.print(asctime(&timeinfo));
}
WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "PASSWORD");
// wait for WiFi connection
Serial.print("Waiting for WiFi to connect...");
while ((WiFiMulti.run() != WL_CONNECTED)) {
Serial.print(".");
}
Serial.println(" connected");
setClock();
}
void loop() {
WiFiClientSecure *client = new WiFiClientSecure;
if(client) {
client -> setCACert(rootCACertificate);
{
// Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is
HTTPClient https;
Serial.print("[HTTPS] begin...\n");
if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
int httpCode = https.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = https.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
// End extra scoping block
}
delete client;
} else {
Serial.println("Unable to create client");
}
Serial.println();
Serial.println("Waiting 10s before the next round...");
delay(10000);
}

View File

@ -0,0 +1,98 @@
/*|----------------------------------------------------------|*/
/*|WORKING EXAMPLE FOR HTTP/HTTPS CONNECTION |*/
/*|TESTED BOARDS: Devkit v1 DOIT, Devkitc v4 |*/
/*|CORE: June 2018 |*/
/*|----------------------------------------------------------|*/
#include <WiFi.h>
#include <HTTPClient.h>
#include "esp_wpa2.h"
#include <Wire.h>
#define EAP_IDENTITY "identity" //if connecting from another corporation, use identity@organisation.domain in Eduroam
#define EAP_PASSWORD "password" //your Eduroam password
const char* ssid = "eduroam"; // Eduroam SSID
int counter = 0;
const char* test_root_ca= \
"-----BEGIN CERTIFICATE-----\n" \
"MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n" \
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" \
"d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD\n" \
"QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT\n" \
"MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n" \
"b20xIDAeBgNVBAMTF0RpZ2lDZXJ0IEdsb2JhbCBSb290IENBMIIBIjANBgkqhkiG\n" \
"9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4jvhEXLeqKTTo1eqUKKPC3eQyaKl7hLOllsB\n" \
"CSDMAZOnTjC3U/dDxGkAV53ijSLdhwZAAIEJzs4bg7/fzTtxRuLWZscFs3YnFo97\n" \
"nh6Vfe63SKMI2tavegw5BmV/Sl0fvBf4q77uKNd0f3p4mVmFaG5cIzJLv07A6Fpt\n" \
"43C/dxC//AH2hdmoRBBYMql1GNXRor5H4idq9Joz+EkIYIvUX7Q6hL+hqkpMfT7P\n" \
"T19sdl6gSzeRntwi5m3OFBqOasv+zbMUZBfHWymeMr/y7vrTC0LUq7dBMtoM1O/4\n" \
"gdW7jVg/tRvoSSiicNoxBN33shbyTApOB6jtSj1etX+jkMOvJwIDAQABo2MwYTAO\n" \
"BgNVHQ8BAf8EBAMCAYYwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUA95QNVbR\n" \
"TLtm8KPiGxvDl7I90VUwHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUw\n" \
"DQYJKoZIhvcNAQEFBQADggEBAMucN6pIExIK+t1EnE9SsPTfrgT1eXkIoyQY/Esr\n" \
"hMAtudXH/vTBH1jLuG2cenTnmCmrEbXjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n" \
"06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n" \
"PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n" \
"YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" \
"CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" \
"-----END CERTIFICATE-----\n";
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.print("Connecting to network: ");
Serial.println(ssid);
WiFi.disconnect(true); //disconnect form wifi to set new wifi connection
WiFi.mode(WIFI_STA); //init wifi mode
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide identity
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username --> identity and username is same
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password
esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT(); //set config settings to default
esp_wifi_sta_wpa2_ent_enable(&config); //set config settings to enable function
WiFi.begin(ssid); //connect to wifi
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
counter++;
if(counter>=60){ //after 30 seconds timeout - reset board
ESP.restart();
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address set: ");
Serial.println(WiFi.localIP()); //print LAN IP
}
void loop() {
if (WiFi.status() == WL_CONNECTED) { //if we are connected to Eduroam network
counter = 0; //reset counter
Serial.println("Wifi is still connected with IP: ");
Serial.println(WiFi.localIP()); //inform user about his IP address
}else if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry
WiFi.begin(ssid);
}
while (WiFi.status() != WL_CONNECTED) { //during lost connection, print dots
delay(500);
Serial.print(".");
counter++;
if(counter>=60){ //30 seconds timeout - reset board
ESP.restart();
}
}
Serial.print("Connecting to website: ");
HTTPClient http;
http.begin("https://arduino.php5.sk/rele/rele1.txt", test_root_ca); //HTTPS example connection
//http.begin("http://www.arduino.php5.sk/rele/rele1.txt"); //HTTP example connection
//if uncomment HTTP example, you can comment root CA certificate too!
int httpCode = http.GET();
if(httpCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
//file found at server --> on unsucessful connection code will be -1
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
}
}else{
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
delay(2000);
}

View File

@ -1,5 +1,5 @@
name=HTTPClient
version=1.1
version=1.2
author=Markus Sattler
maintainer=Markus Sattler
sentence=http Client for ESP32

View File

@ -22,17 +22,23 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Adapted in October 2018
*/
#include <Arduino.h>
#include <esp32-hal-log.h>
#ifdef HTTPCLIENT_1_1_COMPATIBLE
#include <WiFi.h>
#include <WiFiClientSecure.h>
#endif
#include <StreamString.h>
#include <base64.h>
#include "HTTPClient.h"
#ifdef HTTPCLIENT_1_1_COMPATIBLE
class TransportTraits
{
public:
@ -78,6 +84,7 @@ protected:
const char* _clicert;
const char* _clikey;
};
#endif // HTTPCLIENT_1_1_COMPATIBLE
/**
* constructor
@ -91,8 +98,8 @@ HTTPClient::HTTPClient()
*/
HTTPClient::~HTTPClient()
{
if(_tcp) {
_tcp->stop();
if(_client) {
_client->stop();
}
if(_currentHeaders) {
delete[] _currentHeaders;
@ -107,9 +114,81 @@ void HTTPClient::clear()
}
/**
* parsing the url for all needed parameters
* @param client Client&
* @param url String
* @param https bool
* @return success bool
*/
bool HTTPClient::begin(WiFiClient &client, String url) {
#ifdef HTTPCLIENT_1_1_COMPATIBLE
if(_tcpDeprecated) {
log_d("mix up of new and deprecated api");
_canReuse = false;
end();
}
#endif
_client = &client;
// check for : (http: or https:)
int index = url.indexOf(':');
if(index < 0) {
log_d("failed to parse protocol");
return false;
}
String protocol = url.substring(0, index);
if(protocol != "http" && protocol != "https") {
log_d("unknown protocol '%s'", protocol.c_str());
return false;
}
_port = (protocol == "https" ? 443 : 80);
return beginInternal(url, protocol.c_str());
}
/**
* directly supply all needed parameters
* @param client Client&
* @param host String
* @param port uint16_t
* @param uri String
* @param https bool
* @return success bool
*/
bool HTTPClient::begin(WiFiClient &client, String host, uint16_t port, String uri, bool https)
{
#ifdef HTTPCLIENT_1_1_COMPATIBLE
if(_tcpDeprecated) {
log_d("mix up of new and deprecated api");
_canReuse = false;
end();
}
#endif
_client = &client;
clear();
_host = host;
_port = port;
_uri = uri;
_protocol = (https ? "https" : "http");
return true;
}
#ifdef HTTPCLIENT_1_1_COMPATIBLE
bool HTTPClient::begin(String url, const char* CAcert)
{
_transportTraits.reset(nullptr);
if(_client && !_tcpDeprecated) {
log_d("mix up of new and deprecated api");
_canReuse = false;
end();
}
_port = 443;
if (!beginInternal(url, "https")) {
return false;
@ -125,8 +204,12 @@ bool HTTPClient::begin(String url, const char* CAcert)
*/
bool HTTPClient::begin(String url)
{
if(_client && !_tcpDeprecated) {
log_d("mix up of new and deprecated api");
_canReuse = false;
end();
}
_transportTraits.reset(nullptr);
_port = 80;
if (!beginInternal(url, "http")) {
return begin(url, (const char*)NULL);
@ -134,6 +217,7 @@ bool HTTPClient::begin(String url)
_transportTraits = TransportTraitsPtr(new TransportTraits());
return true;
}
#endif // HTTPCLIENT_1_1_COMPATIBLE
bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
{
@ -182,8 +266,15 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
return true;
}
#ifdef HTTPCLIENT_1_1_COMPATIBLE
bool HTTPClient::begin(String host, uint16_t port, String uri)
{
if(_client && !_tcpDeprecated) {
log_d("mix up of new and deprecated api");
_canReuse = false;
end();
}
clear();
_host = host;
_port = port;
@ -195,6 +286,12 @@ bool HTTPClient::begin(String host, uint16_t port, String uri)
bool HTTPClient::begin(String host, uint16_t port, String uri, const char* CAcert)
{
if(_client && !_tcpDeprecated) {
log_d("mix up of new and deprecated api");
_canReuse = false;
end();
}
clear();
_host = host;
_port = port;
@ -210,6 +307,12 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const char* CAcer
bool HTTPClient::begin(String host, uint16_t port, String uri, const char* CAcert, const char* cli_cert, const char* cli_key)
{
if(_client && !_tcpDeprecated) {
log_d("mix up of new and deprecated api");
_canReuse = false;
end();
}
clear();
_host = host;
_port = port;
@ -222,37 +325,60 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const char* CAcer
_transportTraits = TransportTraitsPtr(new TLSTraits(CAcert, cli_cert, cli_key));
return true;
}
#endif // HTTPCLIENT_1_1_COMPATIBLE
/**
* end
* called after the payload is handled
*/
void HTTPClient::end(void)
{
disconnect();
}
/**
* disconnect
* close the TCP socket
*/
void HTTPClient::disconnect()
{
if(connected()) {
if(_tcp->available() > 0) {
log_d("still data in buffer (%d), clean up.", _tcp->available());
_tcp->flush();
if(_client->available() > 0) {
log_d("still data in buffer (%d), clean up.\n", _client->available());
while(_client->available() > 0) {
_client->read();
}
}
if(_reuse && _canReuse) {
log_d("tcp keep open for reuse");
log_d("tcp keep open for reuse\n");
} else {
log_d("tcp stop");
_tcp->stop();
log_d("tcp stop\n");
_client->stop();
_client = nullptr;
#ifdef HTTPCLIENT_1_1_COMPATIBLE
if(_tcpDeprecated) {
_transportTraits.reset(nullptr);
_tcpDeprecated.reset(nullptr);
}
#endif
}
} else {
log_v("tcp is closed");
log_d("tcp is closed\n");
}
}
/**
* connected
* @return connected status
*/
bool HTTPClient::connected()
{
if(_tcp) {
return (_tcp->connected() || (_tcp->available() > 0));
if(_client) {
return ((_client->available() > 0) || _client->connected());
}
return false;
}
@ -309,8 +435,8 @@ void HTTPClient::setAuthorization(const char * auth)
void HTTPClient::setTimeout(uint16_t timeout)
{
_tcpTimeout = timeout;
if(connected() && !_secure) {
_tcp->setTimeout(timeout);
if(connected()) {
_client->setTimeout((timeout + 500) / 1000);
}
}
@ -398,7 +524,7 @@ int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size)
// send Payload if needed
if(payload && size > 0) {
if(_tcp->write(&payload[0], size) != size) {
if(_client->write(&payload[0], size) != size) {
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
}
}
@ -477,7 +603,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
int bytesRead = stream->readBytes(buff, readBytes);
// write it to Stream
int bytesWrite = _tcp->write((const uint8_t *) buff, bytesRead);
int bytesWrite = _client->write((const uint8_t *) buff, bytesRead);
bytesWritten += bytesWrite;
// are all Bytes a writen to stream ?
@ -485,11 +611,11 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
log_d("short write, asked for %d but got %d retry...", bytesRead, bytesWrite);
// check for write error
if(_tcp->getWriteError()) {
log_d("stream write error %d", _tcp->getWriteError());
if(_client->getWriteError()) {
log_d("stream write error %d", _client->getWriteError());
//reset write error for retry
_tcp->clearWriteError();
_client->clearWriteError();
}
// some time for the stream
@ -498,7 +624,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
int leftBytes = (readBytes - bytesWrite);
// retry to send the missed bytes
bytesWrite = _tcp->write((const uint8_t *) (buff + bytesWrite), leftBytes);
bytesWrite = _client->write((const uint8_t *) (buff + bytesWrite), leftBytes);
bytesWritten += bytesWrite;
if(bytesWrite != leftBytes) {
@ -510,8 +636,8 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
}
// check for write error
if(_tcp->getWriteError()) {
log_d("stream write error %d", _tcp->getWriteError());
if(_client->getWriteError()) {
log_d("stream write error %d", _client->getWriteError());
free(buff);
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
}
@ -561,8 +687,8 @@ int HTTPClient::getSize(void)
*/
WiFiClient& HTTPClient::getStream(void)
{
if (connected() && !_secure) {
return *_tcp;
if (connected()) {
return *_client;
}
log_w("getStream: not connected");
@ -577,7 +703,7 @@ WiFiClient& HTTPClient::getStream(void)
WiFiClient* HTTPClient::getStreamPtr(void)
{
if(connected()) {
return _tcp.get();
return _client;
}
log_w("getStreamPtr: not connected");
@ -617,7 +743,7 @@ int HTTPClient::writeToStream(Stream * stream)
if(!connected()) {
return returnError(HTTPC_ERROR_CONNECTION_LOST);
}
String chunkHeader = _tcp->readStringUntil('\n');
String chunkHeader = _client->readStringUntil('\n');
if(chunkHeader.length() <= 0) {
return returnError(HTTPC_ERROR_READ_TIMEOUT);
@ -654,7 +780,7 @@ int HTTPClient::writeToStream(Stream * stream)
// read trailing \r\n at the end of the chunk
char buf[2];
auto trailing_seq_len = _tcp->readBytes((uint8_t*)buf, 2);
auto trailing_seq_len = _client->readBytes((uint8_t*)buf, 2);
if (trailing_seq_len != 2 || buf[0] != '\r' || buf[1] != '\n') {
return returnError(HTTPC_ERROR_READ_TIMEOUT);
}
@ -822,38 +948,46 @@ bool HTTPClient::connect(void)
if(connected()) {
log_d("already connected, try reuse!");
while(_tcp->available() > 0) {
_tcp->read();
while(_client->available() > 0) {
_client->read();
}
return true;
}
if (!_transportTraits) {
#ifdef HTTPCLIENT_1_1_COMPATIBLE
if(!_client) {
_tcpDeprecated = _transportTraits->create();
_client = _tcpDeprecated.get();
}
#endif
if (!_client) {
log_d("HTTPClient::begin was not called or returned error");
return false;
}
_tcp = _transportTraits->create();
// set Timeout for WiFiClient and for Stream::readBytesUntil() and Stream::readStringUntil()
_client->setTimeout((_tcpTimeout + 500) / 1000);
if (!_transportTraits->verify(*_tcp, _host.c_str())) {
log_d("transport level verify failed");
_tcp->stop();
return false;
}
if(!_tcp->connect(_host.c_str(), _port)) {
if(!_client->connect(_host.c_str(), _port)) {
log_d("failed connect to %s:%u", _host.c_str(), _port);
return false;
}
log_d(" connected to %s:%u", _host.c_str(), _port);
// set Timeout for readBytesUntil and readStringUntil
setTimeout(_tcpTimeout);
#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
_tcp->setNoDelay(true);
_client->setNoDelay(true);
#endif
*/
return connected();
@ -907,7 +1041,7 @@ bool HTTPClient::sendHeader(const char * type)
header += _headers + "\r\n";
return (_tcp->write((const uint8_t *) header.c_str(), header.length()) == header.length());
return (_client->write((const uint8_t *) header.c_str(), header.length()) == header.length());
}
/**
@ -928,9 +1062,9 @@ int HTTPClient::handleHeaderResponse()
unsigned long lastDataTime = millis();
while(connected()) {
size_t len = _tcp->available();
size_t len = _client->available();
if(len > 0) {
String headerLine = _tcp->readStringUntil('\n');
String headerLine = _client->readStringUntil('\n');
headerLine.trim(); // remove \r
lastDataTime = millis();
@ -1026,7 +1160,7 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
while(connected() && (len > 0 || len == -1)) {
// get available data size
size_t sizeAvailable = _tcp->available();
size_t sizeAvailable = _client->available();
if(sizeAvailable) {
@ -1041,9 +1175,13 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
if(readBytes > buff_size) {
readBytes = buff_size;
}
// stop if no more reading
if (readBytes == 0)
break;
// read data
int bytesRead = _tcp->readBytes(buff, readBytes);
int bytesRead = _client->readBytes(buff, readBytes);
// write it to Stream
int bytesWrite = stream->write(buff, bytesRead);
@ -1124,7 +1262,7 @@ int HTTPClient::returnError(int error)
log_w("error(%d): %s", error, errorToString(error).c_str());
if(connected()) {
log_d("tcp stop");
_tcp->stop();
_client->stop();
}
}
return error;

View File

@ -27,6 +27,8 @@
#ifndef HTTPClient_H_
#define HTTPClient_H_
#define HTTPCLIENT_1_1_COMPATIBLE
#include <memory>
#include <Arduino.h>
#include <WiFiClient.h>
@ -117,8 +119,10 @@ typedef enum {
HTTPC_TE_CHUNKED
} transferEncoding_t;
#ifdef HTTPCLIENT_1_1_COMPATIBLE
class TransportTraits;
typedef std::unique_ptr<TransportTraits> TransportTraitsPtr;
#endif
class HTTPClient
{
@ -126,11 +130,20 @@ public:
HTTPClient();
~HTTPClient();
/*
* Since both begin() functions take a reference to client as a parameter, you need to
* ensure the client object lives the entire time of the HTTPClient
*/
bool begin(WiFiClient &client, String url);
bool begin(WiFiClient &client, String host, uint16_t port, String uri = "/", bool https = false);
#ifdef HTTPCLIENT_1_1_COMPATIBLE
bool begin(String url);
bool begin(String url, const char* CAcert);
bool begin(String host, uint16_t port, String uri = "/");
bool begin(String host, uint16_t port, String uri, const char* CAcert);
bool begin(String host, uint16_t port, String uri, const char* CAcert, const char* cli_cert, const char* cli_key);
#endif
void end(void);
@ -181,6 +194,7 @@ protected:
};
bool beginInternal(String url, const char* expectedProtocol);
void disconnect();
void clear();
int returnError(int error);
bool connect(void);
@ -189,8 +203,12 @@ protected:
int writeToStreamDataBlock(Stream * stream, int len);
#ifdef HTTPCLIENT_1_1_COMPATIBLE
TransportTraitsPtr _transportTraits;
std::unique_ptr<WiFiClient> _tcp;
std::unique_ptr<WiFiClient> _tcpDeprecated;
#endif
WiFiClient* _client = nullptr;
/// request handling
String _host;

View File

@ -0,0 +1,72 @@
/**
httpUpdate.ino
Created on: 27.11.2015
*/
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <HTTPUpdate.h>
WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "PASSWORD");
}
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
WiFiClient client;
// The line below is optional. It can be used to blink the LED on the board during flashing
// The LED will be on during download of one buffer of data from the network. The LED will
// be off during writing that buffer to flash
// On a good connection the LED should flash regularly. On a bad connection the LED will be
// on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second
// value is used to put the LED on. If the LED is on with HIGH, that value should be passed
// httpUpdate.setLedPin(LED_BUILTIN, LOW);
t_httpUpdate_return ret = httpUpdate.update(client, "http://server/file.bin");
// Or:
//t_httpUpdate_return ret = httpUpdate.update(client, "server", 80, "file.bin");
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
break;
}
}
}

View File

@ -0,0 +1,75 @@
/**
httpUpdateSPIFFS.ino
Created on: 05.12.2015
*/
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <HTTPUpdate.h>
WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "PASSWORD");
}
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
Serial.println("Update SPIFFS...");
WiFiClient client;
// The line below is optional. It can be used to blink the LED on the board during flashing
// The LED will be on during download of one buffer of data from the network. The LED will
// be off during writing that buffer to flash
// On a good connection the LED should flash regularly. On a bad connection the LED will be
// on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second
// value is used to put the LED on. If the LED is on with HIGH, that value should be passed
// httpUpdate.setLedPin(LED_BUILTIN, LOW);
t_httpUpdate_return ret = httpUpdate.updateSpiffs(client, "http://server/spiffs.bin");
if (ret == HTTP_UPDATE_OK) {
Serial.println("Update sketch...");
ret = httpUpdate.update(client, "http://server/file.bin");
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
break;
}
}
}
}

View File

@ -0,0 +1,128 @@
/**
httpUpdateSecure.ino
Created on: 16.10.2018 as an adaptation of the ESP8266 version of httpUpdate.ino
*/
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <HTTPUpdate.h>
#include <time.h>
WiFiMulti WiFiMulti;
// Set time via NTP, as required for x.509 validation
void setClock() {
configTime(0, 0, "pool.ntp.org", "time.nist.gov"); // UTC
Serial.print(F("Waiting for NTP time sync: "));
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
yield();
delay(500);
Serial.print(F("."));
now = time(nullptr);
}
Serial.println(F(""));
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print(F("Current time: "));
Serial.print(asctime(&timeinfo));
}
/**
* This is lets-encrypt-x3-cross-signed.pem
*/
const char* rootCACertificate = \
"-----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-----\n";
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "PASSWORD");
}
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
setClock();
WiFiClientSecure client;
client.setCACert(rootCACertificate);
// Reading data over SSL may be slow, use an adequate timeout
client.setTimeout(12000);
// The line below is optional. It can be used to blink the LED on the board during flashing
// The LED will be on during download of one buffer of data from the network. The LED will
// be off during writing that buffer to flash
// On a good connection the LED should flash regularly. On a bad connection the LED will be
// on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second
// value is used to put the LED on. If the LED is on with HIGH, that value should be passed
// httpUpdate.setLedPin(LED_BUILTIN, HIGH);
t_httpUpdate_return ret = httpUpdate.update(client, "https://server/file.bin");
// Or:
//t_httpUpdate_return ret = httpUpdate.update(client, "server", 443, "file.bin");
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK");
break;
}
}
}

View File

@ -0,0 +1,42 @@
#######################################
# Syntax Coloring Map For ESP8266httpUpdate
#######################################
#######################################
# Library (KEYWORD3)
#######################################
ESP8266httpUpdate KEYWORD3 RESERVED_WORD
#######################################
# Datatypes (KEYWORD1)
#######################################
HTTPUpdateResult KEYWORD1 DATA_TYPE
ESPhttpUpdate KEYWORD1 DATA_TYPE
#######################################
# Methods and Functions (KEYWORD2)
#######################################
rebootOnUpdate KEYWORD2
update KEYWORD2
updateSpiffs KEYWORD2
getLastError KEYWORD2
getLastErrorString KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
HTTP_UE_TOO_LESS_SPACE LITERAL1 RESERVED_WORD_2
HTTP_UE_SERVER_NOT_REPORT_SIZE LITERAL1 RESERVED_WORD_2
HTTP_UE_SERVER_FILE_NOT_FOUND LITERAL1 RESERVED_WORD_2
HTTP_UE_SERVER_FORBIDDEN LITERAL1 RESERVED_WORD_2
HTTP_UE_SERVER_WRONG_HTTP_CODE LITERAL1 RESERVED_WORD_2
HTTP_UE_SERVER_FAULTY_MD5 LITERAL1 RESERVED_WORD_2
HTTP_UE_BIN_VERIFY_HEADER_FAILED LITERAL1 RESERVED_WORD_2
HTTP_UE_BIN_FOR_WRONG_FLASH LITERAL1 RESERVED_WORD_2
HTTP_UPDATE_FAILED LITERAL1 RESERVED_WORD_2
HTTP_UPDATE_NO_UPDATES LITERAL1 RESERVED_WORD_2
HTTP_UPDATE_OK LITERAL1 RESERVED_WORD_2

View File

@ -0,0 +1,9 @@
name=HTTPUpdate
version=1.3
author=Markus Sattler
maintainer=Markus Sattler
sentence=Http Update for ESP32
paragraph=
category=Data Processing
url=https://github.com/Links2004/Arduino/tree/esp8266/hardware/esp8266com/esp8266/libraries/ESP8266httpUpdate
architectures=esp32

View File

@ -0,0 +1,390 @@
/**
*
* @file HTTPUpdate.cpp based om ESP8266HTTPUpdate.cpp
* @date 16.10.2018
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the ESP32 Http Updater.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "HTTPUpdate.h"
#include <StreamString.h>
#include <esp_partition.h>
#include <esp_ota_ops.h> // get running partition
// To do extern "C" uint32_t _SPIFFS_start;
// To do extern "C" uint32_t _SPIFFS_end;
HTTPUpdate::HTTPUpdate(void)
: _httpClientTimeout(8000), _ledPin(-1)
{
}
HTTPUpdate::HTTPUpdate(int httpClientTimeout)
: _httpClientTimeout(httpClientTimeout), _ledPin(-1)
{
}
HTTPUpdate::~HTTPUpdate(void)
{
}
HTTPUpdateResult HTTPUpdate::update(WiFiClient& client, const String& url, const String& currentVersion)
{
HTTPClient http;
http.begin(client, url);
return handleUpdate(http, currentVersion, false);
}
HTTPUpdateResult HTTPUpdate::updateSpiffs(WiFiClient& client, const String& url, const String& currentVersion)
{
HTTPClient http;
http.begin(client, url);
return handleUpdate(http, currentVersion, true);
}
HTTPUpdateResult HTTPUpdate::update(WiFiClient& client, const String& host, uint16_t port, const String& uri,
const String& currentVersion)
{
HTTPClient http;
http.begin(client, host, port, uri);
return handleUpdate(http, currentVersion, false);
}
/**
* return error code as int
* @return int error code
*/
int HTTPUpdate::getLastError(void)
{
return _lastError;
}
/**
* return error code as String
* @return String error
*/
String HTTPUpdate::getLastErrorString(void)
{
if(_lastError == 0) {
return String(); // no error
}
// error from Update class
if(_lastError > 0) {
StreamString error;
Update.printError(error);
error.trim(); // remove line ending
return String(F("Update error: ")) + error;
}
// error from http client
if(_lastError > -100) {
return String(F("HTTP error: ")) + HTTPClient::errorToString(_lastError);
}
switch(_lastError) {
case HTTP_UE_TOO_LESS_SPACE:
return F("Not Enough space");
case HTTP_UE_SERVER_NOT_REPORT_SIZE:
return F("Server Did Not Report Size");
case HTTP_UE_SERVER_FILE_NOT_FOUND:
return F("File Not Found (404)");
case HTTP_UE_SERVER_FORBIDDEN:
return F("Forbidden (403)");
case HTTP_UE_SERVER_WRONG_HTTP_CODE:
return F("Wrong HTTP Code");
case HTTP_UE_SERVER_FAULTY_MD5:
return F("Wrong MD5");
case HTTP_UE_BIN_VERIFY_HEADER_FAILED:
return F("Verify Bin Header Failed");
case HTTP_UE_BIN_FOR_WRONG_FLASH:
return F("New Binary Does Not Fit Flash Size");
}
return String();
}
String getSketchSHA256() {
const size_t HASH_LEN = 32; // SHA-256 digest length
uint8_t sha_256[HASH_LEN] = { 0 };
// get sha256 digest for running partition
if(esp_partition_get_sha256(esp_ota_get_running_partition(), sha_256) == 0) {
char buffer[2 * HASH_LEN + 1];
for(size_t index = 0; index < HASH_LEN; index++) {
uint8_t nibble = (sha_256[index] & 0xf0) >> 4;
buffer[2 * index] = nibble < 10 ? char(nibble + '0') : char(nibble - 10 + 'A');
nibble = sha_256[index] & 0x0f;
buffer[2 * index + 1] = nibble < 10 ? char(nibble + '0') : char(nibble - 10 + 'A');
}
buffer[2 * HASH_LEN] = '\0';
return String(buffer);
} else {
return String();
}
}
/**
*
* @param http HTTPClient *
* @param currentVersion const char *
* @return HTTPUpdateResult
*/
HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs)
{
HTTPUpdateResult ret = HTTP_UPDATE_FAILED;
// use HTTP/1.0 for update since the update handler not support any transfer Encoding
http.useHTTP10(true);
http.setTimeout(_httpClientTimeout);
http.setUserAgent(F("ESP32-http-Update"));
http.addHeader(F("Cache-Control"), F("no-cache"));
http.addHeader(F("x-ESP32-STA-MAC"), WiFi.macAddress());
http.addHeader(F("x-ESP32-AP-MAC"), WiFi.softAPmacAddress());
http.addHeader(F("x-ESP32-free-space"), String(ESP.getFreeSketchSpace()));
http.addHeader(F("x-ESP32-sketch-size"), String(ESP.getSketchSize()));
// To do http.addHeader(F("x-ESP32-sketch-md5"), String(ESP.getSketchMD5()));
// Sketch MD5 is not supported by the core, but SHA256 is, so add a SHA256 instead
String sketchSHA256 = getSketchSHA256();
if(sketchSHA256.length() != 0) {
http.addHeader(F("x-ESP32-sketch-sha256"), sketchSHA256);
}
http.addHeader(F("x-ESP32-chip-size"), String(ESP.getFlashChipSize()));
http.addHeader(F("x-ESP32-sdk-version"), ESP.getSdkVersion());
if(spiffs) {
http.addHeader(F("x-ESP32-mode"), F("spiffs"));
} else {
http.addHeader(F("x-ESP32-mode"), F("sketch"));
}
if(currentVersion && currentVersion[0] != 0x00) {
http.addHeader(F("x-ESP32-version"), currentVersion);
}
const char * headerkeys[] = { "x-MD5" };
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
// track these headers
http.collectHeaders(headerkeys, headerkeyssize);
int code = http.GET();
int len = http.getSize();
if(code <= 0) {
log_e("HTTP error: %s\n", http.errorToString(code).c_str());
_lastError = code;
http.end();
return HTTP_UPDATE_FAILED;
}
log_d("Header read fin.\n");
log_d("Server header:\n");
log_d(" - code: %d\n", code);
log_d(" - len: %d\n", len);
if(http.hasHeader("x-MD5")) {
log_d(" - MD5: %s\n", http.header("x-MD5").c_str());
}
log_d("ESP32 info:\n");
log_d(" - free Space: %d\n", ESP.getFreeSketchSpace());
log_d(" - current Sketch Size: %d\n", ESP.getSketchSize());
if(currentVersion && currentVersion[0] != 0x00) {
log_d(" - current version: %s\n", currentVersion.c_str() );
}
switch(code) {
case HTTP_CODE_OK: ///< OK (Start Update)
if(len > 0) {
bool startUpdate = true;
if(spiffs) {
// To do size_t spiffsSize = ((size_t) &_SPIFFS_end - (size_t) &_SPIFFS_start);
// To do if(len > (int) spiffsSize) {
// To do log_e("spiffsSize to low (%d) needed: %d\n", spiffsSize, len);
// To do startUpdate = false;
// To do }
} else {
if(len > (int) ESP.getFreeSketchSpace()) {
log_e("FreeSketchSpace to low (%d) needed: %d\n", ESP.getFreeSketchSpace(), len);
startUpdate = false;
}
}
if(!startUpdate) {
_lastError = HTTP_UE_TOO_LESS_SPACE;
ret = HTTP_UPDATE_FAILED;
} else {
WiFiClient * tcp = http.getStreamPtr();
// To do? WiFiUDP::stopAll();
// To do? WiFiClient::stopAllExcept(tcp);
delay(100);
int command;
if(spiffs) {
command = U_SPIFFS;
log_d("runUpdate spiffs...\n");
} else {
command = U_FLASH;
log_d("runUpdate flash...\n");
}
if(!spiffs) {
/* To do
uint8_t buf[4];
if(tcp->peekBytes(&buf[0], 4) != 4) {
log_e("peekBytes magic header failed\n");
_lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED;
http.end();
return HTTP_UPDATE_FAILED;
}
*/
// check for valid first magic byte
// if(buf[0] != 0xE9) {
if(tcp->peek() != 0xE9) {
log_e("Magic header does not start with 0xE9\n");
_lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED;
http.end();
return HTTP_UPDATE_FAILED;
}
/* To do
uint32_t bin_flash_size = ESP.magicFlashChipSize((buf[3] & 0xf0) >> 4);
// check if new bin fits to SPI flash
if(bin_flash_size > ESP.getFlashChipRealSize()) {
log_e("New binary does not fit SPI Flash size\n");
_lastError = HTTP_UE_BIN_FOR_WRONG_FLASH;
http.end();
return HTTP_UPDATE_FAILED;
}
*/
}
if(runUpdate(*tcp, len, http.header("x-MD5"), command)) {
ret = HTTP_UPDATE_OK;
log_d("Update ok\n");
http.end();
if(_rebootOnUpdate && !spiffs) {
ESP.restart();
}
} else {
ret = HTTP_UPDATE_FAILED;
log_e("Update failed\n");
}
}
} else {
_lastError = HTTP_UE_SERVER_NOT_REPORT_SIZE;
ret = HTTP_UPDATE_FAILED;
log_e("Content-Length was 0 or wasn't set by Server?!\n");
}
break;
case HTTP_CODE_NOT_MODIFIED:
///< Not Modified (No updates)
ret = HTTP_UPDATE_NO_UPDATES;
break;
case HTTP_CODE_NOT_FOUND:
_lastError = HTTP_UE_SERVER_FILE_NOT_FOUND;
ret = HTTP_UPDATE_FAILED;
break;
case HTTP_CODE_FORBIDDEN:
_lastError = HTTP_UE_SERVER_FORBIDDEN;
ret = HTTP_UPDATE_FAILED;
break;
default:
_lastError = HTTP_UE_SERVER_WRONG_HTTP_CODE;
ret = HTTP_UPDATE_FAILED;
log_e("HTTP Code is (%d)\n", code);
break;
}
http.end();
return ret;
}
/**
* write Update to flash
* @param in Stream&
* @param size uint32_t
* @param md5 String
* @return true if Update ok
*/
bool HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int command)
{
StreamString error;
if(!Update.begin(size, command, _ledPin, _ledOn)) {
_lastError = Update.getError();
Update.printError(error);
error.trim(); // remove line ending
log_e("Update.begin failed! (%s)\n", error.c_str());
return false;
}
if(md5.length()) {
if(!Update.setMD5(md5.c_str())) {
_lastError = HTTP_UE_SERVER_FAULTY_MD5;
log_e("Update.setMD5 failed! (%s)\n", md5.c_str());
return false;
}
}
if(Update.writeStream(in) != size) {
_lastError = Update.getError();
Update.printError(error);
error.trim(); // remove line ending
log_e("Update.writeStream failed! (%s)\n", error.c_str());
return false;
}
if(!Update.end()) {
_lastError = Update.getError();
Update.printError(error);
error.trim(); // remove line ending
log_e("Update.end failed! (%s)\n", error.c_str());
return false;
}
return true;
}
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_HTTPUPDATE)
HTTPUpdate httpUpdate;
#endif

View File

@ -0,0 +1,100 @@
/**
*
* @file HTTPUpdate.h based on ESP8266HTTPUpdate.h
* @date 16.10.2018
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is part of the ESP32 Http Updater.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef ___HTTP_UPDATE_H___
#define ___HTTP_UPDATE_H___
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>
#include <HTTPClient.h>
#include <Update.h>
/// note we use HTTP client errors too so we start at 100
#define HTTP_UE_TOO_LESS_SPACE (-100)
#define HTTP_UE_SERVER_NOT_REPORT_SIZE (-101)
#define HTTP_UE_SERVER_FILE_NOT_FOUND (-102)
#define HTTP_UE_SERVER_FORBIDDEN (-103)
#define HTTP_UE_SERVER_WRONG_HTTP_CODE (-104)
#define HTTP_UE_SERVER_FAULTY_MD5 (-105)
#define HTTP_UE_BIN_VERIFY_HEADER_FAILED (-106)
#define HTTP_UE_BIN_FOR_WRONG_FLASH (-107)
enum HTTPUpdateResult {
HTTP_UPDATE_FAILED,
HTTP_UPDATE_NO_UPDATES,
HTTP_UPDATE_OK
};
typedef HTTPUpdateResult t_httpUpdate_return; // backward compatibility
class HTTPUpdate
{
public:
HTTPUpdate(void);
HTTPUpdate(int httpClientTimeout);
~HTTPUpdate(void);
void rebootOnUpdate(bool reboot)
{
_rebootOnUpdate = reboot;
}
void setLedPin(int ledPin = -1, uint8_t ledOn = HIGH)
{
_ledPin = ledPin;
_ledOn = ledOn;
}
t_httpUpdate_return update(WiFiClient& client, const String& url, const String& currentVersion = "");
t_httpUpdate_return update(WiFiClient& client, const String& host, uint16_t port, const String& uri = "/",
const String& currentVersion = "");
t_httpUpdate_return updateSpiffs(WiFiClient& client, const String& url, const String& currentVersion = "");
int getLastError(void);
String getLastErrorString(void);
protected:
t_httpUpdate_return handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs = false);
bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH);
int _lastError;
bool _rebootOnUpdate = true;
private:
int _httpClientTimeout;
int _ledPin;
uint8_t _ledOn;
};
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_HTTPUPDATE)
extern HTTPUpdate httpUpdate;
#endif
#endif /* ___HTTP_UPDATE_H___ */

View File

@ -468,3 +468,13 @@ size_t Preferences::getBytes(const char* key, void * buf, size_t maxLen){
}
return len;
}
size_t Preferences::freeEntries() {
nvs_stats_t nvs_stats;
esp_err_t err = nvs_get_stats(NULL, &nvs_stats);
if(err){
log_e("Failed to get nvs statistics");
return 0;
}
return nvs_stats.free_entries;
}

View File

@ -64,6 +64,7 @@ class Preferences {
size_t getString(const char* key, char* value, size_t maxLen);
String getString(const char* key, String defaultValue = String());
size_t getBytes(const char* key, void * buf, size_t maxLen);
size_t freeEntries();
};
#endif

View File

@ -50,12 +50,13 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit)
.init = &sdmmc_host_init,
.set_bus_width = &sdmmc_host_set_bus_width,
.get_bus_width = &sdmmc_host_get_slot_width,
.set_bus_ddr_mode = &sdmmc_host_set_bus_ddr_mode,
.set_card_clk = &sdmmc_host_set_card_clk,
.do_transaction = &sdmmc_host_do_transaction,
.deinit = &sdmmc_host_deinit,
.io_int_enable = sdmmc_host_io_int_enable,
.io_int_wait = sdmmc_host_io_int_wait,
.command_timeout_ms = 0,
.io_int_enable = &sdmmc_host_io_int_enable,
.io_int_wait = &sdmmc_host_io_int_wait,
.command_timeout_ms = 0
};
host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
#ifdef BOARD_HAS_1BIT_SDMMC

View File

@ -1,6 +1,11 @@
#include "FS.h"
#include "SPIFFS.h"
/* You only need to format SPIFFS the first time you run a
test or else use the SPIFFS plugin to create a partition
https://github.com/me-no-dev/arduino-esp32fs-plugin */
#define FORMAT_SPIFFS_IF_FAILED true
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\r\n", dirname);
@ -151,7 +156,7 @@ void testFileIO(fs::FS &fs, const char * path){
void setup(){
Serial.begin(115200);
if(!SPIFFS.begin()){
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
Serial.println("SPIFFS Mount Failed");
return;
}

View File

@ -41,7 +41,7 @@ class UpdateClass {
Call this to check the space needed for the update
Will return false if there is not enough space
*/
bool begin(size_t size=UPDATE_SIZE_UNKNOWN, int command = U_FLASH);
bool begin(size_t size=UPDATE_SIZE_UNKNOWN, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW);
/*
Writes a buffer to the flash and increments the address
@ -174,6 +174,9 @@ class UpdateClass {
String _target_md5;
MD5Builder _md5;
int _ledPin;
uint8_t _ledOn;
};
extern UpdateClass Update;

View File

@ -88,6 +88,10 @@ void UpdateClass::_reset() {
_progress = 0;
_size = 0;
_command = U_FLASH;
if(_ledPin != -1) {
digitalWrite(_ledPin, !_ledOn); // off
}
}
bool UpdateClass::canRollBack(){
@ -106,12 +110,15 @@ bool UpdateClass::rollBack(){
return _partitionIsBootable(partition) && !esp_ota_set_boot_partition(partition);
}
bool UpdateClass::begin(size_t size, int command) {
bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
if(_size > 0){
log_w("already running");
return false;
}
_ledPin = ledPin;
_ledOn = !!ledOn; // 0(LOW) or 1(HIGH)
_reset();
_error = 0;
@ -315,16 +322,32 @@ size_t UpdateClass::writeStream(Stream &data) {
if (_progress_callback) {
_progress_callback(0, _size);
}
if(_ledPin != -1) {
pinMode(_ledPin, OUTPUT);
}
while(remaining()) {
toRead = data.readBytes(_buffer + _bufferLen, (SPI_FLASH_SEC_SIZE - _bufferLen));
if(_ledPin != -1) {
digitalWrite(_ledPin, _ledOn); // Switch LED on
}
size_t bytesToRead = SPI_FLASH_SEC_SIZE - _bufferLen;
if(bytesToRead > remaining()) {
bytesToRead = remaining();
}
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead);
if(toRead == 0) { //Timeout
delay(100);
toRead = data.readBytes(_buffer + _bufferLen, (SPI_FLASH_SEC_SIZE - _bufferLen));
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead);
if(toRead == 0) { //Timeout
_abort(UPDATE_ERROR_STREAM);
return written;
}
}
if(_ledPin != -1) {
digitalWrite(_ledPin, !_ledOn); // Switch LED off
}
_bufferLen += toRead;
if((_bufferLen == remaining() || _bufferLen == SPI_FLASH_SEC_SIZE) && !_writeBuffer())
return written;

View File

@ -1,5 +1,5 @@
/*
FSWebServer - Example WebServer with SPIFFS backend for esp8266
FSWebServer - Example WebServer with FS backend for esp8266/esp32
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the WebServer library for Arduino environment.
@ -26,14 +26,21 @@
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <SPIFFS.h>
#define FILESYSTEM SPIFFS
#define FORMAT_FILESYSTEM true
#define DBG_OUTPUT_PORT Serial
#if FILESYSTEM == FFat
#include <FFat.h>
#endif
#if FILESYSTEM == SPIFFS
#include <SPIFFS.h>
#endif
const char* ssid = "wifi-ssid";
const char* password = "wifi-password";
const char* host = "esp32fs";
WebServer server(80);
//holds the current upload
File fsUploadFile;
@ -84,7 +91,7 @@ String getContentType(String filename) {
bool exists(String path){
bool yes = false;
File file = SPIFFS.open(path, "r");
File file = FILESYSTEM.open(path, "r");
if(!file.isDirectory()){
yes = true;
}
@ -103,7 +110,7 @@ bool handleFileRead(String path) {
if (exists(pathWithGz)) {
path += ".gz";
}
File file = SPIFFS.open(path, "r");
File file = FILESYSTEM.open(path, "r");
server.streamFile(file, contentType);
file.close();
return true;
@ -122,7 +129,7 @@ void handleFileUpload() {
filename = "/" + filename;
}
DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename);
fsUploadFile = SPIFFS.open(filename, "w");
fsUploadFile = FILESYSTEM.open(filename, "w");
filename = String();
} else if (upload.status == UPLOAD_FILE_WRITE) {
//DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize);
@ -149,7 +156,7 @@ void handleFileDelete() {
if (!exists(path)) {
return server.send(404, "text/plain", "FileNotFound");
}
SPIFFS.remove(path);
FILESYSTEM.remove(path);
server.send(200, "text/plain", "");
path = String();
}
@ -166,7 +173,7 @@ void handleFileCreate() {
if (exists(path)) {
return server.send(500, "text/plain", "FILE EXISTS");
}
File file = SPIFFS.open(path, "w");
File file = FILESYSTEM.open(path, "w");
if (file) {
file.close();
} else {
@ -186,7 +193,7 @@ void handleFileList() {
DBG_OUTPUT_PORT.println("handleFileList: " + path);
File root = SPIFFS.open(path);
File root = FILESYSTEM.open(path);
path = String();
String output = "[";
@ -212,9 +219,10 @@ void setup(void) {
DBG_OUTPUT_PORT.begin(115200);
DBG_OUTPUT_PORT.print("\n");
DBG_OUTPUT_PORT.setDebugOutput(true);
SPIFFS.begin();
if (FORMAT_FILESYSTEM) FILESYSTEM.format();
FILESYSTEM.begin();
{
File root = SPIFFS.open("/");
File root = FILESYSTEM.open("/");
File file = root.openNextFile();
while(file){
String fileName = file.name();
@ -267,7 +275,7 @@ void setup(void) {
}, handleFileUpload);
//called when the url is not defined here
//use it to load content from SPIFFS
//use it to load content from FILESYSTEM
server.onNotFound([]() {
if (!handleFileRead(server.uri())) {
server.send(404, "text/plain", "FileNotFound");

View File

@ -0,0 +1,53 @@
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
const char *ssid = "........";
const char *password = "........";
WebServer server(80);
void setup(void) {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp32")) {
Serial.println("MDNS responder started");
}
server.on("/", []() {
server.send(200, "text/plain", "hello from esp32!");
});
server.on("/users/{}", []() {
String user = server.pathArg(0);
server.send(200, "text/plain", "User: '" + user + "'");
});
server.on("/users/{}/devices/{}", []() {
String user = server.pathArg(0);
String device = server.pathArg(1);
server.send(200, "text/plain", "User: '" + user + "' and Device: '" + device + "'");
});
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
}

View File

@ -32,6 +32,10 @@
#define DEBUG_OUTPUT Serial
#endif
#ifndef WEBSERVER_MAX_POST_ARGS
#define WEBSERVER_MAX_POST_ARGS 32
#endif
static const char Content_Type[] PROGMEM = "Content-Type";
static const char filename[] PROGMEM = "filename";
@ -355,14 +359,14 @@ void WebServer::_uploadWriteByte(uint8_t b){
_currentUpload->buf[_currentUpload->currentSize++] = b;
}
uint8_t WebServer::_uploadReadByte(WiFiClient& client){
int WebServer::_uploadReadByte(WiFiClient& client){
int res = client.read();
if(res == -1){
while(!client.available() && client.connected())
yield();
delay(2);
res = client.read();
}
return (uint8_t)res;
return res;
}
bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
@ -383,8 +387,9 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
client.readStringUntil('\n');
//start reading the form
if (line == ("--"+boundary)){
RequestArgument* postArgs = new RequestArgument[32];
int postArgsLen = 0;
if(_postArgs) delete[] _postArgs;
_postArgs = new RequestArgument[WEBSERVER_MAX_POST_ARGS];
_postArgsLen = 0;
while(1){
String argName;
String argValue;
@ -445,7 +450,7 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
DEBUG_OUTPUT.println();
#endif
RequestArgument& arg = postArgs[postArgsLen++];
RequestArgument& arg = _postArgs[_postArgsLen++];
arg.key = argName;
arg.value = argValue;
@ -472,19 +477,20 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
if(_currentHandler && _currentHandler->canUpload(_currentUri))
_currentHandler->upload(*this, _currentUri, *_currentUpload);
_currentUpload->status = UPLOAD_FILE_WRITE;
uint8_t argByte = _uploadReadByte(client);
int argByte = _uploadReadByte(client);
readfile:
while(argByte != 0x0D){
if (!client.connected()) return _parseFormUploadAborted();
_uploadWriteByte(argByte);
argByte = _uploadReadByte(client);
if(argByte < 0) return _parseFormUploadAborted();
_uploadWriteByte(argByte);
argByte = _uploadReadByte(client);
}
argByte = _uploadReadByte(client);
if (!client.connected()) return _parseFormUploadAborted();
if(argByte < 0) return _parseFormUploadAborted();
if (argByte == 0x0A){
argByte = _uploadReadByte(client);
if (!client.connected()) return _parseFormUploadAborted();
if(argByte < 0) return _parseFormUploadAborted();
if ((char)argByte != '-'){
//continue reading the file
_uploadWriteByte(0x0D);
@ -492,7 +498,7 @@ readfile:
goto readfile;
} else {
argByte = _uploadReadByte(client);
if (!client.connected()) return _parseFormUploadAborted();
if(argByte < 0) return _parseFormUploadAborted();
if ((char)argByte != '-'){
//continue reading the file
_uploadWriteByte(0x0D);
@ -552,22 +558,25 @@ readfile:
}
int iarg;
int totalArgs = ((32 - postArgsLen) < _currentArgCount)?(32 - postArgsLen):_currentArgCount;
int totalArgs = ((WEBSERVER_MAX_POST_ARGS - _postArgsLen) < _currentArgCount)?(WEBSERVER_MAX_POST_ARGS - _postArgsLen):_currentArgCount;
for (iarg = 0; iarg < totalArgs; iarg++){
RequestArgument& arg = postArgs[postArgsLen++];
RequestArgument& arg = _postArgs[_postArgsLen++];
arg.key = _currentArgs[iarg].key;
arg.value = _currentArgs[iarg].value;
}
if (_currentArgs) delete[] _currentArgs;
_currentArgs = new RequestArgument[postArgsLen];
for (iarg = 0; iarg < postArgsLen; iarg++){
_currentArgs = new RequestArgument[_postArgsLen];
for (iarg = 0; iarg < _postArgsLen; iarg++){
RequestArgument& arg = _currentArgs[iarg];
arg.key = postArgs[iarg].key;
arg.value = postArgs[iarg].value;
arg.key = _postArgs[iarg].key;
arg.value = _postArgs[iarg].value;
}
_currentArgCount = iarg;
if (postArgs)
delete[] postArgs;
if (_postArgs) {
delete[] _postArgs;
_postArgs=nullptr;
_postArgsLen = 0;
}
return true;
}
#ifdef DEBUG_ESP_HTTP_SERVER

View File

@ -54,6 +54,8 @@ WebServer::WebServer(IPAddress addr, int port)
, _lastHandler(nullptr)
, _currentArgCount(0)
, _currentArgs(nullptr)
, _postArgsLen(0)
, _postArgs(nullptr)
, _headerKeysCount(0)
, _currentHeaders(nullptr)
, _contentLength(0)
@ -72,6 +74,8 @@ WebServer::WebServer(int port)
, _lastHandler(nullptr)
, _currentArgCount(0)
, _currentArgs(nullptr)
, _postArgsLen(0)
, _postArgs(nullptr)
, _headerKeysCount(0)
, _currentHeaders(nullptr)
, _contentLength(0)
@ -94,11 +98,13 @@ WebServer::~WebServer() {
void WebServer::begin() {
close();
_server.begin();
_server.setNoDelay(true);
}
void WebServer::begin(uint16_t port) {
close();
_server.begin(port);
_server.setNoDelay(true);
}
String WebServer::_extractParam(String& authReq,const String& param,const char delimit){
@ -503,8 +509,17 @@ void WebServer::_streamFileCore(const size_t fileSize, const String & fileName,
send(200, contentType, "");
}
String WebServer::pathArg(unsigned int i) {
if (_currentHandler != nullptr)
return _currentHandler->pathArg(i);
return "";
}
String WebServer::arg(String name) {
for (int j = 0; j < _postArgsLen; ++j) {
if ( _postArgs[j].key == name )
return _postArgs[j].value;
}
for (int i = 0; i < _currentArgCount; ++i) {
if ( _currentArgs[i].key == name )
return _currentArgs[i].value;
@ -529,6 +544,10 @@ int WebServer::args() {
}
bool WebServer::hasArg(String name) {
for (int j = 0; j < _postArgsLen; ++j) {
if (_postArgs[j].key == name)
return true;
}
for (int i = 0; i < _currentArgCount; ++i) {
if (_currentArgs[i].key == name)
return true;

View File

@ -34,10 +34,10 @@ enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END,
enum HTTPClientStatus { HC_NONE, HC_WAIT_READ, HC_WAIT_CLOSE };
enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH };
#define HTTP_DOWNLOAD_UNIT_SIZE 1460
#define HTTP_DOWNLOAD_UNIT_SIZE 1436
#ifndef HTTP_UPLOAD_BUFLEN
#define HTTP_UPLOAD_BUFLEN 2048
#define HTTP_UPLOAD_BUFLEN 1436
#endif
#define HTTP_MAX_DATA_WAIT 5000 //ms to wait for the client to send the request
@ -97,6 +97,7 @@ public:
virtual WiFiClient client() { return _currentClient; }
HTTPUpload& upload() { return *_currentUpload; }
String pathArg(unsigned int i); // get request path argument by number
String arg(String name); // get request argument value by name
String arg(int i); // get request argument value by number
String argName(int i); // get request argument name by number
@ -147,7 +148,7 @@ protected:
bool _parseForm(WiFiClient& client, String boundary, uint32_t len);
bool _parseFormUploadAborted();
void _uploadWriteByte(uint8_t b);
uint8_t _uploadReadByte(WiFiClient& client);
int _uploadReadByte(WiFiClient& client);
void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);
bool _collectHeader(const char* headerName, const char* headerValue);
@ -179,6 +180,9 @@ protected:
int _currentArgCount;
RequestArgument* _currentArgs;
int _postArgsLen;
RequestArgument* _postArgs;
std::unique_ptr<HTTPUpload> _currentUpload;
int _headerKeysCount;

View File

@ -1,6 +1,9 @@
#ifndef REQUESTHANDLER_H
#define REQUESTHANDLER_H
#include <vector>
#include <assert.h>
class RequestHandler {
public:
virtual ~RequestHandler() { }
@ -14,6 +17,15 @@ public:
private:
RequestHandler* _next = nullptr;
protected:
std::vector<String> pathArgs;
public:
const String& pathArg(unsigned int i) {
assert(i < pathArgs.size());
return pathArgs[i];
}
};
#endif //REQUESTHANDLER_H

View File

@ -15,16 +15,55 @@ public:
, _uri(uri)
, _method(method)
{
int numParams = 0, start = 0;
do {
start = _uri.indexOf("{}", start);
if (start > 0) {
numParams++;
start += 2;
}
} while (start > 0);
pathArgs.resize(numParams);
}
bool canHandle(HTTPMethod requestMethod, String requestUri) override {
if (_method != HTTP_ANY && _method != requestMethod)
return false;
if (requestUri != _uri)
return false;
if (_uri == requestUri)
return true;
return true;
size_t uriLength = _uri.length();
unsigned int pathArgIndex = 0;
unsigned int requestUriIndex = 0;
for (unsigned int i = 0; i < uriLength; i++, requestUriIndex++) {
char uriChar = _uri[i];
char requestUriChar = requestUri[requestUriIndex];
if (uriChar == requestUriChar)
continue;
if (uriChar != '{')
return false;
i += 2; // index of char after '}'
if (i >= uriLength) {
// there is no char after '}'
pathArgs[pathArgIndex] = requestUri.substring(requestUriIndex);
return pathArgs[pathArgIndex].indexOf("/") == -1; // path argument may not contain a '/'
}
else
{
char charEnd = _uri[i];
int uriIndex = requestUri.indexOf(charEnd, requestUriIndex);
if (uriIndex < 0)
return false;
pathArgs[pathArgIndex] = requestUri.substring(requestUriIndex, uriIndex);
requestUriIndex = (unsigned int) uriIndex;
}
pathArgIndex++;
}
return requestUriIndex >= requestUri.length();
}
bool canUpload(String requestUri) override {

View File

@ -0,0 +1,93 @@
/*
WiFiAccessPoint.ino creates a WiFi access point and provides a web server on it.
Steps:
1. Connect to the access point "yourAp"
2. Point your web browser to http://192.168.4.1/H to turn the LED on or http://192.168.4.1/L to turn it off
OR
Run raw TCP "GET /H" and "GET /L" on PuTTY terminal with 192.168.4.1 as IP address and 80 as port
Created for arduino-esp32 on 04 July, 2018
by Elochukwu Ifediora (fedy0)
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#define LED_BUILTIN 2 // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED
// Set these to your desired credentials.
const char *ssid = "yourAP";
const char *password = "yourPassword";
WiFiServer server(80);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");
// You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.println("Server started");
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}

View File

@ -0,0 +1,42 @@
# ESP32-Eduroam
* Eduroam wifi connection with university login identity
* Working under Eduroam networks worldwide
* Methods: PEAP + MsCHAPv2
# Format
* IDENTITY = youridentity --> if connecting from different university, use youridentity@youruniversity.domain format
* PASSWORD = yourpassword
# Usage
* Change IDENTITY
* Change password
* Upload sketch and enjoy!
* After sucessful assign of IP address, board will connect to HTTP page on internet to verify your authentification
* Board will auto reconnect to Eduroam if it lost connection
# Tested locations
|University|Board|Method|Result|
|-------------|-------------| -----|------|
|Technical University in Košice (Slovakia)|ESP32 Devkit v1|PEAP + MsCHAPv2|Working|
|Technical University in Košice (Slovakia)|ESP32 DevKitc v4|PEAP + MsCHAPv2|Working|
|Slovak Technical University in Bratislava (Slovakia)|ESP32 Devkit v1|PEAP + MsCHAPv2|Working|
|University of Antwerp (Belgium)|Lolin32|PEAP + MsCHAPv2|Working|
|UPV Universitat Politècnica de València (Spain)|ESP32 Devmodule v4|PEAP + MsCHAPv2|Working|
|Local Zeroshell powered network|ESP32 Devkit v1|PEAP + MsCHAPv2|*Not working*|
|Hasselt University (Belgium)|xxx|PEAP + MsCHAPv2|Working with fix sketch|
|Universidad de Granada (Spain)|Lolin D32 Pro|PEAP + MsCHAPv2|Working|
|Universidad de Granada (Spain)|Lolin D32|PEAP + MsCHAPv2|Working|
|Universidade Federal de Santa Catarina (Brazil)|xxx|EAP-TTLS + MsCHAPv2|Working|
|University of Regensburg (Germany)|Lolin32|PEAP + MsCHAPv2|Working|
# Common errors - Switch to Debug mode for Serial monitor prints
|Error|Appearance|Solution|
|-------------|-------------|-------------|
|wifi: Set status to INIT|Frequent|Hold EN button for few seconds|
|HANDSHAKE_TIMEOUT|Rare|Bug was found under Zeroshell RADIUS authentization - Unsucessful connection|
|AUTH_EXPIRE|Common|In the case of weak wifi network signal, this error is quite common, bring your device closer to AP|
|ASSOC_EXPIRE|Rare|-|
# Sucessful connection example
![alt text](https://i.nahraj.to/f/24Kc.png)
# Unsucessful connection example
![alt text](https://camo.githubusercontent.com/87e47d1b27f4e8ace87423e40e8edbce7983bafa/68747470733a2f2f692e6e616872616a2e746f2f662f323435572e504e47)

View File

@ -0,0 +1,69 @@
#include <WiFi.h> //Wifi library
#include "esp_wpa2.h" //wpa2 library for connections to Enterprise networks
#define EAP_IDENTITY "login" //if connecting from another corporation, use identity@organisation.domain in Eduroam
#define EAP_PASSWORD "password" //your Eduroam password
const char* ssid = "eduroam"; // Eduroam SSID
const char* host = "arduino.php5.sk"; //external server domain for HTTP connection after authentification
int counter = 0;
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
Serial.print("Connecting to network: ");
Serial.println(ssid);
WiFi.disconnect(true); //disconnect form wifi to set new wifi connection
WiFi.mode(WIFI_STA); //init wifi mode
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide identity
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username --> identity and username is same
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password
esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT(); //set config settings to default
esp_wifi_sta_wpa2_ent_enable(&config); //set config settings to enable function
WiFi.begin(ssid); //connect to wifi
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
counter++;
if(counter>=60){ //after 30 seconds timeout - reset board
ESP.restart();
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address set: ");
Serial.println(WiFi.localIP()); //print LAN IP
}
void loop() {
if (WiFi.status() == WL_CONNECTED) { //if we are connected to Eduroam network
counter = 0; //reset counter
Serial.println("Wifi is still connected with IP: ");
Serial.println(WiFi.localIP()); //inform user about his IP address
}else if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry
WiFi.begin(ssid);
}
while (WiFi.status() != WL_CONNECTED) { //during lost connection, print dots
delay(500);
Serial.print(".");
counter++;
if(counter>=60){ //30 seconds timeout - reset board
ESP.restart();
}
}
Serial.print("Connecting to website: ");
Serial.println(host);
WiFiClient client;
if (client.connect(host, 80)) {
String url = "/rele/rele1.txt";
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: ESP32\r\n" + "Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println(line);
}else{
Serial.println("Connection unsucessful");
}
}

View File

@ -3,34 +3,35 @@
*
*/
/*
/*
* WiFi Events
SYSTEM_EVENT_WIFI_READY < ESP32 WiFi ready
SYSTEM_EVENT_SCAN_DONE < ESP32 finish scanning AP
SYSTEM_EVENT_STA_START < ESP32 station start
SYSTEM_EVENT_STA_STOP < ESP32 station stop
SYSTEM_EVENT_STA_CONNECTED < ESP32 station connected to AP
SYSTEM_EVENT_STA_DISCONNECTED < ESP32 station disconnected from AP
SYSTEM_EVENT_STA_AUTHMODE_CHANGE < the auth mode of AP connected by ESP32 station changed
SYSTEM_EVENT_STA_GOT_IP < ESP32 station got IP from connected AP
SYSTEM_EVENT_STA_LOST_IP < ESP32 station lost IP and the IP is reset to 0
SYSTEM_EVENT_STA_WPS_ER_SUCCESS < ESP32 station wps succeeds in enrollee mode
SYSTEM_EVENT_STA_WPS_ER_FAILED < ESP32 station wps fails in enrollee mode
SYSTEM_EVENT_STA_WPS_ER_TIMEOUT < ESP32 station wps timeout in enrollee mode
SYSTEM_EVENT_STA_WPS_ER_PIN < ESP32 station wps pin code in enrollee mode
SYSTEM_EVENT_AP_START < ESP32 soft-AP start
SYSTEM_EVENT_AP_STOP < ESP32 soft-AP stop
SYSTEM_EVENT_AP_STACONNECTED < a station connected to ESP32 soft-AP
SYSTEM_EVENT_AP_STADISCONNECTED < a station disconnected from ESP32 soft-AP
SYSTEM_EVENT_AP_PROBEREQRECVED < Receive probe request packet in soft-AP interface
SYSTEM_EVENT_GOT_IP6 < ESP32 station or ap or ethernet interface v6IP addr is preferred
SYSTEM_EVENT_ETH_START < ESP32 ethernet start
SYSTEM_EVENT_ETH_STOP < ESP32 ethernet stop
SYSTEM_EVENT_ETH_CONNECTED < ESP32 ethernet phy link up
SYSTEM_EVENT_ETH_DISCONNECTED < ESP32 ethernet phy link down
SYSTEM_EVENT_ETH_GOT_IP < ESP32 ethernet got IP from connected AP
SYSTEM_EVENT_MAX
0 SYSTEM_EVENT_WIFI_READY < ESP32 WiFi ready
1 SYSTEM_EVENT_SCAN_DONE < ESP32 finish scanning AP
2 SYSTEM_EVENT_STA_START < ESP32 station start
3 SYSTEM_EVENT_STA_STOP < ESP32 station stop
4 SYSTEM_EVENT_STA_CONNECTED < ESP32 station connected to AP
5 SYSTEM_EVENT_STA_DISCONNECTED < ESP32 station disconnected from AP
6 SYSTEM_EVENT_STA_AUTHMODE_CHANGE < the auth mode of AP connected by ESP32 station changed
7 SYSTEM_EVENT_STA_GOT_IP < ESP32 station got IP from connected AP
8 SYSTEM_EVENT_STA_LOST_IP < ESP32 station lost IP and the IP is reset to 0
9 SYSTEM_EVENT_STA_WPS_ER_SUCCESS < ESP32 station wps succeeds in enrollee mode
10 SYSTEM_EVENT_STA_WPS_ER_FAILED < ESP32 station wps fails in enrollee mode
11 SYSTEM_EVENT_STA_WPS_ER_TIMEOUT < ESP32 station wps timeout in enrollee mode
12 SYSTEM_EVENT_STA_WPS_ER_PIN < ESP32 station wps pin code in enrollee mode
13 SYSTEM_EVENT_AP_START < ESP32 soft-AP start
14 SYSTEM_EVENT_AP_STOP < ESP32 soft-AP stop
15 SYSTEM_EVENT_AP_STACONNECTED < a station connected to ESP32 soft-AP
16 SYSTEM_EVENT_AP_STADISCONNECTED < a station disconnected from ESP32 soft-AP
17 SYSTEM_EVENT_AP_STAIPASSIGNED < ESP32 soft-AP assign an IP to a connected station
18 SYSTEM_EVENT_AP_PROBEREQRECVED < Receive probe request packet in soft-AP interface
19 SYSTEM_EVENT_GOT_IP6 < ESP32 station or ap or ethernet interface v6IP addr is preferred
20 SYSTEM_EVENT_ETH_START < ESP32 ethernet start
21 SYSTEM_EVENT_ETH_STOP < ESP32 ethernet stop
22 SYSTEM_EVENT_ETH_CONNECTED < ESP32 ethernet phy link up
23 SYSTEM_EVENT_ETH_DISCONNECTED < ESP32 ethernet phy link down
24 SYSTEM_EVENT_ETH_GOT_IP < ESP32 ethernet got IP from connected AP
25 SYSTEM_EVENT_MAX
*/
#include <WiFi.h>
@ -43,18 +44,84 @@ void WiFiEvent(WiFiEvent_t event)
{
Serial.printf("[WiFi-event] event: %d\n", event);
switch (event)
{
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
break;
}
}
switch (event) {
case SYSTEM_EVENT_WIFI_READY:
Serial.println("WiFi interface ready");
break;
case SYSTEM_EVENT_SCAN_DONE:
Serial.println("Completed scan for access points");
break;
case SYSTEM_EVENT_STA_START:
Serial.println("WiFi client started");
break;
case SYSTEM_EVENT_STA_STOP:
Serial.println("WiFi clients stopped");
break;
case SYSTEM_EVENT_STA_CONNECTED:
Serial.println("Connected to access point");
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("Disconnected from WiFi access point");
break;
case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:
Serial.println("Authentication mode of access point has changed");
break;
case SYSTEM_EVENT_STA_GOT_IP:
Serial.print("Obtained IP address: ");
Serial.println(WiFi.localIP());
break;
case SYSTEM_EVENT_STA_LOST_IP:
Serial.println("Lost IP address and IP address is reset to 0");
break;
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
Serial.println("WiFi Protected Setup (WPS): succeeded in enrollee mode");
break;
case SYSTEM_EVENT_STA_WPS_ER_FAILED:
Serial.println("WiFi Protected Setup (WPS): failed in enrollee mode");
break;
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
Serial.println("WiFi Protected Setup (WPS): timeout in enrollee mode");
break;
case SYSTEM_EVENT_STA_WPS_ER_PIN:
Serial.println("WiFi Protected Setup (WPS): pin code in enrollee mode");
break;
case SYSTEM_EVENT_AP_START:
Serial.println("WiFi access point started");
break;
case SYSTEM_EVENT_AP_STOP:
Serial.println("WiFi access point stopped");
break;
case SYSTEM_EVENT_AP_STACONNECTED:
Serial.println("Client connected");
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
Serial.println("Client disconnected");
break;
case SYSTEM_EVENT_AP_STAIPASSIGNED:
Serial.println("Assigned IP address to client");
break;
case SYSTEM_EVENT_AP_PROBEREQRECVED:
Serial.println("Received probe request");
break;
case SYSTEM_EVENT_GOT_IP6:
Serial.println("IPv6 is preferred");
break;
case SYSTEM_EVENT_ETH_START:
Serial.println("Ethernet started");
break;
case SYSTEM_EVENT_ETH_STOP:
Serial.println("Ethernet stopped");
break;
case SYSTEM_EVENT_ETH_CONNECTED:
Serial.println("Ethernet connected");
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
Serial.println("Ethernet disconnected");
break;
case SYSTEM_EVENT_ETH_GOT_IP:
Serial.println("Obtained IP address");
break;
}}
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info)
{
@ -72,7 +139,7 @@ void setup()
delay(1000);
// Examples of diffrent ways to register wifi events
// Examples of different ways to register wifi events
WiFi.onEvent(WiFiEvent);
WiFi.onEvent(WiFiGotIP, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
WiFiEventId_t eventID = WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info){

View File

@ -31,8 +31,6 @@ const char* password = "**********";
WiFiServer server(23);
WiFiClient serverClients[MAX_SRV_CLIENTS];
HardwareSerial Serial1(2); // UART1/Serial1 pins 16,17
void setup() {
Serial.begin(115200);
Serial.println("\nConnecting");
@ -62,7 +60,7 @@ void setup() {
}
//start UART and the server
Serial1.begin(9600);
Serial2.begin(9600);
server.begin();
server.setNoDelay(true);
@ -98,7 +96,7 @@ void loop() {
if (serverClients[i] && serverClients[i].connected()){
if(serverClients[i].available()){
//get data from the telnet client and push it to the UART
while(serverClients[i].available()) Serial1.write(serverClients[i].read());
while(serverClients[i].available()) Serial2.write(serverClients[i].read());
}
}
else {
@ -108,10 +106,10 @@ void loop() {
}
}
//check UART for data
if(Serial1.available()){
size_t len = Serial1.available();
if(Serial2.available()){
size_t len = Serial2.available();
uint8_t sbuf[len];
Serial1.readBytes(sbuf, len);
Serial2.readBytes(sbuf, len);
//push UART data to all connected telnet clients
for(i = 0; i < MAX_SRV_CLIENTS; i++){
if (serverClients[i] && serverClients[i].connected()){

View File

@ -37,7 +37,7 @@ extern "C" {
#include <esp_wifi.h>
#include <esp_event_loop.h>
#include <lwip/ip_addr.h>
#include "apps/dhcpserver_options.h"
#include "dhcpserver/dhcpserver_options.h"
}
@ -94,25 +94,28 @@ bool WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel,
if(!WiFi.enableAP(true)) {
// enable AP failed
log_e("enable AP first!");
return false;
}
if(!ssid || *ssid == 0 || strlen(ssid) > 31) {
// fail SSID too long or missing!
if(!ssid || *ssid == 0) {
// fail SSID missing
log_e("SSID missing!");
return false;
}
if(passphrase && (strlen(passphrase) > 63 || strlen(passphrase) < 8)) {
// fail passphrase to long or short!
if(passphrase && (strlen(passphrase) > 0 && strlen(passphrase) < 8)) {
// fail passphrase too short
log_e("passphrase too short!");
return false;
}
esp_wifi_start();
wifi_config_t conf;
strcpy(reinterpret_cast<char*>(conf.ap.ssid), ssid);
strlcpy(reinterpret_cast<char*>(conf.ap.ssid), ssid, sizeof(conf.ap.ssid));
conf.ap.channel = channel;
conf.ap.ssid_len = strlen(ssid);
conf.ap.ssid_len = strlen(reinterpret_cast<char *>(conf.ap.ssid));
conf.ap.ssid_hidden = ssid_hidden;
conf.ap.max_connection = max_connection;
conf.ap.beacon_interval = 100;
@ -122,7 +125,7 @@ bool WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel,
*conf.ap.password = 0;
} else {
conf.ap.authmode = WIFI_AUTH_WPA2_PSK;
strcpy(reinterpret_cast<char*>(conf.ap.password), passphrase);
strlcpy(reinterpret_cast<char*>(conf.ap.password), passphrase, sizeof(conf.ap.password));
}
wifi_config_t conf_current;
@ -184,6 +187,11 @@ bool WiFiAPClass::softAPdisconnect(bool wifioff)
{
bool ret;
wifi_config_t conf;
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return ESP_ERR_INVALID_STATE;
}
*conf.ap.ssid = 0;
*conf.ap.password = 0;
conf.ap.authmode = WIFI_AUTH_OPEN; // auth must be open if pass=0
@ -204,6 +212,9 @@ bool WiFiAPClass::softAPdisconnect(bool wifioff)
uint8_t WiFiAPClass::softAPgetStationNum()
{
wifi_sta_list_t clients;
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return 0;
}
if(esp_wifi_ap_get_sta_list(&clients) == ESP_OK) {
return clients.num;
}
@ -217,6 +228,9 @@ uint8_t WiFiAPClass::softAPgetStationNum()
IPAddress WiFiAPClass::softAPIP()
{
tcpip_adapter_ip_info_t ip;
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return IPAddress();
}
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ip);
return IPAddress(ip.ip.addr);
}
@ -229,7 +243,9 @@ IPAddress WiFiAPClass::softAPIP()
*/
uint8_t* WiFiAPClass::softAPmacAddress(uint8_t* mac)
{
esp_wifi_get_mac(WIFI_IF_AP, mac);
if(WiFiGenericClass::getMode() != WIFI_MODE_NULL){
esp_wifi_get_mac(WIFI_IF_AP, mac);
}
return mac;
}
@ -241,6 +257,9 @@ String WiFiAPClass::softAPmacAddress(void)
{
uint8_t mac[6];
char macStr[18] = { 0 };
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return String();
}
esp_wifi_get_mac(WIFI_IF_AP, mac);
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
@ -253,9 +272,12 @@ String WiFiAPClass::softAPmacAddress(void)
*/
const char * WiFiAPClass::softAPgetHostname()
{
const char * hostname;
const char * hostname = NULL;
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return hostname;
}
if(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_AP, &hostname)) {
return NULL;
return hostname;
}
return hostname;
}
@ -267,6 +289,9 @@ const char * WiFiAPClass::softAPgetHostname()
*/
bool WiFiAPClass::softAPsetHostname(const char * hostname)
{
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return false;
}
return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_AP, hostname) == ESP_OK;
}
@ -276,6 +301,9 @@ bool WiFiAPClass::softAPsetHostname(const char * hostname)
*/
bool WiFiAPClass::softAPenableIpV6()
{
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return false;
}
return tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_AP) == ESP_OK;
}
@ -286,6 +314,9 @@ bool WiFiAPClass::softAPenableIpV6()
IPv6Address WiFiAPClass::softAPIPv6()
{
static ip6_addr_t addr;
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return IPv6Address();
}
if(tcpip_adapter_get_ip6_linklocal(TCPIP_ADAPTER_IF_AP, &addr)) {
return IPv6Address();
}

View File

@ -31,6 +31,118 @@
#undef write
#undef read
class WiFiClientRxBuffer {
private:
size_t _size;
uint8_t *_buffer;
size_t _pos;
size_t _fill;
int _fd;
bool _failed;
size_t r_available()
{
if(_fd < 0){
return 0;
}
int count;
int res = lwip_ioctl_r(_fd, FIONREAD, &count);
if(res < 0) {
_failed = true;
return 0;
}
return count;
}
size_t fillBuffer()
{
if(!_buffer){
_buffer = (uint8_t *)malloc(_size);
}
if(_fill && _pos == _fill){
_fill = 0;
_pos = 0;
}
if(!_buffer || _size <= _fill || !r_available()) {
return 0;
}
int res = recv(_fd, _buffer + _fill, _size - _fill, MSG_DONTWAIT);
if(res < 0 && errno != EWOULDBLOCK) {
_failed = true;
return 0;
}
_fill += res;
return res;
}
public:
WiFiClientRxBuffer(int fd, size_t size=1436)
:_size(size)
,_buffer(NULL)
,_pos(0)
,_fill(0)
,_fd(fd)
,_failed(false)
{
//_buffer = (uint8_t *)malloc(_size);
}
~WiFiClientRxBuffer()
{
free(_buffer);
}
bool failed(){
return _failed;
}
int read(uint8_t * dst, size_t len){
if(!dst || !len || (_pos == _fill && !fillBuffer())){
return -1;
}
size_t a = _fill - _pos;
if(len <= a || ((len - a) <= (_size - _fill) && fillBuffer() >= (len - a))){
if(len == 1){
*dst = _buffer[_pos];
} else {
memcpy(dst, _buffer + _pos, len);
}
_pos += len;
return len;
}
size_t left = len;
size_t toRead = a;
uint8_t * buf = dst;
memcpy(buf, _buffer + _pos, toRead);
_pos += toRead;
left -= toRead;
buf += toRead;
while(left){
if(!fillBuffer()){
return len - left;
}
a = _fill - _pos;
toRead = (a > left)?left:a;
memcpy(buf, _buffer + _pos, toRead);
_pos += toRead;
left -= toRead;
buf += toRead;
}
return len;
}
int peek(){
if(_pos == _fill && !fillBuffer()){
return -1;
}
return _buffer[_pos];
}
size_t available(){
return _fill - _pos + r_available();
}
};
class WiFiClientSocketHandle {
private:
int sockfd;
@ -58,6 +170,7 @@ WiFiClient::WiFiClient():_connected(false),next(NULL)
WiFiClient::WiFiClient(int fd):_connected(true),next(NULL)
{
clientSocketHandle.reset(new WiFiClientSocketHandle(fd));
_rxBuffer.reset(new WiFiClientRxBuffer(fd));
}
WiFiClient::~WiFiClient()
@ -69,6 +182,7 @@ WiFiClient & WiFiClient::operator=(const WiFiClient &other)
{
stop();
clientSocketHandle = other.clientSocketHandle;
_rxBuffer = other._rxBuffer;
_connected = other._connected;
return *this;
}
@ -76,6 +190,7 @@ WiFiClient & WiFiClient::operator=(const WiFiClient &other)
void WiFiClient::stop()
{
clientSocketHandle = NULL;
_rxBuffer = NULL;
_connected = false;
}
@ -100,6 +215,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
return 0;
}
clientSocketHandle.reset(new WiFiClientSocketHandle(sockfd));
_rxBuffer.reset(new WiFiClientRxBuffer(sockfd));
_connected = true;
return 1;
}
@ -124,6 +240,7 @@ int WiFiClient::setSocketOption(int option, char* value, size_t len)
int WiFiClient::setTimeout(uint32_t seconds)
{
Client::setTimeout(seconds * 1000);
struct timeval tv;
tv.tv_sec = seconds;
tv.tv_usec = 0;
@ -260,11 +377,9 @@ size_t WiFiClient::write(Stream &stream)
int WiFiClient::read(uint8_t *buf, size_t size)
{
if(!available()) {
return -1;
}
int res = recv(fd(), buf, size, MSG_DONTWAIT);
if(res < 0 && errno != EWOULDBLOCK) {
int res = -1;
res = _rxBuffer->read(buf, size);
if(_rxBuffer->failed()) {
log_e("%d", errno);
stop();
}
@ -273,16 +388,12 @@ int WiFiClient::read(uint8_t *buf, size_t size)
int WiFiClient::peek()
{
if(!available()) {
return -1;
}
uint8_t data = 0;
int res = recv(fd(), &data, 1, MSG_PEEK);
if(res < 0 && errno != EWOULDBLOCK) {
int res = _rxBuffer->peek();
if(_rxBuffer->failed()) {
log_e("%d", errno);
stop();
}
return data;
return res;
}
int WiFiClient::available()
@ -290,14 +401,12 @@ int WiFiClient::available()
if(!_connected) {
return 0;
}
int count;
int res = lwip_ioctl_r(fd(), FIONREAD, &count);
if(res < 0) {
int res = _rxBuffer->available();
if(_rxBuffer->failed()) {
log_e("%d", errno);
stop();
return 0;
}
return count;
return res;
}
// Though flushing means to send all pending data,
@ -330,22 +439,23 @@ uint8_t WiFiClient::connected()
if (_connected) {
uint8_t dummy;
int res = recv(fd(), &dummy, 0, MSG_DONTWAIT);
if (res < 0) {
switch (errno) {
case ENOTCONN:
case EPIPE:
case ECONNRESET:
case ECONNREFUSED:
case ECONNABORTED:
_connected = false;
break;
default:
_connected = true;
break;
}
}
else {
_connected = true;
switch (errno) {
case EWOULDBLOCK:
case ENOENT: //caused by vfs
_connected = true;
break;
case ENOTCONN:
case EPIPE:
case ECONNRESET:
case ECONNREFUSED:
case ECONNABORTED:
_connected = false;
log_d("Disconnected: RES: %d, ERR: %d", res, errno);
break;
default:
log_i("Unexpected: RES: %d, ERR: %d", res, errno);
_connected = true;
break;
}
}
return _connected;

View File

@ -23,16 +23,16 @@
#include "Arduino.h"
#include "Client.h"
#undef min
#undef max
#include <memory>
class WiFiClientSocketHandle;
class WiFiClientRxBuffer;
class WiFiClient : public Client
{
protected:
std::shared_ptr<WiFiClientSocketHandle> clientSocketHandle;
std::shared_ptr<WiFiClientRxBuffer> _rxBuffer;
bool _connected;
public:

View File

@ -46,9 +46,6 @@ extern "C" {
} //extern "C"
#include "esp32-hal-log.h"
#undef min
#undef max
#include <vector>
#include "sdkconfig.h"
@ -156,8 +153,8 @@ static bool espWiFiStart(bool persistent){
return false;
}
_esp_wifi_started = true;
system_event_t event;
event.event_id = SYSTEM_EVENT_WIFI_READY;
system_event_t event;
event.event_id = SYSTEM_EVENT_WIFI_READY;
WiFiGenericClass::_eventCallback(nullptr, &event);
return true;
@ -334,7 +331,7 @@ void WiFiGenericClass::removeEvent(wifi_event_id_t id)
* @param arg
*/
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
const char * system_event_names[] = { "WIFI_READY", "SCAN_DONE", "STA_START", "STA_STOP", "STA_CONNECTED", "STA_DISCONNECTED", "STA_AUTHMODE_CHANGE", "STA_GOT_IP", "STA_LOST_IP", "STA_WPS_ER_SUCCESS", "STA_WPS_ER_FAILED", "STA_WPS_ER_TIMEOUT", "STA_WPS_ER_PIN", "AP_START", "AP_STOP", "AP_STACONNECTED", "AP_STADISCONNECTED", "AP_PROBEREQRECVED", "GOT_IP6", "ETH_START", "ETH_STOP", "ETH_CONNECTED", "ETH_DISCONNECTED", "ETH_GOT_IP", "MAX"};
const char * system_event_names[] = { "WIFI_READY", "SCAN_DONE", "STA_START", "STA_STOP", "STA_CONNECTED", "STA_DISCONNECTED", "STA_AUTHMODE_CHANGE", "STA_GOT_IP", "STA_LOST_IP", "STA_WPS_ER_SUCCESS", "STA_WPS_ER_FAILED", "STA_WPS_ER_TIMEOUT", "STA_WPS_ER_PIN", "AP_START", "AP_STOP", "AP_STACONNECTED", "AP_STADISCONNECTED", "AP_STAIPASSIGNED", "AP_PROBEREQRECVED", "GOT_IP6", "ETH_START", "ETH_STOP", "ETH_CONNECTED", "ETH_DISCONNECTED", "ETH_GOT_IP", "MAX"};
#endif
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_WARN
const char * system_event_reasons[] = { "UNSPECIFIED", "AUTH_EXPIRE", "AUTH_LEAVE", "ASSOC_EXPIRE", "ASSOC_TOOMANY", "NOT_AUTHED", "NOT_ASSOCED", "ASSOC_LEAVE", "ASSOC_NOT_AUTHED", "DISASSOC_PWRCAP_BAD", "DISASSOC_SUPCHAN_BAD", "UNSPECIFIED", "IE_INVALID", "MIC_FAILURE", "4WAY_HANDSHAKE_TIMEOUT", "GROUP_KEY_UPDATE_TIMEOUT", "IE_IN_4WAY_DIFFERS", "GROUP_CIPHER_INVALID", "PAIRWISE_CIPHER_INVALID", "AKMP_INVALID", "UNSUPP_RSN_IE_VERSION", "INVALID_RSN_IE_CAP", "802_1X_AUTH_FAILED", "CIPHER_SUITE_REJECTED", "BEACON_TIMEOUT", "NO_AP_FOUND", "AUTH_FAIL", "ASSOC_FAIL", "HANDSHAKE_TIMEOUT" };
@ -365,17 +362,29 @@ esp_err_t WiFiGenericClass::_eventCallback(void *arg, system_event_t *event)
} else if(reason == WIFI_REASON_BEACON_TIMEOUT || reason == WIFI_REASON_HANDSHAKE_TIMEOUT) {
WiFiSTAClass::_setStatus(WL_CONNECTION_LOST);
} else if(reason == WIFI_REASON_AUTH_EXPIRE) {
if(WiFi.getAutoReconnect()){
WiFi.begin();
}
} else {
WiFiSTAClass::_setStatus(WL_DISCONNECTED);
}
clearStatusBits(STA_CONNECTED_BIT | STA_HAS_IP_BIT | STA_HAS_IP6_BIT);
if(reason >= WIFI_REASON_BEACON_TIMEOUT && reason != WIFI_REASON_AUTH_FAIL && WiFi.getAutoReconnect()){
if(((reason == WIFI_REASON_AUTH_EXPIRE) ||
(reason >= WIFI_REASON_BEACON_TIMEOUT && reason != WIFI_REASON_AUTH_FAIL)) &&
WiFi.getAutoReconnect())
{
WiFi.enableSTA(false);
WiFi.enableSTA(true);
WiFi.begin();
}
} else if(event->event_id == SYSTEM_EVENT_STA_GOT_IP) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
uint8_t * ip = (uint8_t *)&(event->event_info.got_ip.ip_info.ip.addr);
uint8_t * mask = (uint8_t *)&(event->event_info.got_ip.ip_info.netmask.addr);
uint8_t * gw = (uint8_t *)&(event->event_info.got_ip.ip_info.gw.addr);
log_d("STA IP: %u.%u.%u.%u, MASK: %u.%u.%u.%u, GW: %u.%u.%u.%u",
ip[0], ip[1], ip[2], ip[3],
mask[0], mask[1], mask[2], mask[3],
gw[0], gw[1], gw[2], gw[3]);
#endif
WiFiSTAClass::_setStatus(WL_CONNECTED);
setStatusBits(STA_HAS_IP_BIT | STA_CONNECTED_BIT);
} else if(event->event_id == SYSTEM_EVENT_STA_LOST_IP) {
@ -403,6 +412,15 @@ esp_err_t WiFiGenericClass::_eventCallback(void *arg, system_event_t *event)
} else if(event->event_id == SYSTEM_EVENT_ETH_DISCONNECTED) {
clearStatusBits(ETH_CONNECTED_BIT | ETH_HAS_IP_BIT | ETH_HAS_IP6_BIT);
} else if(event->event_id == SYSTEM_EVENT_ETH_GOT_IP) {
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
uint8_t * ip = (uint8_t *)&(event->event_info.got_ip.ip_info.ip.addr);
uint8_t * mask = (uint8_t *)&(event->event_info.got_ip.ip_info.netmask.addr);
uint8_t * gw = (uint8_t *)&(event->event_info.got_ip.ip_info.gw.addr);
log_d("ETH IP: %u.%u.%u.%u, MASK: %u.%u.%u.%u, GW: %u.%u.%u.%u",
ip[0], ip[1], ip[2], ip[3],
mask[0], mask[1], mask[2], mask[3],
gw[0], gw[1], gw[2], gw[3]);
#endif
setStatusBits(ETH_CONNECTED_BIT | ETH_HAS_IP_BIT);
} else if(event->event_id == SYSTEM_EVENT_GOT_IP6) {
@ -475,7 +493,7 @@ bool WiFiGenericClass::mode(wifi_mode_t m)
} else if(cm && !m){
return espWiFiStop();
}
esp_err_t err;
err = esp_wifi_set_mode(m);
if(err){

View File

@ -60,13 +60,12 @@ uint8_t WiFiMulti::run(uint32_t connectTimeout)
uint8_t bestBSSID[6];
int32_t bestChannel = 0;
DEBUG_WIFI_MULTI("[WIFI] scan done\n");
delay(0);
log_i("[WIFI] scan done");
if(scanResult <= 0) {
DEBUG_WIFI_MULTI("[WIFI] no networks found\n");
log_e("[WIFI] no networks found");
} else {
DEBUG_WIFI_MULTI("[WIFI] %d networks found\n", scanResult);
log_i("[WIFI] %d networks found", scanResult);
for(int8_t i = 0; i < scanResult; ++i) {
String ssid_scan;
@ -96,24 +95,18 @@ uint8_t WiFiMulti::run(uint32_t connectTimeout)
}
if(known) {
DEBUG_WIFI_MULTI(" ---> ");
log_d(" ---> %d: [%d][%02X:%02X:%02X:%02X:%02X:%02X] %s (%d) %c", i, chan_scan, BSSID_scan[0], BSSID_scan[1], BSSID_scan[2], BSSID_scan[3], BSSID_scan[4], BSSID_scan[5], ssid_scan.c_str(), rssi_scan, (sec_scan == WIFI_AUTH_OPEN) ? ' ' : '*');
} else {
DEBUG_WIFI_MULTI(" ");
log_d(" %d: [%d][%02X:%02X:%02X:%02X:%02X:%02X] %s (%d) %c", i, chan_scan, BSSID_scan[0], BSSID_scan[1], BSSID_scan[2], BSSID_scan[3], BSSID_scan[4], BSSID_scan[5], ssid_scan.c_str(), rssi_scan, (sec_scan == WIFI_AUTH_OPEN) ? ' ' : '*');
}
DEBUG_WIFI_MULTI(" %d: [%d][%02X:%02X:%02X:%02X:%02X:%02X] %s (%d) %c\n", i, chan_scan, BSSID_scan[0], BSSID_scan[1], BSSID_scan[2], BSSID_scan[3], BSSID_scan[4], BSSID_scan[5], ssid_scan.c_str(), rssi_scan, (sec_scan == WIFI_AUTH_OPEN) ? ' ' : '*');
delay(0);
}
}
// clean up ram
WiFi.scanDelete();
DEBUG_WIFI_MULTI("\n\n");
delay(0);
if(bestNetwork.ssid) {
DEBUG_WIFI_MULTI("[WIFI] Connecting BSSID: %02X:%02X:%02X:%02X:%02X:%02X SSID: %s Channal: %d (%d)\n", bestBSSID[0], bestBSSID[1], bestBSSID[2], bestBSSID[3], bestBSSID[4], bestBSSID[5], bestNetwork.ssid, bestChannel, bestNetworkDb);
log_i("[WIFI] Connecting BSSID: %02X:%02X:%02X:%02X:%02X:%02X SSID: %s Channal: %d (%d)", bestBSSID[0], bestBSSID[1], bestBSSID[2], bestBSSID[3], bestBSSID[4], bestBSSID[5], bestNetwork.ssid, bestChannel, bestNetworkDb);
WiFi.begin(bestNetwork.ssid, bestNetwork.passphrase, bestChannel, bestBSSID);
status = WiFi.status();
@ -131,31 +124,31 @@ uint8_t WiFiMulti::run(uint32_t connectTimeout)
case 3:
ip = WiFi.localIP();
mac = WiFi.BSSID();
DEBUG_WIFI_MULTI("[WIFI] Connecting done.\n");
DEBUG_WIFI_MULTI("[WIFI] SSID: %s\n", WiFi.SSID());
DEBUG_WIFI_MULTI("[WIFI] IP: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
DEBUG_WIFI_MULTI("[WIFI] MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
DEBUG_WIFI_MULTI("[WIFI] Channel: %d\n", WiFi.channel());
log_i("[WIFI] Connecting done.");
log_d("[WIFI] SSID: %s", WiFi.SSID().c_str());
log_d("[WIFI] IP: %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
log_d("[WIFI] MAC: %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
log_d("[WIFI] Channel: %d", WiFi.channel());
break;
case 1:
DEBUG_WIFI_MULTI("[WIFI] Connecting Failed AP not found.\n");
log_e("[WIFI] Connecting Failed AP not found.");
break;
case 4:
DEBUG_WIFI_MULTI("[WIFI] Connecting Failed.\n");
log_e("[WIFI] Connecting Failed.");
break;
default:
DEBUG_WIFI_MULTI("[WIFI] Connecting Failed (%d).\n", status);
log_e("[WIFI] Connecting Failed (%d).", status);
break;
}
} else {
DEBUG_WIFI_MULTI("[WIFI] no matching wifi found!\n");
log_e("[WIFI] no matching wifi found!");
}
} else {
// start scan
DEBUG_WIFI_MULTI("[WIFI] delete old wifi config...\n");
log_d("[WIFI] delete old wifi config...");
WiFi.disconnect();
DEBUG_WIFI_MULTI("[WIFI] start scan\n");
log_d("[WIFI] start scan");
// scan wifi async mode
WiFi.scanNetworks(true);
}
@ -172,34 +165,34 @@ bool WiFiMulti::APlistAdd(const char* ssid, const char *passphrase)
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
// fail SSID to long or missing!
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] no ssid or ssid to long\n");
log_e("[WIFI][APlistAdd] no ssid or ssid to long");
return false;
}
if(passphrase && strlen(passphrase) > 63) {
// fail passphrase to long!
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] passphrase to long\n");
log_e("[WIFI][APlistAdd] passphrase to long");
return false;
}
newAP.ssid = strdup(ssid);
if(!newAP.ssid) {
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] fail newAP.ssid == 0\n");
log_e("[WIFI][APlistAdd] fail newAP.ssid == 0");
return false;
}
if(passphrase && *passphrase != 0x00) {
newAP.passphrase = strdup(passphrase);
if(!newAP.passphrase) {
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] fail newAP.passphrase == 0\n");
log_e("[WIFI][APlistAdd] fail newAP.passphrase == 0");
free(newAP.ssid);
return false;
}
}
APlist.push_back(newAP);
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] add SSID: %s\n", newAP.ssid);
log_i("[WIFI][APlistAdd] add SSID: %s", newAP.ssid);
return true;
}

View File

@ -27,20 +27,8 @@
#define WIFICLIENTMULTI_H_
#include "WiFi.h"
#undef min
#undef max
#include <vector>
#ifdef DEBUG_ESP_WIFI
#ifdef DEBUG_ESP_PORT
#define DEBUG_WIFI_MULTI(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
#endif
#endif
#ifndef DEBUG_WIFI_MULTI
#define DEBUG_WIFI_MULTI(...)
#endif
typedef struct {
char * ssid;
char * passphrase;

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