├── README.md ├── LICENSE └── Arduino ├── ESP32WifiShowNetworks └── ESP32WifiShowNetworks.ino └── ESP32WifiNtp └── ESP32WifiNtp.ino /README.md: -------------------------------------------------------------------------------- 1 | # Arduino/ESP32 Wifi Part1 2 | Here some simple code to get started with Wifi on a Arduino with integrated WiFi (tested on an ESP32). NOTE: the ESP32 only supports 2.4G networks. 5G networks will not be seen by the ESP32. 3 | 4 | # ESP32WifiShowNetworks.ino 5 | This is a simple program by Techtutorialsx that scans the available Wifi networks and reports these in the Serial monitor. In addition, it will login onto a hard-coded SSID with password defined in the code. 6 | https://techtutorialsx.com/2017/06/29/esp32-arduino-getting-started-with-wifi/ 7 | 8 | # ESP32WifiNtp.ino 9 | This code reads out an NTP clock to sync time. Code is based on the link below, but adapted to work on ESP32. 10 | https://lastminuteengineers.com/esp8266-ntp-server-date-time-tutorial/ 11 | 12 | # How to use: 13 | See here for a video on ESP32 WiFi basics and how to sync to an NTP clock 14 | https://youtu.be/0AlATlN95Y0 15 | 16 | 17 | 18 | Have fun ;-) 19 | 20 | mo thunderz 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Mo_thunderz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Arduino/ESP32WifiShowNetworks/ESP32WifiShowNetworks.ino: -------------------------------------------------------------------------------- 1 | // code by techtutorialsx - source: 2 | // https://techtutorialsx.com/2017/06/29/esp32-arduino-getting-started-with-wifi/ 3 | 4 | #include 5 | 6 | // SSID and password of Wifi connection: 7 | const char* ssid = "TYPE_YOUR_SSID_HERE"; 8 | const char* password = "TYPE_YOUR_PASSWORD_HERE"; 9 | 10 | // simple function to decipher the encryption type of a network 11 | String translateEncryptionType(wifi_auth_mode_t encryptionType) { 12 | switch (encryptionType) { 13 | case (WIFI_AUTH_OPEN): 14 | return "Open"; 15 | case (WIFI_AUTH_WEP): 16 | return "WEP"; 17 | case (WIFI_AUTH_WPA_PSK): 18 | return "WPA_PSK"; 19 | case (WIFI_AUTH_WPA2_PSK): 20 | return "WPA2_PSK"; 21 | case (WIFI_AUTH_WPA_WPA2_PSK): 22 | return "WPA_WPA2_PSK"; 23 | case (WIFI_AUTH_WPA2_ENTERPRISE): 24 | return "WPA2_ENTERPRISE"; 25 | } 26 | } 27 | 28 | // Function to scan and print all networks that can be detected by the ESP32 29 | void scanNetworks() { 30 | int numberOfNetworks = WiFi.scanNetworks(); 31 | 32 | Serial.print("Number of networks found: "); 33 | Serial.println(numberOfNetworks); 34 | 35 | for (int i = 0; i < numberOfNetworks; i++) { 36 | Serial.print("Network name: "); 37 | Serial.println(WiFi.SSID(i)); 38 | 39 | Serial.print("Signal strength: "); 40 | Serial.println(WiFi.RSSI(i)); 41 | 42 | Serial.print("MAC address: "); 43 | Serial.println(WiFi.BSSIDstr(i)); 44 | 45 | Serial.print("Encryption type: "); 46 | String encryptionTypeDescription = translateEncryptionType(WiFi.encryptionType(i)); 47 | Serial.println(encryptionTypeDescription); 48 | Serial.println("-----------------------"); 49 | } 50 | } 51 | 52 | // function to connect to a Wifi network -> note that there is no time-out function on this 53 | void connectToNetwork() { 54 | WiFi.begin(ssid, password); 55 | Serial.println("Establishing connection to WiFi."); 56 | 57 | while (WiFi.status() != WL_CONNECTED) { 58 | delay(1000); 59 | Serial.print("."); 60 | } 61 | Serial.println("Connected to network"); 62 | } 63 | 64 | void setup() { 65 | Serial.begin(115200); // initiate local communication to PC 66 | 67 | scanNetworks(); // scan for all WiFi networks 68 | connectToNetwork(); // try to connect to SSID defined by user 69 | 70 | Serial.println(WiFi.macAddress()); // print MAC address of WiFi interface 71 | Serial.println(WiFi.localIP()); // print IP address of WiFi interface 72 | } 73 | 74 | void loop() {} 75 | // not used in this example 76 | -------------------------------------------------------------------------------- /Arduino/ESP32WifiNtp/ESP32WifiNtp.ino: -------------------------------------------------------------------------------- 1 | // Source code adapted from: (was originally written for ESP8266): 2 | // https://lastminuteengineers.com/esp8266-ntp-server-date-time-tutorial/ 3 | 4 | // IMPORTANT: install NTPclient library from Fabrice Weinberg in order to run this code (available in the Arduino IDE under Sketch -> Include Library -> Manage Libraries 5 | #include 6 | #include 7 | #include 8 | 9 | #define UTC_OFFSET_IN_SECONDS 3600 // offset from greenwich time 10 | 11 | // SSID and password of Wifi connection: 12 | const char* ssid = "TYPE_YOUR_SSID_HERE"; 13 | const char* password = "TYPE_YOUR_PASSWORD_HERE"; 14 | 15 | char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 16 | 17 | // Define NTP Client to get time 18 | WiFiUDP ntpUDP; 19 | NTPClient timeClient(ntpUDP, "pool.ntp.org", UTC_OFFSET_IN_SECONDS); 20 | 21 | void setup(){ 22 | Serial.begin(19200); 23 | 24 | WiFi.begin(ssid, password); 25 | 26 | while ( WiFi.status() != WL_CONNECTED ) { 27 | delay ( 500 ); 28 | Serial.print ( "." ); 29 | } 30 | 31 | timeClient.begin(); 32 | 33 | // The function timeClient.update() syncs the local time to the NTP server. In the video I call this in the main loop. However, NTP servers dont like it if 34 | // they get pinged all the time, so I recommend to only re-sync to the NTP server occasionally. In this example code we only call this function once in the 35 | // setup() and you will see that in the loop the local time is automatically updated. Of course the ESP/Arduino does not have an infinitely accurate clock, 36 | // so if the exact time is very important you will need to re-sync once in a while. 37 | timeClient.update(); 38 | } 39 | 40 | void loop() { 41 | 42 | // Option1: Get time and day of the week directly (discussed in Youtube video) 43 | Serial.print("Option 1: "); 44 | Serial.print(daysOfTheWeek[timeClient.getDay()]); 45 | Serial.print(", "); 46 | Serial.print(timeClient.getHours()); 47 | Serial.print(":"); 48 | Serial.print(timeClient.getMinutes()); 49 | Serial.print(":"); 50 | Serial.println(timeClient.getSeconds()); 51 | 52 | // Option 2: abstract from formatted date (added later as per request) 53 | Serial.print("Option 2: "); 54 | Serial.println(getTimeStampString()); 55 | 56 | delay(1000); 57 | } 58 | 59 | String getTimeStampString() { 60 | time_t rawtime = timeClient.getEpochTime(); 61 | struct tm * ti; 62 | ti = localtime (&rawtime); 63 | 64 | uint16_t year = ti->tm_year + 1900; 65 | String yearStr = String(year); 66 | 67 | uint8_t month = ti->tm_mon + 1; 68 | String monthStr = month < 10 ? "0" + String(month) : String(month); 69 | 70 | uint8_t day = ti->tm_mday; 71 | String dayStr = day < 10 ? "0" + String(day) : String(day); 72 | 73 | uint8_t hours = ti->tm_hour; 74 | String hoursStr = hours < 10 ? "0" + String(hours) : String(hours); 75 | 76 | uint8_t minutes = ti->tm_min; 77 | String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes); 78 | 79 | uint8_t seconds = ti->tm_sec; 80 | String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds); 81 | 82 | return yearStr + "-" + monthStr + "-" + dayStr + " " + 83 | hoursStr + ":" + minuteStr + ":" + secondStr; 84 | } 85 | --------------------------------------------------------------------------------