Home > Mobile >  Program Stops Without Reason (RasPi, Linux, Python3)
Program Stops Without Reason (RasPi, Linux, Python3)

Time:07-31

First, thank for fixing my post. I'm still not sure how to include a sketch. I've been reading posts here for many months, but never posted one before.

My headless RasPi is running two sketches of mine, one reads data from a pm2.5 sensor (PMS7003) and the other is the program listed above that sends information to another Pi, the client, that turns on a pm2.5 capable air filter. (I live in California) The program that reads the PMS7003 sorts the data, called max_index, into one of six categories, 0 thru 5 and saves the current category to a text file. I'm using the 'w' mode during the write operation, so there is only one character in the text file at any time. The server program listed above reads the text file and sends it to a client that turns on the air filter for categories above 2. The client sends the word "done" back to the server to end the transaction.

Until you mentioned it, I didn't realize my mistake, clientsocket.recv(2). I'll fix that and try again.

So, the listener socket should go outside the while loop, leaving the send and receive inside???

Troubleshooting: I start the two programs using nice nohup python3 xxx.py & nice nohup python3 yyy.py. The program that reads the PMS7003 continues running and updating the text file with current category, but the server program falls out of existence after a few days. top -c -u pi reveals only the PMS7003 program running, while the server program is missing. Also, there's nothing in nohup.out or in socketexceptions.txt and I tried looking through system logs in /var/log but was overwhelmed by information and found nothing that made any sense to me.

Since writing to the socketexceptions.txt file is not in a try/except block, the crash might be happening there.

import socket import time index = " " clientsocket = ""

def getmaxindex(): try: with open('/home/pi/pm25/fan.txt','r')as f: stat = f.read() #gets max_index from pm25b.py return(stat) except: with open("/home/pi/pm25/socketexceptions.txt",'a')as f: f.write("Failed to read max index")

def setup(index): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1) s.bind(("192.168.1.70", 5050)) except: with open("/home/pi/pm25/socketexceptions.txt",'a')as f: f.write("Failed to bind") try: s.listen(1) clientsocket, address = s.accept() clientsocket.send(index) rx = clientsocket.recv(2) if rx == "done": clientsocket.close() except: with open("/home/pi/pm25/socketexceptions.txt",'a')as f: f.write("Failed to communicate with flient")

while True: index = getmaxindex().encode('utf-8') setup(index) time.sleep(5) enter code here

CodePudding user response:

It is unknown what program is supposed to do and where exactly you run into problems, since there is only a code dump and no useful error description (what does "stop" mean - hang or exit, where exactly does it stop). But the following condition can never be met:

    rx = clientsocket.recv(2)
    if rx == "done":

The will receive at most 2 bytes (recv(2)) which is definitely not enough to store the value "done".

Apart from that it makes not real sense to recreate the same listener socket again and again, just to accept a single client and exchange some data. Instead the listener should only be created once and multiple accept should be called on the same listener socket, where each will result in a new client connection.

CodePudding user response:

OOps! Edited in the wrong place.

  • Related