为了账号安全,请及时绑定邮箱和手机立即绑定

无法在命令行中运行 python 脚本

无法在命令行中运行 python 脚本

慕桂英546537 2022-10-05 18:32:58
我目前正在尝试在树莓派上运行一个小脚本,以调查我的功耗。每次我的计数器执行脉冲(紧密接触)并计算“即时”功耗时,此脚本通常都会中断。问题是,当我从 IDE (Thonny) 运行该脚本时,它可以完美运行,但我不能直接在命令行中运行它(python 或 python3,sudo 与否,最后是 & ......)。我的目标是将其作为吸引人的“python3 myscript.py”服务运行。当我运行它时,我没有收到任何消息、错误或任何东西。它似乎停止了。这是我的脚本#!/usr/bin/env python3import mysql.connector as mariadbimport datetimeimport timeimport RPi.GPIO as GPIOmariadb_connection = mariadb.connect(user='Elec', password='****', database='Elec')cursor = mariadb_connection.cursor()cursor.execute("SELECT datetime FROM Compteur ORDER BY nb DESC LIMIT 1")gett = cursor.fetchone()last = gett[0]GPIO.setmode(GPIO.BCM)GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)def impulse(channel):    global last    now = datetime.datetime.now()    delta = datetime.timedelta.total_seconds(now-last)    last = datetime.datetime.now()    Pinst = round(3.6/delta ,3)    heure = int(now.strftime("%H"))    if heure > 20 or heure < 7:        HPHC = 'HC'    else:        HPHC = 'HP'    cursor.execute("INSERT INTO Compteur (Puiss,datetime,HPHC) VALUES (%s,%s,%s)",     (Pinst,now.strftime("%Y-%m-%d %H:%M:%S"), HPHC))    mariadb_connection.commit()GPIO.add_event_detect(17, GPIO.FALLING, callback=impulse, bouncetime=500)我的想法不多了,所以如果您有什么要尝试的,请...提前致谢!
查看完整描述

1 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

所以我的脚本现在运行了一段时间。我对其进行了修改,以便用继电器控制关税开关。这可能会让知道的人感兴趣:)


#!/usr/bin/env python3

import mysql.connector as mariadb

import datetime

import time

import RPi.GPIO as GPIO


mariadb_connection = mariadb.connect(user='', password='', 

database='')

cursor = mariadb_connection.cursor()

print ('connected')

cursor.execute("SELECT datetime FROM Compteur ORDER BY nb DESC LIMIT 1")

gett = cursor.fetchone()

last = gett[0]

print (last)

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.setup(18, GPIO.OUT)

HPHC = ''


def impulse(channel):

    global last

    global HPHC

    now = datetime.datetime.now()

    #print (now)

    delta = datetime.timedelta.total_seconds(now-last)

    #print (delta)

    last = datetime.datetime.now()

    Pinst = round(7.2/delta ,3)

    #print (Pinst)

    heure = int(now.strftime("%H"))

    minute = int(now.strftime("%M"))

    #print(heure,minute)

    if heure == 20 and minute >= 56 :

        HPHC = 'HC'

    elif heure > 20:

        HPHC = 'HC'

    elif heure == 4 and minute <= 56 :

        HPHC = 'HC'

    elif heure < 4:

        HPHC = 'HC'

    else :

        HPHC = 'HP'

    #print (HPHC)

    cursor.execute("INSERT INTO Compteur (Puiss,datetime,HPHC) VALUES (%s,%s,%s)", (Pinst,now.strftime("%Y-%m-%d %H:%M:%S"), HPHC))

    mariadb_connection.commit()

    return HPHC


GPIO.add_event_detect(17, GPIO.FALLING, callback=impulse, bouncetime=500)

print ('boucle')


while True:

    GPIO.setmode(GPIO.BCM)

    time.sleep(0.1)

    if HPHC is 'HC' :

        GPIO.output(18, GPIO.HIGH)

    elif HPHC is 'HP' :

        GPIO.output(18, GPIO.LOW)


查看完整回答
反对 回复 2022-10-05
  • 1 回答
  • 0 关注
  • 130 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信