Delay to read serial port

I have made a project to read Arduino with Python and then use the variables in other calculations. The program works correctly, but I have used a resource that I thought.
I have added a delay to read the serial port at each reading. I used 10 seconds . Arduino send data each 10 seconds too. If I do not use delay Python generate error, can not convert to float. I know that arduino have a reset on port opening, I have resolved that installing capacitor in reset-earth. Nevertheless Python generate error if the delay is not added.
When I install a delay less than 10 seconds, the error is present again. I believe that the serial port send values with zeros. Is that the solution ? or is there another. I am treating to understant the serial port but I do not have other information to study.
I add the python program and arduino program.


import serial,time
from time import sleep
Arduino= serial.Serial("/dev/ttyACM0","9600",timeout=1)
Arduino.setDTR(False)
sleep(1)
Arduino.flushInput()
Arduino.setDTR(True)
# Creamos un buble sin fin

while True:
  
  time.sleep(9.5)
  #este retraso permite que lea correctamente , ya no envía cadenas vacías y admite float o int
  num=Arduino.readline()
  #num= num.strip('\0')
  #num=num.strip('\n')('\r')
  num=num.strip()
  num=num.decode()
  num=num.strip('\r')
  num=num.strip('\0')
  A=float(num)
  print(A)
  B=A+100
  print(B)
  
  
  time.sleep(1)   

#include "DHT.h"

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

const int DHTPin = 5;     // what digital pin we're connected to

DHT dht(DHTPin, DHTTYPE);

void setup() {
  Serial.begin(9600);
  // Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(10000);

  // Reading temperature or humidity takes about 250 milliseconds!
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Serial.print("Humidity: ");
  Serial.print(h);
   //Serial.print(",");
  //Serial.print("\t");
  // Serial.print(" %\t");
  // Serial.print("Temperature: ");
  //Serial.print(t);
  Serial.println();
  // Serial.print(" *C ");
}escribe o pega el código aquí
import serial,time
from time import sleep
Arduino= serial.Serial("/dev/ttyACM0","9600",timeout=1)
Arduino.setDTR(False)
sleep(1)
Arduino.flushInput()
Arduino.setDTR(True)
# Creamos un buble sin fin

while True:
  
  time.sleep(9.5)
  #este retraso permite que lea correctamente , ya no envía cadenas vacías y admite float o int
  num=Arduino.readline()
  #num= num.strip('\0')
  #num=num.strip('\n')('\r')
  num=num.strip()
  num=num.decode()
  num=num.strip('\r')
  num=num.strip('\0')
  A=float(num)
  print(A)
  B=A+100
  print(B)
  
  
  time.sleep(1)    
  

#include "DHT.h"

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11

#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

const int DHTPin = 5; // what digital pin we're connected to

DHT dht(DHTPin, DHTTYPE);

void setup() {
Serial.begin(9600);
// Serial.println("DHTxx test!");

dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
delay(10000);

// Reading temperature or humidity takes about 250 milliseconds!
float h = dht.readHumidity();
float t = dht.readTemperature();

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

// Serial.print("Humidity: ");
Serial.print(h);
//Serial.print(",");
//Serial.print("\t");
// Serial.print(" %\t");
// Serial.print("Temperature: ");
//Serial.print(t);
Serial.println();
// Serial.print(" *C ");
}

edit your post. delete entire sketch. press "code" tag and insert python script wich read serial port. the save the post.

import serial, time
from time import sleep

Arduino = serial.Serial("/dev/ttyACM0", "9600", timeout=1)
Arduino.setDTR(False)
sleep(1)
Arduino.flushInput()
Arduino.setDTR(True)
# Creamos un buble sin fin

while True:
    # este retraso permite que lea correctamente , ya no envía cadenas vacías y admite float o int
    if (Arduino.isOpen() == False):
        Arduino.open()
    if  Arduino.inWaiting() > 0:
        try:
            num = Arduino.readline()
            num = num.decode()
            num = num.strip()

            A = float(num)/100.0
            print(A)
            B = A + 100.0
            print(B)
            
        except:
            pass

    time.sleep(1)
#include "DHT.h"

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

const int DHTPin = 5;     // what digital pin we're connected to

DHT dht(DHTPin, DHTTYPE);

void setup() {
  Serial.begin(9600);
  // Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {

  // Reading temperature or humidity takes about 250 milliseconds!
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Serial.print("Humidity: ");
  Serial.print(h * 100);
  //Serial.print(",");
  //Serial.print("\t");
  // Serial.print(" %\t");
  // Serial.print("Temperature: ");
  //Serial.print(t);
  Serial.println();

  // Wait a few seconds between measurements.
  delay(10000);
}

I'll give it a try, thanks for now

the program operates correctly with your added. I close this post then. Thanks.

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