Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect to existing WiFi #47

Open
justusiv opened this issue Jul 3, 2020 · 8 comments
Open

Connect to existing WiFi #47

justusiv opened this issue Jul 3, 2020 · 8 comments

Comments

@justusiv
Copy link

justusiv commented Jul 3, 2020

In my use case i would prefer the WiFi duck connect to my home WiFi instead of creating a WiFi that devices connect to. I have setup the ESP8266 to connect to my WiFi to host a web page, but would not have the skill set to get the WiFi duck to do it.
Thoughts.

@justusiv justusiv added the question Further information is requested label Jul 3, 2020
@welcome
Copy link

welcome bot commented Jul 3, 2020

Thanks for opening your first issue here! 🎉
👉 Be sure to:
1. 📖 Have a look at the Wiki and README for information
2. 🔍 Search for similar issues (open and closed)
3. ✍️ Provide enough information to understand, recreate and help out with your problem
4. ℹ️ Let us know if you find a solution
5. 📕 Close the issue when your problem has been solved

@aroslanov
Copy link

Same question. I bought WIFI Duck to control my KVM switch using local WIFI. Is there a workaround exists or we'll have to rewrite part of the code?

@spacehuhn spacehuhn added feature request and removed question Further information is requested labels Sep 7, 2020
@ghost
Copy link

ghost commented Sep 25, 2020

Would love this feature.

I also want to controll my KVM switch.

@unarat
Copy link

unarat commented Oct 20, 2020

I made some changes that work. Instead of creating an AP, it will now connect to an existing AP using the SSID and PWD in the config.h file.

It will not be difficult to add some settings that will allow a creation or connection of an AP. I think one solution would be to check if the channel is set to Zero, if it is, connect to the SSID instead of create it.

Changes at lines 82 & 181
webserver.cpp.txt

Script.js change
Line 117: ws = new WebSocket("ws://" + location.hostname + "/ws");

After changing the Script.js you will have to run webconverter.py

@Jorgen-VikingGod
Copy link

Jorgen-VikingGod commented Feb 23, 2021

I managed to do it this way (in case your network could not resolve hostnames)

Connecting WiFi and save the current IP into a file called config.js

  WiFi.begin(STA_SSID, STA_PASSWORD);
  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.printf("STA: Failed!\n");
    WiFi.disconnect(false);
    delay(1000);
    WiFi.begin(STA_SSID, STA_PASSWORD);
  }
  saveConfigToJS();
void saveConfigToJS() {
  // Delete existing file, otherwise the configuration is appended to the file
  SPIFFS.remove("/config.js");
  // Open file for writing
  File file = SPIFFS.open("/config.js", FILE_WRITE);
  if (!file) {
    Serial.println(F("Failed to create file"));
    return;
  }
  file.print("var ip = '" + WiFi.localIP().toString() + "';");
  debugln("var ip = '" + WiFi.localIP().toString() + "';");
  // Close the file
  file.close();
}

in all html files using script.js, like index.html and terminal.html please load config.js before loading all other js files

  <script src="config.js"></script>
  <script src="script.js"></script>
  <script src="index.js"></script>

On top of script.js add these lines:

var address = ip;

in method ws_init use this address

function ws_init() {
  status('connecting...');
  ws = new WebSocket('ws://' + address + '/ws');

This way the ESP modifies the config.js file each time it becomes a new address and the website can connect the websocket to the right server.

@RayPS
Copy link

RayPS commented Jul 3, 2021

+1 I want this feature.

Thanks to @Jorgen-VikingGod I've able to get it working.
But the more simpler solution I did on the second step is just this line

ws = new WebSocket("ws://" + location.host + "/ws");

So no need to saveConfigToJS and other modification to the html.

@Jorgen-VikingGod
Copy link

@RayPS awesome - so simple is the world 👍
I'll adapt my code as well. I didn't read the doc of location.host - thought it was only the hostname; but it handles the ip address too.
What an easy and smart solution 🥳

Thx for sharing your findings!

@justusiv
Copy link
Author

justusiv commented Jan 25, 2022

I have yet to try this but since i just got my duck working thanks to the other threads i will be giving this a go. I will say that i love the way wled https://github.com/Aircoookie/WLED handles this scenario. if it finds the wifi you setup it will connect to that if not it will spin up an AP. sort of the best of both worlds and would not force to to provide connection details at the time of flashing. I am not smart enough to figure out how they have done it but maybe someone here would be be able to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants