|
| 1 | +#include "WiFi.h" |
| 2 | + |
| 3 | +#define STA_SSID "nbis-test" |
| 4 | +#define STA_PASS "1234567890" |
| 5 | +#define AP_SSID "esp32" |
| 6 | + |
| 7 | +static volatile bool wifi_connected = false; |
| 8 | + |
| 9 | +WiFiUDP ntpClient; |
| 10 | + |
| 11 | +void wifiOnConnect(){ |
| 12 | + Serial.println("STA Connected"); |
| 13 | + Serial.print("STA IPv4: "); |
| 14 | + Serial.println(WiFi.localIP()); |
| 15 | + |
| 16 | + ntpClient.begin(2390); |
| 17 | +} |
| 18 | + |
| 19 | +void wifiOnDisconnect(){ |
| 20 | + Serial.println("STA Disconnected"); |
| 21 | + delay(1000); |
| 22 | + WiFi.begin(STA_SSID, STA_PASS); |
| 23 | +} |
| 24 | + |
| 25 | +void wifiConnectedLoop(){ |
| 26 | + //lets check the time |
| 27 | + const int NTP_PACKET_SIZE = 48; |
| 28 | + byte ntpPacketBuffer[NTP_PACKET_SIZE]; |
| 29 | + |
| 30 | + IPAddress address; |
| 31 | + WiFi.hostByName("time.nist.gov", address); |
| 32 | + memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE); |
| 33 | + ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode |
| 34 | + ntpPacketBuffer[1] = 0; // Stratum, or type of clock |
| 35 | + ntpPacketBuffer[2] = 6; // Polling Interval |
| 36 | + ntpPacketBuffer[3] = 0xEC; // Peer Clock Precision |
| 37 | + // 8 bytes of zero for Root Delay & Root Dispersion |
| 38 | + ntpPacketBuffer[12] = 49; |
| 39 | + ntpPacketBuffer[13] = 0x4E; |
| 40 | + ntpPacketBuffer[14] = 49; |
| 41 | + ntpPacketBuffer[15] = 52; |
| 42 | + ntpClient.beginPacket(address, 123); //NTP requests are to port 123 |
| 43 | + ntpClient.write(ntpPacketBuffer, NTP_PACKET_SIZE); |
| 44 | + ntpClient.endPacket(); |
| 45 | + |
| 46 | + delay(1000); |
| 47 | + |
| 48 | + int packetLength = ntpClient.parsePacket(); |
| 49 | + if (packetLength){ |
| 50 | + if(packetLength >= NTP_PACKET_SIZE){ |
| 51 | + ntpClient.read(ntpPacketBuffer, NTP_PACKET_SIZE); |
| 52 | + } |
| 53 | + ntpClient.flush(); |
| 54 | + uint32_t secsSince1900 = (uint32_t)ntpPacketBuffer[40] << 24 | (uint32_t)ntpPacketBuffer[41] << 16 | (uint32_t)ntpPacketBuffer[42] << 8 | ntpPacketBuffer[43]; |
| 55 | + //Serial.printf("Seconds since Jan 1 1900: %u\n", secsSince1900); |
| 56 | + uint32_t epoch = secsSince1900 - 2208988800UL; |
| 57 | + //Serial.printf("EPOCH: %u\n", epoch); |
| 58 | + uint8_t h = (epoch % 86400L) / 3600; |
| 59 | + uint8_t m = (epoch % 3600) / 60; |
| 60 | + uint8_t s = (epoch % 60); |
| 61 | + Serial.printf("UTC: %02u:%02u:%02u (GMT)\n", h, m, s); |
| 62 | + } |
| 63 | + |
| 64 | + delay(9000); |
| 65 | +} |
| 66 | + |
| 67 | +void WiFiEvent(WiFiEvent_t event){ |
| 68 | + switch(event) { |
| 69 | + |
| 70 | + case SYSTEM_EVENT_AP_START: |
| 71 | + //can set ap hostname here |
| 72 | + WiFi.softAPsetHostname(AP_SSID); |
| 73 | + //enable ap ipv6 here |
| 74 | + WiFi.softAPenableIpV6(); |
| 75 | + break; |
| 76 | + |
| 77 | + case SYSTEM_EVENT_STA_START: |
| 78 | + //set sta hostname here |
| 79 | + WiFi.setHostname(AP_SSID); |
| 80 | + break; |
| 81 | + case SYSTEM_EVENT_STA_CONNECTED: |
| 82 | + //enable sta ipv6 here |
| 83 | + WiFi.enableIpV6(); |
| 84 | + break; |
| 85 | + case SYSTEM_EVENT_AP_STA_GOT_IP6: |
| 86 | + //both interfaces get the same event |
| 87 | + Serial.print("STA IPv6: "); |
| 88 | + Serial.println(WiFi.localIPv6()); |
| 89 | + Serial.print("AP IPv6: "); |
| 90 | + Serial.println(WiFi.softAPIPv6()); |
| 91 | + break; |
| 92 | + case SYSTEM_EVENT_STA_GOT_IP: |
| 93 | + wifiOnConnect(); |
| 94 | + wifi_connected = true; |
| 95 | + break; |
| 96 | + case SYSTEM_EVENT_STA_DISCONNECTED: |
| 97 | + wifi_connected = false; |
| 98 | + wifiOnDisconnect(); |
| 99 | + break; |
| 100 | + default: |
| 101 | + break; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +void setup(){ |
| 106 | + Serial.begin(115200); |
| 107 | + WiFi.disconnect(true); |
| 108 | + WiFi.onEvent(WiFiEvent); |
| 109 | + WiFi.mode(WIFI_MODE_APSTA); |
| 110 | + WiFi.softAP(AP_SSID); |
| 111 | + WiFi.begin(STA_SSID, STA_PASS); |
| 112 | +} |
| 113 | + |
| 114 | +void loop(){ |
| 115 | + if(wifi_connected){ |
| 116 | + wifiConnectedLoop(); |
| 117 | + } |
| 118 | + while(Serial.available()) Serial.write(Serial.read()); |
| 119 | +} |
0 commit comments