met98
Posts: 7
Joined: Sun Jul 27, 2014 6:00 pm

sensor dht11 + lcd + log csv

Tue Dec 04, 2018 4:02 pm

Hi, how can I add a command to create a csv file in this script? every 3 seconds write a new line with date, time, temperature and humidity.
thank you

Code: Select all

import RPi.GPIO as GPIO
import time
import sys
import Adafruit_DHT
import Adafruit_CharLCD as LCD

# Sensore
sensor = Adafruit_DHT.DHT11
pin = 14

# PIN LCD
lcd_rs		= 26
lcd_en		= 24
lcd_d4		= 22
lcd_d5		= 18
lcd_d6		= 16
lcd_d7		= 12
lcd_backlight	= 4

# Colonne e righe
lcd_columns	= 16
lcd_rows	= 2

# Driver LCD
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
				lcd_columns, lcd_rows, lcd_backlight)
while True:
	try:
		humidity, temperature = Adafruit_DHT.read_retry(sensor,pin)
		temp = "{:0.1f}*C".format(temperature)
		hum = "{:0.1f}%".format(humidity)

		# reset
		lcd.clear()

		# display temperature
		lcd.message ("Temperatura:\n")
		toMove = lcd_columns - len(temp)
		lcd.set_cursor(toMove,1)
		lcd.message(temp)
		time.sleep(5)
		lcd.clear()
		
		# display umidita
		lcd.message ("Umidita:\n")
		toMove = lcd_columns - len(hum)
		lcd.set_cursor(toMove,1)
		lcd.message (hum)
		time.sleep(5)

	except KeyboardInterrupt:
		lcd.clear()
		lcd.message("Ciao testina!")
		time.sleep(3)
		sys.exit()

pcmanbob
Posts: 5254
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: sensor dht11 + lcd + log csv

Tue Dec 04, 2018 4:33 pm

This should do what you want .

Code: Select all

import RPi.GPIO as GPIO
import time
import sys
import Adafruit_DHT
import Adafruit_CharLCD as LCD

# Sensore
sensor = Adafruit_DHT.DHT11
pin = 14

# PIN LCD
lcd_rs		= 26
lcd_en		= 24
lcd_d4		= 22
lcd_d5		= 18
lcd_d6		= 16
lcd_d7		= 12
lcd_backlight	= 4

# Colonne e righe
lcd_columns	= 16
lcd_rows	= 2

# Driver LCD
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
				lcd_columns, lcd_rows, lcd_backlight)
while True:
	try:
		humidity, temperature = Adafruit_DHT.read_retry(sensor,pin)
		temp = "{:0.1f}*C".format(temperature)
		hum = "{:0.1f}%".format(humidity)
                tnow = time.strftime("%a %b %d %Y %H:%M:%S")          # gets date/time
        
                results = tnow + "," + str(temp) + "," + str(hum) + "/n"    # assemble line of text to write to file 
        
                with open("/home/pi/data_log.csv", "a") as file:            # open a file for appending data
                    file.write(results)                                                             # write line of text to file 
        
		# reset
		lcd.clear()

		# display temperature
		lcd.message ("Temperatura:\n")
		toMove = lcd_columns - len(temp)
		lcd.set_cursor(toMove,1)
		lcd.message(temp)
		time.sleep(5)
		lcd.clear()
		
		# display umidita
		lcd.message ("Umidita:\n")
		toMove = lcd_columns - len(hum)
		lcd.set_cursor(toMove,1)
		lcd.message (hum)
		time.sleep(5)

	except KeyboardInterrupt:
		lcd.clear()
		lcd.message("Ciao testina!")
		time.sleep(3)
		sys.exit()
		
		
note untested code so expect errors.
Edited to remove indent spacing errors caused when posting.
Last edited by pcmanbob on Tue Dec 04, 2018 7:31 pm, edited 3 times in total.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

rkn704
Posts: 32
Joined: Sun Dec 30, 2012 4:25 pm

Re: sensor dht11 + lcd + log csv

Tue Dec 04, 2018 4:36 pm

