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