如何使用python将传感器数据输出保存到excel中

我目前正在使用raspberry pi并使用DHT11每秒读取温度和湿度值.我必须将这些值实时保存到数据库中.这是我每秒钟显示传感器数据的代码,我不知道如何在excel中保存数据/结果.

import RPi.GPIO as GPIO
import dht11
import time
import datetime
import os


# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

instance = dht11.DHT11(pin=dht11_pin)

    while True:

        cnt += 1
        if cnt%limit_sec == 0 or cnt == 1:

            result = instance.read()
            if result.is_valid():

                if previous_temperature != result.temperature or previous_humidity != result.humidity:

                    previous_temperature = result.temperature
                    previous_humidity = result.humidity

                    counter += 1
                    rightnow = datetime.datetime.now()

                    if result.humidity>=40:
                        print(str(counter)+". Last valid input: " )
                        print("Date: " + rightnow.strftime("%d/%m/%Y"))
                        print("Time: " + rightnow.strftime("%H:%M:%S"))
                        print("Status: Your plant is on the good condition.")
                        print("Temperature: %d C" % result.temperature)
                        print("Humidity: %d %%" % result.humidity)
                        print("*******************************************")


                    else:
                        print(str(counter)+". Last valid input: " )
                        print("Date: " + rightnow.strftime("%d/%m/%Y"))
                        print("Time: " + rightnow.strftime("%H:%M:%S"))
                        print("Status: Your plant is on the bad condition. Please open the water supply.")
                        print("Temperature: %d C" % result.temperature)
                        print("Humidity: %d %%" % result.humidity)
                        print("*******************************************")

            else:
                print "Invalid result!"
                pass

        time.sleep(sleep_time)

解决方法:

首先是导入csv模块然后使用
使用open(‘file_name.csv’,’w’,newline =”)作为csvfile:

writer = csv.DictWriter(csvfile,fieldnames = field_names)
field_names只是列的键值

writer.writerow(
        {‘日期’:’日期’,’时间’:’时间’,
         ‘状态’:’状态’,’温度’:’温度’,’湿度’:’湿度’})为excel文件写标题

writer.writerow(
                        {‘Date’:rightnow.strftime(“%d /%m /%Y”),’Time’:rightnow.strftime(“%H:%M:%S”),
                         ‘Status’:status,’Temperature’:result.temperature,’Humidity’:result.humidity})根据field_names中的键值在csv文件中写入数据

完整代码:

import RPi.GPIO as GPIO
import dht11
import time
import datetime
import csv
import os


# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

instance = dht11.DHT11(pin=dht11_pin)
with open('file_name.csv', 'w', newline='') as csvfile:
    field_names = ['Date', 'Time', 'Status', 'Temperature', 'Humidity']
    writer = csv.DictWriter(csvfile, fieldnames=field_names)
    writer.writerow(
        {'Date': 'Date', 'Time': 'Time',
         'Status': 'Status', 'Temperature': 'Temperature', 'Humidity': 'Humidity'})

    while True:

        cnt += 1
        if cnt%limit_sec == 0 or cnt == 1:

            result = instance.read()
            if result.is_valid():

                if previous_temperature != result.temperature or previous_humidity != result.humidity:

                    previous_temperature = result.temperature
                    previous_humidity = result.humidity

                    counter += 1
                    rightnow = datetime.datetime.now()

                    if result.humidity>=40:
                        status = 'Your plant is on the good condition.'
                        print(str(counter)+". Last valid input: " )
                        print("Date: " + rightnow.strftime("%d/%m/%Y"))
                        print("Time: " + rightnow.strftime("%H:%M:%S"))
                        print("Status: Your plant is on the good condition.")
                        print("Temperature: %d C" % result.temperature)
                        print("Humidity: %d %%" % result.humidity)
                        print("*******************************************")


                    else:
                        status = 'Your plant is on the bad condition. Please open the water supply.'
                        print(str(counter)+". Last valid input: " )
                        print("Date: " + rightnow.strftime("%d/%m/%Y"))
                        print("Time: " + rightnow.strftime("%H:%M:%S"))
                        print("Status: Your plant is on the bad condition. Please open the water supply.")
                        print("Temperature: %d C" % result.temperature)
                        print("Humidity: %d %%" % result.humidity)
                        print("*******************************************")
                    writer.writerow(
                        {'Date': rightnow.strftime("%d/%m/%Y"), 'Time': rightnow.strftime("%H:%M:%S"),
                         'Status': status, 'Temperature':result.temperature, 'Humidity': result.humidity})
            else:
                print "Invalid result!"
                pass

        time.sleep(sleep_time)

第一个writer.writerow将是你的标题,而field_names只是用作将数据填充到特定列的键

存储你的状态=”并将它放在writer.writerow()等.

上一篇:旋转矩阵到底是什么?


下一篇:android-获取全局坐标中的磁场值