MQTT controlled loop

Hi all! I have a practical issue that has to do with my project in which I am using an ESP8266 with Mongoose OS.

  1. My goal is:
    I would like the ESP8266 to receive a signal through MQTT that starts a loop (does not matter what type of loop) and stops this loop. Assume that there is a sender who sends True via MQTT to my ESP8266. This sender is a Firebase data base that reached my ESP8266 via MQTT implemented in Google Functions. The sender works, the MQTT connection works and is used through my project.
    MY ESP8266 has an LED and a light intensity sensor connected.

In the loop, I would like to measure light intensity and adjust the light of an LED accordingly (higher surrounding light intensity = lower LED light). This loop should start when my ESP8266 receives a Boolean true via the topic “topic”. So what I want is that the ESP8266 receives an MQTT message since it is subscribed to the topic called “topic”. Then, the following functions starts: “mqtt::add_command_handler(“topic”, [](std::string msg) {…}”, and this function somehow implements this loop.

  1. My actions are:
    I have working code for the LED and the light intensity sensor, and the MQTT. I instantiate an object in the main.cpp file for the light intensity sensor and the LED. Using them, I can set the LED light and measure the light intensity. However, they are only available in the scope of the main.cpp file, not outside.

In my led.cpp, I have something like the following:
void led_lamp::begin() {
automatic_led_interval = mgos_utils::interval(“led_lamp”, update_seconds*1000, this{
if(automatic_led_loop.activated) {
float intensity = light_intensity_sensor.measure_ligh_intensity(); //This line does not work
change_intensity_smoothly(intensity);
}
});
}

In the above piece of code, the light_intensity_sensor is not available (that is instantiated in the main.cpp file). This is my main issue.

  1. The result I see is:

I have not results yet.

  1. My expectation & question is:

How can I access my light intensity sensor object within a separate led.cpp file? The loop that adjust the LEd according to the light intensity is within the led.cpp file and so I do not have access to the light intensity sensor.

I have stumbled upon the following functions (that could help you understand what I want) that seemed to be somewhat relevant to what I want to implment:

  • mgos_utils::interval
  • using function_task_t = std::function<void(void)>;

Many thanks in advance for spending your time on reading this! I would really appreciate your input and thought with regard to this issue!

All the best,
Mark

You seem to be trying to describe some sort of control loop.
If you are dreaming on closing the loop with MQTT, search this forum for some response I already gave to someone else and why it did not work for him and probably won’t work for no one else unless that one clearly knows what he is doing and the application is delay tolerant.
If you would like to control things in a tight loop by applying the centuries of knowledge in Control Theory, and change operational parameters through the net via MQTT or whatever, go ahead, everybody does that since the 90’s; there is no need to try to invent the software equivalent of the wheel.

You can pass a light_intensity_sensor pointer to led_lamp::begin().