How does a wifi module work? How to control an LED from your phone? Introduction The esp8266 wifi module is a wifi communication module for Arduino and Raspberry Pi. This module can receive and transmit data via the TX/RX link, and therefore acts as a host for your WiFi applications. This module will enable you to bring wifi to all your electronic projects. It has two modes:Station modeThe esp8266 connects to an existing wifi network (the one created by your router). In station mode, the esp8266 obtains the ip address of the router to which it is connected. With this address, it can configure a web server and provide web pages to all devices connected under the existing Wifi network. Wifi Access Point ModeIn this mode, the esp8266 creates its own wifi network and acts as a router. Several devices, such as your telephone or a computer, can connect to it. However, the wifi access point created by the esp8266 is not connected to the Internet.Finally, you can connect a maximum of 5 devices at the same time to the module. By default, the Wifi module is configured as a wifi access point. You’ll need to specify in a program whether it should connect to a network or act as a wifi access point. These are the esp8266 pins:GND : GroundGPIO2 : Programmable pinGPIO0 : Second programmable pinRX : UART link receiverTX : UART link transmitterCH_PD : To be connected in 3.3VREST : Reset : resets the program. Connect to 3.3V for normal operation and GND for reset.VCC : Supply voltage In our course, the RST pin, GPIO0 and GPIO2 will not be used. Esp8266 Library To run your program, you can use the esp8266 library, which will facilitate module control. It’s a .zip library, so all you need to do is download it and tell the Arduino IDE software where to find it.Next, you need to install the Esp3266 board so that the program can compile and download according to it. To do this, go to tools then board manager and type esp8266 : Once the board has been installed, you must select the Esp8266 card as the card to be programmed for the program to run: You’re now ready to use it. If you’re having trouble installing it, we recommend our course on libraries. Circuit diagram Here’s the circuit diagram we’ll need to run the programs. The highest green pins correspond to the first line of pins on your wifi module. The lowest pin line corresponds to the second line on the wifi module.The circuit does not change for all programs. Disconnect RX and TX terminals before uploading programs! As you can see from the circuit diagram, the RX and TX terminals on the arduino board are connected to the wifi module. The wires connecting terminals RX and TX must be disconnected before uploading a program, and reconnected afterwards if you don’t want any errors when uploading. For more information, see uploading problems. Connect to a Wifi access point We’ll now look at how to connect your esp8266 module to your wifi network. To do this, we’ll use the module as a client, as it will connect to the network and not create one. The diagram linked to the program is the one shown in the previous section. /* This code connects to the wifi network and sends hello and waits for the response */ #include <ESP8266WiFi.h> #ifndef STASSID #define STASSID “your-ssid” // The name of your home wifi network #define STAPSK “your-password” // Your password #endif const char* ssid = STASSID; const char* password = STAPSK; const char* host = “djxmmx.net”; const uint16_t port = 17; void setup() { Serial.begin(115200); // Start serial monitor Serial.println(); Serial.println(); Serial.print("Connect to ”); Serial.println(ssid); WiFi.mode(WIFI_STA); // The esp3266 module is told to connect as a wifi client and not as an access point. WiFi.begin(ssid, password); // Start the connection while (WiFi.status() != WL_CONNECTED) { // Wait until the module is connected delay(500); Serial.print(“.”); } Serial.println(“”); Serial.println(“Wifi connection successful”); Serial.println("Your IP address: ”); Serial.println(WiFi.localIP()); } void loop() { } Scan wifi access points Another way to see our wifi network is to scan all nearby wifi networks. To do this, we’ll use the Wifi scan network function: * This code scans the wifi access points around the module */ #include <ESP8266WiFi.h> void setup() { Serial.begin(115200); // Set the module to wifi station mode WiFi.mode(WIFI_STA); // disconnect the module from any previous connection WiFi.disconnect(); delay(100); } void loop() { String ssid; int32_t rssi; uint8_t encryptionType; uint8_t* bssid; int32_t channel; bool hidden; int scanResult; Serial.println(F(“Starting WiFi scan...”)); scanResult = WiFi.scanNetworks(/*async=*/false, /*hidden=*/true); if (scanResult == 0) { Serial.println(F(“No wifi network found”)); } else if (scanResult > 0) { Serial.printf(PSTR(“%d Here are the wifi access points”), scanResult); for (int8_t i = 0; i < scanResult; i++) { // Loop through all access points found WiFi.getNetworkInfo(i, ssid, encryptionType, rssi, bssid, channel, hidden); Serial.printf(PSTR(“%02d: [CH %02d] [%02X:%02X:%02X:%02X:%02X:%02X] %ddBm %c %c %s\n”), // Display networks according to their syntax i, channel, bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], rssi, (encryptionType == ENC_TYPE_NONE) ? ' ' : '*', hidden ? 'H' : 'V', ssid.c_str()); yield(); } } else { Serial.printf(PSTR(“Problem with wifi scan %d”), scanResult); } // Wait a while before re-scanning delay(5000); } Send a message over the Wifi network We’ll now look at how to connect to a network and send a message over it. We’ll then see how the network responds to our request: /* This code allows you to connect to a wifi network and send Hello World and wait for the response */ #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #ifndef STASSID #define STASSID “your-ssid” // Name of the wifi you want to connect to #define STAPSK “your-password” // The wifi password #endif const char* ssid = STASSID; const char* password = STAPSK; const char* host = “192.168.1.1”; const uint16_t port = 3000; ESP8266WiFiMulti WiFiMulti; void setup() { Serial.begin(115200); // Serial monitor startup // Start WiFi connection WiFi.mode(WIFI_STA); WiFiMulti.addAP(ssid, password); Serial.println(); Serial.println(); Serial.print("Connection in progress... ”); while (WiFiMulti.run() != WL_CONNECTED) { // Wait until connection is established Serial.print(“.”); delay(500); } Serial.println(“”); Serial.println(“Connected to Wifi”); Serial.println("Your IP address: ”); Serial.println(WiFi.localIP()); delay(500); } void loop() { Serial.print("connecting to ”);// Now we connect to send the message Serial.print(host); Serial.print(':'); Serial.println(port); //Create a TCP connection WiFiClient client; if (!client.connect(host, port)) { Serial.println(“Connection attempt failed”); Serial.println(“Wait 5 seconds...”); delay(5000); return; } // This will send a request to the server client.println(“Hello World”); // We wait for the server's response Serial.println("Here's the server's response: ”); String line = client.readStringUntil('\r'); Serial.println(line); Serial.println(“Connection closed”); client.stop(); Serial.println(“Wait 5 seconds...”); delay(5000); } Create a Wifi access point We’ll now look at how to use the esp8266 module as a host. It will take the place of the router, and peripherals will be able to connect to it. However, it will not be connected to the Internet. Here, the ssid and password to connect are :ssid: Arduino_FactoryPassword: Arduino_Factory_12!You can change this as you wish in the program. /* Create a WiFi access point*/ #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #ifndef APSSID #define APSSID “Arduino_Factory” #define APPSK “Arduino_Factory_12!” #endif const char *ssid = APSSID; // The name of the wifi you want to create const char *password = APPSK; // Your wifi password ESP8266WebServer server(80); /* You need to connect to http://192.168.4.1 in your browser to see the wifi access point */ void handleRoot() { server.send(200, “text/html”, “<h1>You're logged in </h1>”); // You'll be sent back logged in once you're on the page. } void setup() { delay(1000); Serial.begin(115200); // Open serial monitor Serial.println(); Serial.print(“Configuring the wifi access point...”); // Configure the wifi access point /* You can remove the password parameter if you want the AP to be open. */ WiFi.softAP(ssid, password); // Associate wifi access point with login and password IPAddress myIP = WiFi.softAPIP(); // Assign an IP address to the WiFi access point Serial.print("IP address of wifi access point: ”); Serial.println(myIP); } void loop() { }