Esp32 servidor wed

Hola.

Tengo un inconveniente con un servidor web que estoy diseñando con un esp32. Estoy recibiendo datos por el ADC del esp32 y graficando los valores recibidos mediante la función Chart en el esp32, la grafica sale sin problemas. Quise agregar botones en la pagina web para controlar otros procesos por el mismo servidor, el problema esta en que cuando presiono los botones, se pierde todo el muestreo de la gráfica debido a que la pagina se vuelve a reiniciar, los botones cumplen su acometido pero no quiero perder la gráfica que se llevaba antes de presionar los botones. adjunto imagen para que se entienda el ejemplo:

Allí se puede ver el funcionamiento del mismo (imagen izquierda), pero una vez que apreto algún botón se pierde el progreso tal como se ve en la imagen de la derecha

Si bien se comienza a graficar nuevamente, además de que los botones cumplen su función, no quiero que el progreso de la grafica se pierda. De momento no tengo idea de como corregir tal problema. Adjunto código del esp32:

#ifdef ESP32
  #include <WiFi.h>
  #include <ESPAsyncWebServer.h>
  #include <SPIFFS.h>
#else
  #include <Arduino.h>
  #include <ESP8266WiFi.h>
  #include <Hash.h>
  #include <ESPAsyncTCP.h>
  #include <ESPAsyncWebServer.h>
  #include <FS.h>
#endif
#include <Wire.h>

  #define led 14

// Replace with your network credentials
const char* ssid = "";
const char* password = "";

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

String readPOT()
{ 
    float valor_1;
    int pot = analogRead(33);
    Serial.println(pot);
    valor_1 =(100*pot)/4098.0;
    delay(1000);
    String myString = String(valor_1);
    return myString;

}

void setup(){
  pinMode(33, INPUT);
  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);
  
  // Serial port for debugging purposes
  Serial.begin(115200);
  bool status; 

  // Initialize SPIFFS
  if(!SPIFFS.begin()){
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println(WiFi.localIP());

  // graphic
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index.html");
  });
  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readPOT().c_str());
  });
  server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readPOT().c_str());
  });

  //button
  server.on("/led/on", HTTP_GET, [](AsyncWebServerRequest * request)
  { 
    digitalWrite(led, HIGH);
  request->send(SPIFFS, "/index.html", "text/html");
  });
  server.on("/led/off", HTTP_GET, [](AsyncWebServerRequest * request)
  { 
    digitalWrite(led, LOW);
  request->send(SPIFFS, "/index.html", "text/html");
  });

  
  // Start server
  server.begin();
}
 
void loop(){
      Serial.println(analogRead(33));
      delay(1000);
}

¿Cómo puedo corregir eso?

I apologize if I am completely off base but looking at the code and the images I think the OP has issue with the ESP32's A:D converter.

I get much better results with the ESP32 using the ESP32's A:D API over using the Arduino IDE ESP32's core.

The code below, set up the A:D converter using the A:D API. That is the link to the ESP32's API documentation.

#include <driver/adc.h>

setuo()
{
  adc1_config_width(ADC_WIDTH_12Bit);
  adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11);// using GPIO 34 wind direction
  adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_11);// using GPIO 39 current
  adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_11);// using GPIO 36 battery volts
}

This is code using the ESP32 A:D converter API.

void fReadBattery( void * parameter )
{
  //float ADbits = 4096.0f;
  //float ref_voltage = 3.3f;
  float ADscale = 3.3f / 4096.0f;
  float adcValue = 0.0f;
  float offSET = 0.0f;
  const float r1 = 50500.0f; // R1 in ohm, 50K
  const float r2 = 10000.0f; // R2 in ohm, 10k potentiometer
  //float Vscale = (r1+r2)/r2;
  float Vbatt = 0.0f;
  int printCount = 0;
  float vRefScale = (3.3f / 4096.0f) * ((r1 + r2) / r2);
  uint64_t TimePastKalman  = esp_timer_get_time(); // used by the Kalman filter UpdateProcessNoise, time since last kalman calculation
  SimpleKalmanFilter KF_ADC_b( 1.0f, 1.0f, .01f );
  TickType_t xLastWakeTime = xTaskGetTickCount();
  const TickType_t xFrequency = 1000; //delay for mS
  for (;;)
  {
    adc1_get_raw(ADC1_CHANNEL_0); //read and discard
    adcValue = float( adc1_get_raw(ADC1_CHANNEL_0) ); //take a raw ADC reading
    KF_ADC_b.setProcessNoise( (esp_timer_get_time() - TimePastKalman) / 1000000.0f ); //get time, in microsecods, since last readings
    adcValue = KF_ADC_b.updateEstimate( adcValue ); // apply simple Kalman filter
    Vbatt = adcValue * vRefScale;
    xSemaphoreTake( sema_CalculatedVoltage, portMAX_DELAY );
    CalculatedVoltage = Vbatt;
    xSemaphoreGive( sema_CalculatedVoltage );
    /*
      printCount++;
      if ( printCount == 3 )
      {
      log_i( "Vbatt %f", Vbatt );
      printCount = 0;
      }
    */
    TimePastKalman = esp_timer_get_time(); // time of update complete
    xLastWakeTime = xTaskGetTickCount();
    vTaskDelayUntil( &xLastWakeTime, xFrequency );
    //log_i( "fReadBattery %d",  uxTaskGetStackHighWaterMark( NULL ) );
  }
  vTaskDelete( NULL );
}

Link to SimpleKalmanFilter.

En realidad tu problema no esta del lado arduino, esta en la pagina web que cargas con SPIFFS y la falta de una base de datos o un simple FIFO de los datos.

El servidor web lo puedo abrir por múltiples dispositivos, por ejemplo, cuando lo abro en el celular y en la pc, puedo controlar el proceso por cualquiera de los dos dispositivos, digamos que quiero encender el led desde la pc, el led se enciende pero pierdo el contenido de la grafica en la pc (por supuesto, se comienza a graficar nuevamente pero la grafica ya no contiene los datos que habían antes de presionar los botones), pero, el contenido muestreado en el celular no se pierde, es decir, donde ejecuto los botones es donde se pierde el contenido. No se si me di a entender

thanks a lot, I will keep it in mind

El problema es que no tienes en el programa nada que almacene los datos. Al recargar la pagina empieza el gráfico con datos nuevos, porque envías nuevamente el archivo HTML al navegador. Que veas diferencia en el celular o la pc se puede deber a la configuración de ahorro de datos del celular, seguro si lo desconectas se comportaran igual.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.