After success with yesterday’s project, ESP8266 and Using the Arduino IDE, I was ready to try to get the ESP8266 to connect to my WIFI network. That is today’s project.
My hardware setup is exactly the same as yesterday, namely using a ESP-01 mounted on a breadboard adapter.
Serial Port Debugging
Before I could really go any further I needed to determine if Serial.print() works properly for the ESP8266 or if I need to do something else.
It works fine and exactly like it would have for an Arduino – just bring up whichever serial port monitor you prefer and attach to the proper COM port.
There is one minor glitch: When you connect to an Arduino with a serial monitor, it causes DTR to toggle and the Arduino will start. This means when you connect to the Arduino, you get to watch the code start executing from the beginning.
In the case of the ESP8266, this doesn’t work. Between the time the upload completes and you get the serial monitor connected, you may well loose some debugging.
This is a problem for the Teensy and Leonardo as well. Teensy implements Serial.dtr() which you can monitor and Leonardo uses the test if (!Serial) to detect when the monitor connects. I can find nothing like that for the ESP8266.
Not all is lost though. The breadboard adapter has a reset button on it (as do many of the various ESP8266 breakout boards I’ve seen). Once you get the serial monitor running, just manually press the restart button.
WIFI Header File
There is a WiFi.h file in the normal Arduino libraries, BUT you do not use that one. It is for an Arduino WiFi shield, not for the ESP8266. The include file you want is
#include <ESP8266WiFi.h>
I assumed this file would be in the Arduino directory (installed by there by the ESP8266 add-on), but it is not. You will find it in
C:\Users\<user>\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.0.0\libraries
I’ve looked at several of the header files and they are well documented.
You can also find all of the libraries at https://github.com/esp8266/Arduino along with some documentation.
Program/Sketch to Make a Wifi Connection
OK, I had the preliminaries figured out. Time to write some real code. Here is my program.
#include <ESP8266WiFi.h> const int ledPin = 0; // --------------------------------------------------------------------------- void setup() { char pass[] = "1234"; char ssid[] = "AP101"; int status; Serial.begin(9600); pinMode(ledPin, OUTPUT); Serial.print("Trying to connect to "); Serial.println(ssid); // attempt to connect to Wifi network: status = WiFi.begin(ssid, pass); status = WiFi.waitForConnectResult(); if (status != WL_CONNECTED) { Serial.println("Connection Failed"); while (true) {} } Serial.println("Connected."); Serial.print("MAC Addr: "); Serial.println(WiFi.macAddress()); Serial.print("IP Addr: "); Serial.println(WiFi.localIP()); Serial.print("Subnet: "); Serial.println(WiFi.subnetMask()); Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP()); Serial.print("DNS Addr: "); Serial.println(WiFi.dnsIP()); Serial.print("Channel: "); Serial.println(WiFi.channel()); Serial.print("Status: "); Serial.println(WiFi.status()); } // setup // --------------------------------------------------------------------------- void loop() { int k; for (k = 0; k <= 100; k = k + 1) { Serial.println("on"); digitalWrite(ledPin, HIGH); delay(500); Serial.println("off"); digitalWrite(ledPin, LOW); delay(500); } WiFi.disconnect(true); while (true) {} }
As you can see, there isn’t much to it. WiFi.begin specifies the SSID and password. SSID is CASE SIGNIFICANT.
I then simply wait for the status result. If we are connected, I print information about the connection like this:
I left the blinky code in because I will make use of it again down the road.
After I blink 100 times, I close the network connection with WiFi.disconnect(true) which disconnects and turns the radio off.
Note that after I disconnect, I put the ESP8266 in an infinite loop to stop all processing. This has an interesting side effect:
Evidently (and not surprisingly), the ESP8266 implements a watchdog timer. I have not researched this WDT yet, but I am fairly certain you must allow control to return to some unseen routine that manages the WiFi connection. Perhaps I’ll research that a bit as well.
Pingback: ESP8266/Arduino IDE: Communicating with TCP | Big Dan the Blogging Man
Your guess about the watchdog is right; you’re supposed to put in a “delay(1)” every now and then to let other things have a chance to get their work done.
What is the approach for configuring a device using an ESP8266 when the SSID is not know. Very similar to getting an Echo or Echo dot. It is unaware of the network in needs to connect to. Through a button, you put the device into AP mode and connect to it with your phone through their app. Then you set the SSID/Password for the network and it connects. Is this the same approach you would take for making the configuration of the ESP8266 dynamic?
Right after posting this comment, I found this. https://tzapu.com/esp8266-wifi-connection-manager-library-arduino-ide/
explain communication between server client