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

我目前正在尝试在树莓派上运行一个小脚本,以调查我的功耗。


每次我的计数器执行脉冲(紧密接触)并计算“即时”功耗时,此脚本通常都会中断。


问题是,当我从 IDE (Thonny) 运行该脚本时,它可以完美运行,但我不能直接在命令行中运行它(python 或 python3,sudo 与否,最后是 & ......)。我的目标是将其作为吸引人的“python3 myscript.py”服务运行。当我运行它时,我没有收到任何消息、错误或任何东西。它似乎停止了。这是我的脚本


#!/usr/bin/env python3

import mysql.connector as mariadb

import datetime

import time

import RPi.GPIO as GPIO


mariadb_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)

我的想法不多了,所以如果您有什么要尝试的,请...提前致谢!


慕桂英546537
浏览 131回答 1
1回答

冉冉说

所以我的脚本现在运行了一段时间。我对其进行了修改,以便用继电器控制关税开关。这可能会让知道的人感兴趣:)#!/usr/bin/env python3import mysql.connector as mariadbimport datetimeimport timeimport RPi.GPIO as GPIOmariadb_connection = mariadb.connect(user='', password='',&nbsp;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):&nbsp; &nbsp; global last&nbsp; &nbsp; global HPHC&nbsp; &nbsp; now = datetime.datetime.now()&nbsp; &nbsp; #print (now)&nbsp; &nbsp; delta = datetime.timedelta.total_seconds(now-last)&nbsp; &nbsp; #print (delta)&nbsp; &nbsp; last = datetime.datetime.now()&nbsp; &nbsp; Pinst = round(7.2/delta ,3)&nbsp; &nbsp; #print (Pinst)&nbsp; &nbsp; heure = int(now.strftime("%H"))&nbsp; &nbsp; minute = int(now.strftime("%M"))&nbsp; &nbsp; #print(heure,minute)&nbsp; &nbsp; if heure == 20 and minute >= 56 :&nbsp; &nbsp; &nbsp; &nbsp; HPHC = 'HC'&nbsp; &nbsp; elif heure > 20:&nbsp; &nbsp; &nbsp; &nbsp; HPHC = 'HC'&nbsp; &nbsp; elif heure == 4 and minute <= 56 :&nbsp; &nbsp; &nbsp; &nbsp; HPHC = 'HC'&nbsp; &nbsp; elif heure < 4:&nbsp; &nbsp; &nbsp; &nbsp; HPHC = 'HC'&nbsp; &nbsp; else :&nbsp; &nbsp; &nbsp; &nbsp; HPHC = 'HP'&nbsp; &nbsp; #print (HPHC)&nbsp; &nbsp; cursor.execute("INSERT INTO Compteur (Puiss,datetime,HPHC) VALUES (%s,%s,%s)", (Pinst,now.strftime("%Y-%m-%d %H:%M:%S"), HPHC))&nbsp; &nbsp; mariadb_connection.commit()&nbsp; &nbsp; return HPHCGPIO.add_event_detect(17, GPIO.FALLING, callback=impulse, bouncetime=500)print ('boucle')while True:&nbsp; &nbsp; GPIO.setmode(GPIO.BCM)&nbsp; &nbsp; time.sleep(0.1)&nbsp; &nbsp; if HPHC is 'HC' :&nbsp; &nbsp; &nbsp; &nbsp; GPIO.output(18, GPIO.HIGH)&nbsp; &nbsp; elif HPHC is 'HP' :&nbsp; &nbsp; &nbsp; &nbsp; GPIO.output(18, GPIO.LOW)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python