Integrating AirLink - no sensor data

All about the standard Meteobridge devices based on mobile routers from TP-Link, D-Link, ASUS

Moderator: Mattk

User avatar
jasonmfarrow
Gold Boarder
Gold Boarder
Posts: 249
Joined: Mon May 25, 2020 4:21 pm
Contact:

Re: Integrating AirLink - no sensor data

Post by jasonmfarrow »

Mine does not, but that is for two reasons.
1) The sensor script I run is supposed to start the sensor, gather data over a given period to get a fresh air sample, and then stop.
Sometimes though, for reasons unknown, the code dies or throws a fit and then the fan continues to run. This leads me to reason #2
2) I have a reset script to kill it ready for the next attempt

In my aqireset.py code it is exactly the same as the aqi.py code *except* that the main program section of the code is cut down to just these few lines

Code: Select all

if __name__ == "__main__":
	cmd_set_sleep(0)
	time.sleep(1)
	cmd_firmware_ver()
	time.sleep(1)
	cmd_set_sleep(1)
	quit()
This wakes up the sensor, gets a firmware level, and then turns it off.

However, after all that, this sometimes fails as well. So, I have a 3rd script to nuke everything: aqikill.py:

Code: Select all

import os
import signal
import sys
import time

print("Searching for 'aqi.py' in processes")
for line in os.popen("ps ax | grep aqi.py | grep -v grep"):
    fields = line.split()
    print(line)
    with open("/home/pi/aqikilled.log", "a") as logfile:
       logtext = ' '.join([time.strftime("%Y/%m/%d %H:%M:%S "), "killed: ", line])
       logfile.write(logtext)
       logfile.close()
       print(logtext)

    pid = fields[0]
    os.kill(int(pid), signal.SIGKILL)

print("Searching for 'aqireset.py' in processes")
for line in os.popen("ps ax | grep aqireset.py | grep -v grep"):
    fields = line.split()
    print(line)
    with open("/home/pi/aqikilled.log", "a") as logfile:
       logtext = ' '.join([time.strftime("%Y/%m/%d %H:%M:%S "), "killed: ", line])
       logfile.write(logtext)
       logfile.close()
       print(logtext)

    pid = fields[0]
    os.kill(int(pid), signal.SIGKILL)

print("Completed.")
Then I have an updated crontab to ensure that everything runs, resets or kills as required (even after a reboot) to ensure good data at xx:00 and xx:30.

Code: Select all

*/30 * * * * python /home/pi/aqi.py > /home/pi/aqi.log 2>&1
03,25,29,33,55,59 * * * * python /home/pi/aqikill.py > /home/pi/aqikill.log 2>&1
04,26,34,56 * * * * python /home/pi/aqireset.py > /home/pi/aqireset.log 2>&1
@reboot sleep 30 && python /home/pi/aqireset.py > /home/pi/aqireset.log 2>&1
Regards
Jason
https://jmfweather.uk | @jasonmfarrow
woolfg
Expert Boarder
Expert Boarder
Posts: 157
Joined: Wed Mar 20, 2019 4:01 pm
Contact:

Re: Integrating AirLink - no sensor data

Post by woolfg »

Hi Jason

I might have to do that

The problem I am having is that having set up all my scripts and cron the same as you , the .data files only get created the first time the cron job runs

This is what I am using

*/10 * * * * python /home/pi/aqi.py > /home/pi/aqi.log 2>&1

So I get the .data files at minute 10 but from then on nothing at minute 20, 30 40 etc - the aqi.log file is created every time but its empty

Regards

Graham
User avatar
jasonmfarrow
Gold Boarder
Gold Boarder
Posts: 249
Joined: Mon May 25, 2020 4:21 pm
Contact:

Re: Integrating AirLink - no sensor data

Post by jasonmfarrow »

It took me some time, and trial and error, to get all this to work and not need to manually intervene.

One thing to check: Make sure your aqi.py script has this line *before* you start writing or sending the data elsewhere.

Code: Select all

cmd_set_sleep(1)
That way the sensor is told to stop before any subsequent code runs. Code which can, sometimes, fail.

If you don't have the various log files or output files created then run this code:

Code: Select all

cd /var/www/html
touch aqi.data
Replace the directory with where ever you want to put your files: eg /home/pi. The touch command will create an empty file ensuring that the scripts will be able to write to them.
Regards
Jason
https://jmfweather.uk | @jasonmfarrow
woolfg
Expert Boarder
Expert Boarder
Posts: 157
Joined: Wed Mar 20, 2019 4:01 pm
Contact:

Re: Integrating AirLink - no sensor data

Post by woolfg »

Hi Jason

Thanks I'll check all that

Thanks for your help -its much appreciated

Kind Regards

Graham
Post Reply