dreed47 / WifiMQTTManager

ESP32/ESP8266 library for managing your IoT devices.
GNU General Public License v3.0
82 stars 33 forks source link

unable to access AP portal to configure Wifi #11

Closed kenken64 closed 5 years ago

kenken64 commented 5 years ago

I am using ESP32

4:11:41.890 -> *WM: [2] Enabling AP 
04:11:41.890 -> *WM: [1] StartAP with SSID:  ESP_a4cf12243d24
04:11:42.403 -> *WM: [1] AP IP address: 192.168.4.1
04:11:42.403 -> WMM: entering config mode...
04:11:42.403 -> WMM: 192.168.4.1
04:11:42.403 -> WMM: connect your device to WiFi SSID ESP_a4cf12243d24 to configure WiFi and MQTT...
04:11:42.403 -> *WM: [3] setupConfigPortal 
04:11:42.403 -> *WM: [1] Starting Web Portal 
04:11:42.403 -> *WM: [3] dns server started with ip:  192.168.4.1
04:11:42.436 -> *WM: [2] HTTP server started 
04:11:42.436 -> *WM: [2] WiFi Scan ASYNC started 
04:11:42.436 -> *WM: [2] Config Portal Running, blocking, waiting for clients... 
04:11:45.314 -> *WM: [2] WiFi Scan ASYNC completed in 2904 ms
04:11:45.314 -> *WM: [2] WiFi Scan ASYNC found: 17
04:11:55.255 -> dhcps: send_offer>>udp_sendto result 0
04:11:56.216 -> *WM: [3] -> clients3.google.com 
04:11:56.216 -> *WM: [2] <- Request redirected to captive portal 
04:11:56.491 -> *WM: [3] -> clients3.google.com 
04:11:56.491 -> *WM: [2] <- Request redirected to captive portal 
04:11:56.628 -> *WM: [2] <- HTTP Root 
04:11:56.628 -> *WM: [3] -> 192.168.4.1 
04:11:56.628 -> *WM: [3] lastconxresulttmp: WL_IDLE_STATUS
04:11:56.628 -> *WM: [3] lastconxresult: WL_DISCONNECTED
04:11:56.628 -> *WM: [2] Scan is cached 11309 ms ago
04:11:57.725 -> *WM: [3] -> clients3.google.com 
04:11:57.759 -> *WM: [2] <- Request redirected to captive portal 
04:11:57.998 -> *WM: [3] -> clients3.google.com 
04:11:58.033 -> *WM: [2] <- Request redirected to captive portal 
kenken64 commented 5 years ago

I made changes to the ESP32 DNSServer.cpp now it works !

from

void DNSServer::replyWithIP()
{
  if (_buffer == NULL) return;

  _udp.beginPacket(_udp.remoteIP(), _udp.remotePort());

  // Change the type of message to a response and set the number of answers equal to 
  // the number of questions in the header
  _dnsHeader->QR      = DNS_QR_RESPONSE;
  _dnsHeader->ANCount = _dnsHeader->QDCount;
  _udp.write( (unsigned char*) _dnsHeader, DNS_HEADER_SIZE ) ;

  // Write the question
  _udp.write(_dnsQuestion->QName, _dnsQuestion->QNameLength) ;
  _udp.write( (unsigned char*) &_dnsQuestion->QType, 2 ) ;
  _udp.write( (unsigned char*) &_dnsQuestion->QClass, 2 ) ;

  // Write the answer 
  // Use DNS name compression : instead of repeating the name in this RNAME occurence,
  // set the two MSB of the byte corresponding normally to the length to 1. The following
  // 14 bits must be used to specify the offset of the domain name in the message 
  // (<255 here so the first byte has the 6 LSB at 0) 
  _udp.write((uint8_t) 0xC0); 
  _udp.write((uint8_t) DNS_OFFSET_DOMAIN_NAME);  

  // DNS type A : host address, DNS class IN for INternet, returning an IPv4 address 
  uint16_t answerType = htons(DNS_TYPE_A), answerClass = htons(DNS_CLASS_IN), answerIPv4 = htons(DNS_RDLENGTH_IPV4)  ; 
  _udp.write((unsigned char*) &answerType, 2 );
  _udp.write((unsigned char*) &answerClass, 2 );
  _udp.write((unsigned char*) &_ttl, 4);        // DNS Time To Live
  _udp.write((unsigned char*) &answerIPv4, 2 );
  _udp.write(_resolvedIP, sizeof(_resolvedIP)); // The IP address to return
  _udp.endPacket();
}

to

void DNSServer::replyWithIP()
{
  if (_buffer == NULL) return;
  _dnsHeader->QR = DNS_QR_RESPONSE;
  _dnsHeader->ANCount = _dnsHeader->QDCount;
  _dnsHeader->QDCount = _dnsHeader->QDCount; 
  // _dnsHeader->QDCount = 0;

  _udp.beginPacket(_udp.remoteIP(), _udp.remotePort());
  _udp.write(_buffer, _currentPacketSize);

  _udp.write((uint8_t)192); //  answer name is a pointer
  _udp.write((uint8_t)12);  // pointer to offset at 0x00c

  _udp.write((uint8_t)0);   // 0x0001  answer is type A query (host address)
  _udp.write((uint8_t)1);

  _udp.write((uint8_t)0);   //0x0001 answer is class IN (internet address)
  _udp.write((uint8_t)1);

  _udp.write((unsigned char*)&_ttl, 4);
  _udp.write((uint8_t)0);
  _udp.write((uint8_t)4);
  _udp.write(_resolvedIP, 4);
  _udp.endPacket();
}