I posted a logging type reply the other day see

viewtopic.php?f=32&t=227957

For your application write the data with commas separating the the values and change the file extension to CSV instead of TXT

PhatFil
Posts: 913
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: sensor dht11 + lcd + log csv

Tue Dec 04, 2018 6:41 pm

A KISS approach is to write a simple script to return to the command line the 'record' formatted as you would want to save it to file,
for example "Temperature:30,Humidity:55,TimeRecorded: $Now()" or " 30,55,19:30"etc...

This makes for easy testing.. Then use cron/crontab to schedule the script to run how often you want to save the data, and redirect that to append to your selected log file.
>>file.txt : Open file.txt in append mode and redirect stdout there


ie to run every ten minutes
Make the script executable by: chmod u+x /path/to/script. py. Note that, you need a shebang (i.e. indicate interpreter in the first line of the script), for python2 : #!/usr/bin/env python2. ...
Open your cron table by crontab -e.
Add the following cron entry: */10 * * * * /path/to/script. py >> /path/to/Logfile.csv
to archive or separate data by day/week? hour ?? schedule another job to rename your logfile to perhaps something with a datestamped name to indicate its date/time collected every day,week or hour, just ensuring its in the middle of the dead window of the file writing job.
Next time your data collection command is run and no file exists to append to it will create a new one for you automatically.

met98
Posts: 7
Joined: Sun Jul 27, 2014 6:00 pm

Re: sensor dht11 + lcd + log csv

Wed Dec 05, 2018 2:43 pm

Thank you work fine, have edit but work very fine

Code: Select all

import RPi.GPIO as GPIO
import time
import sys
import Adafruit_DHT
import Adafruit_CharLCD as LCD

# Sensore
sensor = Adafruit_DHT.DHT11
pin = 14

# PIN LCD
lcd_rs          = 26
lcd_en          = 24
lcd_d4          = 22
lcd_d5          = 18
lcd_d6          = 16
lcd_d7          = 12
lcd_backlight   = 4

# Colonne e righe
lcd_columns     = 16
lcd_rows        = 2

# Driver LCD
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
                                lcd_columns, lcd_rows, lcd_backlight)
while True:
        try:
                humidity, temperature = Adafruit_DHT.read_retry(sensor,pin)
                temp = "{:0.1f}".format(temperature)
                hum = "{:0.1f}".format(humidity)

                tnow = time.strftime("%d/%m/%Y" ";" "%H:%M:%S")
                results = tnow + ";" + str(temp) + ";" + str(hum) + "\n"    # assemble line of text to write to file
                with open("/home/pi/log.csv", "a") as file:            # open a file for appending data
                     file.write(results)                                     # write line of text to file

                # reset
                lcd.clear()

                # display temperature
                lcd.message ("Temperat:  ")
                toMove = lcd_columns - len(temp)
                lcd.message(temp)
                lcd.message ("C\n" )
                lcd.message ("Umidita:   ")
                toMove = lcd_columns - len(hum)
                lcd.message (hum)
                lcd.message ("%" )
                time.sleep(1)

        except KeyboardInterrupt:
                lcd.clear()
                lcd.message("Errore")
                time.sleep(3)
                sys.exit()

pcmanbob
Posts: 5254
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: sensor dht11 + lcd + log csv

Wed Dec 05, 2018 3:47 pm

A CSV file is a comma separated values file that produces a table of values, why did you need to change it to semicolon ? It's now nolonger a CSV file more like just a text file.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

met98
Posts: 7
Joined: Sun Jul 27, 2014 6:00 pm

Re: sensor dht11 + lcd + log csv

Wed Dec 05, 2018 10:16 pm

in windows italian the delimiter is ; in automatic with office

pcmanbob
Posts: 5254
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: sensor dht11 + lcd + log csv

Thu Dec 06, 2018 9:26 am

met98 wrote:
Wed Dec 05, 2018 10:16 pm
in windows italian the delimiter is ; in automatic with office
I understand now , this is were having at least a country in your profile is helpful.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Return to “Python”