2017年3月1日 星期三

MicroPython PM2.5 Show Result to OLED

MicroPython PM2.5 Show Result to OLED

#https://laurentblogs.wordpress.com/2017/01/09/capteur-de-poussieres-et-micropython-partie-1/
# ADC : A0
# ILED : D3 / GIPO00
#=====================================================
#// Formulas to to calculate dust by minimum & maximum values.
#// According to specification: min=600mv; max=3520mv
#// using linear function: y = a*x + b;
#// and Fig. 3 Output Voltage vs. Dust Density from specification
#// 0.6 = a*0 + b  =>  b = 0.6
#// 3.52 = a*0.5 + 0.6 =>  a = (3.52 - 0.6)/0.5 = 5.84
#// y = 5.84*x + 0.6
#//
#// Lets's inverse function, because y is unknown and x is our measured voltage
#//
#// y - 0.6 = 5.84 * x
#// x = (y-0.6)/5.84
#// x = y*1/5.84-0.6/5.84
#// x = 0.171*y - 0.1

#https://laurentblogs.wordpress.com/2017/01/09/capteur-de-poussieres-et-micropython-partie-1/
# ADC : A0
# ILED : D3 / GIPO00
#=====================================================================
from machine import Pin, ADC
import time

from machine import Pin,I2C
from time import sleep_ms
from ubinascii import hexlify
from umqtt.simple import MQTTClient
from dht import DHT11
from ssd1306 import SSD1306_I2C
import machine


p0 = Pin(5, Pin.OUT)
adc = ADC(0)
def measure():
p0.high()                       # d?but du cr?neau
time.sleep_us(280)         # les 0.28 ms
readvalue = adc.read()    # lecture de l’adc
time.sleep_us(40)           # compl?ment du cr?neau ? 0.32 ms
p0.low()                        # fin du cr?neau
time.sleep_us(9680)       # compl?ment du cycle ? 10 ms
return readvalue
#--------------------------------------

#---OLED IIC 128x64---
i2c = I2C(sda = Pin(2), scl = Pin(14), freq=100000)
display = SSD1306_I2C(128, 64, i2c)
led_blue = machine.Pin(2, Pin.OUT)  # ?置 GPIO2 ??出
led_blue.high()
def displaytem(calcVoltage,dustDensity):
    display.fill(0)
    vol_1 = 'Voltage: ' + str(calcVoltage)[:6]
    dus_1 = 'DustDensity: ' + str(dustDensity*1000)[:6]
    display.text(vol_1,2,2,1)
    display.text(dus_1,2,18,1)
    display.show()
#---End OLED---


print('PM2.5 Result:')
t,tot,t1=0,0,0
ppm=0.0
t1=measure()
print('Test measure Value :  ',t1,'---- this value always too low')
for i in range(10): # on fait 10 mesures
t=measure()
print(i,"---",t)
tot=tot+t
time.sleep_ms(20)
ppm=tot/10
tot2='PM2.5 Value = '+str(ppm)[:5]
print(tot2)

# 0 - 3.3V mapped to 0 - 1023 integer values
# recover voltage
calcVoltage=0.0
calcVoltage = ppm * (3.3 / 1024)
print(' - Voltage:  ---- ',calcVoltage)

# linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
# Chris Nafis (c) 2012
dustDensity = 0.0

if (calcVoltage < 0.583) :
dustDensity = 0
else:
dustDensity = 6 * calcVoltage / 35 - 0.1    # 6/35 =0.171

print(' - Dust Density: ',dustDensity*1000,'  ug/m3 ')

displaytem(calcVoltage,dustDensity)

print('Finish.....')

沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...