hackAir-Arduino icon indicating copy to clipboard operation
hackAir-Arduino copied to clipboard

add authorization token to WifiManager

Open coelner opened this issue 8 years ago • 6 comments

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.

coelner avatar Feb 13 '18 18:02 coelner

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.

thgeorgiou avatar Feb 14 '18 11:02 thgeorgiou

At the same time, it adding a field for the authorisation token to the wifimanager will allow us to preprogramme sensors - less technical users then just have to enter their wifi details + auth code at home, and they are set, no need to touch the IDE for them.

Cheerio * W.

On Wed, Feb 14, 2018 at 12:34 PM, Thanasis Georgiou < [email protected]> wrote:

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.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/hackair-project/hackAir-Arduino/issues/8#issuecomment-365576556, or mute the thread https://github.com/notifications/unsubscribe-auth/Ad8nOFO063Fv_e4ksMPXj_D-x-2hPsfRks5tUsS2gaJpZM4SEOFJ .

-- Wiebke Herding (+31 61 55 073 66) ON:SUBJECT | Momentum for Sustainability

[email protected] http://www.onsubject.eu

wiebkehere avatar Feb 14 '18 12:02 wiebkehere

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");

coelner avatar Feb 18 '18 17:02 coelner

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.

thgeorgiou avatar Mar 21 '18 09:03 thgeorgiou

I vote for option #2.

wiebkehere avatar Mar 21 '18 09:03 wiebkehere

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

coelner avatar Mar 21 '18 16:03 coelner