Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:espressif_arduino [2023/10/09 11:15] ktokarzen:iot-open:espressif_arduino [2023/11/21 21:57] (current) ktokarz
Line 1: Line 1:
-===  === +====== Programming ESP8266 for the Network ======
-<box #5374d5></box> +
-<box #5374d5></box> +
-=== ESP Network Layers === +
-<box #5374d5></box> +
-<box #5374d5></box>+
  
-Programming networking services with Espressif SoCs requires the connection established on the networking layer between parties, mostly with TCP protocol.\\ +Programming networking services with Espressif SoCs requires the connection established on the networking layer between parties, mainly with TCP protocol.\\ 
-ESP8266 or ESP32 SoC can act as an Access Point (AP): a device to connect to like connecting a notebook to the Internet router, and as a client: ESP then behaves like any wifi enabled device, i.e. tablet or mobile phone, connecting to the Internet infrastructure. Interestingly, Espressif SoCs can act simultaneously in both modes at once, even, if they have only one WiFi interface!\\  +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.\\
-Below is a sample code for ESP8266of how to implement both modesusing 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.\\ 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 showhow to make a handy WiFi scanner showing available networks nearby.+Examples on further pages show how to make a handy WiFi scanner showing available networks nearby.
  
 == ESP8266 AP (Access Point) Mode == == ESP8266 AP (Access Point) Mode ==
-This sketch based on a standard example demonstrates how to program ESP8266 in AP 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"
 <code c> <code c>
  
Line 33: Line 28:
 void setup() { void setup() {
  delay(1500);  delay(1500);
- /* You can remove the password parameter if you want the AP to be open. */+ /* You can remove the password parameter  
 +           if you want the AP to be open. */
  WiFi.softAP(ssid, password);  WiFi.softAP(ssid, password);
  
Line 46: Line 42:
 } }
 </code> </code>
 +
 == ESP8266 Client Mode == == ESP8266 Client Mode ==
-This sketch (standard exampledemonstrates how to program ESP8266 in 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. 
 <code c> <code c>
  
Line 57: Line 54:
 void setup() { void setup() {
     delay(1000);     delay(1000);
 +    
 +    // Initialise serial port to monitor program behaviour
 +    Serial.begin(115200);
  
     // We start by connecting to a WiFi network     // We start by connecting to a WiFi network
Line 80: Line 80:
         return;         return;
     }     }
 +    // This will print the IP address assigned by the DHCP server
 +    Serial.println(WiFi.localIP());
  
     // This will send the request to the server     // This will send the request to the server
     client.println("Send this data to 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     //read back one line from server
Line 97: Line 101:
  
 == ESP8266 and UDP == == ESP8266 and UDP ==
-This sketch (based on a standard example) demonstrates how to program ESP8266 as NTP client using UDP packets (send and receive):+This sketch (based on a standard example) demonstrates how to program ESP8266 as an NTP client using UDP packets (send and receive):
 <code c> <code c>
  
Line 107: Line 111:
 char pass[] = "**************";  // your network password char pass[] = "**************";  // your network password
  
-unsigned int localPort = 2390;      // local port to listen for UDP packets+unsigned int localPort = 2390;   // local port to listen for UDP packets
  
 // NTP servers // NTP servers
Line 125: Line 129:
 // A UDP instance to let us send and receive packets over UDP // A UDP instance to let us send and receive packets over UDP
 WiFiUDP 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() void setup()
Line 172: Line 180:
   Serial.println(ntpServerIP);   Serial.println(ntpServerIP);
  
-  sendNTPpacket(ntpServerIP); // send an NTP packet to a time server+  // send an NTP packet to a time server 
 +  sendNTPpacket(ntpServerIP);  
 +  
   // wait to see if a reply is available   // wait to see if a reply is available
   delay(1000);   delay(1000);
Line 190: Line 200:
     Serial.println(cb);     Serial.println(cb);
     // We've received a packet, read the data from it     // We've received a packet, read the data from it
-    udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer+    // 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, +    // the timestamp starts at byte 40  
-    // or two words, long. First, esxtract the two words:+    // 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 highWord = word(packetBuffer[40], packetBuffer[41]);
Line 205: Line 217:
     // now convert NTP time into everyday time:     // now convert NTP time into everyday time:
     Serial.print("Unix time = ");     Serial.print("Unix time = ");
-    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:+    // Unix time starts on Jan 1 1970.  
 +    // In seconds, that's 2208988800:
     const unsigned long seventyYears = 2208988800UL;     const unsigned long seventyYears = 2208988800UL;
     // subtract seventy years:     // subtract seventy years:
Line 261: Line 274:
   udp.endPacket();   udp.endPacket();
 } }
- 
  
 </code> </code>
  
en/iot-open/espressif_arduino.1696850131.txt.gz · Last modified: 2023/10/09 11:15 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