mirror of
https://github.com/0xFEEDC0DE64/arduino-esp32.git
synced 2025-07-29 10:17:15 +02:00
Add initial IPv6 Support
This commit is contained in:
119
libraries/WiFi/examples/WiFiIPv6/WiFiIPv6.ino
Normal file
119
libraries/WiFi/examples/WiFiIPv6/WiFiIPv6.ino
Normal file
@ -0,0 +1,119 @@
|
||||
#include "WiFi.h"
|
||||
|
||||
#define STA_SSID "nbis-test"
|
||||
#define STA_PASS "1234567890"
|
||||
#define AP_SSID "esp32"
|
||||
|
||||
static volatile bool wifi_connected = false;
|
||||
|
||||
WiFiUDP ntpClient;
|
||||
|
||||
void wifiOnConnect(){
|
||||
Serial.println("STA Connected");
|
||||
Serial.print("STA IPv4: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
ntpClient.begin(2390);
|
||||
}
|
||||
|
||||
void wifiOnDisconnect(){
|
||||
Serial.println("STA Disconnected");
|
||||
delay(1000);
|
||||
WiFi.begin(STA_SSID, STA_PASS);
|
||||
}
|
||||
|
||||
void wifiConnectedLoop(){
|
||||
//lets check the time
|
||||
const int NTP_PACKET_SIZE = 48;
|
||||
byte ntpPacketBuffer[NTP_PACKET_SIZE];
|
||||
|
||||
IPAddress address;
|
||||
WiFi.hostByName("time.nist.gov", address);
|
||||
memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
|
||||
ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode
|
||||
ntpPacketBuffer[1] = 0; // Stratum, or type of clock
|
||||
ntpPacketBuffer[2] = 6; // Polling Interval
|
||||
ntpPacketBuffer[3] = 0xEC; // Peer Clock Precision
|
||||
// 8 bytes of zero for Root Delay & Root Dispersion
|
||||
ntpPacketBuffer[12] = 49;
|
||||
ntpPacketBuffer[13] = 0x4E;
|
||||
ntpPacketBuffer[14] = 49;
|
||||
ntpPacketBuffer[15] = 52;
|
||||
ntpClient.beginPacket(address, 123); //NTP requests are to port 123
|
||||
ntpClient.write(ntpPacketBuffer, NTP_PACKET_SIZE);
|
||||
ntpClient.endPacket();
|
||||
|
||||
delay(1000);
|
||||
|
||||
int packetLength = ntpClient.parsePacket();
|
||||
if (packetLength){
|
||||
if(packetLength >= NTP_PACKET_SIZE){
|
||||
ntpClient.read(ntpPacketBuffer, NTP_PACKET_SIZE);
|
||||
}
|
||||
ntpClient.flush();
|
||||
uint32_t secsSince1900 = (uint32_t)ntpPacketBuffer[40] << 24 | (uint32_t)ntpPacketBuffer[41] << 16 | (uint32_t)ntpPacketBuffer[42] << 8 | ntpPacketBuffer[43];
|
||||
//Serial.printf("Seconds since Jan 1 1900: %u\n", secsSince1900);
|
||||
uint32_t epoch = secsSince1900 - 2208988800UL;
|
||||
//Serial.printf("EPOCH: %u\n", epoch);
|
||||
uint8_t h = (epoch % 86400L) / 3600;
|
||||
uint8_t m = (epoch % 3600) / 60;
|
||||
uint8_t s = (epoch % 60);
|
||||
Serial.printf("UTC: %02u:%02u:%02u (GMT)\n", h, m, s);
|
||||
}
|
||||
|
||||
delay(9000);
|
||||
}
|
||||
|
||||
void WiFiEvent(WiFiEvent_t event){
|
||||
switch(event) {
|
||||
|
||||
case SYSTEM_EVENT_AP_START:
|
||||
//can set ap hostname here
|
||||
WiFi.softAPsetHostname(AP_SSID);
|
||||
//enable ap ipv6 here
|
||||
WiFi.softAPenableIpV6();
|
||||
break;
|
||||
|
||||
case SYSTEM_EVENT_STA_START:
|
||||
//set sta hostname here
|
||||
WiFi.setHostname(AP_SSID);
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_CONNECTED:
|
||||
//enable sta ipv6 here
|
||||
WiFi.enableIpV6();
|
||||
break;
|
||||
case SYSTEM_EVENT_AP_STA_GOT_IP6:
|
||||
//both interfaces get the same event
|
||||
Serial.print("STA IPv6: ");
|
||||
Serial.println(WiFi.localIPv6());
|
||||
Serial.print("AP IPv6: ");
|
||||
Serial.println(WiFi.softAPIPv6());
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_GOT_IP:
|
||||
wifiOnConnect();
|
||||
wifi_connected = true;
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||
wifi_connected = false;
|
||||
wifiOnDisconnect();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void setup(){
|
||||
Serial.begin(115200);
|
||||
WiFi.disconnect(true);
|
||||
WiFi.onEvent(WiFiEvent);
|
||||
WiFi.mode(WIFI_MODE_APSTA);
|
||||
WiFi.softAP(AP_SSID);
|
||||
WiFi.begin(STA_SSID, STA_PASS);
|
||||
}
|
||||
|
||||
void loop(){
|
||||
if(wifi_connected){
|
||||
wifiConnectedLoop();
|
||||
}
|
||||
while(Serial.available()) Serial.write(Serial.read());
|
||||
}
|
@ -26,6 +26,7 @@
|
||||
|
||||
#include "Print.h"
|
||||
#include "IPAddress.h"
|
||||
#include "IPv6Address.h"
|
||||
|
||||
#include "WiFiType.h"
|
||||
#include "WiFiSTA.h"
|
||||
|
@ -232,3 +232,48 @@ String WiFiAPClass::softAPmacAddress(void)
|
||||
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
return String(macStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the softAP interface Host name.
|
||||
* @return char array hostname
|
||||
*/
|
||||
const char * WiFiAPClass::softAPgetHostname()
|
||||
{
|
||||
const char * hostname;
|
||||
if(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_AP, &hostname)) {
|
||||
return NULL;
|
||||
}
|
||||
return hostname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the softAP interface Host name.
|
||||
* @param hostname pointer to const string
|
||||
* @return true on success
|
||||
*/
|
||||
bool WiFiAPClass::softAPsetHostname(const char * hostname)
|
||||
{
|
||||
return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_AP, hostname) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable IPv6 on the softAP interface.
|
||||
* @return true on success
|
||||
*/
|
||||
bool WiFiAPClass::softAPenableIpV6()
|
||||
{
|
||||
return tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_AP) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the softAP interface IPv6 address.
|
||||
* @return IPv6Address softAP IPv6
|
||||
*/
|
||||
IPv6Address WiFiAPClass::softAPIPv6()
|
||||
{
|
||||
static ip6_addr_t addr;
|
||||
if(tcpip_adapter_get_ip6_linklocal(TCPIP_ADAPTER_IF_AP, &addr)) {
|
||||
return IPv6Address();
|
||||
}
|
||||
return IPv6Address(addr.addr);
|
||||
}
|
||||
|
@ -45,6 +45,12 @@ public:
|
||||
|
||||
IPAddress softAPIP();
|
||||
|
||||
bool softAPenableIpV6();
|
||||
IPv6Address softAPIPv6();
|
||||
|
||||
const char * softAPgetHostname();
|
||||
bool softAPsetHostname(const char * hostname);
|
||||
|
||||
uint8_t* softAPmacAddress(uint8_t* mac);
|
||||
String softAPmacAddress(void);
|
||||
|
||||
|
@ -453,3 +453,48 @@ int32_t WiFiSTAClass::RSSI(void)
|
||||
{
|
||||
return 0;//wifi_station_get_rssi();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the station interface Host name.
|
||||
* @return char array hostname
|
||||
*/
|
||||
const char * WiFiSTAClass::getHostname()
|
||||
{
|
||||
const char * hostname;
|
||||
if(tcpip_adapter_get_hostname(TCPIP_ADAPTER_IF_STA, &hostname)){
|
||||
return NULL;
|
||||
}
|
||||
return hostname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the station interface Host name.
|
||||
* @param hostname pointer to const string
|
||||
* @return true on success
|
||||
*/
|
||||
bool WiFiSTAClass::setHostname(const char * hostname)
|
||||
{
|
||||
return tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, hostname) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable IPv6 on the station interface.
|
||||
* @return true on success
|
||||
*/
|
||||
bool WiFiSTAClass::enableIpV6()
|
||||
{
|
||||
return tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the station interface IPv6 address.
|
||||
* @return IPv6Address
|
||||
*/
|
||||
IPv6Address WiFiSTAClass::localIPv6()
|
||||
{
|
||||
static ip6_addr_t addr;
|
||||
if(tcpip_adapter_get_ip6_linklocal(TCPIP_ADAPTER_IF_STA, &addr)){
|
||||
return IPv6Address();
|
||||
}
|
||||
return IPv6Address(addr.addr);
|
||||
}
|
||||
|
@ -63,6 +63,12 @@ public:
|
||||
IPAddress subnetMask();
|
||||
IPAddress gatewayIP();
|
||||
IPAddress dnsIP(uint8_t dns_no = 0);
|
||||
|
||||
bool enableIpV6();
|
||||
IPv6Address localIPv6();
|
||||
|
||||
const char * getHostname();
|
||||
bool setHostname(const char * hostname);
|
||||
|
||||
// STA WiFi info
|
||||
wl_status_t status();
|
||||
|
Reference in New Issue
Block a user