├── .gitattributes ├── .gitignore ├── HTTPSRequest └── HTTPSRequest.ino ├── NTPClient └── NTPClient.ino ├── README.md ├── WiFiAccessPoint └── WiFiAccessPoint.ino ├── WiFiClientBasic └── WiFiClientBasic.ino ├── WiFiClientEvents └── WiFiClientEvents.ino ├── WiFiMulti └── WiFiMulti.ino └── WiFiTelnetToSerial └── WiFiTelnetToSerial.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /HTTPSRequest/HTTPSRequest.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * HTTP over TLS (HTTPS) example sketch 3 | * 4 | * This example demonstrates how to use 5 | * WiFiClientSecure class to access HTTPS API. 6 | * We fetch and display the status of 7 | * esp8266/Arduino project continuous integration 8 | * build. 9 | * 10 | * Created by Ivan Grokhotkov, 2015. 11 | * This example is in public domain. 12 | * 13 | *. Code explained and commented by Sachin Soni 14 | * 15 | * For tutorial of this code, watch out this video 16 | * https://youtu.be/dsmMzS3Qvg0 17 | * 18 | * visit 19 | * http://www.techiesms.com 20 | * for IoT project tutorials. 21 | * 22 | * #techiesms 23 | * explore | learn | share 24 | */ 25 | 26 | #include 27 | #include 28 | 29 | const char* ssid = "SSID"; // Name of the Host 30 | const char* password = "PASSWORD"; // Password of the corresponding Host 31 | 32 | const char* host = "api.github.com"; // Server from which data is to be fetched 33 | const int httpsPort = 443; // Default port for HTTPS 34 | 35 | // Use web browser to view and copy 36 | // SHA1 fingerprint of the certificate 37 | const char* fingerprint = "35 85 74 ef 67 35 a7 ce 40 69 50 f3 c0 f6 80 cf 80 3b 2e 19"; // Fingerprint/Thumbprint for website api.github.com 38 | 39 | void setup() { 40 | Serial.begin(115200); //default baud rate for esp12e 41 | Serial.println(); 42 | Serial.print("connecting to "); 43 | Serial.println(ssid); 44 | WiFi.begin(ssid, password); // establish connection with mentioned Host 45 | while (WiFi.status() != WL_CONNECTED) { 46 | delay(500); 47 | Serial.print("."); 48 | } 49 | Serial.println(""); 50 | Serial.println("WiFi connected"); 51 | Serial.println("IP address: "); 52 | Serial.println(WiFi.localIP()); // Print out the Local IP assigned by the router to ESP8266 53 | 54 | 55 | WiFiClientSecure client; // Use WiFiClientSecure class to create client instance 56 | Serial.print("connecting to "); 57 | Serial.println(host); 58 | if (!client.connect(host, httpsPort)) { // establishing connection with the server(api.github.com) at port 443 59 | Serial.println("connection failed"); 60 | return; // this line will return the function to the starting of void setup() 61 | } 62 | 63 | if (client.verify(fingerprint, host)) { // verfying fingerprint with the server 64 | Serial.println("certificate matches"); 65 | } else { 66 | Serial.println("certificate doesn't match"); 67 | } 68 | 69 | String url = "/repos/esp8266/Arduino/commits/master/status"; //address from which we need to get the data inside the server. 70 | Serial.print("requesting URL: "); 71 | Serial.println(url); 72 | 73 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 74 | "Host: " + host + "\r\n" + 75 | "User-Agent: BuildFailureDetectorESP8266\r\n" + 76 | "Connection: close\r\n\r\n"); 77 | /* 78 | * GET /repos/esp8266/Arduino/commits/master/status HTTP/1.1 79 | * Host : api.github.com 80 | * User-Agent : BuildFailureDetectorESP8266 81 | * Connection : close 82 | */ 83 | 84 | Serial.println("request sent"); 85 | while (client.connected()) { // until the client is connected, read out the response 86 | String line = client.readStringUntil('\n'); 87 | if (line == "\r") { 88 | Serial.println("headers received"); 89 | break; 90 | } 91 | } 92 | String line = client.readStringUntil('\n'); 93 | if (line.startsWith("{\"state\":\"success\"")) { 94 | Serial.println("esp8266/Arduino CI successfull!"); 95 | } else { 96 | Serial.println("esp8266/Arduino CI has failed"); 97 | } 98 | Serial.println("reply was:"); 99 | Serial.println("=========="); 100 | Serial.println(line); 101 | Serial.println("=========="); 102 | Serial.println("closing connection"); 103 | } 104 | 105 | void loop() { 106 | } 107 | -------------------------------------------------------------------------------- /NTPClient/NTPClient.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Udp NTP Client 4 | 5 | Get the time from a Network Time Protocol (NTP) time server 6 | Demonstrates use of UDP sendPacket and ReceivePacket 7 | For more on NTP time servers and the messages needed to communicate with them, 8 | see http://en.wikipedia.org/wiki/Network_Time_Protocol 9 | 10 | created 4 Sep 2010 11 | by Michael Margolis 12 | modified 9 Apr 2012 13 | by Tom Igoe 14 | updated for the ESP8266 12 Apr 2015 15 | by Ivan Grokhotkov 16 | 17 | This code is in the public domain. 18 | 19 | 20 | For tutorial video of this code, watch out this video 21 | https://youtu.be/E2PkqGceTuA 22 | 23 | visit 24 | http://www.techiesms.com 25 | for IoT project tutorials. 26 | 27 | #techiesms 28 | explore | learn | share 29 | 30 | */ 31 | 32 | #include 33 | #include 34 | 35 | char ssid[] = "SSID"; // your network SSID (name) 36 | char pass[] = "PASSWORD"; // your network password 37 | 38 | 39 | unsigned int localPort = 2390; // local port to listen for UDP packets 40 | 41 | /* Don't hardwire the IP address or we won't get the benefits of the pool. 42 | * Lookup the IP address for the host name instead */ 43 | 44 | //IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server --> This will pickup IP server with address 129.6.15.28 from the website time.nist.gov 45 | IPAddress timeServerIP; // time.nist.gov NTP server address --> This will pickup random IP server from the website, time.nist.gov 46 | const char* ntpServerName = "time.nist.gov"; 47 | 48 | const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message 49 | byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets 50 | 51 | // A UDP instance to let us send and receive packets over UDP 52 | WiFiUDP udp; 53 | 54 | void setup() 55 | { 56 | Serial.begin(115200); // Establishing Serial Communication at default baudrate of ESP8266 115200 57 | Serial.println(); 58 | Serial.println(); 59 | 60 | // We start by connecting to a WiFi network 61 | Serial.print("Connecting to "); 62 | Serial.println(ssid); 63 | WiFi.begin(ssid, pass);// Connecting to router with particular host name and password declared above. 64 | 65 | while (WiFi.status() != WL_CONNECTED) { 66 | delay(500); 67 | Serial.print("."); 68 | } 69 | Serial.println(""); 70 | 71 | Serial.println("WiFi connected"); 72 | Serial.println("IP address: "); 73 | Serial.println(WiFi.localIP()); // Printing out Local IP address assigned to ESP8266 by your router. 74 | 75 | Serial.println("Starting UDP"); 76 | udp.begin(localPort); // begining UDP communication at local port 77 | Serial.print("Local port: "); 78 | Serial.println(udp.localPort()); // Printing out the local port at which UDP communication is established 79 | } 80 | 81 | void loop() 82 | { 83 | //get a random server from the pool 84 | WiFi.hostByName(ntpServerName,timeServerIP); // (name of website,IP address) 85 | 86 | sendNTPpacket(timeServerIP); // send an NTP packet to a time server 87 | // wait to see if a reply is available 88 | delay(1000); 89 | 90 | int cb = udp.parsePacket(); // --> returns number of characcter we got in response (length of the string) 91 | if (!cb) { 92 | Serial.println("no packet yet"); 93 | } 94 | else { 95 | Serial.print("packet received, length="); 96 | Serial.println(cb); 97 | // We've received a packet, read the data from it 98 | udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer 99 | 100 | //the timestamp starts at byte 40 of the received packet and is four bytes, 101 | // or two words, long. First, esxtract the two words: 102 | 103 | unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); 104 | unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); 105 | // combine the four bytes (two words) into a long integer 106 | // this is NTP time (seconds since Jan 1 1900): 107 | unsigned long secsSince1900 = highWord << 16 | lowWord; 108 | Serial.print("Seconds since Jan 1 1900 = " ); 109 | Serial.println(secsSince1900); 110 | 111 | 112 | //-------------------------------------------------------------------------------------------- Conversion of NTP time(from 1900) to Unix time(from 1970) 113 | Serial.print("Unix time = "); 114 | // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: 115 | const unsigned long seventyYears = 2208988800UL; // seventyYears = [(17 x 366) + (53 x 365)] x 24 x 60 x 60 = 22,08,988,800 sec 116 | // subtract seventy years: // (leap year) (common year) (hours per day) (minutes per hour) (seconds per minute) (seconds in 70 years) 117 | unsigned long epoch = secsSince1900 - seventyYears; 118 | // print Unix time: 119 | Serial.println(epoch); 120 | 121 | 122 | 123 | 124 | //-------------------------------------------------------------------------------------------- print the hour, minute and second: 125 | Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT) 126 | Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day) --> 24 x 60 x 60 = 86400) 127 | // (epoch % 86400) --> number of seconds from today 12:00 am. 128 | // ((epoch % 86400) / 3600) --> number of hour from today 12:00 am. 129 | Serial.print(':'); 130 | if ( ((epoch % 3600) / 60) < 10 ) { 131 | // In the first 10 minutes of each hour, we'll want a leading '0' 132 | Serial.print('0'); 133 | } 134 | Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute) 135 | // (epoch % 3600) --> number of seconds in this hour. 136 | // ((epoch % 3600) / 60 ) --> number of minute in this hour. 137 | Serial.print(':'); 138 | if ( (epoch % 60) < 10 ) { 139 | // In the first 10 seconds of each minute, we'll want a leading '0' 140 | Serial.print('0'); 141 | } 142 | Serial.println(epoch % 60); // print the second 143 | // (epoch % 60) --> number of seconds 144 | } 145 | // wait ten seconds before asking for the time again 146 | delay(10000); 147 | } 148 | 149 | // send an NTP request to the time server at the given address 150 | unsigned long sendNTPpacket(IPAddress& address) 151 | { 152 | Serial.println("sending NTP packet..."); 153 | // set all bytes in the buffer to 0 154 | memset(packetBuffer, 0, NTP_PACKET_SIZE); 155 | // Initialize values needed to form NTP request 156 | // (see URL above for details on the packets) 157 | packetBuffer[0] = 0b11100011; // LI, Version, Mode 158 | packetBuffer[1] = 0; // Stratum, or type of clock 159 | packetBuffer[2] = 6; // Polling Interval 160 | packetBuffer[3] = 0xEC; // Peer Clock Precision 161 | // 8 bytes of zero for Root Delay & Root Dispersion 162 | packetBuffer[12] = 49; 163 | packetBuffer[13] = 0x4E; 164 | packetBuffer[14] = 49; 165 | packetBuffer[15] = 52; 166 | 167 | // all NTP fields have been given values, now 168 | // you can send a packet requesting a timestamp: 169 | udp.beginPacket(address, 123); //NTP requests are to port 123 170 | udp.write(packetBuffer, NTP_PACKET_SIZE); 171 | udp.endPacket(); 172 | } 173 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266-Series 2 | 3 | This Repo contains the well exaplined, well commented code of ESP8266WiFi examples available in Arduino IDE. 4 | 5 | I have also made a video series in which I have explained all the examples of ESP8266WiFi. For video series visit my YouTube Channel, 6 | 7 | https://www.youtube.com/user/sachinsms1990 8 | -------------------------------------------------------------------------------- /WiFiAccessPoint/WiFiAccessPoint.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Create a WiFi access point and provide a web server on it. 3 | 4 | For tutorial video of this code, watch out this video 5 | https://youtu.be/fcmb_3aBfH4 6 | 7 | visit 8 | http://www.techiesms.com 9 | for IoT project tutorials. 10 | 11 | #techiesms 12 | explore | learn | share 13 | 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | /* Set these to your desired credentials. */ 21 | const char *ssid = "ESPap"; // You can change it according to your ease 22 | const char *password = "thereisnospoon"; // You can change it according to your ease 23 | 24 | ESP8266WebServer server(80); // establishing server at port 80 (HTTP protocol's default port) 25 | 26 | 27 | // Writing a simple HTML page. 28 | char HTML[] = "

