从文本文件中读取整数,如果整数不在特定范围内,则激活GPIO引脚

2024-06-16 12:03:56 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我需要一个python脚本从文本文件中读取数字,然后激活raspberry pi上的GPIO引脚

例如Textfile.txt

Temp=10
Pressure=20
Humd=60

所以我们假设温度应该是20,然后GPIO引脚应该激活加热器,直到温度值达到20,然后GPIO关闭

谁能帮帮我! 提前谢谢


Tags: txt脚本gpiopi数字温度tempraspberry
1条回答
网友
1楼 · 发布于 2024-06-16 12:03:56

我有答案:

只需编写一个脚本,它做的第一件事就是检查文件是否存在,如果存在,它将继续,否则它将在您的情况下创建文本文件“Textfile.txt”,其中包含温度、压力和湿度

For example:

# CoolingScript.py
# Used to cool my rasbry pie

# Importing modules
import sys, os

# Making Variables
File = "Textfile.txt" # The file name.
Exists = False # Weather or not the file exists.
Contents = "" # The contents of 'File'.

def CheckForFile(FileName):
    if(os.path.exists(FileName)):
        return True
    else: return False

def ReadFromFile(FileName, Param)
    if(Param == True):
        with open(FileName, "r") as f:
            Contents += f.read()

def ChangePie(Option, Value):
    # Use this function to cool/change
    #     you're raspberry pie!
    # if(Option == 1 or 2 or 3 etc...)

def CheckContents(Content):
    ContentX = Content.split("=")
    if(len(ContentX) == 6):
        for x in range(0, len(ContentX)):
            if(ContentX[x] == "Temp"):
                ChangePie(1, ContentX[x+1])
            # etc...

if(__name__ == "__main__"):
    Exists = CheckForFile(File)
    ReadFromFile(File, Exists)
    CheckContents(Contents)

看在我理智的份上,请不要使用此代码这只是一个示例 我不知道它是否会起作用,因为我以前从未与RPIE合作过,所以请容忍我,这只是一个例子。希望这不是浪费时间lol:-/

相关问题 更多 >