Table Of ContentPage 1
Table of Contents
Introduction................................................................................................................................24
Overview....................................................................................................................................25
The ESP8266........................................................................................................................26
Maturity.................................................................................................................................26
The specification...................................................................................................................27
ESP8266 Modules.................................................................................................................28
ESP-12.............................................................................................................................28
ESP-1...............................................................................................................................32
Adafruit HUZZAH..............................................................................................................38
NodeMCU devKit..............................................................................................................38
node.IT (aka ESP-210).....................................................................................................40
SparkFun WiFi Shield – ESP8266....................................................................................40
Espresso Lite....................................................................................................................40
Wemos D1........................................................................................................................41
Oak by digistump..............................................................................................................41
Connecting to the ESP8266.......................................................................................................41
WiFi Theory...............................................................................................................................42
AT Command Programming.......................................................................................................44
Commands............................................................................................................................45
Installing the latest AT command processor..........................................................................51
Assembling circuits....................................................................................................................51
USB to UART converters.......................................................................................................51
Breadboards..........................................................................................................................53
Power....................................................................................................................................54
Multi-meter / Logic probe / Logic Analyzer.............................................................................55
Sundry components..............................................................................................................55
Physical construction.............................................................................................................55
Recommended setup for programming ESP8266.................................................................55
Configuration for flashing the device.....................................................................................58
Programming.............................................................................................................................59
Boot mode.............................................................................................................................59
The ESP8266 SDK................................................................................................................60
Include directories.............................................................................................................60
Compiling..............................................................................................................................61
Loading a program into the ESP8266...............................................................................68
Programming environments..............................................................................................72
Compilation tools..............................................................................................................72
ar..................................................................................................................................72
esptool.py.....................................................................................................................72
esptool-ck.....................................................................................................................74
Page 2
gcc...............................................................................................................................77
gen_appbin.py..............................................................................................................77
make............................................................................................................................78
nodemcu-flasher..........................................................................................................79
nm................................................................................................................................80
objcopy.........................................................................................................................81
objdump.......................................................................................................................81
xxd...............................................................................................................................81
Linking...................................................................................................................................81
Flashing over the air – FOTA.................................................................................................83
Debugging.............................................................................................................................86
Logging to UART1............................................................................................................86
Run a Blinky.....................................................................................................................86
Dumping IP Addresses.....................................................................................................88
Exception handling...........................................................................................................88
Using a debugger (GDB)..................................................................................................90
Debugging and testing TCP and UDP connections...........................................................90
Android – Socket Protocol............................................................................................90
Android – UDP Sender/Receiver..................................................................................90
Windows – Hercules....................................................................................................90
Curl..............................................................................................................................91
Eclipse – TCP/MON.....................................................................................................91
httpbin.org....................................................................................................................93
Architecture...........................................................................................................................93
Custom programs.............................................................................................................93
WiFi at startup.......................................................................................................................94
Working with WiFi..................................................................................................................94
Scanning for access points...............................................................................................94
Defining the operating mode.............................................................................................95
Handling WiFi events........................................................................................................96
Station configuration.........................................................................................................97
Connecting to an access point..........................................................................................98
Control and data flows when connecting as a station.......................................................99
Being an access point.......................................................................................................99
The DHCP server...........................................................................................................101
Current IP Address, netmask and gateway.....................................................................101
WiFi Protected Setup – WPS..........................................................................................102
Working with TCP/IP...........................................................................................................102
The espconn architecture...............................................................................................103
TCP................................................................................................................................103
Sending and receiving TCP data................................................................................107
Flow control................................................................................................................109
TCP Error handling.....................................................................................................109
UDP................................................................................................................................110
Page 3
Broadcast with UDP...................................................................................................112
Ping request....................................................................................................................113
Name Service.................................................................................................................113
Multicast Domain Name Systems...................................................................................114
Installing Bonjour........................................................................................................115
Working with SNTP.........................................................................................................117
ESP-NOW...........................................................................................................................118
GPIOs..................................................................................................................................119
Pullup and pull down settings.........................................................................................126
GPIO Interrupt handling..................................................................................................126
Expanding the number of available GPIOs.....................................................................127
ESP_PCF8574 C library.............................................................................................131
PCF8574 JavaScript Library......................................................................................132
Working with I2C.................................................................................................................132
Working with SPI – Serial Peripheral Interface....................................................................135
Hardware SPI.................................................................................................................136
The MetalPhreak/ESP8266_SPI_Driver.....................................................................139
Working with serial..............................................................................................................140
Task handling......................................................................................................................141
Timers and time...................................................................................................................142
Working with memory..........................................................................................................143
Working with flash memory.................................................................................................147
Pulse Width Modulation – PWM..........................................................................................149
Analog to digital conversion.................................................................................................150
Sleep modes.......................................................................................................................152
Watchdog timer...................................................................................................................153
Yielding control...............................................................................................................154
Security...............................................................................................................................155
Mapping from Arduino.........................................................................................................155
Spiffs File System................................................................................................................156
Partner TCP/IP APIs................................................................................................................157
TCP/IP Sockets...................................................................................................................158
Handling errors...............................................................................................................158
Sockets – accept()..........................................................................................................161
Sockets – bind()..............................................................................................................162
Sockets – close()............................................................................................................162
Sockets – closesocket()..................................................................................................162
Sockets – connect()........................................................................................................162
Sockets – fcntl()..............................................................................................................162
Sockets – freeaddrinfo()..................................................................................................163
Sockets – getaddrinfo()...................................................................................................163
Sockets – gethostbyname()............................................................................................163
Sockets – getpeername()................................................................................................163
Sockets – getsockname()...............................................................................................163
Page 4
Sockets – getsockopt()...................................................................................................163
Sockets – inet_ntop()......................................................................................................163
Sockets – inet_pton()......................................................................................................163
Sockets – ioctlsocket()....................................................................................................163
Sockets – listen()............................................................................................................163
Sockets – read().............................................................................................................164
Sockets – recv()..............................................................................................................164
Sockets – recvfrom().......................................................................................................164
Sockets – select()...........................................................................................................164
Sockets – send().............................................................................................................164
Sockets – sendto()..........................................................................................................164
Sockets – setsockopt()....................................................................................................164
Sockets – shutdown().....................................................................................................164
Sockets – socket()..........................................................................................................165
Sockets – write().............................................................................................................165
Socket data structures....................................................................................................165
Sockets – struct sockaddr..........................................................................................165
Sockets – struct sockaddr_in.....................................................................................165
Java Sockets.......................................................................................................................166
WebSockets........................................................................................................................169
A WebSocket browser app..............................................................................................169
FreeRTOS WebSocket...................................................................................................170
Mongoose WebSocket....................................................................................................171
Web Servers........................................................................................................................172
Mongoose.......................................................................................................................172
Programming using Eclipse.....................................................................................................173
Installing the Eclipse Serial terminal....................................................................................176
Web development using Eclipse..........................................................................................182
Programming using the Arduino IDE........................................................................................183
Implications of Arduino IDE support.....................................................................................184
Installing the Arduino IDE with ESP8266 support................................................................185
Tips for working in the Arduino environment........................................................................191
Initialize global classes in setup()....................................................................................191
Invoking Espressif SDK API from a sketch......................................................................191
Exception handling.........................................................................................................192
The SPIFFS file system.......................................................................................................192
The mkspiffs command...................................................................................................192
The architecture of the Arduino IDE support........................................................................193
Building ESP Arduino apps using the Eclipse IDE...............................................................200
Reasons to consider using Eclipse over Arduino IDE.....................................................214
Notes on using the Eclipse Arduino package..................................................................215
Arduino ESP Libraries.........................................................................................................216
The WiFi library...............................................................................................................216
WiFi.begin..................................................................................................................216
Page 5
WiFi.beingSmartConfig..............................................................................................217
WiFi.beginWPSConfig................................................................................................217
WiFi.BSSID................................................................................................................217
WiFi.BSSIDstr............................................................................................................217
WiFi channel..............................................................................................................217
WiFi.config.................................................................................................................217
WiFi.disconnect..........................................................................................................218
WiFi.encryptionType...................................................................................................218
WiFi.gatewayIP..........................................................................................................218
WiFi.getNetworkInfo...................................................................................................218
WiFi.hostByName......................................................................................................219
WiFi.hostname...........................................................................................................219
WiFi.isHidden.............................................................................................................219
WiFi.localIP................................................................................................................219
WiFi.macAddress.......................................................................................................219
WiFi.mode..................................................................................................................219
WiFi.printDiag.............................................................................................................220
WiFi.RSSI...................................................................................................................220
WiFi.scanComplete....................................................................................................220
WiFi.scanDelete.........................................................................................................221
WiFi.scanNetworks.....................................................................................................221
WiFi.smartConfigDone...............................................................................................221
WiFi.softAP................................................................................................................221
WiFi.softAPConfig......................................................................................................222
WiFi.softAPdisconnect...............................................................................................222
WiFi.softAPmacAddress.............................................................................................222
WiFi.softAPIP.............................................................................................................222
WiFi.SSID...................................................................................................................222
WiFi.status.................................................................................................................222
WiFi.stopSmartConfig................................................................................................223
WiFi.subnetMask........................................................................................................223
WiFi.waitForConnectResult........................................................................................223
WiFiClient.......................................................................................................................223
WiFiClient...................................................................................................................223
WiFiClient.available....................................................................................................223
WiFiClient.connect.....................................................................................................224
WiFiClient.connected.................................................................................................224
WiFiClient.flush..........................................................................................................224
WiFiClient.getNoDelay...............................................................................................224
WiFiClient.peek..........................................................................................................224
WiFiClient.read...........................................................................................................224
WiFiClient.remoteIP...................................................................................................224
WiFiClient.remotePort................................................................................................225
WiFiClient.setLocalPortStart......................................................................................225
Page 6
WiFiClient.setNoDelay...............................................................................................225
WiFiClient.status........................................................................................................225
WiFiClient.stop...........................................................................................................225
WiFiClient.stopAll.......................................................................................................225
WiFiClient.write..........................................................................................................225
WiFiServer......................................................................................................................226
WiFiServer.................................................................................................................226
WiFiServer.available...................................................................................................226
WiFiServer.begin........................................................................................................226
WiFiServer.getNoDelay..............................................................................................226
WiFiServer.hasClient..................................................................................................226
WiFiServer.setNoDelay..............................................................................................226
WiFiServer.status.......................................................................................................226
WiFiServer.write.........................................................................................................226
IPAddress.......................................................................................................................227
ESP8266WebServer.......................................................................................................227
ESP8266WebServer..................................................................................................229
ESP8266WebServer.arg............................................................................................230
ESP8266WebServer.argName...................................................................................230
ESP8266WebServer.args..........................................................................................230
ESP8266WebServer.begin.........................................................................................230
ESP8266WebServer.client.........................................................................................230
ESP8266WebServer.handleClient..............................................................................230
ESP8266WebServer.hasArg......................................................................................230
ESP8266WebServer.method......................................................................................231
ESP8266WebServer.on.............................................................................................231
ESP8266WebServer.onFileUpload............................................................................231
ESP8266WebServer.onNotFound..............................................................................231
ESP8266WebServer.send..........................................................................................232
ESP8266WebServer.sendContent.............................................................................232
ESP8266WebServer.sendHeader..............................................................................232
ESP8266WebServer.setContentLength.....................................................................232
ESP8266WebServer.streamFile.................................................................................232
ESP8266WebServer.upload.......................................................................................232
ESP8266WebServer.uri.............................................................................................232
ESP8266mDNS library...................................................................................................233
MDNS.addService......................................................................................................233
MDNS.begin...............................................................................................................233
MDNS.update.............................................................................................................233
I2C – Wire.......................................................................................................................233
Wire.available.............................................................................................................234
Wire.begin..................................................................................................................234
Wire.beginTransmission.............................................................................................235
Wire.endTransmission................................................................................................235
Page 7
Wire.flush...................................................................................................................235
Wire.onReceive..........................................................................................................235
Wire.onReceiveService..............................................................................................235
Wire.onRequest.........................................................................................................236
Wire.onRequestService..............................................................................................236
Wire.peek...................................................................................................................236
Wire.pins....................................................................................................................236
Wire.read...................................................................................................................236
Wire.requestFrom......................................................................................................237
Wire.setClock.............................................................................................................237
Wire.write...................................................................................................................237
Ticker library...................................................................................................................237
Ticker.........................................................................................................................238
attach.........................................................................................................................238
attach_ms..................................................................................................................238
detach........................................................................................................................238
once...........................................................................................................................239
once_ms....................................................................................................................239
EEPROM library.............................................................................................................239
EEPROM.begin..........................................................................................................239
EEPROM.commit.......................................................................................................239
EEPROM.end.............................................................................................................239
EEPROM.get..............................................................................................................240
EEPROM.getDataPtr..................................................................................................240
EEPROM.put..............................................................................................................240
EEPROM.read...........................................................................................................240
EEPROM.write...........................................................................................................240
SPIFFS...........................................................................................................................240
SPIFFS.begin.............................................................................................................240
SPIFFS.open..............................................................................................................241
SPIFFS.openDir.........................................................................................................241
SPIFFS.remove..........................................................................................................241
SPIFFS.rename.........................................................................................................241
File.available..............................................................................................................241
File.close....................................................................................................................242
File.flush.....................................................................................................................242
File.name...................................................................................................................242
File.peek....................................................................................................................242
File.position................................................................................................................242
File.read.....................................................................................................................242
File.seek.....................................................................................................................242
File.size......................................................................................................................243
File.write.....................................................................................................................243
Dir.fileName................................................................................................................243
Page 8
Dir.next.......................................................................................................................243
Dir.open......................................................................................................................243
Dir.openDir.................................................................................................................243
Dir.remove..................................................................................................................243
Dir.rename..................................................................................................................243
ESP library......................................................................................................................243
ESP.deepSleep..........................................................................................................243
ESP.eraseConfig........................................................................................................244
ESP.getBootMode......................................................................................................244
ESP.getBootVersion...................................................................................................244
ESP.getChipId............................................................................................................244
ESP.getCpuFreqMHz.................................................................................................244
ESP.getCycleCount....................................................................................................244
ESP.getFlashChipId...................................................................................................244
ESP.getFlashChipMode.............................................................................................244
ESP.getFlashChipRealSize........................................................................................244
ESP.getFlashChipSize...............................................................................................244
ESP.getFlashChipSizeByChipId.................................................................................244
ESP.getFlashChipSpeed............................................................................................245
ESP.getFreeHeap.......................................................................................................245
ESP.getFreeSketchSpace..........................................................................................245
ESP.getResetInfo.......................................................................................................245
ESP.getResetInfoPtr...................................................................................................245
ESP.getSdkVersion....................................................................................................245
ESP.getSketchSize.....................................................................................................245
ESP.getVcc................................................................................................................245
ESP.reset...................................................................................................................245
ESP.restart.................................................................................................................245
ESP.updateSketch......................................................................................................246
ESP.wdtDisable..........................................................................................................246
ESP.wdtEnable...........................................................................................................246
ESP.wdtFeed..............................................................................................................246
String library....................................................................................................................247
Constructor.................................................................................................................247
String.c_str.................................................................................................................247
String.reserve.............................................................................................................247
String.length...............................................................................................................247
String.concat..............................................................................................................247
String.equalsIgnoreCase............................................................................................247
String.startsWith.........................................................................................................247
String.endsWith..........................................................................................................248
String.charAt..............................................................................................................248
String.setCharAt.........................................................................................................248
String.getBytes...........................................................................................................248
Page 9
String toCharArray.....................................................................................................248
String.indexOf............................................................................................................248
String.lastIndexOf.......................................................................................................248
String.substring..........................................................................................................248
String.replace.............................................................................................................248
String.remove.............................................................................................................248
String.toLowerCase....................................................................................................248
String.toUpperCase....................................................................................................248
String.trim...................................................................................................................249
String.toInt..................................................................................................................249
String.toFloat..............................................................................................................249
Programming with JavaScript..................................................................................................249
Smart.js...............................................................................................................................250
Smart.js GPIO.................................................................................................................251
Setting up an HTTP server.............................................................................................252
Debugging......................................................................................................................253
Espruino..............................................................................................................................254
Building Espruino-ESP....................................................................................................254
Writing network socket applications using Espruino........................................................254
Writing a REST client using Espruino.........................................................................254
Writing a Web Server using Espruino.........................................................................255
Working with I2C and JavaScript....................................................................................256
Debugging JavaScript.....................................................................................................257
Editing JavaScript...........................................................................................................257
Core JavaScript capabilities............................................................................................258
Running code at intervals...........................................................................................258
Working with GPIO.....................................................................................................259
SPI.............................................................................................................................259
Programming with Lua.............................................................................................................260
ESPlorer IDE.......................................................................................................................260
GPIO with Lua.....................................................................................................................260
WiFi with Lua.......................................................................................................................261
Networking with Lua............................................................................................................261
Programming with Basic..........................................................................................................261
Integration with Web Apps.......................................................................................................261
REST Services....................................................................................................................261
REST protocol................................................................................................................262
ESP8266 as a REST client.............................................................................................262
Making a REST request using Mongoose..................................................................262
ESP8266 as a REST service provider............................................................................262
Tasker..................................................................................................................................263
AutoRemote........................................................................................................................263
DuckDNS............................................................................................................................265
Mobile apps.............................................................................................................................266
Page 10
Description:Introduction. Howdy Folks,. I've been working in the software business for over 30 years but until recently, hadn't been playing directly with Micro Processors. When I bought a Raspberry PI and then an Arduino, I'm afraid I got hooked. In my house I am surrounded by computers of all shapes, sizes a