theelims / ESP32-sveltekit

A simple and extensible framework for ESP32 based IoT projects with a feature-rich, beautiful, and responsive front-end build with Sveltekit, Tailwind CSS and DaisyUI. This is a project template to get you started in no time with a fully integrated build chain.
https://theelims.github.io/ESP32-sveltekit/
Other
85 stars 15 forks source link

web interface #56

Open JonyBest opened 1 month ago

JonyBest commented 1 month ago

Help, I can't figure out how to transfer the sensor value to the web interface. You can use a detailed example with an explanation.

theelims commented 1 month ago

@JonyBest It is described in the docs how to update a state and push a new value to the web interface: https://theelims.github.io/ESP32-sveltekit/statefulservice/#read-update-state

You could define your sensor class

class SensorState {
 public:
  uint32_t sensorValue = 255;

// remaining code like in the lightstate example
};

and update it in your Arduino loop()-function

void loop()
{
  uint32_t readSensor = sensor.read();
  sensorStateService.update([&](SensorState& state) {
    state.sensorValue = readSensor;  
    return StateUpdateResult::CHANGED; 
  }, "loop");

  delay(100);
}