ESP8266 WiFi settings in EEPROM



  • Hi!

    Is there a way to use WiFi SSID/password/etc. stored in EEPROM?
    As I can see all WiFi initialization hidden from "end-user".

    I want to read config from EEPROM or filesystem and use some web-based (re-)configuration (as example https://github.com/tzapu/WiFiManager) but can't because WiFi stack initialized internally with defines.

    BTW, while general wifi settings can be re-defined to local variables, MQTT Client Gateway uses "inplace" string join so can not be compiled with variables.


  • Admin

    If you have some idea how to solve it, please submit a PR.



  • @hek This is my first try with MySensors, so just thinking of loud:

    One way: Globally redesign internal initialization sequence and add extra EEPROM arguments. Also provide function to write new SSID/password to EEPROM and reboot. Read SSID/IP/password/etc from config in TransportXXX

    Another way: Be more Arduin-ish. All standard Arduino classes have function begin or/and class initializer. So I want to call for example mySensors.begin() for basic init, and some kind of mySensorsTransportXXXBegin(ssid, password, ip, topic, etc) for specific transport init. As an option should be flag like "already connected" or "do not reconnect" for WiFi

    As for me, the second way is preferred. If I decide to make my next sensor on MySensors instead of "pure" C/Arduino, I'll refactor at least MyMainESP8266.c:loop_wrapper by removing setup call and adding separate public _begin() alternative


  • Mod

    @Alexander-Voronin I recently found out that the ESP will always store the last access point & password it connected to in flash anyway, so just connecting once with new settings should be sufficient (does however need modifications to MySensors library to not pass this stuff on connect)
    Maybe the same holds for IP address etc.



  • @Yveaux This is slightly not my case. The ESP stores only SSID/Password, but I want to store extra config for at least MQTT broker.

    The main issue is that WiFi.begin() and awaiting for WiFi.state == WL_CONNECTED logic executed before setup() so I can not invoke web-config or change hardcoded AP name. In my previous "pure" arduino sensor (https://bitbucket.org/esp8266/sensor-v2) I use WiFiMulti to connect first available pre-configured AP's, so I need at least two AP/password settings (for example home and work wifi)

    BTW, de-facto MySensors internally "ignores" last saved AP because calls begin(SSID, PWD) in MyGatewayTransportXXX.cpp but it is good idea as for me. As an option - do not call WiFi.begin() if wifi state already "connected" but required some functionality to force reconnect to /other/ AP

    So I need 4 steps:

    1. Try connect to saved AP (may be more than one)
    2. If not success, bring up softAP and Web/Telnet configurator
    3. Reconnect to new configured AP or reboot an goto step 1
    4. Start MySensors with already connected WiFi and new SSID

  • Admin



  • @hek Thanks! Looks promising. I''ll try it.

    This may solve an issue with AP (compiled ok but untested char[] as MY_ESP8266_SSID) but not for MQTT transport - a lot of PSTR(MY_MQTT_XXX) "/+/+/+" code required refactoring to be used with at least const char*



  • OK!
    All, except MY_MQTT_xxx_TOPIC_PREFIX can be used as variables:

    char ssid[32] = "MyAP";
    char pwd[32] = "MyPass";
    word mqttPort = 1883;
    char mqttHost[64] = "my.mqtt.example.com";
    
    #define MY_GATEWAY_ESP8266
    #define MY_GATEWAY_MQTT_CLIENT
    #define MY_ESP8266_SSID (ssid)
    #define MY_ESP8266_PASSWORD (pwd)
    
    #define MY_PORT (mqttPort)
    #define MY_CONTROLLER_URL_ADDRESS (mqttHost)
    #define MY_MQTT_CLIENT_ID  (String("MyS_") + String(ESP.getChipId(), HEX)).c_str()
    
    // DOES NOT SUPPORT VARS:
    #define MY_MQTT_PUBLISH_TOPIC_PREFIX "/MySe/00/out"
    #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "/MySe/00/in"
    
    #include <MySensor.h>
    
    void before() {
      // read config into local vars, e.g. from FS via json
      strncpy(ssid, "AltSSID", sizeof(ssid)); // example
    }
    
    void setup() {
    }
    
    void loop() {
    }
    

    FYI, The wifi manager from first post is not so useful here for me - on first look there is some issues with dhcp vs static ip, so I'll try to find another solution or write my own :)


  • Mod

    @Alexander-Voronin Great to hear your issue is solved, but this solution will permanently claim 128 of the scarce ATMega RAM...



  • @Yveaux Not all but enough. Hmm how do you plan to use mqtt with wifi with ATMega? ;) IMHO it is not so good :)

    So this solution is for somebody, who need dynamic reconfiguration of system settings. Remember, that char[] sizes just example and may be reduced or changed to String(...).c_str(). But I don't know arduino well (prefer pure C) and can't predict memory usage/fragmentation on that way.

    BTW, as an idea - add wifi and/or mqtt settings as MyMessage to be changed via "standard" way


  • Mod

    @Yveaux said:

    scarce ATMega RAM...

    I mean ESP RAM ofcourse... :facepunch:



  • @Yveaux I have 34 of 80k RAM used for this sketch, so 128 bytes looks like "a bit more".

    You can not use progmem with ESP and mysensors now because PSTR redefined as PSTR(x) (x) so all strings are really in RAM :(


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.