Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 6970

Python • Port switching on/off

$
0
0
Have the following code to turn on a relay via a positive trigger to control a contactor switching the Grid Supply, it switches on for about 1 second and then goes off for 10 seconds and repeats. Have disconnected Trigger system and just connected a LED/470ohm but the same happens.
I assume there is a code problem but cannot see it, is it where it rechecks the Internet is availale? Have commerted out both port LOW commands but that does not alter the problem.
Any Ideas?

Code:

import time # The time library is useful for delaysimport RPi.GPIO as gpioimport subprocessimport paho.mqtt.client as mqttfrom datetime import datetime#***********************************batSOC = "solar_assistant/total/battery_state_of_charge/state"#vlist = []topiclist = []#globalprint_debug = TruebatSOCv = 0batSOCo = 0#***********************        def gpioSetup():    gpio.setwarnings(False) #to disable warnings.    #set pin numbering toBroadcom scheme    gpio.setmode(gpio.BCM)    #set GPIO6 as an output pin    gpio.setup(6, gpio.OUT)   #signal to relay trigger    gpio.output(6, gpio.LOW)  # relay off  #**************def mqtt_sendTopic(topicsub):       attempts = 0    while attempts < 5:        try:            ourClient = mqtt.Client("makc_mqtt")            ourClient.connect("192.168.10.156")            ourClient.on_message = messageFunction_topic            ourClient.loop_start()             ourClient.subscribe(topicsub)            time.sleep(6)            ourClient.loop_stop()             break        except ConnectionError:            time.sleep(10)            attempts += 1                        if print_debug:                print("ConnectionError")           def messageFunction_topic (client, userdata, message):    topic = str(message.topic)    message = str(message.payload.decode("utf-8"))    topiclist.append(message)    #*******************def batSOC_value():    topicsub = batSOC    mqtt_sendTopic(topicsub)    vlist = []    vlist = topiclist    batSOCvLen = len(vlist)    batSOCv = 0    for value in vlist:        batSOCv = batSOCv + float(value)        topiclist.clear()    try:        batSOCv = batSOCv/batSOCvLen    except ZeroDivisionError:        if print_debug:            print("ZeroDivisionError:")        pass    return batSOCv      #battery state of charge value#******************* def check_internet_connection():    try:       subprocess.check_output(["ping", "-c", "1", "192.168.10.156"])       return True    except subprocess.CalledProcessError:       if print_debug:           print("Internet is not connected.")       return False      #*******************def main():    gpioSetup()    while True:        if check_internet_connection():            batSOCo = batSOC_value()      #get battery state of charge            if print_debug:                dtime = time.strftime("%d/%m/%Y-%H:%M:%S")                print(dtime)                print("Internet is connected.")                print('Battery state of charge = ', batSOCo)            batSOCo = 8            if batSOCo <= 10:                gpio.output(6, gpio.HIGH) # turn Grid ON                if print_debug:                    dtime = time.strftime("%d/%m/%Y-%H:%M:%S")                    print(dtime)                                print("Grid ON Soc", batSOCo)            else:                gpio.output(6, gpio.LOW) # turn Grid OFF                if print_debug:                    dtime = time.strftime("%d/%m/%Y-%H:%M:%S")                    print(dtime)                                print("Grid off SOC = ", batSOCo)        else:               gpio.output(6, gpio.LOW)            if print_debug:                dtime = time.strftime("%d/%m/%Y-%H:%M:%S")                print(dtime)                print("Internet NOT connected")                print("Grid Control not available")main()

Statistics: Posted by OLWDave — Wed Oct 01, 2025 1:39 pm — Replies 4 — Views 130



Viewing all articles
Browse latest Browse all 6970

Trending Articles