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 ");
}