def poll_sensor(): i2c = I2C(scl=Pin(5), sda=Pin(4)) sensor = DHT12(i2c) sensor.measure() t = sensor.temperature() h = sensor.humidity() return t, h
def __init__(self, port): from dht12 import DHT12 from bmp280 import BMP280 self.i2c = i2c_bus.get(port) self._available() self.dht12 = DHT12(self.i2c) self.bmp280 = BMP280(self.i2c) self.time = 0 self.data = None
def __init__(self, port=PORTA): from dht12 import DHT12 from bmp280 import BMP280 global i2c_used_0 if i2c_used_0: self.i2c = i2c_used_0 else: i2c_used_0 = I2C(id=0, sda=port[0], scl=port[1]) self.i2c = i2c_used_0 self.dht12 = DHT12(self.i2c) self.bmp280 = BMP280(self.i2c)
def __init__(self, port=PORTA): global class_map from dht12 import DHT12 from bmp280 import BMP280 self.i2c = i2c_bus.get(i2c_bus.M_BUS) if class_map['dht12'] == None: class_map['dht12'] = DHT12(self.i2c) if class_map['bmp280'] == None: class_map['bmp280'] = BMP280(self.i2c) self.dht12 = class_map['dht12'] self.bmp280 = class_map['bmp280'] self.time = 0 self.data = None
def __init__(self, port=PORTA): from dht12 import DHT12 from bmp280 import BMP280 self.i2c = i2c_bus.get(i2c_bus.M_BUS) self.dht12 = DHT12(self.i2c) self.bmp280 = BMP280(self.i2c)
from time import sleep from dht12 import DHT12 from bmp280 import BMP280 from ili934xnew import ILI9341, color565 # M5Stack Pin definitions TFT_LED_PIN = const(32) TFT_DC_PIN = const(27) TFT_CS_PIN = const(14) TFT_MOSI_PIN = const(23) TFT_CLK_PIN = const(18) TFT_RST_PIN = const(33) TFT_MISO_PIN = const(19) i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000) dht = DHT12(i2c) bmp = BMP280(i2c) power = Pin(TFT_LED_PIN, Pin.OUT) power.value(1) spi = SPI(2, baudrate=10000000, miso=Pin(TFT_MISO_PIN), mosi=Pin(TFT_MOSI_PIN), sck=Pin(TFT_CLK_PIN)) display = ILI9341(spi, cs=Pin(TFT_CS_PIN), dc=Pin(TFT_DC_PIN), rst=Pin(TFT_RST_PIN)) display.erase() while True: dht.measure() sleep(0.5) pres = bmp.get() # print("Temperature: ",dht.temperature(),"°C") txt_temp = "Temperature: " + str(dht.temperature()) + " C "
lcd.FONT_DejaVu40, 0xFFFFFF, rotate=0) s_temp_min = M5TextBox(25, 130, "12.3", lcd.FONT_DejaVu24, 0xD0D0D0, rotate=0) s_humidity = M5TextBox(25, 170, "12", lcd.FONT_DejaVu24, 0xFFFFFF, rotate=0) label5 = M5TextBox(100, 170, "%", lcd.FONT_Default, 0xFFFFFF, rotate=0) s_pressure = M5TextBox(25, 200, "1234", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0) label4 = M5TextBox(85, 200, "hPa", lcd.FONT_Default, 0xFFFFFF, rotate=0) # temperature inside s_temp_in = M5TextBox(200, 90, "12.3", lcd.FONT_DejaVu40, 0xFFFFFF, rotate=0) s_hum_in = M5TextBox(200, 170, "12", lcd.FONT_DejaVu24, 0xFFFFFF, rotate=0) label1 = M5TextBox(260, 170, "%", lcd.FONT_Default, 0xFFFFFF, rotate=0) i2c0 = i2c_bus.easyI2C(i2c_bus.PORTA, 0x5c) dht12 = DHT12(i2c0.i2c) def read_dht12(): global dht12 try: dht12.measure() h = dht12.humidity() s_hum_in.setText(str(h)) t = dht12.temperature() s_temp_in.setText(str(t)) except: s_hum_in.setText("Error") s_temp_in.setText("Error")
from machine import I2C, Pin from dht12 import DHT12 from BH1750 import BH1750 import ssd1306 import time, json, machine, ubinascii import _thread as th from umqtt import MQTTClient import wifi_connect as wlan CLIENT_ID = ubinascii.hexlify(machine.unique_id()) client = None # OLED rst = Pin(16, Pin.OUT) rst.value(1) oledScl = Pin(15, Pin.OUT, Pin.PULL_UP) oledSda = Pin(4, Pin.OUT, Pin.PULL_UP) i2cOled = I2C(scl=oledScl, sda=oledSda, freq=450000) oled = ssd1306.SSD1306_I2C(128, 64, i2cOled, addr=0x3c) oled.fill(0) oled.text('SENSOR', 40, 5) oled.text('MicroPython', 10, 20) oled.text('Waiting...', 10, 35) oled.show() wlan.connect() oled.text('{0}'.format(wlan.get_ip()), 10, 50) oled.show() # MQTTClient time.sleep(3) def on_message(topic, msg): print(topic, msg)
# UI.Flow 1.3 以降は以下 # ntptime は以下のモジュールの改訂版(オフセット指定を追加) # https://github.com/micropython/micropython/blob/master/ports/esp8266/modules/ntptime.py import ntptime_custom ntptime_custom.settime(9 * 60 * 60) # +09:00:00 for JST # UI.Flow 1.2 以前はタイムスタンプの出力に以下のように time.strftime を使用していたが #print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), ' Script Name: ', __name__) # UI.Flow 1.3 以降は time.strftime が使えないため以下で対応 print('{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(*time.localtime()[:6]), ' Script Name: ', __name__) am = ambient.Ambient(AMBIENT_CHANNEL_ID, AMBIENT_WRITE_KEY) i2c = i2c_bus.get(i2c_bus.M_BUS) dht12 = DHT12(i2c) bmp280 = BMP280(i2c) mhz19 = machine.UART(2, tx=17, rx=16) mhz19.init(9600, bits=8, parity=None, stop=1) lcd.setColor(lcd.BLACK, lcd.WHITE) lcd.setTextColor(lcd.BLACK, lcd.WHITE) lcd.clear(lcd.BLACK) win_w, win_h = lcd.winsize() # (320, 240) meter_mode = 0 while True: lcd.clear(lcd.BLACK)
import time from dht12 import DHT12 sensor = DHT12() while True: sensor.measure() print("Temperature:%.2f" % (sensor.temperature())) print("Humidity:%.2f \n" % (sensor.humidity())) time.sleep(1)
def main(): ########## 初期化 ########## # 液晶画面を初期化する lcd.setColor(color, background_color) lcd.clear() # センサーを初期化する sensor = DHT12() # 温度計、湿度計の表示を初期化する temperature_view = ThermometerView(10, 10, min_value=-15, max_value=45) humidity_view = ThermometerView(80, 10, min_value=0, max_value=100, color=lcd.BLUE, label='%') # SDカードのマウント uos.mountsd() # 背景の描画 temperature_view.init() humidity_view.init() ########## 無限ループ ########## while not buttonC.isPressed(): # センサーで温度と湿度を計測する sensor.measure() temperature = sensor.temperature() humidity = sensor.humidity() # デバッグ用 print("Temperature: {} `C, Humidity: {} %".format( temperature, humidity)) # 温度計、湿度計の表示を更新する temperature_view.update(temperature) humidity_view.update(humidity) # WBGT値を求める try: wbgt = calc_wbgt(temperature, humidity) except ValueError: wbgt = None # WBGT値を画面に表示する lcd.font(lcd.FONT_Default, color=color, transparent=False) lcd.textClear(160, 30, 'WBGT: Unknown') if wbgt is not None: lcd.text(160, 30, 'WBGT: {}'.format(wbgt)) else: lcd.text(160, 30, 'WBGT: Unknown') image_path = None # WBGT値に応じて画像を描画する # 31度以上 : 危険 # 28度以上31度未満 : 厳重警戒 # 25度以上28度未満 : 警戒 # 25度未満 : 注意 if temperature >= 21 and wbgt is not None: if wbgt >= 31: # 危険 image_path = image_path_kiken elif wbgt >= 28: # 厳重警戒 image_path = image_path_genjukeikai elif wbgt >= 25: # 警戒 image_path = image_path_keikai # 湿度に応じてインフルエンザ注意情報を表示する # 湿度40%未満 : インフルエンザ感染注意 if humidity < 40: image_path = image_path_influenza if image_path is not None: # 画像を描画する lcd.image(image_x, image_y, image_path) else: # 画像を描画した領域を背景色で塗りつぶす lcd.rect(image_x, image_y, image_width, image_height, background_color, background_color) # 表示の更新間隔 utime.sleep(interval)
from machine import I2C, Pin from dht12 import DHT12 from BH1750 import BH1750 import ssd1306 import time import _thread as th # OLED rst = Pin(16, Pin.OUT) rst.value(1) oledScl = Pin(15, Pin.OUT, Pin.PULL_UP) oledSda = Pin(4, Pin.OUT, Pin.PULL_UP) i2cOled = I2C(scl=oledScl, sda=oledSda, freq=450000) oled = ssd1306.SSD1306_I2C(128, 64, i2cOled, addr=0x3c) oled.fill(0) oled.text('SENSOR', 40, 5) # Sensor i2c = I2C(scl=Pin(22), sda=Pin(21), freq=20000) dht = DHT12(i2c) l = BH1750(i2c) def main(e): while True: try: dht.measure() oled.fill(0) oled.text('SENSOR', 40, 5) oled.text('T: {0:.1f} C'.format(dht.temperature()), 10, 20) oled.text('H: {0:.1f} %'.format(dht.humidity()), 10, 35) oled.text('L: {0:.0f} lux'.format(l.getLightIntensity()), 10, 50) oled.show()