#!/usr/bin/python # # $ Air Sensors # $ Beta 1: Revision 001a $ # $ Author: WhimickRH $ # $ US - UK $ # $ Date: 2017-10-29 $ # $ Wind_Rain $ # $ For GrovePi Weather Station $ # $ Raspberry Pi Air Sensors $ # $ import sys import time import threading from multiprocessing import Process import logging import smbus import RPi.GPIO as GPIO import grovepi import atexit import cPickle # -------------------------------GROVEPI BOARD & SENSOR SET UP------------------------------- rev = GPIO.RPI_REVISION if rev == 2 or rev == 3: bus = smbus.SMBus(1) else: bus = smbus.SMBus(0) GPIO.setwarnings(False) atexit.register(grovepi.dust_sensor_dis) grovepi.dust_sensor_en() # -------------------------------ERROR LOGGING----------------------------------------------- logging.basicConfig(filename='/home/pi/WxRam/Air_Sensors_Error.txt', filemode='a', level=logging.ERROR, format='%(asctime)s %(levelname)s %(name)s %(message)s') logger=logging.getLogger(__name__) # -------------------------------AIR SENSORS CLASS------------------------------------- class Air_Sensors(object): def __init__(self): self.dust_ppm = 0 self.starttime = time.time() # -------------------------------READ DUST SENSOR & DUST SENSOR LOOP---------------------------- def dust_sensor(self): reading_no = 0 print("Starting Dust Sensor") while True: try: [new_val,lowpulseoccupancy] = grovepi.dustSensorRead() if new_val: self.lowpulseoccupancy = lowpulseoccupancy reading_no +=1 air_file = open("/home/pi/WxRam/air.txt", 'w') print("Reading_no:",reading_no, "Dust lowpulseoccupancy",self.lowpulseoccupancy) cPickle.dump(self.lowpulseoccupancy, air_file) air_file.close() time.sleep(5.0 - ((time.time() - self.starttime) % 5.0)) except Exception as e: logging.exception(str(e)) print(logging.exception(str(e))) # -------------------------------MAIN -------------------------------------------------------- def main(): Gas = Air_Sensors() Air_loop = threading.Thread(name='dust_sensor', target=Gas.dust_sensor()) # Air_loop = Process(target=Gas.dust_sensor(), args=()) Air_loop.setDaemon(True) Air_loop.start() return # --------------------------------RUN MAIN---------------------------------------------------- if __name__ == '__main__': try: main() except Exception as e: logging.exception(str(e)) print(logging.exception(str(e))) sys.exit() """ lowpulseoccupancy is the number of ms in a 30 second period for which the sensor data was low which indicates a reading from the dust sensor. If you check the wiki here: http://www.seeedstudio.com/wiki/Grove_-_Dust_Sensor18, to change that into concentration just do this: ratio = lowpulseoccupancy/(sampletime_ms*10.0) # Integer percentage concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62 #using spec sheet curve """