2017年3月1日 星期三

MicroPython 溫度 濕度 DHT11 PM2.5 Sharp Sensor MQTT Thingspeak



#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

import network
import time
import gc

p0 = Pin(5, Pin.OUT)
adc = ADC(0)
#----------PM2.5 sharp sensor----------------------------
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
#----------PM2.5 sharp sensor----------------------------

#---DHT11---
ds=DHT11(machine.Pin(4))

def medirTemHum():
    try:
        ds.measure()
        tem=ds.temperature()
        hum=ds.humidity()
        return (tem,hum)
    except Exception as e:
        return (-1,-1)
#---End DHT11---

#---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(tem,hum):
    display.fill(0)
    temperatura = 'Tem: ' + str(tem)[:5] + 'C'
    humedad = 'Hum: ' + str(hum)[:5] + '%'
    display.text(temperatura,2,2,1)
    display.text(humedad,2,18,1)
    #display.show()
#---End OLED---

#---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 displayvolt(calcVoltage,dustDensity):
    #display.fill(0)
    vol_1 = 'Voltage: ' + str(calcVoltage)[:6]
    dus_1 = 'Dust    :' + str(dustDensity*1000)[:6]
    display.text(vol_1,2,34,1)
    display.text(dus_1,2,50,1)
    display.show()
#---End OLED---



#----------Main Program------------
sleep_ms(10000)

# connect the ESP8266 to local wifi network
#
yourWifiSSID = "74170287" # <--- replace with your WIFI network name
yourWifiPassword = "24063173" # <--- replace with your WIFI network password
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(yourWifiSSID, yourWifiPassword)
while not sta_if.isconnected():
  pass

# connect ESP8266 to Thingspeak using MQTT
#
myMqttClient = "my-mqtt-client"  # can be anything unique
thingspeakIoUrl = "mqtt.thingspeak.com"
c = MQTTClient(myMqttClient, thingspeakIoUrl, 1883)  # uses unsecure TCP connection
c.connect()

#
# publish temperature and free heap to Thingspeak using MQTT
#

thingspeakChannelId = "234714"   # <--- replace with your Thingspeak Channel ID
thingspeakChannelWriteapi = "5HHEM1Q4QLS24XRH" # <--- replace with your Thingspeak Write API Key
publishPeriodInSec = 30

while True:
(tem,hum) = medirTemHum()
    displaytem(tem,hum)
#--------------PM2.5 Sensor----------------
# 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 ')
# print('Finish.....')

#--------------PM2.5 Sensor----------------

displayvolt(calcVoltage,dustDensity)

    # note:  string concatenations below follow best practices as described in micropython reference doc
    credentials = "channels/{:s}/publish/{:s}".format(thingspeakChannelId, thingspeakChannelWriteapi)
    payload = "field1={:.1f}&field2={:.1f}&field3={:.1f}&field4={:.1f}&field5={:d}\n".format(tem,hum, calcVoltage,dustDensity*1000,gc.mem_free())
    c.publish(credentials, payload)
    time.sleep(publishPeriodInSec)

c.disconnect()
#---END Main Program---




沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...