If you have a long loop in your sketch, you can add a
delay([milliseconds]) call within, to allow the critical background
functions to execute. The ESP8266’s delay() funciton, while of course
delaying for a set number of milliseconds, also makes a quick call to the
background functions.
The amazing creators of the ESP8266 Arduino libraries also implemented a
yield() function, which calls on the background functions to allow them to
do their thing. As an example, if your sketch is waiting for someone to press
a button attached to pin 12, creating a loop like this will keep the ESP8266
from crashing:
pinMode(12,INPUT_PULLUP);//Setpin12asaninputw/pullu
p
while (digitalRead(12)== HIGH)//Whilepin12isHIGH(nota
ctivated)
yield();//Do(almost)nothing yieldtoallowESP8266
backgroundfunctions
Serial.println("Buttonispressed!");//Printbuttonpressed
message.
ESP8266WiFi Class
This is the ESP8266, so the WiFi class will probably be included in just
about every sketch there is. If you’ve used the Arduino WiFi library before,
the ESP8266 WiFi library will be very similar, there’s just a few key
differences:
•To include the ESP8266 WiFi library call
#include<ESP8266WiFi.h> not <WiFi.h> .
•To connect to a network, like the normal WiFi library, call
WiFi.begin(NetworkSSID,NetworkPassword) . You can also set the
ESP8266 up as a WiFi access point by calling
WiFi.softAP(AP_SSID,AP_Password) .
• To set the ESP8266’s mode, which can be access point (AP), station
(STA), or combo (the ESP8266 can do both at the same time!), call
WiFi.setMode([mode]) with either WIFI_AP , WIFI_STA , or
WIFI_STA_AP as the parameter.
The examples earlier in this tutorial should have demonstrated all of these
differences.
Libraries Available/Not Available and the
Differences
A lot of the core Arduino libraries have been re-written to work for the
ESP8266, including:
•Wire – The ESP8266 should work with any I C sensor you can throw
at it – just use the same Wire API calls you’re used to. There are a
few differences:
◦Pin definition: The ESP2866 doesn’t actually have any
hardware I C pins – those labeled on the Thing are the default,
but you can actually use any two pins as SDA and SCL.
Calling Wire.begin() will assume pins 2 and 14 are SDA and
SCL, but you can manually set them to any other pin by calling
Wire.begin([SDA],[SCL]) .
•SPI – The ESP8266 Thing can control an SPI bus using function
calls made standard by the Arduino SPI library.
◦An additional function to set the frequency –
SPI.setFrequency([frequency]) – is added. You may need
to call that in your setup to slow the clock down from its default
2
2
Page 2
of 2