I am trying to get a .service file to run on start up, I have an LED function to ensure it is running, the led function runs on start up but the other raspberry pis are not receiving my connection. Im unsure if it has to do with me trying to connect tcp and the board wont allow it or what. The entire program works perfect when ran in Thonny.
THIS IS MY PYTHON SCRIPT:
import time
import RPi.GPIO as GPIO
import os
import socket
import threading
import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
MotorIP= "192.168.2.184"
PORT = 5005
MyPort = 5006
GPIO.setmode(GPIO.BCM)
led_pin = 13
i2c = busio.I2C(board.SCL, board.SDA)
ads = ADS.ADS1115(i2c)
pot = AnalogIn(ads, ADS.P0)
GPIO.setup(led_pin, GPIO.OUT)
#
def LED():
try:
while True:
GPIO.output(led_pin, GPIO.HIGH)
print(f"LED SUPPOSED TO BE ON")
time.sleep(1)
GPIO.output(led_pin, GPIO.LOW)
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
print(f"STOP")
def send():
while True:
try:
while os.system(f"ping -c 1 {MotorIP} > /dev/null 2>&1") != 0:
print(f"Pinging {MotorIP}...")
time.sleep(3)
print(f"Connected to {MotorIP}, sending data...")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((MotorIP, PORT))
while True:
pot_value = pot.voltage
s.sendall(str(pot_value).encode())
print(f"Sent {pot_value:.2f}V")
time.sleep(1)
#break
except Exception as e:
print(f"send error: {e}")
time.sleep(3)
def receive_data():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
server.bind(("", MyPort))
server.listen(1)
print(f"Listening for data on port {MyPort}...")
try:
while True:
conn, addr = server.accept()
with conn:
data = conn.recv(1024)
if data:
print(f"Received: {data.decode()}")
except Exception as e:
print(f"send error: {e}")
time.sleep(3)
def StartThreads():
threading.Thread(target=send, daemon=True).start()
threading.Thread(target=receive_data, daemon=True).start()
threading.Thread(target=LED, daemon=True).start()
def main():
StartThreads()
while True:
time.sleep(1)
if __name__ == "__main__":
main()
THIS IS MY .SERVICE SCRIPT
[Unit]
Description=TCP Client Script
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/alovell9196/Send_And_ReceivePotentiometerCode.>
WorkingDirectory=/home/alovell9196
StandardOutput=append:/home/alovell9196/client_log.txt
StandardError=append:/home/alovell9196/client_log.txt
User=alovell9196
Environment="PYTHONUNBUFFERED=1"
Environment="PATH=/usr/bin:/bin:/usr/local/bin"
AmbientCapabilities=CAP_NET_BIND_SERVICE
Restart=on-failure
TimeoutSec=0 RestartSec=5 [Install] WantedBy=multi-user.target