Understanding how the thinger handle() routine works

Hi
I have a simple sketch which sends light level to the dashboard and also which reads a pushbutton and toggles a led (the standard example). I am using a ESP8266 based board (Wemos D1)
I would just like to understand a bit more more about what happens ‘under the hood’:
In particular since the sensors are assigned once in the setup() part of the sketch and the main loop just consists only of thing.handle(); I’m curious to know how the sensor gets read (or led responds to the button) if these are not in the main loop of the sketch.
I’ve had a look at the client code on github but it’s not easy I find, to wade through this. I wondered if anyone could give a simple explanation or direct me to some reading.
thanks and regards
Russ

Hi @rascal,

Your curiosity about what happens ‘under the hood’ in your ESP8266 setup using the Thinger.io platform is a great opportunity to delve into the inner workings of IoT communication and device management.

In your sketch, the setup() function plays a crucial role. Here, you define ‘callbacks’ within the Thinger.io client. A callback is essentially a function specified to run when a certain event occurs. In the context of Thinger.io, these callbacks are set up to interact with your sensors (like the light level sensor) or actuators (like the LED).

When you use the thing.handle(); in your main loop, it’s not just idling. This function is the heart of the Thinger.io client’s communication process. Each time it’s called, it performs several critical tasks:

  1. Connection Management: It maintains and manages the connection to the Thinger.io cloud. This includes handling reconnections if the connection is lost.
  2. Data Handling: It checks for incoming requests from the cloud. For example, if there’s a request to read the light sensor’s data, Thinger.io triggers the corresponding callback function that you defined in the setup(). This way, even though the sensor reading code isn’t explicitly in your main loop, it gets executed when requested by the cloud.
  3. Actuator Control: Similarly, if there’s an instruction to toggle the LED (like from a dashboard button or an API call), the client receives this command and executes the callback function responsible for controlling the LED.
  4. Sensor Data Transmission: It also handles the automatic sending of sensor data to the cloud at predefined intervals or under specific conditions that you’ve set up in your callbacks.
  5. Efficiency and Optimization: This approach ensures efficient use of the device’s resources. Instead of constantly polling sensors or checking device states, the thing.handle(); function allows the device to react to specific requests or commands, reducing unnecessary processing and power consumption.

In summary, the Thinger.io platform abstracts the complexity of direct sensor readings and actuator control into these callback functions. The thing.handle(); function orchestrates the interaction between your device and the cloud, ensuring data is exchanged and actions are performed as needed, without cluttering your main loop with explicit sensor reading or device control code.

Hope it helps!