ebook img

The-ESP8266-Book-Sep.. PDF

309 Pages·2015·4.88 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 The-ESP8266-Book-Sep..

Page 1 Table of Contents Introduction................................................................................................................................18 Overview....................................................................................................................................19 The ESP8266........................................................................................................................20 Maturity.................................................................................................................................20 The specification...................................................................................................................21 ESP8266 Modules.................................................................................................................22 ESP-12.............................................................................................................................22 ESP-1...............................................................................................................................26 Adafruit HUZZAH..............................................................................................................32 NodeMCU.........................................................................................................................32 node.IT (aka ESP-210).....................................................................................................32 SparkFun WiFi Shield – ESP8266....................................................................................32 Connecting to the ESP8266.......................................................................................................33 WiFi Theory...............................................................................................................................34 AT Command Programming.......................................................................................................36 Commands............................................................................................................................37 Assembling circuits....................................................................................................................42 USB to UART converters.......................................................................................................43 Breadboards..........................................................................................................................44 Power....................................................................................................................................45 Multi-meter / Logic probe / Logic Analyzer.............................................................................45 Sundry components..............................................................................................................46 Physical construction.............................................................................................................46 Recommended setup for programming ESP8266.................................................................46 Configuration for flashing the device.....................................................................................49 Programming.............................................................................................................................50 Boot mode.............................................................................................................................50 The ESP8266 SDK................................................................................................................51 Include directories.............................................................................................................51 Compiling..............................................................................................................................52 Loading a program into the ESP8266...............................................................................59 Programming environments..............................................................................................63 Compilation tools..............................................................................................................63 ar..................................................................................................................................63 esptool.py.....................................................................................................................63 esptool-ck.....................................................................................................................65 gcc...............................................................................................................................67 gen_appbin.py..............................................................................................................68 make............................................................................................................................69 nm................................................................................................................................69 objcopy.........................................................................................................................70 Page 2 objdump.......................................................................................................................70 xxd...............................................................................................................................70 Linking...................................................................................................................................70 Debugging.............................................................................................................................72 Logging to UART1............................................................................................................72 Run a Blinky.....................................................................................................................72 Dumping IP Addresses.....................................................................................................73 Exception handling...........................................................................................................74 Debugging and testing TCP and UDP connections...........................................................75 Android – Socket Protocol............................................................................................75 Android – UDP Sender/Receiver..................................................................................75 Windows – Hercules....................................................................................................76 Curl..............................................................................................................................76 Architecture...........................................................................................................................76 Custom programs.............................................................................................................76 WiFi at startup.......................................................................................................................77 Working with WiFi..................................................................................................................77 Scanning for access points...............................................................................................77 Defining the operating mode.............................................................................................78 Handling WiFi events........................................................................................................79 Station configuration.........................................................................................................80 Connecting to an access point..........................................................................................81 Control and data flows when connecting as a station.......................................................81 Being an access point.......................................................................................................82 The DHCP server.............................................................................................................83 Current IP Address, netmask and gateway.......................................................................84 WiFi Protected Setup – WPS............................................................................................84 Working with TCP/IP.............................................................................................................85 The ESPConn architecture...............................................................................................85 TCP..................................................................................................................................86 Sending and receiving TCP data..................................................................................90 TCP Error handling.......................................................................................................91 UDP..................................................................................................................................92 Broadcast with UDP.....................................................................................................95 Ping request.....................................................................................................................95 Name Service...................................................................................................................95 Multicast Domain Name Systems.....................................................................................96 Working with SNTP...........................................................................................................96 ESP-NOW.............................................................................................................................97 GPIOs...................................................................................................................................98 Expanding the number of available GPIOs.....................................................................104 ESP_PCF8574 library................................................................................................107 Working with I2C.................................................................................................................108 Working with SPI – Serial Peripheral Interface....................................................................110 Page 3 Working with serial...............................................................................................................112 Task handling.......................................................................................................................113 Timers and time...................................................................................................................114 Working with memory..........................................................................................................114 Pulse Width Modulation – PWM..........................................................................................119 Analog to digital conversion.................................................................................................120 Sleep modes.......................................................................................................................122 Watchdog timer...................................................................................................................123 Security...............................................................................................................................124 Mapping from Arduino.........................................................................................................124 Partner TCP/IP APIs................................................................................................................125 Java Sockets.......................................................................................................................126 WebSockets........................................................................................................................129 Programming using Eclipse.....................................................................................................129 Installing the Eclipse Serial terminal....................................................................................132 Programming using the Arduino IDE........................................................................................139 Implications of Arduino IDE support.....................................................................................140 Installing the Arduino IDE with ESP8266 support................................................................141 Tips for working in the Arduino environment........................................................................147 Initialize global classes in setup()....................................................................................147 Invoking Espressif SDK API from a sketch......................................................................147 Exception handling.........................................................................................................148 The SPIFFS file system.......................................................................................................148 The mkspiffs command...................................................................................................148 The architecture of the Arduino IDE support........................................................................149 Building ESP Arduino apps using the Eclipse IDE...............................................................156 Reasons to consider using Eclipse over Arduino IDE.....................................................170 Notes on using the Eclipse Arduino package..................................................................171 Arduino ESP Libraries.........................................................................................................172 The WiFi library...............................................................................................................172 WiFi.begin..................................................................................................................172 WiFi.beingSmartConfig..............................................................................................173 WiFi.beginWPSConfig................................................................................................173 WiFi.BSSID................................................................................................................173 WiFi.BSSIDstr............................................................................................................173 WiFi channel..............................................................................................................173 WiFi.config.................................................................................................................173 WiFi.disconnect..........................................................................................................174 WiFi.encryptionType...................................................................................................174 WiFi.gatewayIP..........................................................................................................174 WiFi.getNetworkInfo...................................................................................................174 WiFi.hostByName......................................................................................................175 WiFi.hostname...........................................................................................................175 WiFi.isHidden.............................................................................................................175 Page 4 WiFi.localIP................................................................................................................175 WiFi.macAddress.......................................................................................................175 WiFi.mode..................................................................................................................175 WiFi.printDiag.............................................................................................................176 WiFi.RSSI...................................................................................................................176 WiFi.scanComplete....................................................................................................176 WiFi.scanDelete.........................................................................................................177 WiFi.scanNetworks.....................................................................................................177 WiFi.smartConfigDone...............................................................................................177 WiFi.softAP................................................................................................................177 WiFi.softAPConfig......................................................................................................178 WiFi.softAPdisconnect...............................................................................................178 WiFi.softAPmacAddress.............................................................................................178 WiFi.softAPIP.............................................................................................................178 WiFi.SSID...................................................................................................................178 WiFi.status.................................................................................................................178 WiFi.stopSmartConfig................................................................................................179 WiFi.subnetMask........................................................................................................179 WiFi.waitForConnectResult........................................................................................179 WiFiClient.......................................................................................................................179 WiFiClient...................................................................................................................179 WiFiClient.available....................................................................................................179 WiFiClient.connect.....................................................................................................180 WiFiClient.connected.................................................................................................180 WiFiClient.flush..........................................................................................................180 WiFiClient.getNoDelay...............................................................................................180 WiFiClient.peek..........................................................................................................180 WiFiClient.read...........................................................................................................180 WiFiClient.remoteIP...................................................................................................180 WiFiClient.remotePort................................................................................................181 WiFiClient.setLocalPortStart......................................................................................181 WiFiClient.setNoDelay...............................................................................................181 WiFiClient.status........................................................................................................181 WiFiClient.stop...........................................................................................................181 WiFiClient.stopAll.......................................................................................................181 WiFiClient.write..........................................................................................................181 WiFiServer......................................................................................................................182 WiFiServer.................................................................................................................182 WiFiServer.available...................................................................................................182 WiFiServer.begin........................................................................................................182 WiFiServer.getNoDelay..............................................................................................182 WiFiServer.hasClient..................................................................................................182 WiFiServer.setNoDelay..............................................................................................182 WiFiServer.status.......................................................................................................182 Page 5 WiFiServer.write.........................................................................................................182 IPAddress.......................................................................................................................183 ESP8266WebServer.......................................................................................................183 ESP8266WebServer..................................................................................................185 ESP8266WebServer.arg............................................................................................186 ESP8266WebServer.argName...................................................................................186 ESP8266WebServer.args..........................................................................................186 ESP8266WebServer.begin.........................................................................................186 ESP8266WebServer.client.........................................................................................186 ESP8266WebServer.handleClient..............................................................................186 ESP8266WebServer.hasArg......................................................................................186 ESP8266WebServer.method......................................................................................187 ESP8266WebServer.on.............................................................................................187 ESP8266WebServer.onFileUpload............................................................................187 ESP8266WebServer.onNotFound..............................................................................187 ESP8266WebServer.send..........................................................................................188 ESP8266WebServer.sendContent.............................................................................188 ESP8266WebServer.sendHeader..............................................................................188 ESP8266WebServer.setContentLength.....................................................................188 ESP8266WebServer.streamFile.................................................................................188 ESP8266WebServer.upload.......................................................................................188 ESP8266WebServer.uri.............................................................................................188 ESP8266mDNS library...................................................................................................189 MDNS.addService......................................................................................................189 MDNS.begin...............................................................................................................189 MDNS.update.............................................................................................................189 I2C – Wire.......................................................................................................................189 Wire.available.............................................................................................................190 Wire.begin..................................................................................................................190 Wire.beginTransmission.............................................................................................191 Wire.endTransmission................................................................................................191 Wire.flush...................................................................................................................191 Wire.onReceive..........................................................................................................191 Wire.onReceiveService..............................................................................................191 Wire.onRequest.........................................................................................................192 Wire.onRequestService..............................................................................................192 Wire.peek...................................................................................................................192 Wire.pins....................................................................................................................192 Wire.read...................................................................................................................192 Wire.requestFrom......................................................................................................193 Wire.setClock.............................................................................................................193 Wire.write...................................................................................................................193 Ticker library...................................................................................................................193 Ticker.........................................................................................................................194 Page 6 attach.........................................................................................................................194 attach_ms..................................................................................................................194 detach........................................................................................................................194 once...........................................................................................................................195 once_ms....................................................................................................................195 EEPROM library.............................................................................................................195 EEPROM.begin..........................................................................................................195 EEPROM.commit.......................................................................................................195 EEPROM.end.............................................................................................................195 EEPROM.get..............................................................................................................196 EEPROM.getDataPtr..................................................................................................196 EEPROM.put..............................................................................................................196 EEPROM.read...........................................................................................................196 EEPROM.write...........................................................................................................196 SPIFFS...........................................................................................................................196 SPIFFS.begin.............................................................................................................196 SPIFFS.open..............................................................................................................197 SPIFFS.openDir.........................................................................................................197 SPIFFS.remove..........................................................................................................197 SPIFFS.rename.........................................................................................................197 File.available..............................................................................................................197 File.close....................................................................................................................198 File.flush.....................................................................................................................198 File.name...................................................................................................................198 File.peek....................................................................................................................198 File.position................................................................................................................198 File.read.....................................................................................................................198 File.seek.....................................................................................................................198 File.size......................................................................................................................199 File.write.....................................................................................................................199 Dir.fileName................................................................................................................199 Dir.next.......................................................................................................................199 Dir.open......................................................................................................................199 Dir.openDir.................................................................................................................199 Dir.remove..................................................................................................................199 Dir.rename..................................................................................................................199 ESP library......................................................................................................................199 ESP.deepSleep..........................................................................................................199 ESP.eraseConfig........................................................................................................200 ESP.getBootMode......................................................................................................200 ESP.getBootVersion...................................................................................................200 ESP.getChipId............................................................................................................200 ESP.getCpuFreqMHz.................................................................................................200 ESP.getCycleCount....................................................................................................200 Page 7 ESP.getFlashChipId...................................................................................................200 ESP.getFlashChipMode.............................................................................................200 ESP.getFlashChipRealSize........................................................................................200 ESP.getFlashChipSize...............................................................................................200 ESP.getFlashChipSizeByChipId.................................................................................200 ESP.getFlashChipSpeed............................................................................................201 ESP.getFreeHeap.......................................................................................................201 ESP.getFreeSketchSpace..........................................................................................201 ESP.getResetInfo.......................................................................................................201 ESP.getResetInfoPtr...................................................................................................201 ESP.getSdkVersion....................................................................................................201 ESP.getSketchSize.....................................................................................................201 ESP.getVcc................................................................................................................201 ESP.reset...................................................................................................................201 ESP.restart.................................................................................................................201 ESP.updateSketch......................................................................................................202 ESP.wdtDisable..........................................................................................................202 ESP.wdtEnable...........................................................................................................202 ESP.wdtFeed..............................................................................................................202 String library....................................................................................................................203 Constructor.................................................................................................................203 String.c_str.................................................................................................................203 String.reserve.............................................................................................................203 String.length...............................................................................................................203 String.concat..............................................................................................................203 String.equalsIgnoreCase............................................................................................203 String.startsWith.........................................................................................................203 String.endsWith..........................................................................................................204 String.charAt..............................................................................................................204 String.setCharAt.........................................................................................................204 String.getBytes...........................................................................................................204 String toCharArray.....................................................................................................204 String.indexOf............................................................................................................204 String.lastIndexOf.......................................................................................................204 String.substring..........................................................................................................204 String.replace.............................................................................................................204 String.remove.............................................................................................................204 String.toLowerCase....................................................................................................204 String.toUpperCase....................................................................................................204 String.trim...................................................................................................................205 String.toInt..................................................................................................................205 String.toFloat..............................................................................................................205 Programming with JavaScript..................................................................................................205 Building Espruino-ESP........................................................................................................206 Page 8 Integration with Web Apps.......................................................................................................206 REST Services....................................................................................................................206 REST protocol................................................................................................................206 ESP8266 as a REST client.............................................................................................206 ESP8266 as a REST service provider............................................................................207 DuckDNS............................................................................................................................207 Sample applications.................................................................................................................208 Sample – Light an LED based on the arrival of a UDP datagram........................................209 Sample – Ultrasonic distance measurement........................................................................211 Sample – WiFi Scanner.......................................................................................................213 Sample – Working with micro SD cards...............................................................................214 Sample – Playing audio from an event................................................................................214 Sample Libraries......................................................................................................................214 Function list.........................................................................................................................214 authModeToString...........................................................................................................214 checkError......................................................................................................................214 delayMilliseconds............................................................................................................215 dumpBSSINFO...............................................................................................................215 dumpEspConn................................................................................................................215 dumpRestart...................................................................................................................215 dumpState......................................................................................................................215 errorToString...................................................................................................................215 eventLogger....................................................................................................................216 eventReasonToString......................................................................................................216 flashSizeAndMapToString...............................................................................................216 setAsGpio.......................................................................................................................216 setupBlink.......................................................................................................................216 toHex..............................................................................................................................216 API Reference.........................................................................................................................218 Timer functions....................................................................................................................218 os_timer_arm..................................................................................................................218 os_timer_disarm.............................................................................................................219 os_timer_setfn................................................................................................................219 system_timer_reinit.........................................................................................................220 os_timer_arm_us............................................................................................................220 hw_timer_init...................................................................................................................220 hw_timer_arm.................................................................................................................220 hw_timer_set_func..........................................................................................................220 System Functions................................................................................................................220 system_restore...............................................................................................................220 system_restart................................................................................................................220 system_init_done_cb......................................................................................................220 system_get_chip_id........................................................................................................221 system_get_vdd33..........................................................................................................221 Page 9 system_adc_read...........................................................................................................221 system_deep_sleep........................................................................................................221 system_deep_sleep_set_option......................................................................................221 system_phys_set_rfoption..............................................................................................221 system_phys_set_max_tpw............................................................................................221 system_phys_set_tpw_via_vdd33..................................................................................222 system_set_os_print.......................................................................................................222 system_print_meminfo....................................................................................................222 system_get_free_heap_size...........................................................................................222 system_os_task..............................................................................................................223 system_os_post..............................................................................................................223 system_get_time.............................................................................................................224 system_get_rtc_time.......................................................................................................224 system_rtc_clock_cali_proc............................................................................................225 system_rtc_mem_write...................................................................................................225 system_rtc_mem_read...................................................................................................225 system_uart_swap..........................................................................................................225 system_uart_de_swap....................................................................................................225 system_get_boot_version...............................................................................................225 system_get_userbin_addr...............................................................................................225 system_get_boot_mode..................................................................................................225 system_restart_enhance.................................................................................................226 system_update_cpu_freq................................................................................................226 system_get_cpu_freq.....................................................................................................226 system_get_flash_size_map...........................................................................................226 system_get_rst_info........................................................................................................227 system_get_sdk_version()..............................................................................................227 system_soft_wdt_feed....................................................................................................227 system_soft_wdt_stop....................................................................................................227 system_soft_wdt_restart.................................................................................................228 os_memset.....................................................................................................................228 os_memcmp...................................................................................................................228 os_memcpy....................................................................................................................228 os_malloc.......................................................................................................................229 os_zalloc.........................................................................................................................229 os_free............................................................................................................................229 os_bzero.........................................................................................................................230 os_delay_us....................................................................................................................230 os_printf..........................................................................................................................230 os_install_putc1..............................................................................................................231 os_random......................................................................................................................231 os_get_random...............................................................................................................231 os_strlen.........................................................................................................................231 os_strcat.........................................................................................................................232 Page 10

Description:
Building ESP Arduino apps using the Eclipse IDE156. Reasons to consider using Eclipse over Arduino IDE.
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.