Serial Communication Stops when trying to read

Hi, I’m a newbie doing a microcontroller course for school and I’m having trouble getting things working.
My TA has been unable to help, and I can’t really find anything online, but that could largely be due to me not knowing what I’m searching for.
I’m using an ESP32 Devkit microcontroller with the arduino IDE.

I’m trying to send data through comport via serial communication to other programs to display/output it. When I output something from the microcontroller, it works as I can see it in the serial monitor. if I try to read it with another program (I’m using python and Processing) they don’t read anything, as if there’s nothing being sent to the comport. If I go back and open serial monitor after closing/stopping the other program, nothing is being printed there either. I can get it printing again by using the reset button on the microcontroller. I can also sometimes get some output to Processing or Python if I keep pressing that button repeatedly; it doesn’t work every time though, maybe once in every 10 times.

I’ve confirmed it’s the correct COMPORT, and I’ve tried it on a different computer as well with the same problems. I made a very simple code wherein the microcontroller just printed “Hello world” to serial, and encountered the same problems. However, when I sent that simplified code to my TA, it worked for him without any errors. This makes me think it’s something hardware-related to do with the microcontroller itself

My TA said I can submit code even if it doesn’t work for me, but then I’m unable to know if my code even works and if it’ll pass the assignment.

#include <String> //generic library
#include <DHT.h> //DHT-11 sensor library

//Declaring Objects

#define DHTPIN 4 //define the pin that the DHT sensor's signal is connected to
#define DHTTYPE DHT11 //define the type of DHT sensor, there are two types: DHT-11, DHT-22. This course is using DHT-11
DHT dht(DHTPIN, DHTTYPE); //object declaration constructor

void setup() {


  //Serial Monitor initialization
  //The serial monitor is the built in output form, similar to a console.

  Serial.begin(9600); //baud rate of 96000Hz
  pinMode(0,OUTPUT); //IO 0 Output



  //DHT-11 Initialization
  dht.begin(); //function that starts the sensor during the initial setup

}

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

  
  //DHT-11 Sensor Data Extraction

  //Read humidity as percentage
  float h = dht.readHumidity();
  
  //Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  
  //Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  //Read photoresister values
  float l = analogRead(0);

  //Display Sensor data on the Serial Monitor
  Serial.print("Humidity: "); 
  Serial.println(h);
  delay(500);
  Serial.print("Temperature C: ");
  Serial.println(t);
    delay(500);
  Serial.print("Temperature F: ");
  Serial.println(f);
    delay(500);
  Serial.print("Light: ");
  Serial.println(l);
  delay(1000);
  
}

@mirm2

Your topic was Moved to it's current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

This Simple Python - Arduino demo should get you started.

The Python code should work on Windows if you edit it to use the Windows style of COM ports.

...R

@Robin2

I'm not sure what exactly that code's meant to do, but from what I understand after python receives the 'Arduino is ready' string, they're meant to communicate. I'm getting nothing after 'Arduino is ready'. It seems like the program's just waiting for a reply from the Arduino.

When I first ran the python code with the arduino code uploaded, it said 'Waiting for Arduino to reset'. I then pressed the hard reset button on the microcontroller, and that's when it said 'Arduino is ready'.

Again, I'm very new to this so I don't really know what the potential problems could be.

Might also be worth mentioning that the "/dev/acyttm0" never works for me, and I changed it to "COM3" to get that working; not sure if that's important at all.

EDIT Upon further investigation, I've found that once it says 'Arduino is ready', if I press the hard reset button again, it outputs one string saying "Time xxxxxx Reply Arduino is ready". This doesn't happen every time, but if I keep pressing the reset button, it does happen eventually. It only outputs a string once and I can then keep pressing the button again and eventually get another string; I haven't been able to get more than 2 consecutive strings with this method though.

quote author=Robin2 link=msg=4756526 date=1601921135]
This Simple Python - Arduino demo should get you started.

The Python code should work on Windows if you edit it to use the Windows style of COM ports.

...R

[/quote]

Serial.begin(...) should set the pinMode for pins 0 and 1 so pinMode for these pins is redundant or, as in this case, WRONG. Pin 0 is used for receiving, so setting it as an output is wrong.

@vaj4088

I believe that should only affect the sensor data if I'm not mistaken. I actually receive sensor data fine, and accurately so that doesn't seem to be my problem. I can see the sensor data written in the serial monitor so I'm fine for the data collection. It's just the serial communication that I can't get working.

  pinMode(0,OUTPUT); //IO 0 Output

Pin 0 is used for receiving serial data. I have not seen a schematic or a wiring diagram but the code suggests that the sensor is on a different pin. Pins 0 and 1 should not be used for anything else, and pin 0 is an INPUT.

@vaj4088

My TA made a custom PCB for our course, and on it the pin that the sensor's attached to is labeled IO 0. I changed it to input as per your advice nonetheless and I still get values from the sensor. However, I'm still having the same problem where python doesn't read anything unless I hard reset the microcontroller repeatedly and eventually get something.

How do you have things connected ?

If your sensor is connected to Arduino pin 0, how does your Arduino communicate with python ?

mirm2:
When I first ran the python code with the arduino code uploaded, it said 'Waiting for Arduino to reset'. I then pressed the hard reset button on the microcontroller, and that's when it said 'Arduino is ready'.

The program assumes that the PC is connected to an Arduino Uno or Mega in the normal way with a USB cable. It also assumes the normal behaviour that when the PC program opens the serial connection that causes the Arduino to reset. If that happens there would be no need to press the reset switch.

I have never tested the programs with an ESP8266 or ESP32. I expect the same general principles will apply but some details may need to be different.

It may be that the connection to your ESP32 does not cause the ESP32 to reset when the serial port is opened. If so you could try disabling the part of the Python program where is waits for the message from the Arduino and just substitute a short delay.

...R

Pin 0 is not used by Serial on ESP32, on most boards TX = 1 and RX = 3.

I got an ESP32 devkit pinout and you are correct. Sorry.

I still want to see a schematic or a wiring diagram.