Advertisement
Guest User

Untitled

a guest
Sep 6th, 2021
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define IP_FORWARD 1
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <WiFiUDP.h>
  5.  
  6. int status_LED_pin = D6;
  7. int signal_LED_pin = D1;
  8. unsigned long lastSwitch = 0;
  9.  
  10. /* Set these to your desired credentials for ESP8266 AP. */
  11. const char *ssid = "wemos SSID";
  12. const char *password = "my pass";
  13. IPAddress apIP(192, 168, 4, 1);
  14.  
  15.  
  16. // Setup credentials for original WiFi, that we plan to repeat
  17. const char* ssidExt = "router SSID";
  18. const char* passwordExt = "my pass";
  19.  
  20. const byte DNS_PORT = 53;
  21.  
  22. // A UDP instance to let us send and receive packets over UDP
  23. WiFiUDP Udp;
  24. byte packetBuffer[512]; //buffer to hold incoming and outgoing packets
  25.  
  26. WiFiUDP Udp8;
  27.  
  28. // Update these with values suitable for your network.
  29. IPAddress ip(192, 168, 2, 61); //Node static IP
  30. IPAddress gateway(192, 168, 2, 1);
  31. IPAddress subnet(255, 255, 255, 0);
  32.  
  33. uint8_t dis_count = 0;
  34.  
  35.  
  36. void setup()
  37. {
  38.   Serial.begin(115200);
  39.   Serial.println();
  40.   Serial.println("!!!!!!!!!!!!!!!!!!!++");
  41.   pinMode(status_LED_pin, OUTPUT);
  42.   analogWrite(status_LED_pin, 0);
  43.   pinMode(signal_LED_pin, OUTPUT);
  44.   analogWrite(signal_LED_pin, 0);
  45.   lastSwitch = millis();
  46.    WiFi.mode(WIFI_AP_STA);
  47.    //WiFi.mode(WIFI_AP);
  48.  
  49.  
  50.  
  51.  
  52.   WiFi.setOutputPower(20.5);
  53.   WiFi.begin(ssidExt, passwordExt);
  54.   WiFi.setOutputPower(20.5);
  55.   WiFi.config(ip, gateway, subnet);
  56.  
  57.   //Wifi connection
  58.   while (WiFi.status() != WL_CONNECTED) {
  59.     analogWrite(status_LED_pin, 150);
  60.     delay(250);
  61.     analogWrite(status_LED_pin, 0);
  62.     delay(250);
  63.     Serial.print(".");
  64.     dis_count++;
  65.     if (dis_count >= 20)
  66.     {
  67.       Serial.print("Connection failed - resetting");
  68.       delay(1000);
  69.       ESP.reset();
  70.     }
  71.   }
  72.   dis_count = 0;
  73.   Serial.println("");
  74.   Serial.print("Connected to ");
  75.   Serial.println(ssidExt);
  76.   Serial.print("IP address: ");
  77.   Serial.println(WiFi.localIP());
  78.   Serial.print("dnsIP address: ");
  79.   Serial.println(WiFi.dnsIP());
  80.   Serial.print("gatewayIP address: ");
  81.   Serial.println(WiFi.gatewayIP());
  82.   Serial.print("subnetMask address: ");
  83.   Serial.println(WiFi.subnetMask());
  84.  
  85.  
  86.   Serial.println("");
  87.   Serial.println("Configuring access point...");
  88.   /* You can remove the password parameter if you want the AP to be open. */
  89.   WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  90.   WiFi.softAP(ssid, password);
  91.  
  92.   IPAddress myIP = WiFi.softAPIP();
  93.   Serial.print("AP IP address: ");
  94.   Serial.println(myIP);
  95.  
  96.  
  97.  
  98.   Udp.begin(DNS_PORT);
  99.   Udp8.begin(DNS_PORT);
  100. }
  101.  
  102. void loop()
  103. {
  104.   if (WiFi.status() != WL_CONNECTED) {
  105.     Serial.print("WiFi Connection lost - resetting");
  106.     ESP.reset();
  107.   }  
  108.   setSignalLED();
  109.  
  110.   int noBytes = Udp.parsePacket();
  111.   if ( noBytes ) {
  112.     analogWrite(status_LED_pin, 150);
  113.         Serial.print(millis() / 1000);
  114.         Serial.print(":Packet of ");
  115.         Serial.print(noBytes);
  116.         Serial.print(" received from ");
  117.         Serial.print(Udp.remoteIP());
  118.         Serial.print(":");
  119.         Serial.println(Udp.remotePort());
  120.     // We've received a packet, read the data from it
  121.     Udp.read(packetBuffer, noBytes); // read the packet into the buffer
  122.  
  123.     // display the packet contents in HEX
  124.         for (int i = 1; i <= noBytes; i++) {
  125.           Serial.print(packetBuffer[i - 1], HEX);
  126.           if (i % 32 == 0) {
  127.             Serial.println();
  128.           }
  129.           else Serial.print(' ');
  130.         } // end for
  131.         Serial.println();
  132.  
  133.     IPAddress ip8(8, 8, 8, 8);
  134.     Udp8.beginPacket(ip8, DNS_PORT); //NTP requests are to port 123
  135.     Udp8.write(packetBuffer, noBytes);
  136.     Udp8.endPacket();
  137.     delay(100);
  138.  
  139.     int cb = Udp8.parsePacket();
  140.     if (!cb) {
  141.       Serial.println("no packet yet");
  142.       analogWrite(status_LED_pin, 0);
  143.     }
  144.     else {
  145.             Serial.print("packet received, length=");
  146.             Serial.println(cb);
  147.       byte packetBuffer8[cb];
  148.       Udp8.read(packetBuffer8, cb);
  149.       Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  150.       Udp.write(packetBuffer8, cb);
  151.       Udp.endPacket();
  152.       analogWrite(status_LED_pin, 0);
  153.     }
  154.  
  155.   } // end if
  156. }
  157.  
  158. void setSignalLED()
  159. {
  160.   if (millis() - lastSwitch < 1000) return;
  161.   lastSwitch = millis();
  162.   long rssi = WiFi.RSSI();
  163.   int val = map(rssi, -100, -50, 0, 250);
  164.   if (val > 250) val = 250;
  165.   else if (val < 0) val = 0;
  166.   analogWrite(signal_LED_pin, val);
  167.   val = map(val, 0, 250, 0, 100);
  168.   Serial.println();
  169.   Serial.println("WiFi signal: " + String(val) + "%");
  170.   Serial.println();  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement