Python Forum
get data (temperature and humidity) from DHT22 into CSV and sending them over the net
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get data (temperature and humidity) from DHT22 into CSV and sending them over the net
#1
Dear Python-fellows Smile


hope youre all right and everything goes well at your hometown. Hopefully youre well.

Currently i work on a little Raspberry Pi project. I want to learn Python, and want to do this with some raspi and arduino-projects.

Today: i want to get data (temperature and humidity) from DHT22 into CSV and sending them over the net

I am trying to have the Rpi 3 doing some measurements

a. read temperature and humidity from a Adafruit DHT22 sensor,
b. take all the data and writing them into a .csv file,

Allready i have a setup to read the sensor and print it to the csv-file.

note: the formate of the CSV-file data: it should look like so:

Temperature,Humidity, Date,Time
",6,5,.,6,*,F,",",7,1,.,1,%,"0,6,/,3,4,/,4,0,",",0,6,:,1,4,.,7,2,"
some background-thoughts:
a. why i choose the DHT22: well, basically the DHT (DHT stands for Digital Humidity & Temperature) Those sensors are pretty cheap - and we can do alot with them: they are really low cost digital sensors with capacitive humidity sensors and thermistors are just great for all measures of the surrounding air. Very good for me to go the first steps in using them with Python. A great advantage is that the sensors handle analog to digital conversion very nicely and subsequently also provide a 1-wire interface. So here we go:

import os
import time
import csv
import Adafruit_DHT

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

def read_sensor():
#Read data from sensor
    h, t = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
    
    #Convert from C to F
    t = t * 9/5.0 + 32
    return (t,h)

    if humidity is not None and temperature is not None:
        print("Temp={0:0.1f}*C  Humidity={1:0.1f}%".format(temperature, humidity))

# note; i use this little infinite loop by using the “while TRUE:” code. i hope that i can do this here. 
# what is aimed: this little loop will continually run until a user tries to kills the script.
# right at the beginning of every single loop, we utilize the Adafruit DHT lib to retrieve all the data: a. humidity and b. temperature

    else:
        print("Failed to retrieve data from humidity sensor")

f=open("the_data_logger.csv", "a+")

#Write to CSV file

#If the file is empty write headers
if os.stat('/home/raspi/the_data_logger.csv').st_size == 0:
        f.write('Temperature,Humidity\r\n',Date,Time)

wc=csv.writer(f)

#Take 10 measurments and write them to the csv file
for x in range(10):
    h, t = read_sensor()
    wc.writerow('{0},{1},{2:0.1f}*F,{3:0.1f}%\r\n'.format(t, h,time.strftime('%m/%d/%y'),time.strftime('%H:%M.%S'))
    time.sleep(3)
  
    
f.close()
Well the input into the csv is a little weird but I think it should work just.


What i want to do next is to take the calc-data and send it automatically over the network to my PC; After this i should be able to input that data into a google sheet for data interpretation.
But this is at the moment a bit tricky. I will muse about this job - the sending of the data over the network.

My guess: guess that i can do this with ssmpt (or msmpt) while using a gmail-account; amd besides this i should use mailutils

so far my current datat and thoughts. Now i am going to test the code for storing the data in the csv-file.
And then i am going to make up my mind how to send the data over the net - to my pc.


have a great day.

your apollo Smile


update: well - for doing this task - we can also use MQTT too: i am not so familiar with MQTT - protocolls. But i guess i could do this in MQTT too: we could read the DHT-sensor on BananaPi and mqtt the results.

But what if we do it - in other words we can read a sensor like a DHT22 or similar and mqtt the results -
(sending the data to GPIO pins)
So - we can read a the sensor (DHT22) and then we can MQTT the result to a MQTT broker (the broker could be

a. RaspBerryPi or bananaPi or similar, or - even much much better
b. ESP 32 or 8266

the MQTT broker could be the banana pi (or a raspberrypi or similar) on which i connect the DHT22 to.
so if I wanted I could read it directly, but just in case I wanted to read it from another raspberry.

Smile To sume up: MQTT is a pretty nice solution. i try to work out this plan too - and with this i can dive into a new technique of doing things on different ways & methods.


Many thanks for running this great forum - it is so awesome. Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending data from the form to DB quisatz 6 1,240 Nov-13-2023, 09:23 AM
Last Post: Annaat20
  Raspberry PI with DHT22 Sensors itpilot 6 1,206 May-12-2023, 02:52 AM
Last Post: deanhystad
  Help gathering Temperature from API response road102 5 1,019 Dec-16-2022, 08:30 PM
Last Post: road102
  Help Sending Socket Data snippyro 0 1,014 Sep-23-2021, 01:52 AM
Last Post: snippyro
  monitoring the temperature of the CPU with Python apollo 2 8,517 Apr-13-2021, 05:39 PM
Last Post: apollo
  print CPU temperature samuelbachorik 12 5,918 Aug-04-2020, 03:28 PM
Last Post: Axel_Erfurt
  show and storage real temperature data Arduino in Python3 linkxxx 0 1,818 Aug-21-2019, 04:07 PM
Last Post: linkxxx
  Read temperature once mada72 2 2,739 Apr-28-2019, 07:18 PM
Last Post: mada72
  sending data to second python script Cyberfly 1 3,130 Jan-29-2018, 10:09 AM
Last Post: Cyberfly
  Importing a temperature converting module RedSkeleton007 2 7,920 Nov-12-2017, 01:20 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020