Programming ESP8266 for the Network

Programming networking services with Espressif SoCs requires the connection established on the networking layer between parties, mainly with TCP protocol.
Below are two code examples for ESP8266 of how to implement access point and network station modes using libraries that came during installation of the development environment for Arduino framework.
The third example shows how to send and receive a UDP packet while in client mode. It is the full solution to connect ESP8266 to the NTP (Network Time Protocol) server to obtain the current date and time from the Internet.
Examples on further pages show how to make a handy WiFi scanner showing available networks nearby.

ESP8266 AP (Access Point) Mode

Based on a standard example, this program demonstrates how to program ESP8266 in AP mode. After compilation and uploading this program, an ESP8266 starts serving as the access point that can be connected to, e.g., a smartphone. It presents a simple web server available at the local IP address 192.168.4.1 (the default address of the ESP access point). This web server responds with a short message: “You are connected”.

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
 
/* Set these variables to your desired credentials. */
const char *ssid = "APmode";
const char *password = "password";
 
ESP8266WebServer server(80);
 
void hRoot() {
	server.send(200, "text/html", "<h1>You are connected</h1>");
}
 
/* Initialization */
void setup() {
	delay(1500);
	/* You can remove the password parameter 
           if you want the AP to be open. */
	WiFi.softAP(ssid, password);
 
	IPAddress myIP = WiFi.softAPIP();
 
	server.on("/", hRoot);
	server.begin();
}
 
void loop() {
	server.handleClient();
}
ESP8266 Client Mode

This standard example demonstrates how to program ESP8266 in client mode. It tries to connect to the WiFi network with a specified name (SSID) and password.

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
 
ESP8266WiFiMulti WiFiMulti;
 
void setup() {
    delay(1000);
 
    // Initialise serial port to monitor program behaviour
    Serial.begin(115200);
 
    // We start by connecting to a WiFi network
    WiFi.mode(WIFI_STA);
    WiFiMulti.addAP("SSID", "password");
 
    while(WiFiMulti.run() != WL_CONNECTED) {
        delay(500);
    }
    delay(500);
}
 
 
void loop() {
    const uint16_t port = 80;
    const char * host = "192.168.1.1"; // ip or dns
 
    // Use WiFiClient class to create TCP connections
    WiFiClient client;
 
    if (!client.connect(host, port)) {
        delay(5000);
        return;
    }
    // This will print the IP address assigned by the DHCP server
    Serial.println(WiFi.localIP());
 
    // This will send the request to the server
    client.println("Send this data to server");
    // Trying to send the GET request possibly responses (with error)
    // client.println("GET /echo");
 
    //read back one line from server
    String line = client.readStringUntil('\r');
    Serial.println(line);
 
    Serial.println("closing connection");
    client.stop();
 
    Serial.println("wait 5 sec...");
    delay(5000);
}
ESP8266 and UDP

This sketch (based on a standard example) demonstrates how to program ESP8266 as an NTP client using UDP packets (send and receive):

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
 
 
char ssid[] = "**************";  //  your network SSID (name)
char pass[] = "**************";  // your network password
 
unsigned int localPort = 2390;   // local port to listen for UDP packets
 
// NTP servers
IPAddress ntpServerIP; // 0.pl.pool.ntp.org NTP server address
const char* ntpServerName[] = 
 {"0.pl.pool.ntp.org","1.pl.pool.ntp.org","2.pl.pool.ntp.org","3.pl.pool.ntp.org"};
 
const int timeZone = 1;  //Central European Time
int servernbr=0;
 
// NTP time stamp is in the first 48 bytes of the message
const int NTP_PACKET_SIZE = 48; 
 
//buffer to hold incoming and outgoing packets
byte packetBuffer[ NTP_PACKET_SIZE]; 
 
// A UDP instance to let us send and receive packets over UDP
WiFiUDP udp;
 
// Prototype of the function defined at the end of this file 
// (required in Visual Studio Code)
void sendNTPpacket(IPAddress& address);
 
void setup()
{
  Serial.begin(115200);
  Serial.println();
 
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
//  WiFi.persistent(false);
  WiFi.mode(WIFI_OFF);
  delay(2000);
 
// We start by connecting to a WiFi network
  WiFi.mode(WIFI_STA);
  delay(3000); 
  WiFi.begin(ssid, pass); 
 
  while (WiFi.status() != WL_CONNECTED) {   
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
 
  Serial.println("WiFi connected");
  Serial.println("DHCP assigned IP address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Starting UDP");
  udp.begin(localPort);
  Serial.print("Local port: ");
  Serial.println(udp.localPort());
 
  // first ntp server
  servernbr = 0;
}
 
void loop()
{
  //get a random server from the pool
 
  WiFi.hostByName(ntpServerName[servernbr], ntpServerIP); 
  Serial.print(ntpServerName[servernbr]); 
  Serial.print(":");
  Serial.println(ntpServerIP);
 
  // send an NTP packet to a time server
  sendNTPpacket(ntpServerIP); 
 
  // wait to see if a reply is available
  delay(1000);
 
  int cb = udp.parsePacket();
  if (!cb) {
    Serial.println("no packet yet");
    if ( servernbr = 5 ) {
      servernbr =0;
    }
    else {
      servernbr++;
    }
  }
  else {
    Serial.print("packet received, length=");
    Serial.println(cb);
    // We've received a packet, read the data from it
    // read the packet into the buffer
    udp.read(packetBuffer, NTP_PACKET_SIZE); 
 
    // the timestamp starts at byte 40 
    // of the received packet and is four bytes,
    // or two words, long. First, extract the two words:
 
    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;
    Serial.print("Seconds since Jan 1 1900 = " );
    Serial.println(secsSince1900);
 
    // now convert NTP time into everyday time:
    Serial.print("Unix time = ");
    // Unix time starts on Jan 1 1970. 
    // In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800UL;
    // subtract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears;
    // print Unix time:
    Serial.println(epoch);
 
 
    // print the hour, minute and second:
    // UTC is the time at Greenwich Meridian (GMT)
    Serial.print("The UTC time is ");       
    // print the hour (86400 equals secs per day)
    Serial.print((epoch  % 86400L) / 3600); 
    Serial.print(':');
    if ( ((epoch % 3600) / 60) < 10 ) {
      // In the first 10 minutes of each hour, we'll want a leading '0'
      Serial.print('0');
    }
    // print the minute (3600 equals secs per minute)
    Serial.print((epoch  % 3600) / 60); 
    Serial.print(':');
    if ( (epoch % 60) < 10 ) {
      // In the first 10 seconds of each minute, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.println(epoch % 60); // print the second
  }
  // wait ten seconds before asking for the time again
  delay(10000);
}
 
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress& address)
{
  Serial.print("sending NTP packet to: ");
  Serial.println( address );
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
 
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:
  udp.beginPacket(address, 123); //NTP requests are to port 123
  udp.write(packetBuffer, NTP_PACKET_SIZE);
  udp.endPacket();
}
en/iot-open/espressif_arduino.txt · Last modified: 2023/11/21 21:57 by ktokarz
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0