How to send Arduino analog readout data to a web server with esp8266-01

I have a code that I made but I don't know why it won't connect me.

I can't help but I do know that those who may be able to help will ask you for your sketch so far, so why don't you post that in the meantime for folk to see as soon as they open this thread.

that's what I will do

#include <WiFi.h>
 

const char* ssid = ""; // remplazar por nombre de la red a conectarse
const char* password = "";  // remplazar por clave de acceso a la red seleccionada

int estadoLDR  = 0;
int pinLDR = A0;

WiFiServer server(80);

void setup() {
  // put your setup code here, to run once:

    Serial.begin(115200);
    WiFi.begin(ssid, password);
    Serial.println("Conectando");
    while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
    }
    Serial.println("Conectado");
    Serial.print("Direccion: ");
    Serial.println(WiFi.localIP());
    //Se inicia el servidor
    server.begin();

}

void loop() {
  // put your main code here, to run repeatedly:

    WiFiClient client = server.available();
    if(!client) { //No se detecto ningun cliente
    return;
    }
    Serial.println("Ingreso un nuevo cliente/requerimiento");
    //Se espera hasta que el cliente haya enviado todos los requerimientos
    while(!client.available()) {
    delay(1);
    }

    estadoLDR = analogRead(pinLDR);
    
    String req = client.readStringUntil('\r');
    Serial.print("Requerimiento: ");
    Serial.println(req);
    
    client.flush();
    String respuesta = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Lectura Analogica Sensor LDR</h1></html> ";
    client.print(respuesta);
    client.print("Valor: ");
    client.print(estadoLDR);
    
    delay(10);
    Serial.println("Cliente desconectado");

  
}

1 Like

Relax- Not everyone's in your timezone.... There's no point ever bumping on an international forum for 24 hours, ie until the Earth's gone round once and all the timezones have been awake. It could just be your bad luck that the best answer comes from someone who just went to sleep :wink:

in that you are right karmagician sorry

what board are you using? e.g. an ESP32
try setting the WiFi mode, e.g.

 WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

does it fail to connect to the WiFi network or the client?
I assume you see the .... on the serial monitor as it attempts to connect?
may be worth running wireshark and check WiFi traffic?

Edit: if you want to run a webserver why not use the WebServer class

then what is failing me is not having WebServer class

looking back I see the post title refers to an ESP8266-01

in your code of post #4 try replacing <WiFi.h>

#include <ESP8266WiFi.h>

if I then run your code the Serial Monitor displays

......Conectado
Direccion: 192.168.1.91
Ingreso un nuevo cliente/requerimiento
Requerimiento: GET / HTTP/1.1
Cliente desconectado

and my web browser displays
browser_1

please give me a moment

#include <ESP8266WiFi.h>

const char* ssid = ""; // remplazar por nombre de la red a conectarse
const char* password = "";  // remplazar por clave de acceso a la red seleccionada

int estadoLDR  = 0;
int pinLDR = A0;

WiFiServer server(80);

void setup() {
  // put your setup code here, to run once:

    Serial.begin(115200);
    WiFi.begin(ssid, password);
    Serial.println("Conectando");
    while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
    }
    Serial.println("Conectado");
    Serial.print("Direccion: ");
    Serial.println(WiFi.localIP());
    //Se inicia el servidor
    server.begin();

}

void loop() {
  // put your main code here, to run repeatedly:

    WiFiClient client = server.available();
    if(!client) { //No se detecto ningun cliente
    return;
    }
    Serial.println("Ingreso un nuevo cliente/requerimiento");
    //Se espera hasta que el cliente haya enviado todos los requerimientos
    while(!client.available()) {
    delay(1);
    }

    estadoLDR = analogRead(pinLDR);
    
    String req = client.readStringUntil('\r');
    Serial.print("Requerimiento: ");
    Serial.println(req);
    
    client.flush();
    String respuesta = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Lectura Analogica Sensor LDR</h1></html> ";
    client.print(respuesta);
    client.print("Valor: ");
    client.print(estadoLDR);
    
    delay(10);
    Serial.println("Cliente desconectado");

  
}

class #include <ESP8266WiFi.h> is not detected.

Have you Instaled ESP8266 Arduino Core documentation?

yes, it is already installed, but now I do not have a communication error.


Subiendo: image.png(1)...

the sensor I use is esp8266-01

Try one of the examples to see if the ESP connects.

I will verify, momens plis

remains the same in error in communication

Show us a good schematic of your proposed circuit.