I have tried like 5 methods to get this script loaded after raspi3 load.
#!/usr/bin/python3.7
#sudo python /home/pi/masterBaseInterface.py
import socket # Import socket module
import thread
import time
import serial
def get_ip_address():
ip_address = '';
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8",80))
ip_address = s.getsockname()[0]
s.close()
return ip_address
def on_new_client(clientsocket,addr):
while True:
msg = clientsocket.recv(2048)
if msg != "":
if msg == "findingNemo":
clientsocket.send('heyDad')
else:
#msg = "5A00000d0a71"
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=2
)
print "Received message from client: "+msg
msg = msg.replace('\n', ' ').replace('\r', '').replace(' ', '')
msg.strip()
print "Dispatching message to master base"
ser.write(msg.decode('hex'))
print "Message sent, waiting for response"
#time.sleep(2)
#output = ser.readline()
output=""
time.sleep(2)
while ser.inWaiting() > 0:
output += ser.read(10)
if len(output)>0:
output = output.replace('\n', ' ').replace('\r', '').replace(' ', '')
print "Master base replies with "+output.encode('hex')
clientsocket.send('OK,'+output.encode('hex'))
clientsocket.close()
print "Message sent to client, connection closed"
break
else:
print "Master base not responding, sending error message to client"
clientsocket.send('ER,noResponse')
clientsocket.close()
print "Message sent to client, connection closed"
break
msg=""
#clientsocket.send(output.encode('hex'))
#clientsocket.close()
s = socket.socket() # Create a socket object
host = get_ip_address() # Get local machine name
port = 1000 # Reserve a port for your service.
print 'Server started with local IP '+ get_ip_address()
print 'Ready to serve clients'
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
thread.start_new_thread(on_new_client,(c,addr))
#Note it's (addr,) not (addr) because second parameter is a tuple
#Edit: (c,addr)
#that's how you pass arguments to functions when creating new threads using thread module.
s.close()
Posting the script source because I don't know if it contains something illegal...
What I have tried? This crontab
@reboot sudo python /home/pi/masterBaseInterface.py
Also the rc.local thing
sudo nano /etc/rc.local
sudo python /home/pi/masterBaseInterface.py &
The bashrc too, but this ones only works when I login via ssh with putty
sudo nano /home/pi/.bashrc
echo Running at boot
sudo python /home/pi/masterBaseInterface.py
I tried other things, but can't remmeber now. So, what's happening here?