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

add authorization token to WifiManager #8

Open
coelner opened this issue Feb 13, 2018 · 6 comments
Open

add authorization token to WifiManager #8

coelner opened this issue Feb 13, 2018 · 6 comments
Assignees
Milestone

Comments

@coelner
Copy link

coelner commented Feb 13, 2018

it is possible to add custom entries to the wifimanager (https://github.com/tzapu/WiFiManager#custom-parameters)
if we add an option to add the token while the setup process, we could provide precompiled images.

@thgeorgiou
Copy link
Member

Hello,
since the Arduino IDE doesn't provide a clean interface for uploading precompiled binaries I am not sure I see the benefit of going through the trouble. Maybe the platform can generate sketches with the API key baked in as an alternative.

@wiebkehere
Copy link

wiebkehere commented Feb 14, 2018 via email

@coelner
Copy link
Author

coelner commented Feb 18, 2018

this is the git diff for this feature. I don't know the real length of the token, this need to changed accordingly.

diff --git a/examples/Wemos-Advanced/Wemos-Advanced.ino b/examples/Wemos-Advanced/Wemos-Advanced.ino
index 819fc81..dfb72d3 100644
--- a/examples/Wemos-Advanced/Wemos-Advanced.ino
+++ b/examples/Wemos-Advanced/Wemos-Advanced.ino
@@ -24,7 +24,8 @@
 
 // Replace the string below with your Authorization Token from the hackAIR
 // platform
-#define AUTHORIZATION "AUTHORIZATION TOKEN"
+//#define AUTHORIZATION "AUTHORIZATION TOKEN"
+char hackair_auth_token[50] = "";
 
 // Sensor initialization
 hackAIR sensor(SENSOR_SDS011);
@@ -59,8 +60,13 @@ void setup() {
   // Initialize temperature and humidity sensor
   dht.begin();
 
+  // add token per webUI
+  WiFiManagerParameter hackair_authorization_token("hackair_auth_token", "hackAIR AUTH Token", hackair_auth_token, 49);
   // Initialize the WiFi connection
   WiFiManager wifiManager;
+  wifiManager.addParameter(&hackair_authorization_token);
+
+
   if (!wifiManager.autoConnect("ESP-wemos")) {
     Serial.println("failed to connect, please push reset button and try again");
     delay(3000);
@@ -71,6 +77,8 @@ void setup() {
   Serial.println("network connected:)");
   Serial.println("Your local ip is:");
   Serial.println(WiFi.localIP());
+  //copy token to our space
+  strcpy(hackair_auth_token, hackair_authorization_token.getValue());
 }
 
 void loop() {
@@ -129,7 +137,8 @@ void loop() {
     client.print("Host: api.hackair.eu\r\n");
     client.print("Connection: close\r\n");
     client.print("Authorization: ");
-    client.println(AUTHORIZATION);
+    //client.println(AUTHORIZATION);
+    client.println(hackair_auth_token);
     client.print("Accept: application/vnd.hackair.v1+json\r\n");
     client.print("Cache-Control: no-cache\r\n");
     client.print("Content-Type: application/json\r\n");

@thgeorgiou
Copy link
Member

Hello,
I was just working on this but I realized that the custom settings are not being saved by the WiFi manager so they don't persist through reboots. This is problematic because the WiFi network is stored and if there is a power loss the device will enter a state of confusion, knowing where to connect but not the API key.

I see a couple of paths going on:

  • We can add WiFiManager.clear() at the start of the sketch to clear the saved WiFi network and redo the whole setup process on every boot. This is tedious since temporarily unplugging the device will reset all the settings.
  • Save the API key to the EEPROM ourselves. This will cause the slightly confusing issue that WiFiManager.clear() will erase the saved networks but not the API key.
  • Fix this upstream (at the WiFiManager project). I think this is the best solution but it's not very simple as the fix would have to accommodate all different kinds of settings (lenghts, etc) and save them to the EEPROM.

Tell me your opinion on this so we can plan our next move.

@wiebkehere
Copy link

I vote for option #2.

@thgeorgiou thgeorgiou self-assigned this Mar 21, 2018
@thgeorgiou thgeorgiou added this to the Version 1.0 milestone Mar 21, 2018
@coelner
Copy link
Author

coelner commented Mar 21, 2018

option 2 is the predefined behaviour (https://github.com/tzapu/WiFiManager#custom-parameters)
.
WiFiManager.clear() only uses the features of the esp8266 core, which internally save the last connected network. Maybe this could be helpful: https://arduino-esp8266.readthedocs.io/en/latest/libraries.html#esp-specific-apis

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

3 participants