1
votes

I want to send data from arduino to computer by using serial. Then i want to record the time based on the current timestamp and i save it in the txt file. What should I do? Your suggestions are very useful to me.

this my code on arduino:

#include <DHT.h>

#define DHTPIN 2   
#define DHTTYPE DHT11  

DHT dht(DHTPIN, DHTTYPE);

void setup(){
    Serial.begin(9600);
    dht.begin();
}
void loop(){
    float hum = dht.readHumidity();
    float temp= dht.readTemperature();

    Serial.print("Humidity: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.println(" Celsius");
    delay(2000);
}
2
Are you receiving the data over the serial port on the computer? Where's your Python code that reads it in?stevieb

2 Answers

3
votes

You can use this python script as your receiver code. Change 'port' to your Arduino port.

To find your Arduino port, before you plug it in, in the shell terminal use

ls /dev/tty*

Use it again after you have plugged your Arduino in and compare the lists to find the new connection. (For me, it ended up being ACM0)

Do not forget to download serial library if it is missing: https://pypi.python.org/pypi/pyserial

import serial
import time
import datetime

ser = serial.Serial(
    port='COM5',\
    baudrate=9600,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=0)

print("connected to: " + ser.portstr)

while True:
    line = ser.readline()
    timestamp = str(time.time())
    #timestamp = str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
    with open('output.txt', 'a') as pyfile:
        pyfile.write(line + ' ' + timestamp +'\n')

ser.close()
0
votes

In case you are on Windows, you can Try this Free console application: https://hiterminallogger.sourceforge.io/ Just download and run the exe in a bash console emulator

$./HiTerminalLogger.exe COM<portnumber> 9600

you will get it Timestamped log as well as html report. you can as well as highlight the Text to make it more conspicuous.

enter image description here