MyIoTWidget
in a convenient locationAt the top of your sketch, add the following code:
// Base ESP8266
#include <ESP8266WiFi.h>
WiFiClient WIFI_CLIENT;
This will “include” a reference to ESP8266-specific libraries and constants which are necessary for your code to run on an ESP8266. It will also create a global WifiClient which other parts of your code may use to access the network.
Note: Anything that comes after //
is a comment - it will be ignored by Arduino but is helpful to keep around so that you remember what your code is supposed to do.
In the Arduino language, you can define one name to mean something else using #define
. This is typically used to make your code easier to read.
Right after the #include
lines you added earlier, but before setup()
, add the following code:
#define LIGHT_SENSOR A0
#define LED 15
#define BUTTON 4
Here we have included a #define
for each of the pins that we will use on the ESP8266 module - this way we can refer to them by the names we have given above rather than having to remember their pin numbers.
The code within the curly braces of the setup()
function is run once every time the ESP8266 starts up. Let’s change this function to the following:
// This function runs once on startup
void setup() {
// Initialize the serial port
Serial.begin(115200);
// Configure light sensor pin as an input
pinMode(LIGHT_SENSOR, INPUT);
// Configure LED pin as an output
pinMode(LED, OUTPUT);
// Configure BUTTON pin as an input with a pullup
pinMode(BUTTON, INPUT_PULLUP);
}
The code within the curly braces of the loop()
function is run once in a never-ending loop. let’s change this function to the following:
// This function runs over and over again in a continuous loop
void loop() {
// Turn the LED on (HIGH is the voltage level)
digitalWrite(LED, HIGH);
// Wait for 1000 milliseconds
delay(1000);
// Turn the LED off by making the voltage LOW
digitalWrite(LED, LOW);
// Wait another 1000 milliseconds
delay(1000);
// Send a message over the serial port
Serial.println("Finished loop");
}
This is the classic “blink an LED” program taken straight from the Arduino examples, slightly modified to work with the ESP8266 and also exercise the Serial port.
Before we proceed, make sure your code looks like this
NodeMCU 1.0 (ESP-12E Module)
insteadNote: You generally only have to set this up once each time you open a new sketch.
Note: This step is entirely optional - your sketch is recompiled and checked for errors each time it is downloaded. This is just a way to get an preview of any errors without having to download.
COM4
/dev/wchusbserialfd120
Note: You generally only have to do this once each time you open a new sketch. Arduino should remember the port selection even when you unplug and replug the device, but it’s always good to double-check
For the ESP8266 modules, this is generally the trickiest part - be sure to follow these directions in the exact order they are listed, otherwise it will not work.
Flash
on your ESP8266 module while simultaneously plugging it into your computer over USB
Flash
button - the module will remain in download mode until code is downloaded to it or it is unpluggedThis is a very simple sketch, so it’s easy to see if it’s working or not.
115200
Finished loop
messages appearing every two seconds