I am reading humidity sensor data with Arduino and Python. I want to store data in Python and I can do it but the file is not sorted. I can't use data without order. Which is the reason? I have tried making changes and it does not maintain the order to use it in a spreadsheet.
I add the program code and storage file.
import time,serial
Lectura=Arduino.readline().strip().decode("utf-8")
if len(Lectura)>0:
with open('Medicion.csv','a', newline="") as Archivo:
Archivo.write(f'{Lectura}\n')
print("Data appended")
else:
print("No Data appended")
Arduino code
#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 ");
}
On the python side, have you separated the issues between reading in the data and storing it in a file? When you find where your problem lies you can focus your efforts.
do you say? : Arduino.close() . The code is working all the time with While (true)
If I close serial port, I would need to open it every time in While.
I have been looking at the python end of this program, and I am finding that Archivo.close() is not required for the data being saved in the file.
I'm not certain what issuses @sonntag94 is having, but when I send data as the two comma separated floats and the .println( ) terminator from the Arduino to python on a PC I can read the saved file.
import serial
import time
Arduino= serial.Serial("COM5","9600",timeout=0)
time.sleep (2)
while True:
Lectura=Arduino.readline().strip().decode("utf-8")
if(Lectura):
print("Data Read:")
print(Lectura)
if len(Lectura)>0:
with open('Medicion.csv','a', newline="") as Archivo:
Archivo.write(f'{Lectura}\n')
print("Data appended")
else:
print("No Data appended")
The data is cut, a part of the temperature is written next to the humidity, you can see it in what I showed. I don't think a spreadsheet can solve that, or understand it.
Try running this simple program on the Arduino with the python code I posted in #15.
void setup() {
Serial.begin(9600);
}
void loop() {
// Wait a few seconds between measurements.
delay(3000);
// Reading temperature or humidity takes about 250 milliseconds!
static float h = 10.0;
static float t = 20.0;
Serial.print(h);
Serial.print(",");
Serial.print(t);
Serial.println();
h += .1;
t += .1;
}