ebook img

Programming for ESP32 PDF

1063 Pages·2017·14.17 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming for ESP32

Page 1 Table of Contents Introduction................................................................................................................................48 Important Documentation Notes – ESP8266 and ESP32...........................................................49 Overview....................................................................................................................................49 The ESP32............................................................................................................................50 The ESP32 specification.......................................................................................................51 Modules.................................................................................................................................52 ESP-WROOM-32..............................................................................................................52 ESP32-DevKitC................................................................................................................53 ESP-WROVER-KIT..........................................................................................................55 The SparkFun ESP32 thing..............................................................................................60 Connecting to the ESP32...........................................................................................................61 Assembling circuits....................................................................................................................62 USB to UART converters.......................................................................................................63 Breadboards..........................................................................................................................64 Power....................................................................................................................................65 Multi-meter / Logic probe / Logic Analyzer.............................................................................66 Sundry components..............................................................................................................66 Physical construction.............................................................................................................66 Configuration for flashing the device.....................................................................................67 Programming for ESP32............................................................................................................68 Espressif IoT Development framework..................................................................................68 Application entry point......................................................................................................71 How ESP-IDF works.........................................................................................................72 Error handling...................................................................................................................75 The build environment menu configuration.......................................................................75 Adding a custom ESP-IDF component.............................................................................82 Working with memory.......................................................................................................85 Compiling..............................................................................................................................86 Compilation.......................................................................................................................87 Flashing............................................................................................................................89 Loading a program............................................................................................................91 Programming environments..............................................................................................93 Compilation tools..............................................................................................................93 xntensa-esp32-elf-ar....................................................................................................94 esptool.py.....................................................................................................................95 xtensa-esp32-elf-gcc....................................................................................................97 gen_appbin.py..............................................................................................................98 make............................................................................................................................99 xtensa-esp32-elf-strip...................................................................................................99 xtensa-esp32-elf-nm...................................................................................................100 Page 2 xtensa-esp32-elf-objcopy...........................................................................................100 xtensa-esp32-elf-objdump..........................................................................................100 xxd.............................................................................................................................100 Linking.................................................................................................................................101 Sizes of images..............................................................................................................104 Debugging...........................................................................................................................105 ESP-IDF logging.............................................................................................................105 Exception handling.........................................................................................................108 Address to source line....................................................................................................109 Core dump processing....................................................................................................110 Using a debugger (GDB).................................................................................................115 OpenOCD and JTAG......................................................................................................116 Using the ESP-WROVER-KIT for JTAG.....................................................................118 Dumping IP Addresses....................................................................................................119 Debugging and testing TCP and UDP connections.........................................................119 Android – Socket Protocol..........................................................................................119 Android – UDP Sender/Receiver................................................................................119 Windows – Hercules...................................................................................................120 SocketTest..................................................................................................................120 Linux – netcat (nc)......................................................................................................120 Curl............................................................................................................................120 Eclipse – TCP/MON...................................................................................................121 httpbin.org..................................................................................................................123 RequestBin.................................................................................................................123 tcpdump.....................................................................................................................124 ESP-IDF component debugging.....................................................................................124 LWIP..........................................................................................................................124 Run a Blinky....................................................................................................................124 WiFi subsystem........................................................................................................................125 WiFi Theory.........................................................................................................................125 Initializing the WiFi environment..........................................................................................127 Setting the operation mode.................................................................................................127 Scanning for access points..................................................................................................128 Handling WiFi events..........................................................................................................130 Station configuration............................................................................................................137 Starting up the WiFi environment........................................................................................138 Connecting to an access point............................................................................................138 Being an access point.........................................................................................................141 Working with connected stations.........................................................................................145 WiFi at boot time.................................................................................................................145 The DHCP client..................................................................................................................146 The DHCP server................................................................................................................146 Page 3 Current IP Address, netmask and gateway..........................................................................147 WiFi Protected Setup – WPS..............................................................................................147 Designs for bootstrapping WiFi...........................................................................................148 Working with TCP/IP................................................................................................................149 The Lightweight IP Stack – lwip...........................................................................................150 TCP.....................................................................................................................................151 TCP/IP Sockets...................................................................................................................152 Handling errors...............................................................................................................155 Configuration settings.....................................................................................................159 Using select()..................................................................................................................160 Differences from "standard" sockets...............................................................................160 UDP/IP Sockets...................................................................................................................160 TLS, SSL and security.........................................................................................................162 mbedTLS client app structure.........................................................................................164 mbedTLS client example................................................................................................166 mbedTLS server app structure.......................................................................................168 mbedTLS Debugging......................................................................................................170 OpenSSL........................................................................................................................171 RSA encryption/decryption..............................................................................................171 Name Service......................................................................................................................171 Multicast Domain Name Systems........................................................................................172 mDNS API programming.................................................................................................173 Installing Bonjour............................................................................................................174 Avahi...............................................................................................................................177 Working with SNTP.............................................................................................................178 Java Sockets.......................................................................................................................180 Bluetooth..................................................................................................................................183 Bluetooth specification........................................................................................................183 Bluetooth UUIDs.............................................................................................................186 Bluetooth GAP................................................................................................................186 Low level packet.........................................................................................................187 GAP Advertizing data.................................................................................................188 Advertisability – limited and general...........................................................................191 Directed advertising....................................................................................................191 Non-connectable advertising......................................................................................192 Filtering devices.........................................................................................................192 Performing a scan......................................................................................................192 Performing advertising...............................................................................................195 Bluetooth GATT..............................................................................................................196 GATT Characteristic...................................................................................................198 Being a GATT client...................................................................................................199 Being a GATT Server.................................................................................................201 Notifications and indications.......................................................................................201 GATT XML descriptions..............................................................................................202 Page 4 Service Discovery Protocol.............................................................................................202 ESP32 and Bluetooth..........................................................................................................203 GATT Server – Read request..........................................................................................204 Debugging ESP32 Bluetooth..........................................................................................205 Bluetooth C Programming in Linux......................................................................................205 hci_get_route..................................................................................................................205 hci_open_dev.................................................................................................................205 hci_inquiry......................................................................................................................206 hci_read_remote_name..................................................................................................207 str2ba..............................................................................................................................207 ba2str..............................................................................................................................207 Bluetooth programming in Node.js......................................................................................208 Using Noble....................................................................................................................208 Using Bleno....................................................................................................................213 Bluetooth Audio...................................................................................................................214 Bluetooth RFCOMM............................................................................................................215 Bluetooth tools.....................................................................................................................217 l2ping..............................................................................................................................217 rfcomm............................................................................................................................217 bluetoothctl.....................................................................................................................218 hciconfig.........................................................................................................................219 hcidump..........................................................................................................................220 hcitool.............................................................................................................................220 gatttool............................................................................................................................221 Bluetooth examples.............................................................................................................222 The iTag peripheral.........................................................................................................222 Smart Watch / The TW64 Band......................................................................................224 Web Bluetooth.....................................................................................................................226 The Physical Web...............................................................................................................233 BLE beacons.......................................................................................................................233 Hardware interfacing................................................................................................................233 GPIOs.................................................................................................................................234 Pull up and pull down settings........................................................................................236 GPIO Interrupt handling..................................................................................................236 Expanding the number of available GPIOs.....................................................................240 PCF8574....................................................................................................................240 PCF8575....................................................................................................................244 MCP23017.................................................................................................................245 Interrupt Service Routines – ISRs.......................................................................................250 Working with I2C.................................................................................................................252 Using the ESP-IDF I2C driver.........................................................................................254 Common I2C devices......................................................................................................257 Page 5 Working with SPI – Serial Peripheral Interface....................................................................257 Using the ESP-IDF SPI driver.........................................................................................260 The Arduino Hardware Abstraction Layer SPI.................................................................263 Common SPI devices.....................................................................................................265 Working with UART/serial....................................................................................................266 Using the VFS component with serial.............................................................................269 I2S Bus................................................................................................................................269 I2S – Camera..................................................................................................................270 I2S – LCD.......................................................................................................................270 I2S – DMA......................................................................................................................270 RMT – The Remote Peripheral............................................................................................275 Timers and time...................................................................................................................281 LEDC – Pulse Width Modulation – PWM.............................................................................284 Automated PWM fading..................................................................................................288 Analog to digital conversion.................................................................................................289 Sleep modes.......................................................................................................................292 Security...............................................................................................................................294 Working with flash memory.................................................................................................294 Working with RAM memory.................................................................................................295 RAM Utilization...............................................................................................................297 Heap diagnosis / memory leaks......................................................................................298 Heap corruption..........................................................................................................300 Using PSRAM.................................................................................................................303 EFUSE................................................................................................................................305 EFuse #3 – 0x6001a00c.................................................................................................306 Button press detection.........................................................................................................306 GPS....................................................................................................................................308 GPS decoding.................................................................................................................310 Temperature and pressure – BMP180.................................................................................311 Using the Arduino APIs...................................................................................................313 NeoPixels............................................................................................................................313 NeoPixel theory..............................................................................................................313 NeoPixels and the ESP32...............................................................................................316 APA102...........................................................................................................................317 LED 7-Segment displays.....................................................................................................317 MAX7219/MAX7221 – Serial interface, 8-digit, led display drivers..................................318 The U8g2 library..................................................................................................................322 LCD display – Nokia 5110 – PCD8544................................................................................323 OLED 128x32, 128x64 – SSD1306.....................................................................................325 TFT displays........................................................................................................................327 Ambient light level sensor – BH1750FVI.............................................................................328 Ambient light and proximity sensor......................................................................................330 Infrared receivers................................................................................................................331 RFID MFRC522...................................................................................................................333 Page 6 MFRC522 – Low levels...................................................................................................335 Initialization................................................................................................................338 AntennaOn.................................................................................................................339 Cameras..............................................................................................................................339 Ivan's sample..................................................................................................................339 OV7670..........................................................................................................................341 Accelerometer and Gyroscope – MPU-6050 (aka GY-521).................................................341 The math of accelerometers...........................................................................................347 Visualizing orientation.....................................................................................................348 Compass – HMC5883L (aka GY-271) (aka CJ-M49)...........................................................348 Tilt compensation of the compass...................................................................................354 Real time clocks..................................................................................................................355 Servos.................................................................................................................................358 The Mini/Micro SG90......................................................................................................360 Audio...................................................................................................................................360 PCM5102 – I2S DAC......................................................................................................360 Graphic Equalizer...........................................................................................................361 External networking.............................................................................................................364 The nRF24......................................................................................................................364 Using the Arduino APIs...............................................................................................370 Integrating the nRF24 with the ESP32.......................................................................377 Programming using Eclipse.....................................................................................................380 Installing the Eclipse Serial terminal....................................................................................390 Web development using Eclipse..........................................................................................395 Programming using the Arduino IDE........................................................................................396 Mapping from the Arduino to the ESP32.............................................................................397 Implications of Arduino IDE support.....................................................................................398 Installing the Arduino IDE with ESP32 support....................................................................399 Using the Arduino libraries as an ESP-IDF component.......................................................401 Tips for working in the Arduino environment........................................................................402 Initialize global classes in setup()....................................................................................402 Invoking Espressif SDK API from a sketch......................................................................402 Reasons to consider using Eclipse over Arduino IDE.....................................................403 Programming with JavaScript..................................................................................................403 Duktape...............................................................................................................................404 Compiling code...............................................................................................................405 Building for ESP32..........................................................................................................405 Integrating Duktape in an ESP32 application..................................................................405 The Duktape stack..........................................................................................................406 Working with object properties........................................................................................406 Calling C from a JavaScript program..............................................................................406 JerryScript...........................................................................................................................407 Page 7 Platform specific files......................................................................................................407 JerryScript life-cycle........................................................................................................408 Accessing the global environment..................................................................................408 The jerry_value_t............................................................................................................408 Handling errors...............................................................................................................409 Interfacing JerryScript with C..........................................................................................409 IoT.js....................................................................................................................................410 Programming with Python........................................................................................................410 Pycom Micropython.............................................................................................................410 Programming with Lua.............................................................................................................410 Lua-RTOS for ESP32..........................................................................................................410 Integration with Web Apps........................................................................................................411 HTTP Protocol.....................................................................................................................411 HTTP Headers................................................................................................................411 Accept header............................................................................................................411 Authorization header..................................................................................................411 Connection header.....................................................................................................412 Content-Length header..............................................................................................412 Content-Type header..................................................................................................412 Host header................................................................................................................412 User-Agent header.....................................................................................................412 Web Servers........................................................................................................................412 Mongoose networking library..........................................................................................413 Setting up Mongoose on an ESP32...........................................................................416 Sending a request from Mongoose............................................................................417 The Mongoose struct mg_connection........................................................................417 Handling file uploads..................................................................................................417 GoAhead Web Server.....................................................................................................418 JavaScript Webserver.....................................................................................................419 REST Services....................................................................................................................419 REST protocol................................................................................................................419 ESP32 as a REST client.................................................................................................420 Making a REST request using Curl............................................................................420 Making a REST request using Mongoose..................................................................425 ESP32 as a REST service provider................................................................................425 WebSockets........................................................................................................................425 A WebSocket browser app..............................................................................................426 Mongoose WebSocket....................................................................................................428 Other Websocket implementations.................................................................................429 Tasker..................................................................................................................................429 AutoRemote........................................................................................................................429 DuckDNS............................................................................................................................431 Networking protocols...............................................................................................................432 MQTT..................................................................................................................................432 Page 8 Mosquitto MQTT.............................................................................................................434 Installing on Windows.................................................................................................436 Writing ESP32 MQTT clients..........................................................................................438 Using Mongoose as an MQTT client..........................................................................438 Using Espruino as an MQTT client.............................................................................440 Writing non ESP32 MQTT clients...................................................................................440 Eclipse paho...............................................................................................................441 C – Mosquitto client library.........................................................................................442 Node.js JavaScript – MQTT.......................................................................................444 Browser JavaScript – MQTT......................................................................................445 CoAP – Constrained Application Protocol............................................................................447 FTP.....................................................................................................................................449 TFTP...................................................................................................................................449 Telnet...................................................................................................................................450 DNS Protocol......................................................................................................................452 Mobile apps.............................................................................................................................455 Blynk...................................................................................................................................455 Cloud environments.................................................................................................................459 IBM Bluemix........................................................................................................................459 If This Then That – IFTTT....................................................................................................460 Storage programming..............................................................................................................461 Partition table......................................................................................................................461 Non Volatile Storage............................................................................................................465 Virtual File System..............................................................................................................466 VFS Implementations......................................................................................................467 FATFS File System..............................................................................................................468 Spiffs File System................................................................................................................469 Building SPIFFs for the ESP32.......................................................................................471 mkspiffs tool....................................................................................................................471 The ESP File System – EspFs............................................................................................472 SD, MMC and SDIO interfacing...........................................................................................474 ZIP files...............................................................................................................................475 zlib..................................................................................................................................475 miniz...............................................................................................................................475 kuba--/zip........................................................................................................................476 Charting data...........................................................................................................................476 Kst.......................................................................................................................................477 Sample Snippets......................................................................................................................478 Sample applications.................................................................................................................478 Sample – Ultrasonic distance measurement.......................................................................479 Sample – WiFi Scanner.......................................................................................................482 Sample – A changeable mood light.....................................................................................482 Page 9 Using FreeRTOS.....................................................................................................................487 The architecture of a task in FreeRTOS..............................................................................488 Stacks and FreeRTOS tasks...........................................................................................490 Timers in FreeRTOS............................................................................................................493 Blocking and synchronization within FreeRTOS..................................................................493 Semaphores and Mutices within FreeRTOS........................................................................495 Queues within FreeRTOS...................................................................................................496 Ring buffer withing FreeRTOS.............................................................................................497 Working with queue sets.....................................................................................................498 Monitoring capabilities.........................................................................................................499 Running untested functions.................................................................................................500 The Serial AT command processor..........................................................................................500 Mongoose OS..........................................................................................................................501 The Mongoose OS file system............................................................................................503 Setting up Mongoose OS WiFi............................................................................................503 Building a Mongoose OS App..............................................................................................503 AWS IoT..................................................................................................................................503 The ESP-IDF aws_iot component.......................................................................................505 Using the TI CC2650 SensorTag..............................................................................................506 IR Temperature Sensor.......................................................................................................509 Humidity Service.................................................................................................................510 Ambient light sensor............................................................................................................510 Key Press............................................................................................................................510 Developing solutions on Linux..................................................................................................511 Building a Linux environment..........................................................................................512 Hardware architecture..............................................................................................................513 The CPU and cores.............................................................................................................513 Intrinsic data types..............................................................................................................513 Native byte order, endian and network byte order...............................................................513 Memory mapping and address spaces................................................................................515 Reading and writing registers..............................................................................................516 Pads and multiplexing.........................................................................................................517 Register based GPIO..........................................................................................................520 GPIO_OUT_REG...........................................................................................................522 GPIO_OUT_W1TS_REG................................................................................................522 GPIO_OUT_W1TC_REG...............................................................................................522 GPIO_OUT1_REG.........................................................................................................523 GPIO_OUT1_W1TS_REG..............................................................................................523 GPIO_OUT1_W1TC_REG.............................................................................................523 GPIO_ENABLE_REG.....................................................................................................523 GPIO_ENABLE_W1TS_REG.........................................................................................523 GPIO_ENABLE_W1TC_REG.........................................................................................523 GPIO_ENABLE1_REG...................................................................................................523 GPIO_ENABLE1_W1TS_REG.......................................................................................524 Page 10

Description:
This is an extremely small spacing. The fantastic web site called PIGHIXXX provides the most top quality pin-out images I have ever seen. Please visit their site. They have pin-outs for almost every conceivable device I have ever wanted to know about. Here is a schematic of the current device: Page
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.