For ESP8266 tutorials visit

www.techiesms.com

Go to Page 1 "; // --> Simple HTML page 29 | 30 | 31 | // This function will be called whenever anyone requests 192.168.4.1 within the local area connection of this ESP module. 32 | void handleRoot() 33 | { 34 | server.send(200, "text/html",HTML); 35 | } 36 | 37 | // This function will be called whenever anyone requests 192.168.4.1/1 within the local area connection of this ESP module. 38 | void Page1() 39 | { 40 | server.send(200, "text/html", "

techiesms

explore | learn | share

"); 41 | } 42 | 43 | 44 | // This function will be called whenever anyone requests 192.168.4.1/toggle within the local area connection of this ESP module. 45 | void toggle() 46 | { 47 | digitalWrite(LED_BUILTIN,!digitalRead(LED_BUILTIN)); 48 | server.send(200, "text/html",HTML); 49 | } 50 | 51 | void setup() { 52 | delay(1000); 53 | pinMode(LED_BUILTIN,OUTPUT); 54 | Serial.begin(115200); 55 | Serial.println(); 56 | 57 | Serial.print("Configuring access point..."); 58 | /* You can remove the password parameter if you want the AP to be open. */ 59 | WiFi.softAP(ssid, password); // --> This line will create a WiFi hotspot. 60 | 61 | IPAddress myIP = WiFi.softAPIP(); 62 | Serial.print("AP IP address: "); 63 | Serial.println(myIP); 64 | server.on("/", handleRoot); 65 | server.on("/1",Page1); 66 | server.on("/toggle",toggle); 67 | server.begin(); 68 | Serial.println("HTTP server started"); 69 | } 70 | 71 | void loop() { 72 | server.handleClient(); 73 | } 74 | -------------------------------------------------------------------------------- /WiFiClientBasic/WiFiClientBasic.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * This sketch sends a message to a TCP server 3 | * visit 4 | * http://www.techiesms.com 5 | * for IoT project tutorials. 6 | * 7 | * #techiesms 8 | * explore | learn | share 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | ESP8266WiFiMulti WiFiMulti; 15 | String line=""; 16 | 17 | void setup() { 18 | Serial.begin(115200); 19 | delay(10); 20 | 21 | // We start by connecting to a WiFi network 22 | WiFiMulti.addAP("SmS", "sms123458955"); 23 | 24 | Serial.println(); 25 | Serial.println(); 26 | Serial.print("Wait for WiFi... "); 27 | 28 | while(WiFiMulti.run() != WL_CONNECTED) { 29 | Serial.print("."); 30 | delay(500); 31 | } 32 | 33 | Serial.println(""); 34 | Serial.println("WiFi connected"); 35 | Serial.println("IP address: "); 36 | Serial.println(WiFi.localIP()); 37 | 38 | delay(500); 39 | } 40 | 41 | 42 | void loop() { 43 | const uint16_t port = 80; 44 | const char * host = "api.thingspeak.com"; 45 | 46 | 47 | 48 | Serial.print("connecting to "); 49 | Serial.println(host); 50 | 51 | // Use WiFiClient class to create TCP connections 52 | WiFiClient client; 53 | 54 | if (!client.connect(host, port)) { 55 | Serial.println("connection failed"); 56 | Serial.println("wait 5 sec..."); 57 | delay(5000); 58 | return; 59 | } 60 | 61 | // This will send the request to the server 62 | Serial.println("Connected!!!!!"); 63 | //Serial.print("Send this data to server"); 64 | 65 | String url = "/apps/thinghttp/send_request?api_key=QOSIIMQ9NBH5YQQN"; 66 | Serial.print("requesting URL: "); 67 | Serial.println(url); 68 | 69 | client.print(String("GET ") + url + " HTTP/1.1\r\n" + 70 | "Host: " + host + "\r\n" + 71 | "Cache-Control: no-cache\r\n\r\n"); 72 | 73 | Serial.print(String("GET ") + url + " HTTP/1.1\r\n" + 74 | "Host: " + host + "\r\n" + 75 | "Connection: close\r\n\r\n"); 76 | 77 | /* 78 | * GET /apps/thinghttp/send_request?api_key=QOSIIMQ9NBH5YQQN HTTP/1.1 79 | * Host: api.thingspeak.com 80 | * Connection; close 81 | */ 82 | delay(1000); 83 | 84 | while(client.available()){ 85 | String line = client.readStringUntil('\r'); 86 | Serial.print(line); 87 | } 88 | 89 | //Serial.println(line); 90 | Serial.println("closing connection"); 91 | client.stop(); 92 | 93 | Serial.println("wait 5 sec..."); 94 | delay(5000); 95 | } 96 | 97 | -------------------------------------------------------------------------------- /WiFiClientEvents/WiFiClientEvents.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * This sketch shows the WiFi event usage 3 | * 4 | */ 5 | 6 | #include 7 | 8 | const char* ssid = "SSID"; 9 | const char* password = "PASSWORD"; 10 | 11 | 12 | void WiFiEvent(WiFiEvent_t event) { 13 | Serial.printf("[WiFi-event] event: %d\n", event); 14 | 15 | switch(event) { 16 | case WIFI_EVENT_STAMODE_CONNECTED: 17 | Serial.printf("[WiFi] %d, Connected\n", event); 18 | break; 19 | 20 | case WIFI_EVENT_STAMODE_DISCONNECTED: 21 | Serial.printf("[WiFi] %d, Disconnected - Status %d, %s\n", event, WiFi.status(), connectionStatus( WiFi.status() ).c_str() ); 22 | break; 23 | 24 | case WIFI_EVENT_STAMODE_AUTHMODE_CHANGE: 25 | Serial.printf("[WiFi] %d, AuthMode Change\n", event); 26 | break; 27 | 28 | case WIFI_EVENT_STAMODE_GOT_IP: 29 | Serial.printf("[WiFi] %d, Got IP\n", event); 30 | break; 31 | 32 | case WIFI_EVENT_STAMODE_DHCP_TIMEOUT: 33 | Serial.printf("[WiFi] %d, DHCP Timeout\n", event); 34 | break; 35 | 36 | case WIFI_EVENT_SOFTAPMODE_STACONNECTED: 37 | Serial.printf("[AP] %d, Client Connected\n", event); 38 | break; 39 | 40 | case WIFI_EVENT_SOFTAPMODE_STADISCONNECTED: 41 | Serial.printf("[AP] %d, Client Disconnected\n", event); 42 | break; 43 | 44 | case WIFI_EVENT_SOFTAPMODE_PROBEREQRECVED: 45 | // Serial.printf("[AP] %d, Probe Request Recieved\n", event); 46 | break; 47 | } 48 | } 49 | 50 | String connectionStatus( int which ) 51 | { 52 | switch ( which ) 53 | { 54 | case WL_CONNECTED: 55 | return "Connected"; 56 | break; 57 | 58 | case WL_NO_SSID_AVAIL: 59 | return "Network not availible"; 60 | break; 61 | 62 | case WL_CONNECT_FAILED: 63 | return "Wrong password"; 64 | break; 65 | 66 | case WL_IDLE_STATUS: 67 | return "Idle status"; 68 | break; 69 | 70 | case WL_DISCONNECTED: 71 | return "Disconnected"; 72 | break; 73 | 74 | default: 75 | return "Unknown"; 76 | break; 77 | } 78 | 79 | } 80 | 81 | void setup() { 82 | Serial.begin(115200); 83 | 84 | // delete old config 85 | WiFi.disconnect(true); 86 | 87 | delay(1000); 88 | 89 | WiFi.onEvent(WiFiEvent); 90 | WiFi.mode(WIFI_STA); // Station mode 91 | 92 | WiFi.begin(ssid, password); 93 | 94 | Serial.println(); 95 | Serial.println(); 96 | Serial.println("Wait for WiFi... "); 97 | } 98 | 99 | 100 | void loop() { 101 | delay(1000); 102 | } 103 | 104 | -------------------------------------------------------------------------------- /WiFiMulti/WiFiMulti.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * This sketch trys to Connect to the best AP based on a given list 3 | * 4 | * visit 5 | * http://www.techiesms.com 6 | * for IoT project tutorials. 7 | * 8 | * #techiesms 9 | * explore | learn | share 10 | */ 11 | 12 | #include 13 | #include 14 | 15 | ESP8266WiFiMulti wifiMulti; 16 | 17 | void setup() { 18 | Serial.begin(115200); // --> default baudrate for ESP 12e board. 19 | delay(10); 20 | 21 | wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1"); 22 | wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2"); 23 | //wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3"); 24 | 25 | 26 | 27 | 28 | } 29 | 30 | void loop() { 31 | if(wifiMulti.run() != WL_CONNECTED) { 32 | Serial.println("WiFi not connected!"); 33 | delay(1000); 34 | } 35 | if(wifiMulti.run() == WL_CONNECTED) { 36 | Serial.println(""); 37 | Serial.println("WiFi connected to"); 38 | Serial.println(WiFi.SSID());// --> Name of the Access Point to which ESP is connected 39 | Serial.println("IP address: "); 40 | Serial.println(WiFi.localIP()); // --> Local IP address of ESP board 41 | delay(1000); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /WiFiTelnetToSerial/WiFiTelnetToSerial.ino: -------------------------------------------------------------------------------- 1 | /* 2 | WiFiTelnetToSerial - Example Transparent UART to Telnet Server for esp8266 3 | 4 | Copyright (c) 2015 Hristo Gochkov. All rights reserved. 5 | This file is part of the ESP8266WiFi library for Arduino environment. 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library; if not, write to the Free Software 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 | 21 | visit 22 | http://www.techiesms.com 23 | for IoT project tutorials. 24 | 25 | #techiesms 26 | explore | learn | share 27 | 28 | 29 | */ 30 | #include 31 | 32 | //how many clients should be able to telnet to this ESP8266 33 | #define MAX_SRV_CLIENTS 2 34 | const char* ssid = "SSID"; 35 | const char* password = "PASS"; 36 | 37 | WiFiServer server(23); // --> default port for communication usign TELNET protocol | Server Instance 38 | WiFiClient serverClients[MAX_SRV_CLIENTS]; // --> Client Instanse 39 | 40 | void setup() { 41 | Serial.begin(115200); 42 | WiFi.begin(ssid, password); 43 | 44 | // -->Try to connect to particular host for 20 times, If still not connected then automatically resets. 45 | Serial.print("\nConnecting to "); Serial.println(ssid); 46 | uint8_t i = 0; 47 | while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500); 48 | if(i == 21){ 49 | Serial.print("Could not connect to"); Serial.println(ssid); 50 | while(1); 51 | } 52 | //start UART and the server 53 | Serial.begin(115200); 54 | server.begin(); 55 | server.setNoDelay(true); // --> Won't be storing data into buffer and wait for the ack. rather send the next data and in case nack is received, it will resend the whole data 56 | 57 | Serial.print("Ready! Use 'telnet "); 58 | Serial.print(WiFi.localIP()); 59 | Serial.println(" 23' to connect"); 60 | } 61 | 62 | void loop() { 63 | uint8_t i; 64 | //check if there are any new clients 65 | if (server.hasClient()){ 66 | for(i = 0; i < MAX_SRV_CLIENTS; i++){ 67 | //find free/disconnected spot 68 | if (!serverClients[i] || !serverClients[i].connected()){ 69 | if(serverClients[i]) serverClients[i].stop(); 70 | serverClients[i] = server.available(); 71 | Serial.print("New client: "); Serial.print(i); 72 | continue; 73 | } 74 | } 75 | //no free/disconnected spot so reject 76 | WiFiClient serverClient = server.available(); 77 | serverClient.stop(); 78 | } 79 | //check clients for data 80 | for(i = 0; i < MAX_SRV_CLIENTS; i++){ 81 | if (serverClients[i] && serverClients[i].connected()){ 82 | if(serverClients[i].available()){ 83 | //get data from the telnet client and push it to the UART 84 | while(serverClients[i].available()) Serial.write(serverClients[i].read()); 85 | } 86 | } 87 | } 88 | //check UART for data 89 | if(Serial.available()){ 90 | size_t len = Serial.available(); 91 | uint8_t sbuf[len]; 92 | Serial.readBytes(sbuf, len); 93 | //push UART data to all connected telnet clients 94 | for(i = 0; i < MAX_SRV_CLIENTS; i++){ 95 | if (serverClients[i] && serverClients[i].connected()){ 96 | serverClients[i].write(sbuf, len); 97 | delay(1); 98 | } 99 | } 100 | } 101 | } 102 | --------------------------------------------------------------------------------