UserMod - unable to connect wifi

Hi there,

I’m really new in wled, but it rocks so much!!

My led project is a connected bedside lamp, based on wemos D1 mini.

For that project, I need to control brightness with a potentiometer, so i would implement a custom sketch to read analog pin.

I first flash a full blank image to the wemos.

I’ve dl git master (0.10.2) , configure libs, … and i’m perfectly able to compile the wled000, fully functionnal, connect to my home wifi, interact with my domotic by mqtt, great!

So i’m confident in my setup.

And then i’ve add my “usermode_potar_set.h” modify from usermode rotaryEncoder from steveway

code
#pragma once

#include "wled.h"

//v2 usermod that allows to change brightness and color using a rotary encoder, 
//change between modes by pressing a button (many encoder have one included)
class PotarBrightSet : public Usermod
{
private:
  //Private class members. You can declare variables and functions only accessible to your usermod here
  unsigned long lastTime = 0;
  /*
** Rotary Encoder Example
** Use the Sparkfun Rotary Encoder to vary brightness of LED
**
** Sample the encoder at 500Hz using the millis() function
*/

int fadeAmount = 5; // how many points to fade the Neopixel with each step
int sensorPin = A0;    // select the input pin for the potentiometer 
int sensorValue = 0;  // variable to store the value coming from the sensor
unsigned long currentTime;
unsigned long loopTime;
unsigned long lastValue;
unsigned int brightValue;

public:
  //Functions called by WLED

  /*
     * setup() is called once at boot. WiFi is not yet connected at this point.
     * You can use it to initialize variables, sensors or similar.
     */
  void setup()
  {
    //Serial.println("Hello from my usermod!");
    currentTime = millis();
    loopTime = currentTime;
    lastValue = 0;
    brightValue = 0;
  }

  /*
     * connected() is called every time the WiFi is (re)connected
     * Use it to initialize network interfaces
     */
  void connected()
  {
    //Serial.println("Connected to WiFi!");
  }

  /*
     * loop() is called continuously. Here you can check for events, read sensors, etc.
     * 
     * Tips:
     * 1. You can use "if (WLED_CONNECTED)" to check for a successful network connection.
     *    Additionally, "if (WLED_MQTT_CONNECTED)" is available to check for a connection to an MQTT broker.
     * 
     * 2. Try to avoid using the delay() function. NEVER use delays longer than 10 milliseconds.
     *    Instead, use a timer check as shown here.
     */
  void loop()
  {
    currentTime = millis();
    if (currentTime >= (loopTime + 2)) // 2ms since last check of encoder = 500Hz
    {
        sensorValue = analogRead(sensorPin);
        if (abs(lastValue - sensorValue) > 3 ){
          brightValue = 255*sensorValue/1024;
          if(brightValue <2)
          {
            brightValue = 0;
          }else if (brightValue > 253)
          {
             brightValue = 255;
          }
          bri = brightValue;
          //Serial.println(brightValue);
          lastValue = sensorValue;
          //call for notifier -> 0: init 1: direct change 2: button 3: notification 4: nightlight 5: other (No notification)
          // 6: fx changed 7: hue 8: preset cycle 9: blynk 10: alexa
          colorUpdated(6);
          //updateInterfaces();
        }
       loopTime = currentTime; // Updates loopTime
    }
  }

  /*
     * addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
     * Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI.
     * Below it is shown how this could be used for e.g. a light sensor
     */
  /*
    void addToJsonInfo(JsonObject& root)
    {
      int reading = 20;
      //this code adds "u":{"Light":[20," lux"]} to the info object
      JsonObject user = root["u"];
      if (user.isNull()) user = root.createNestedObject("u");

      JsonArray lightArr = user.createNestedArray("Light"); //name
      lightArr.add(reading); //value
      lightArr.add(" lux"); //unit
    }
    */

  /*
     * addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object).
     * Values in the state object may be modified by connected clients
     */
  void addToJsonState(JsonObject &root)
  {
    //root["user0"] = userVar0;
  }

  /*
     * readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object).
     * Values in the state object may be modified by connected clients
     */
  void readFromJsonState(JsonObject &root)
  {
    //userVar0 = root["user0"] | userVar0; //if "user0" key exists in JSON, update, else keep old value
    //if (root["bri"] == 255) Serial.println(F("Don't burn down your garage!"));
  }

  /*
     * getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!).
     * This could be used in the future for the system to determine whether your usermod is installed.
     */
  uint16_t getId()
  {
    return 0xABBB;
  }

  //More methods can be added in the future, this example will then be extended.
  //Your usermod will remain compatible as it does not need to implement all methods from the Usermod base class!
};

sorry, i can’t see the code balise, except the “preformatted text”

I’ve added to `usermods_list.cpp

    #include "usermode_potar_set.h"

    void registerUsermods()
    {
      usermods.add(new PotarBrightSet());
    }

Then I compile and send it to the wemos (arduino ide).

=> And that works, i’m able to control brightness with this potentiometer!

But here comes issues.

The wifi access point seems quite unstable, but I’m able to connect to it oftenly.

I set up my wifi ssid and password, and save - reboot or not, i’ve try both.
Without reboot:
wled-AP remain active,
If i stay on the page, i reconnect to wled UI, of course no connection to my router

With reboot
wled-AP wifi desapear, but it do not connect to my wifi!

Sometime I’ve got a really transcient feeling it connect to my router, but desapear immediatly.

I’ve try to connect it to a guest network locked in 2.4GHz if there would be any issue with 5GHz

And if i recompile master unmodded wled, it works as it should…

To Conclude, I’m able to compile master branch and works perfectly,*
Add usermod, functionnal, but unable to connect to my wifi.

Have yoiu got any clues?

Note I’m not used to compile with arduino ide btw.

Ben

Ok, long story short,

I do a massive cleaning of the code, discarding everything unused and changing all var name… almost no effect, but I was able to see the device connection to my wifi for some seconds.

All in all, I ve erase all the file, and add line after line and compile each time.

SOOOooooo
It doesn’t like the analogRead every two ms…

I’ve extended the time between each read, and it seems working perfectly…

1 Like