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/03 09:46] – ktokarz | en:iot-open:espressif_arduino [2023/11/21 21:57] (current) – ktokarz | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== ==== | + | ====== |
- | <box # | + | |
- | <box # | + | |
- | ==== | + | |
- | <box # | + | |
- | <box # | + | |
- | Espressif | + | Programming networking services with Espressif |
- | * as WiFi client connected to WiFi router (Figure {{ref> | + | Below are two code examples for ESP8266 of how to implement |
- | <figure esp_client> | + | The third example shows how to send and receive a UDP packet while in client |
- | {{ : | + | Examples on further pages show how to make a handy WiFi scanner showing available networks nearby. |
- | < | + | |
- | </ | + | |
- | * as independent WiFi access point (Figure {{ref> | + | |
- | <figure esp_ap> | + | |
- | {{ : | + | |
- | < | + | |
- | </ | + | |
- | * as a repeater with devices connected | + | |
- | <figure esp_both> | + | |
- | {{ : | + | |
- | < | + | |
- | </ | + | |
- | * as client | + | |
- | <figure esp_mesh> | + | |
- | {{ : | + | |
- | < | + | |
- | </ | + | |
+ | == 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" | ||
+ | |||
+ | <code c> | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | /* Set these variables to your desired credentials. */ | ||
+ | const char *ssid = " | ||
+ | const char *password = " | ||
+ | |||
+ | ESP8266WebServer server(80); | ||
+ | |||
+ | void hRoot() { | ||
+ | server.send(200, | ||
+ | } | ||
+ | |||
+ | /* Initialization */ | ||
+ | void setup() { | ||
+ | delay(1500); | ||
+ | /* You can remove the password parameter | ||
+ | if you want the AP to be open. */ | ||
+ | WiFi.softAP(ssid, | ||
+ | |||
+ | IPAddress myIP = WiFi.softAPIP(); | ||
+ | |||
+ | server.on("/", | ||
+ | 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. | ||
+ | <code c> | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | 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(" | ||
+ | |||
+ | while(WiFiMulti.run() != WL_CONNECTED) { | ||
+ | delay(500); | ||
+ | } | ||
+ | delay(500); | ||
+ | } | ||
+ | |||
+ | |||
+ | void loop() { | ||
+ | const uint16_t port = 80; | ||
+ | const char * host = " | ||
+ | |||
+ | // Use WiFiClient class to create TCP connections | ||
+ | WiFiClient client; | ||
+ | |||
+ | if (!client.connect(host, | ||
+ | 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(" | ||
+ | // Trying to send the GET request possibly responses (with error) | ||
+ | // client.println(" | ||
+ | |||
+ | //read back one line from server | ||
+ | String line = client.readStringUntil(' | ||
+ | Serial.println(line); | ||
+ | |||
+ | Serial.println(" | ||
+ | client.stop(); | ||
+ | | ||
+ | Serial.println(" | ||
+ | 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): | ||
+ | <code c> | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | |||
+ | char ssid[] = " | ||
+ | char pass[] = " | ||
+ | |||
+ | unsigned int localPort = 2390; // local port to listen for UDP packets | ||
+ | |||
+ | // NTP servers | ||
+ | IPAddress ntpServerIP; | ||
+ | const char* ntpServerName[] = | ||
+ | | ||
+ | |||
+ | 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& | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(115200); | ||
+ | Serial.println(); | ||
+ | |||
+ | Serial.print(" | ||
+ | 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, | ||
+ | | ||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | | ||
+ | Serial.println("" | ||
+ | | ||
+ | Serial.println(" | ||
+ | Serial.println(" | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | |||
+ | Serial.println(" | ||
+ | udp.begin(localPort); | ||
+ | Serial.print(" | ||
+ | Serial.println(udp.localPort()); | ||
+ | |||
+ | // first ntp server | ||
+ | servernbr = 0; | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | //get a random server from the pool | ||
+ | |||
+ | WiFi.hostByName(ntpServerName[servernbr], | ||
+ | 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(" | ||
+ | if ( servernbr = 5 ) { | ||
+ | servernbr =0; | ||
+ | } | ||
+ | else { | ||
+ | servernbr++; | ||
+ | } | ||
+ | } | ||
+ | else { | ||
+ | Serial.print(" | ||
+ | Serial.println(cb); | ||
+ | // We've received a packet, read the data from it | ||
+ | // read the packet into the buffer | ||
+ | udp.read(packetBuffer, | ||
+ | |||
+ | // 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], | ||
+ | unsigned long lowWord = word(packetBuffer[42], | ||
+ | // 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(" | ||
+ | Serial.println(secsSince1900); | ||
+ | |||
+ | // now convert NTP time into everyday time: | ||
+ | Serial.print(" | ||
+ | // 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(" | ||
+ | // print the hour (86400 equals secs per day) | ||
+ | Serial.print((epoch | ||
+ | Serial.print(':' | ||
+ | if ( ((epoch % 3600) / 60) < 10 ) { | ||
+ | // In the first 10 minutes of each hour, we'll want a leading ' | ||
+ | Serial.print(' | ||
+ | } | ||
+ | // print the minute (3600 equals secs per minute) | ||
+ | Serial.print((epoch | ||
+ | Serial.print(':' | ||
+ | if ( (epoch % 60) < 10 ) { | ||
+ | // In the first 10 seconds of each minute, we'll want a leading ' | ||
+ | Serial.print(' | ||
+ | } | ||
+ | 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& | ||
+ | { | ||
+ | Serial.print(" | ||
+ | Serial.println( address ); | ||
+ | // set all bytes in the buffer to 0 | ||
+ | memset(packetBuffer, | ||
+ | // Initialize values needed to form NTP request | ||
+ | // (see URL above for details on the packets) | ||
+ | packetBuffer[0] = 0b11100011; | ||
+ | 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] | ||
+ | packetBuffer[13] | ||
+ | packetBuffer[14] | ||
+ | packetBuffer[15] | ||
+ | |||
+ | // all NTP fields have been given values, now | ||
+ | // you can send a packet requesting a timestamp: | ||
+ | udp.beginPacket(address, | ||
+ | udp.write(packetBuffer, | ||
+ | udp.endPacket(); | ||
+ | } | ||
+ | |||
+ | </ | ||