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/09 11:01] – created ktokarz | en:iot-open:espressif_arduino [2023/11/21 21:57] (current) – ktokarz | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== ESP32 Wifi Scanner | + | ====== Programming ESP8266 for the Network ====== |
- | There are many different development software and tools which can be used for ESP32 programming ((https:// | + | |
- | * Arduino COre ( C++) | + | |
- | * ESP-IDF (Espressif IoT Development Framework) | + | |
- | * Mongoose OS | + | |
- | * MicroPython | + | |
- | * Simba Embedded Programming Platform | + | |
- | * Lua | + | |
- | * JacvaSript | + | |
- | * mruby | + | |
- | * BASIC | + | |
- | Of course, for programming ESP32 We can use all the previously described Arduino | + | Programming networking services with Espressif SoCs requires |
+ | 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 | ||
- | This example shows how to use the All Channel Scan or Fast Scan to connect to a Wi-Fi network. In the Fast Scan mode, the scan will stop as soon as the first network matching the SSID is found. In this mode, an application can set the threshold for the authentication mode and the Signal strength. Networks that do not meet the threshold requirements will be ignored. In the All Channel Scan mode, the scan will end after all the channels are scanned, and the connection will start with the best network. The networks can be sorted based on Authentication Mode or Signal Strength. The priority for the Authentication mode is: WPA2 > WPA > WEP > Open. | ||
<code c> | <code c> | ||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | /*Set the SSID and Password via "make menuconfig" | + | #include < |
- | #define DEFAULT_SSID CONFIG_WIFI_SSID | + | #include < |
- | #define DEFAULT_PWD CONFIG_WIFI_PASSWORD | + | #include < |
- | #if CONFIG_WIFI_ALL_CHANNEL_SCAN | + | /* Set these variables to your desired credentials. |
- | #define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN | + | const char *ssid = " |
- | #elif CONFIG_WIFI_FAST_SCAN | + | const char *password = " |
- | #define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN | + | |
- | #else | + | |
- | #define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN | + | |
- | # | + | |
- | #if CONFIG_WIFI_CONNECT_AP_BY_SIGNAL | + | ESP8266WebServer server(80); |
- | #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL | + | |
- | #elif CONFIG_WIFI_CONNECT_AP_BY_SECURITY | + | |
- | #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY | + | |
- | #else | + | |
- | #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL | + | |
- | #endif / | + | |
- | #if CONFIG_FAST_SCAN_THRESHOLD | + | void hRoot() { |
- | #define DEFAULT_RSSI CONFIG_FAST_SCAN_MINIMUM_SIGNAL | + | server.send(200, |
- | #if CONFIG_EXAMPLE_OPEN | + | } |
- | #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN | + | |
- | #elif CONFIG_EXAMPLE_WEP | + | |
- | #define DEFAULT_AUTHMODE WIFI_AUTH_WEP | + | |
- | #elif CONFIG_EXAMPLE_WPA | + | |
- | #define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK | + | |
- | #elif CONFIG_EXAMPLE_WPA2 | + | |
- | #define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK | + | |
- | #else | + | |
- | #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN | + | |
- | #endif | + | |
- | #else | + | |
- | #define DEFAULT_RSSI -127 | + | |
- | #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN | + | |
- | # | + | |
- | static const char *TAG = " | + | /* Initialization */ |
+ | void setup() { | ||
+ | delay(1500); | ||
+ | /* You can remove the password parameter | ||
+ | if you want the AP to be open. */ | ||
+ | WiFi.softAP(ssid, | ||
- | static esp_err_t event_handler(void *ctx, system_event_t *event) | + | IPAddress myIP = WiFi.softAPIP(); |
- | { | + | |
- | | + | server.on("/", hRoot); |
- | case SYSTEM_EVENT_STA_START: | + | server.begin(); |
- | ESP_LOGI(TAG, | + | } |
- | | + | |
- | | + | void loop() { |
- | case SYSTEM_EVENT_STA_GOT_IP: | + | server.handleClient(); |
- | | + | } |
- | | + | </code> |
- | ip4addr_ntoa(& | + | |
- | break; | + | == ESP8266 Client Mode == |
- | case SYSTEM_EVENT_STA_DISCONNECTED: | + | 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> |
- | | + | |
- | break; | + | #include < |
- | | + | #include < |
- | break; | + | |
+ | 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) { | ||
+ | | ||
} | } | ||
- | | + | |
} | } | ||
- | /* Initialize Wi-Fi as sta and set scan method | + | |
- | static | + | 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 | ||
+ | <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() | ||
{ | { | ||
- | tcpip_adapter_init(); | + | Serial.begin(115200); |
- | | + | |
+ | |||
+ | 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, pass); | ||
+ | |||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | |||
+ | Serial.println("" | ||
+ | |||
+ | Serial.println(" | ||
+ | Serial.println(" | ||
+ | Serial.println(WiFi.localIP()); | ||
- | wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | + | Serial.println(" |
- | | + | |
- | ESP_LOGI(TAG, DEFAULT_SSID); | + | |
- | ESP_LOGI(TAG, DEFAULT_PWD); | + | |
- | wifi_config_t wifi_config = { | + | |
- | | + | // first ntp server |
- | .ssid = DEFAULT_SSID, | + | servernbr = 0; |
- | .password = DEFAULT_PWD, | + | |
- | .scan_method = DEFAULT_SCAN_METHOD, | + | |
- | .sort_method = DEFAULT_SORT_METHOD, | + | |
- | .threshold.rssi = DEFAULT_RSSI, | + | |
- | .threshold.authmode = DEFAULT_AUTHMODE, | + | |
- | }, | + | |
- | }; | + | |
- | ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); | + | |
- | | + | |
- | | + | |
} | } | ||
- | void app_main() | + | void loop() |
{ | { | ||
- | | + | |
- | | + | |
- | | + | WiFi.hostByName(ntpServerName[servernbr], |
- | | + | |
- | | + | |
+ | 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; | ||
} | } | ||
- | | + | |
+ | 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, | ||
- | | + | |
- | } | + | // of the received packet and is four bytes, |
- | </code> | + | // or two words, long. First, extract the two words: |
- | To properly set up Station mode, it is necessary to enter SSID and password. To enter these values, before compiling | + | unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); |
+ | unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); | ||
+ | // combine | ||
+ | // this is NTP time (seconds since Jan 1 1900): | ||
+ | unsigned long secsSince1900 = highWord << 16 | lowWord; | ||
+ | Serial.print(" | ||
+ | Serial.println(secsSince1900); | ||
- | <code c> | + | // now convert NTP time into everyday time: |
- | make menuconfig | + | |
- | </code> | + | // 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); | ||
- | and then | ||
- | <code c> | + | // print the hour, minute and second: |
- | make all | + | // UTC is the time at Greenwich Meridian (GMT) |
- | </code> | + | 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 ' | ||
+ | | ||
+ | } | ||
+ | // print the minute (3600 equals secs per minute) | ||
+ | Serial.print((epoch | ||
+ | Serial.print(':' | ||
+ | if ( (epoch % 60) < 10 ) { | ||
+ | | ||
+ | Serial.print(' | ||
+ | } | ||
+ | Serial.println(epoch % 60); // print the second | ||
+ | } | ||
+ | // wait ten seconds before asking for the time again | ||
+ | delay(10000); | ||
+ | } | ||
- | or | + | // 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, | ||
+ | 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] | ||
- | <code c> | + | // all NTP fields have been given values, now |
- | make flash | + | // you can send a packet requesting a timestamp: |
- | </code> | + | |
+ | udp.write(packetBuffer, | ||
+ | udp.endPacket(); | ||
+ | } | ||
+ | </ | ||