Problem opening serial port without resetting LoLin NodeMCU ESP8266

Hi to everybody. I am using aN esp8266 Lolin NodeMCU v3 to collect data from some sensors printing data on the serial port. Then i developed a Processing application for reading from serial port and for writing data on a txt file. My processing application must run on a raspberry pi 3b+ connected to esp8266 through usb.
once i programmed my esp8266 from my pc(COM5), i can access serial data because the acquisition occurs correctly, but if i disconnect the usb and then i connect the usb again, the sensor does not work as the setup method does not start. if i press reset button on my esp8266 the sensor starts working.
i would like to avoid to press the reset button, because i need an automatic acquisition of data when opening the serial port, connecting esp8266 to raspberrypi through USB(dev/ttyUSB0).

can you help me, please? this is my code with libraries i'm using.

#include <Arduino.h>

#include "SparkFun_AS7265X.h"

#include <Adafruit_Sensor.h>
#include <Adafruit_MPU6050.h>
#include <Wire.h>
#include "RTClib.h"

const int TRIG_PIN = 14;
const int ECHO_PIN = 12;
const int TRIG_PIN2 = 13;
const int ECHO_PIN2 = 15;
float duration_us, distance_cm,duration_us2, distance_cm2;
const int numSens = 5;
AS7265X sensors[numSens];
Adafruit_MPU6050 mpu;
void setup() {

  Serial.begin(115200);
  Wire.begin();

 TCA9548A(5);
 if (!mpu.begin()) {
  Serial.println("Sensor init failed");
  while (1)
    yield();
}

  setMPUParams(mpu);

  for (int i = 0; i < numSens ; i++) {
    TCA9548A(i);
    //Serial.println();
    if (sensors[i].begin() == false) {
      Serial.println("Sensor does not appear to be connected. Please check wiring. Freezing...");
      while (1)
        ;
    }

    setSensorParam(sensors[i], 50, AS7265X_GAIN_64X, AS7265X_MEASUREMENT_MODE_4CHAN_2);
    }

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);

  pinMode(TRIG_PIN2, OUTPUT);
 pinMode(ECHO_PIN2, INPUT);

}

void loop() {

  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  pinMode(ECHO_PIN, INPUT);
  duration_us = pulseIn(ECHO_PIN, HIGH);

   digitalWrite(TRIG_PIN2, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN2, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN2, LOW);
  pinMode(ECHO_PIN2, INPUT);
  duration_us2 = pulseIn(ECHO_PIN2, HIGH);

  // calculate the distance
  distance_cm = microsecondsToCentimeters(duration_us);
   distance_cm2 = microsecondsToCentimeters(duration_us2);

    String accData= "0";

    String gyroData="0";
    String degree = "0";
  for (int i = 0; i < numSens; i++) {
    TCA9548A(i);
    sensors[i].takeMeasurements();  //This is a hard wait while all 18 channels are measured
   
    DateTime test(2021,1,1,22,0,0);

    printData(sensors[i], i, distance_cm,distance_cm2, lux, test.timestamp(DateTime::TIMESTAMP_FULL),accData, gyroData,degree);
  }

}

please post a complete schematic.

and a complete sketch

this function definition is clearly missing.

It could be as simple as doing
Serial.end() and Serial.begin(115200) to reset the Serial port. It could also be related to onboard USB to TTL converter.

When re-plugging the USB, do you keep the power on ?

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