Esp8266 Serial.read problem

Hello everyone, i am doing a project that needs serial comunication between an esp8266 and an arduino UNO, and i been habing a lot of problems with the code, before the serial print of what the esp received was numbers like 10,13,52... etc, when i was printing a constant 5 with the uno. i have been using esp-01 and nodeMCU to make the test, but everything its the same, and now the esp doesnt receive anything, or at least doesnt print anything. and i cant program the esp-01 becose of this error. please i need a lot of help¡¡¡¡¡ (I am a total noob, please be patient)

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
//#include <SoftwareSerial.h>
//SoftwareSerial SerialESP(2, 3);

#ifndef STASSID
#define STASSID "INFINITUMagfc"
#define STAPSK "99b6aaba8c"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

WiFiServer server(80);

char valor;

void setup() {
  Serial.begin(115200);
  //  SerialESP.begin(9600);
  Serial.println("");
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }

  // Port defaults to 8266
  // ArduinoOTA.setPort(8266);

  // Hostname defaults to esp8266-[ChipID]
  // ArduinoOTA.setHostname("myesp8266");

  // No authentication by default
  // ArduinoOTA.setPassword("admin");

  // Password can be set with it's md5 value as well
  // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");

  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else {  // U_FS
      type = "filesystem";
    }

    // NOTE: if updating FS this would be the place to unmount FS using FS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
  Serial.println(F("Server started"));
}

void loop() {
  ArduinoOTA.handle();
  leerSerial();

  WiFiClient client = server.accept();
  if (!client) {
    return;
  }

  //  client.print("Valor: ");
  //  client.print(valor);
  //  client.print("buebos");

  delay(500);
}

void leerSerial() {
  if (Serial.available() > 0) {
    Serial.print("Valor: ");
    while (Serial.available() > 0) {
      valor = Serial.read();
      Serial.println(valor);
    }
  }
}

Arduino:1.8.19 (Windows Store 1.8.57.0) (Windows 10), Tarjeta:"Generic ESP8266 Module, 80 MHz, Flash, Disabled (new aborts on oom), Disabled, All SSL ciphers (most compatible), 32KB cache + 32KB IRAM (balanced), Use pgm_read macros for IRAM/PROGMEM, dtr (aka nodemcu), 26 MHz, 40MHz, DOUT (compatible), 1MB (FS:256KB OTA:~374KB), 2, nonos-sdk 2.2.1+100 (190703), v2 Lower Memory, Disabled, None, Sketch + WiFi Settings, 115200"

. Variables and constants in RAM (global, static), used 29028 / 80192 bytes (36%)

║ SEGMENT BYTES DESCRIPTION

╠══ DATA 1504 initialized variables

╠══ RODATA 1356 constants

╚══ BSS 26168 zeroed variables

. Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR), used 60824 / 65536 bytes (92%)

║ SEGMENT BYTES DESCRIPTION

╠══ ICACHE 32768 reserved space for flash instruction cache

╚══ IRAM 28056 code in IRAM

. Code in flash (default, ICACHE_FLASH_ATTR), used 286580 / 1048576 bytes (27%)

║ SEGMENT BYTES DESCRIPTION

╚══ IROM 286580 code in flash

esptool.py v3.0

Serial port COM8

Connecting......................................____

A fatal esptool.py error occurred: Failed to connect to ESP8266: Timed out waiting for packet headerTraceback (most recent call last):

File "C:\Users\sanje\OneDrive\Documentos\ArduinoData\packages\esp8266\hardware\esp8266\3.1.2/tools/upload.py", line 72, in

os.remove(erase_file)

PermissionError: [WinError 32] El proceso no tiene acceso al archivo porque est� siendo utilizado por otro proceso: 'C:\Users\sanje\AppData\Local\Temp\tmp_rogjm6g'

_

el puerto seleccionado _

no existe o tu placa no esta conectada

Este informe podría contener más información con
"Mostrar salida detallada durante la compilación"
opción habilitada en Archivo -> Preferencias.

You can’t do much with that way of reading Serial

I would suggest to study Serial Input Basics to handle this

Also remember your ESP is using 3.3V and your UNO is using 5V. Don’t Tx 5V from the Arduino to the ESP’s Rx pin

are you using EspSoftwareSerail Implementation of the Arduino software serial library for the ESP8266 / ESP32 family

you have compiled but not uploaded.

can you show us also the code you have on the UNO ?

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
//#include <SoftwareSerial.h>
//SoftwareSerial SerialESP(2, 3);

#ifndef STASSID
#define STASSID "INFINITUMagfc"
#define STAPSK "99b6aaba8c"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  //  SerialESP.begin(9600);
  Serial.println("");
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }

  // Port defaults to 8266
  // ArduinoOTA.setPort(8266);

  // Hostname defaults to esp8266-[ChipID]
  // ArduinoOTA.setHostname("myesp8266");

  // No authentication by default
  // ArduinoOTA.setPassword("admin");

  // Password can be set with it's md5 value as well
  // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");

  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else {  // U_FS
      type = "filesystem";
    }

    // NOTE: if updating FS this would be the place to unmount FS using FS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) {
      Serial.println("Auth Failed");
    } else if (error == OTA_BEGIN_ERROR) {
      Serial.println("Begin Failed");
    } else if (error == OTA_CONNECT_ERROR) {
      Serial.println("Connect Failed");
    } else if (error == OTA_RECEIVE_ERROR) {
      Serial.println("Receive Failed");
    } else if (error == OTA_END_ERROR) {
      Serial.println("End Failed");
    }
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
  Serial.println(F("Server started"));
}

void loop() {
  ArduinoOTA.handle();
  leerSerial();

  WiFiClient client = server.accept();
  if (!client) {
    return;
  }

  //  client.print("Valor: ");
  //  client.print(valor);
  //  client.print("buebos");

  delay(500);
}

void leerSerial() {
  if (Serial.available() > 0) {
    Serial.print("Valor: ");
    char valor = Serial.read();
    if (valor >= ' ') Serial.print(valor);
    else Serial.println();
  }
}

that is the code for the ESP.

I´ll study a lot.
i use my esp8266 with an external source of 3.3v, and i am using a tensor divisor on the reception pin of esp.

void setup() {
  Serial.begin(9600);
}
void loop() {

    Serial.println(5);
    delay(1000);
}

As simple as that

this was the solution, thank you!!!!!

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