Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define IP_FORWARD 1
- #include <ESP8266WiFi.h>
- #include <WiFiUDP.h>
- int status_LED_pin = D6;
- int signal_LED_pin = D1;
- unsigned long lastSwitch = 0;
- /* Set these to your desired credentials for ESP8266 AP. */
- const char *ssid = "wemos SSID";
- const char *password = "my pass";
- IPAddress apIP(192, 168, 4, 1);
- // Setup credentials for original WiFi, that we plan to repeat
- const char* ssidExt = "router SSID";
- const char* passwordExt = "my pass";
- const byte DNS_PORT = 53;
- // A UDP instance to let us send and receive packets over UDP
- WiFiUDP Udp;
- byte packetBuffer[512]; //buffer to hold incoming and outgoing packets
- WiFiUDP Udp8;
- // Update these with values suitable for your network.
- IPAddress ip(192, 168, 2, 61); //Node static IP
- IPAddress gateway(192, 168, 2, 1);
- IPAddress subnet(255, 255, 255, 0);
- uint8_t dis_count = 0;
- void setup()
- {
- Serial.begin(115200);
- Serial.println();
- Serial.println("!!!!!!!!!!!!!!!!!!!++");
- pinMode(status_LED_pin, OUTPUT);
- analogWrite(status_LED_pin, 0);
- pinMode(signal_LED_pin, OUTPUT);
- analogWrite(signal_LED_pin, 0);
- lastSwitch = millis();
- WiFi.mode(WIFI_AP_STA);
- //WiFi.mode(WIFI_AP);
- WiFi.setOutputPower(20.5);
- WiFi.begin(ssidExt, passwordExt);
- WiFi.setOutputPower(20.5);
- WiFi.config(ip, gateway, subnet);
- //Wifi connection
- while (WiFi.status() != WL_CONNECTED) {
- analogWrite(status_LED_pin, 150);
- delay(250);
- analogWrite(status_LED_pin, 0);
- delay(250);
- Serial.print(".");
- dis_count++;
- if (dis_count >= 20)
- {
- Serial.print("Connection failed - resetting");
- delay(1000);
- ESP.reset();
- }
- }
- dis_count = 0;
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssidExt);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- Serial.print("dnsIP address: ");
- Serial.println(WiFi.dnsIP());
- Serial.print("gatewayIP address: ");
- Serial.println(WiFi.gatewayIP());
- Serial.print("subnetMask address: ");
- Serial.println(WiFi.subnetMask());
- Serial.println("");
- Serial.println("Configuring access point...");
- /* You can remove the password parameter if you want the AP to be open. */
- WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
- WiFi.softAP(ssid, password);
- IPAddress myIP = WiFi.softAPIP();
- Serial.print("AP IP address: ");
- Serial.println(myIP);
- Udp.begin(DNS_PORT);
- Udp8.begin(DNS_PORT);
- }
- void loop()
- {
- if (WiFi.status() != WL_CONNECTED) {
- Serial.print("WiFi Connection lost - resetting");
- ESP.reset();
- }
- setSignalLED();
- int noBytes = Udp.parsePacket();
- if ( noBytes ) {
- analogWrite(status_LED_pin, 150);
- Serial.print(millis() / 1000);
- Serial.print(":Packet of ");
- Serial.print(noBytes);
- Serial.print(" received from ");
- Serial.print(Udp.remoteIP());
- Serial.print(":");
- Serial.println(Udp.remotePort());
- // We've received a packet, read the data from it
- Udp.read(packetBuffer, noBytes); // read the packet into the buffer
- // display the packet contents in HEX
- for (int i = 1; i <= noBytes; i++) {
- Serial.print(packetBuffer[i - 1], HEX);
- if (i % 32 == 0) {
- Serial.println();
- }
- else Serial.print(' ');
- } // end for
- Serial.println();
- IPAddress ip8(8, 8, 8, 8);
- Udp8.beginPacket(ip8, DNS_PORT); //NTP requests are to port 123
- Udp8.write(packetBuffer, noBytes);
- Udp8.endPacket();
- delay(100);
- int cb = Udp8.parsePacket();
- if (!cb) {
- Serial.println("no packet yet");
- analogWrite(status_LED_pin, 0);
- }
- else {
- Serial.print("packet received, length=");
- Serial.println(cb);
- byte packetBuffer8[cb];
- Udp8.read(packetBuffer8, cb);
- Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
- Udp.write(packetBuffer8, cb);
- Udp.endPacket();
- analogWrite(status_LED_pin, 0);
- }
- } // end if
- }
- void setSignalLED()
- {
- if (millis() - lastSwitch < 1000) return;
- lastSwitch = millis();
- long rssi = WiFi.RSSI();
- int val = map(rssi, -100, -50, 0, 250);
- if (val > 250) val = 250;
- else if (val < 0) val = 0;
- analogWrite(signal_LED_pin, val);
- val = map(val, 0, 250, 0, 100);
- Serial.println();
- Serial.println("WiFi signal: " + String(val) + "%");
- Serial.println();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement