Page 1 of 1

Datetime issue

Posted: Thu Aug 31, 2017 5:05 pm
by DareDrop
I am using a DHT22 sensor to log temp and humidity and save it to a csv file.

What I need is a date for each read in this format MM/DD/YYYY

And the time as HH:MM:SS

Been try to work this out, have looked and at ref and still can not get it.

Pls help

Thanks

Re: Datetime issue

Posted: Thu Aug 31, 2017 5:17 pm
by DougieLawson
What software are you using to read the sensor? What language is it written in? Where did you download it from?

Re: Datetime issue

Posted: Thu Aug 31, 2017 5:23 pm
by DareDrop
Sorry should have put that in, my bad.

Raspi 3
Raspbain Stetch updated Upgraded
Python

Re: Datetime issue

Posted: Thu Aug 31, 2017 5:52 pm
by DougieLawson
Post your python code in [code]... your stuff goes here ...[/code] tags. As that makes the difference between

from datetime import datetime

very_True=True

iso_format=True
silly_american_fmt=very_True

date_str = datetime.now()
yyyy_mm_dd = datetime.strftime(date_str, "%Y-%m-%d %H:%M:%S")
american_fmt = datetime.strftime(date_str, "%m/%d/%Y %H:%M:%S")

if silly_american_fmt == True:
print (american_fmt)
else:
print (yyyy_mm_dd)

And

Code: Select all

from datetime import datetime

very_True=True

iso_format=True
silly_american_fmt=very_True

date_str = datetime.now()
yyyy_mm_dd = datetime.strftime(date_str, "%Y-%m-%d %H:%M:%S")
american_fmt = datetime.strftime(date_str, "%m/%d/%Y %H:%M:%S")

if silly_american_fmt == True:
  print (american_fmt)
else:
  print (yyyy_mm_dd)

Re: Datetime issue

Posted: Thu Aug 31, 2017 6:32 pm
by DareDrop
Post your python code in

Code: Select all

... your stuff goes here ...
tags. As that makes the difference between

from datetime import datetime

very_True=True

iso_format=True
silly_american_fmt=very_True
Not sure what this is, would you explain pls
date_str = datetime.now()
yyyy_mm_dd = datetime.strftime(date_str, "%Y-%m-%d %H:%M:%S")
american_fmt = datetime.strftime(date_str, "%m/%d/%Y %H:%M:%S")
This is the format get set up Date in 2 forms, OK

if silly_american_fmt == True:
print (american_fmt)
else:
print (yyyy_mm_dd)
This is the print statement OK

Re: Datetime issue

Posted: Thu Aug 31, 2017 6:53 pm
by bensimmo
He is showing you and example between using 'code tags' and not using them, it's one of the format option when posting on here.


He wants you to post your code.

Re: Datetime issue

Posted: Thu Aug 31, 2017 9:14 pm
by DareDrop

Code: Select all

import time
import datetime
import csv
import sys

import Adafruit_DHT

sensor = Adafruit_DHT.DHT22
pin = 4


with open('dht.csv', 'w') as csv_file:
    dhtwriter=csv.writer(csv_file)
    dhtwriter.writerow(['Time', 'Tempemperature (F)', 'Humidity (%)'])
    while True:
        reading_time = datetime.datetime.now()
        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        if humidity is not None and temperature is not None:
            time_now = datetime.datetime.now()
            Tempemperature = ('Temp={0:0.1f}*C')
            print('Time: {0} Tempemperature: {1} Humidity: {2}'.format(time_now, temperature, humidity))
            dhtwriter.writerow([time_now, temperature, humidity])
            time.sleep(600)
Here is the code.

Re: Datetime issue

Posted: Thu Aug 31, 2017 9:48 pm
by pcmanbob
This shows you how to get the date and time or just the time.

Code: Select all

import time

datetime = time.strftime("%d-%m-%Y %H:%M:%S")
print datetime

time = time.strftime("%H:%M:%S")

print time
results look like this
31-08-2017 22:47:36
22:47:36

Re: Datetime issue

Posted: Fri Sep 01, 2017 12:52 am
by DougieLawson
Change these lines

Code: Select all

        if humidity is not None and temperature is not None:
            time_now = datetime.datetime.now()
            Tempemperature = ('Temp={0:0.1f}*C')
to

Code: Select all

        if humidity is not None and temperature is not None:
            time_now = datetime.datetime.now()
            time_now = time_now.strftime("%m/%d/%Y %H:%M:%S")
            Tempemperature = ('Temp={0:0.1f}*C')
NOTE that ISO dates (2017-09-01) naturally sort in date order. American dates (09/01/2017) need special handing to sort correctly (Excel can do that, because it converts your format to a day number internally).

BTW, if you want temps in °F rather than °C change

Code: Select all

        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        
to

Code: Select all

        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        temperature = (temperature*1.8)+32

Re: Datetime issue

Posted: Fri Sep 01, 2017 12:54 am
by DareDrop

Code: Select all

>>> import time
>>> datetime = time.strftime("%d-%m-%Y %H:%M:%S")
>>> print datetime
SyntaxError: Missing parentheses in call to 'print'

>>> print (datetime)
31-08-2017 20:47:23

>>> time = time.strftime("%H:%M:%S")
>>> print time
SyntaxError: Missing parentheses in call to 'print'

>>> print(time)
20:49:44
This works with the change to the print statement.

Thanks

Re: Datetime issue

Posted: Fri Sep 01, 2017 12:58 am
by DougieLawson
That's a feechur of python2 code being run with python3.

Re: Datetime issue

Posted: Fri Sep 01, 2017 1:52 am
by DareDrop
import time
import datetime
import csv
import sys

import Adafruit_DHT

sensor = Adafruit_DHT.DHT22
pin = 4

with open('dht.csv', 'w') as csv_file:
dhtwriter=csv.writer(csv_file)
dhtwriter.writerow(['Date', 'Time', 'Tempemperature (F)', 'Humidity (%)'])
while True:
reading_time = datetime.datetime.now()
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
sensor_date = time.strftime("%m-%d-%Y")
sensor_time = time.strftime("%H:%M:%S")
Tempemperature = ('Temp={0:0.1f}*C')
Humidity = ('Humidity={1:0.1f}%')
print('Date: {0} Time: {1} Tempemperature: {2} Humidity: {3}'.format(sensor_date,
sensor_time, temperature, humidity))
dhtwriter.writerow([sensor_date, sensor_time, temperature, humidity])
time.sleep(4)
New code

pi@raspisensor:~/Adafruit_Python_DHT/examples $ sudo python simpletest_h.py
Date: 08-31-2017 Time: 21:44:19 Tempemperature: 23.7000007629 Humidity: 44.5
Date: 08-31-2017 Time: 21:44:24 Tempemperature: 23.6000003815 Humidity: 44.9000015259
Date: 08-31-2017 Time: 21:44:28 Tempemperature: 23.6000003815 Humidity: 44.9000015259
Date: 08-31-2017 Time: 21:44:33 Tempemperature: 23.6000003815 Humidity: 44.9000015259
Date: 08-31-2017 Time: 21:44:38 Tempemperature: 23.6000003815 Humidity: 44.9000015259
Date: 08-31-2017 Time: 21:44:45 Tempemperature: 23.6000003815 Humidity: 44.9000015259

output

now i just need to format the numbers.

Thanks