This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-open:espressif_arduino [2023/10/10 09:07] – ktokarz | en:iot-open:espressif_arduino [2023/11/21 21:57] (current) – ktokarz | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | === === | + | ====== |
- | <box # | + | |
- | <box # | + | Programming networking services with Espressif SoCs requires the connection established on the networking layer between parties, |
- | === ESP Network Layers | + | Below are two code examples |
- | <box # | + | |
- | <box # | + | |
- | Programming networking services with Espressif SoCs requires the connection established on the networking layer between parties, | + | |
- | 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, | + | |
- | Below is a sample | + | |
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 show, how 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 program based on a standard example 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 with e.g. 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" | + | Based on a standard example, this program |
<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, | WiFi.softAP(ssid, | ||
Line 84: | Line 80: | ||
return; | return; | ||
} | } | ||
- | // This will print the IP address assigned by DHCP server | + | // This will print the IP address assigned by the DHCP server |
Serial.println(WiFi.localIP()); | Serial.println(WiFi.localIP()); | ||
// This will send the request to the server | // This will send the request to the server | ||
- | // You can try send the GET request to possibly get the response (with error) | ||
client.println(" | client.println(" | ||
+ | // Trying to send the GET request possibly responses (with error) | ||
// client.println(" | // client.println(" | ||
Line 105: | 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 115: | Line 111: | ||
char pass[] = " | char pass[] = " | ||
- | unsigned int localPort = 2390; // local port to listen for UDP packets | + | unsigned int localPort = 2390; |
// NTP servers | // NTP servers | ||
Line 134: | Line 130: | ||
WiFiUDP udp; | WiFiUDP udp; | ||
- | // Prototype of the function defined at the end of this file (required in Visual Studio Code) | + | // Prototype of the function defined at the end of this file |
+ | // (required in Visual Studio Code) | ||
void sendNTPpacket(IPAddress& | void sendNTPpacket(IPAddress& | ||
Line 183: | Line 180: | ||
Serial.println(ntpServerIP); | Serial.println(ntpServerIP); | ||
- | | + | // 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 201: | 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, | + | |
+ | | ||
- | //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, | + | // of the received packet and is four bytes, |
+ | // or two words, long. First, | ||
unsigned long highWord = word(packetBuffer[40], | unsigned long highWord = word(packetBuffer[40], | ||
Line 216: | Line 217: | ||
// now convert NTP time into everyday time: | // now convert NTP time into everyday time: | ||
Serial.print(" | Serial.print(" | ||
- | // 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: |