Exemple #1
0
from machine import Pin
from time import sleep
import dht

import ubinascii
import machine

from machine import Pin
from neopixel import NeoPixel

import network
from ntptime import settime

from simple import MQTTClient

sensor = dht.DHT22(Pin(32))

pin = Pin(14, Pin.OUT)  # set GPIO14 to output to drive NeoPixels
np = NeoPixel(pin, 1)  # create NeoPixel driver on GPIO14 for 1 pixels


def do_red():
    np[0] = (0, 255, 0)  # set the first pixel to red
    np.write()  # write data to all pixels


def do_blue():
    np[0] = (0, 0, 255)  # set the first pixel to blue
    np.write()  # write data to all pixels

machine.freq(160000000)
CLIENT_ID = ubinascii.hexlify(machine.unique_id())
gc.collect()
scl = Pin(2)
sda = Pin(0)
gpio4 = Pin(4, Pin.OUT)
gpio13 = Pin(13, Pin.OUT)
i2c = I2C(scl=scl, sda=sda, freq=100000)
oled = SSD1306_I2C(128, 64, i2c)
oled.fill(0)
oled.text('ESP8266', 35, 5)
oled.text('MicroPython', 20, 20)
oled.show()

d = dht.DHT22(Pin(5))
client = MQTTClient(CLIENT_ID, 'q.emqtt.com', port=1883, keepalive=20)
will_msg = {'id': CLIENT_ID, 'status': False, 'msg': 'The connection from this device is lost:('}
client.set_last_will('/device/will/status', json.dumps(will_msg))
client.connect()
tim0 = Timer(0)

def display():
    d.measure()
    oled.fill(0)
    oled.text('ESP8266', 35, 5)
    oled.text('MicroPython', 20, 20)
    oled.text('T:{0:.2f} C'.format(d.temperature()), 3, 35) 
    oled.text('H:{0:.2f} %'.format(d.humidity()), 3, 50)
    oled.show()
    msg =  json.dumps({
Exemple #3
0
from machine import Pin
import dht
import time

gpio_dht = Pin('D7')
d = dht.DHT22(gpio_dht)

def measure():
    d.measure()
    temperature = d.temperature()    
    humidity = d.humidity()

    return temperature, humidity


def run():
    print('dht module demo')   
    while 1:    
        temp, hum = measure()
        print('Temperature: ' + str(temp) + ' Celsius')
        print('Humidity: ' + str(hum) + ' % RH')        
        time.sleep(2)

Exemple #4
0
import time
from pyb import Pin, I2C, ADC
import dht

from pyb_i2c_lcd import I2cLcd
from pH_monitor import PH_Monitor

LCD_ADDRESS = 0x27

pump_1 = Pin('Y9', mode=Pin.OUT_PP)
pump_2 = Pin('Y10', mode=Pin.OUT_PP)

button_pin = ADC('X11')
ph_pin = ADC('X7')

d_temp_humid = dht.DHT22(Pin('X6'))

i2c = I2C(1, I2C.MASTER)
lcd = I2cLcd(i2c, LCD_ADDRESS, 2, 16)

ph_monitor = PH_Monitor(ph_pin, button_pin, pump_1, pump_2, d_temp_humid, lcd)

ph_monitor.loop()
Exemple #5
0
import gc
import math
import sys

if sys.implementation.name == 'micropython':
    import machine
    import dht
    import esp
    import network
    import usocket
    import utime

    wlan = network.WLAN(network.STA_IF)
    dht_sensor = dht.DHT22(machine.Pin(13))
    use_deep_sleep_pin = machine.Pin(14, machine.Pin.IN)
    use_deep_sleep = use_deep_sleep_pin.value()
    rtc = machine.RTC()

    machineid = machine.unique_id()
    # Turn off access point
    network.WLAN(network.AP_IF).active(False)

else:
    import socket as usocket
    import random
    import time as utime
    wlan = None
    rtc = None
    machineid = 'host1'
    use_deep_sleep = False
Exemple #6
0
 def __init__(self):
     self.sensor = dht.DHT22(machine.Pin(14))
Exemple #7
0
import dht
from machine import Pin
from time import sleep

pindht = Pin(14)
ledpin = Pin(2, Pin.OUT)
sensor = dht.DHT22(pindht)

while True:
    try:
        ledpin.value(1)
        sensor.measure()
        slee(1)
        ledpin.value(0)
        temp = sensor.temperature()
        hum = sensor.humidity()
        print(temp)
        if (isinstance(temp, float)
                and isinstance(hum, float)) or (isinstance(temp, int)
                                                and isinstance(hum, int)):
            msg = (b'{0:3.1f},{1:3.1f}'.format(temp, hum))

            # uncomment for Fahrenheit
            #temp = temp * (9/5) + 32.0

            hum = round(hum, 2)
            print(msg)
        else:
            print('Invalid sensor readings.')
    except OSError as e:
        print('Failed to read sensor.')
Exemple #8
0

def blink(sec):
    pin.off()
    time.sleep(sec)
    pin.on()


global iter
iter = 0

#x = 5
#while x != 0:
while True:

    d = dht.DHT22(machine.Pin(12))
    d.measure()
    #print('temp:', d.temperature())
    #print('hum:', d.humidity())
    #x -= 1
    blink(1)
    oled.fill(0)
    t = d.temperature()
    h = d.humidity()
    fan = None
    # output to lcd
    oled.text('TEMP:{}C'.format(str(t)), 0, 0)
    oled.text('HUM:{}%'.format(str(h)), 0, 10)
    oled.text('FAN:{}'.format(fan), 0, 20)

    # socket
# Manuel Lameira
# DTH 22 readings using MicroPython

# DTH22 --> ESP32
# GND   --> GND
# VCC   --> 5v or 3.3v
# DAT   --> D14

from machine import Pin, I2C
from time import sleep
import dht
import ssd1306

sensor = dht.DHT22(Pin(14))
#sensor = dht.DHT11(Pin(14))

# ESP32 Pin assignment
i2c = I2C(-1, scl=Pin(4),
          sda=Pin(5))  # 5=SDK/SDA  4=SCK/SCL  As per labeling on ESP32 DevKi

# Define the OLED width and height
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

for i in range(0, 10):
    try:
        # The DHT22 need 2s to get the readings
        sleep(2)
        sensor.measure()
        temp = sensor.temperature()
Exemple #10
0
import logging
import usocketio.client
from machine import Pin
import time
import dht

logging.basicConfig(level=logging.DEBUG)
led = Pin(2, Pin.OUT)
d = dht.DHT22(Pin(4))
state = 0

serverIp = '192.168.1.109'
port = '8010'

with usocketio.client.connect('http://' + serverIp + ':' + port +
                              '/') as socketio:

    @socketio.on('led_control')
    def on_led(self, message):
        state = (state + 1) % 2
        led.value(state)
        print("led", state)

    @socketio.at_interval(5)
    def sendevent(self, message):
        d.measure()
        temp = d.temperature()
        humi = d.humidity()
        socketio.emit(
            "events", "{\"temperature\": " + str(temp) + ", \"humidity\": " +
            str(humi) + "}")
Exemple #11
0
pm = {}
pm['3'] = 0
pm['8'] = 15
pm['7'] = 13
pm['6'] = 12
pm['SCL'] = 14

i2c = I2C(-1, Pin(5), Pin(4))
oled = ssd1306.SSD1306_I2C(128, 32, i2c)

#Enable DHT22
power = Pin(12, Pin.OUT)
power.on()

#Pin that pin
d = dht.DHT22(Pin(0, Pin.IN, Pin.PULL_UP))

sent = "Starting The Thing!"


def writer(string, flush=True, height=0):
    if flush:
        oled.fill(0)
        oled.show()
    oled.text(string, 0, height)
    oled.show()


def dhter(o):
    oled.fill(0)
    oled.show()
def setup_display():
    #mosi=sda
    spi = SPI(2, 14500000, miso=Pin(19), mosi=Pin(18), sck=Pin(5))
    display = Display(spi, rst=Pin(26), dc=Pin(25), cs=Pin(4))
    return display

def cleanup_display(display):
    if display is not None:
        print('clearing display')
        display.clear()
        display.cleanup()

button = machine.Pin(27, machine.Pin.IN, machine.Pin.PULL_UP)
display=setup_display()
d = dht.DHT22(machine.Pin(22))
font = XglcdFont('fonts/Bally7x9.c', 7, 9)

def interrupt(p):
    d.measure()
    display.clear()
    display.draw_text(40, 30, "Temp: " + str(d.temperature()), font, color565(0, 0, 255))
    display.draw_text(38, 40, "Hum: " + str(d.humidity()), font, color565(0, 0, 255))

button.irq(trigger=Pin.IRQ_FALLING, handler=interrupt)

adcl = ADC(Pin(32))
adcl.atten(ADC.ATTN_11DB)
adcr = ADC(Pin(35))
adcr.atten(ADC.ATTN_11DB)
Exemple #13
0
topic_pub = b'temperature'
last_message = 0
message_interval = 5

station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
    pass
print('Connection successful')
print(station.ifconfig())

# main program
from machine import Pin

d = dht.DHT22(Pin(0))
d1 = Pin(5, Pin.OUT)
d2 = Pin(4, Pin.OUT)
d3 = Pin(0, Pin.OUT)
d4 = Pin(2, Pin.OUT)

d5 = Pin(14, Pin.OUT)
d6 = Pin(12, Pin.OUT)
d7 = Pin(13, Pin.OUT)
d8 = Pin(15, Pin.OUT)


def closeWindow():
    for i in range(1000):
        d1.on()
        d5.on()
Exemple #14
0
)

#Project ID of IoT Core
PROJECT_ID = "iot-2021-tim-03"
# Location of server
REGION_ID = "asia-east1"
# ID of IoT registry
REGISTRY_ID = "kelompok3"
# ID of this device
DEVICE_ID = "coba_coba"

# MQTT Information
MQTT_BRIDGE_HOSTNAME = "mqtt.googleapis.com"
MQTT_BRIDGE_PORT = 8883

dht22_obj = dht.DHT22(Pin(4))
led_obj = Pin(21, Pin.OUT)


def suhu():
    # Read temperature from DHT 22
    #
    # Return
    #    * List (temperature, humidity)
    #    * None if failed to read from sensor
    while True:
        try:
            dht22_obj.measure()
            return dht22_obj.temperature()
            sleep(3)
            break
Exemple #15
0
import dht
import machine
import time

#d = dht.DHT11(machine.Pin(0))
#d.measure()
#d.temperature() # eg. 23 (°C)
#d.humidity() # eg. 41 (% RH)
d = dht.DHT22(machine.Pin(0))
  while True:
  d.measure()
  d.temperature() # eg. 23.6 (°C)
  d.humidity() # eg. 41.3 (% RH)
  print(d.measure())
  time.sleep(0.5)
  
Exemple #16
0
from machine import Pin
import dht
import time

dht_pin = Pin(14)  #D5 GPIO 14
refresh_rate = 2  #Seconds (DHT22 maximum sampling rate)

dht_sensor = dht.DHT22(dht_pin)


def get_readings():
    dht_sensor.measure()

    temperature = dht_sensor.temperature()
    humidity = dht_sensor.humidity()
    # temperature = temperature * (9/5) + 32.0  #Fahrenheit

    return temperature, humidity


def main():
    print('Checking temperature every ' + str(refresh_rate) + 'seconds')
    while True:
        try:
            temp, hum = get_readings()
            print("Temperature " + str(temp) + "°C  |  Humidity " + str(hum) +
                  "%")
            time.sleep(refresh_rate)
        except KeyboardInterrupt:
            break
Exemple #17
0
 def __init__(self, aPin: int):
     Pin = machine.Pin(aPin)
     self.Obj = dht.DHT22(Pin)
Exemple #18
0
__NUMBER_OF_PIXELS = const(8)  #neopixel-stick
__DHT_PIN = const(13)
__MAX_BRIGHTNESS = const(255)

np = neopixel.NeoPixel(machine.Pin(__NEOPIXEL_PIN, machine.Pin.OUT),
                       __NUMBER_OF_PIXELS)
# test the pixels
np.fill((0, 10, 0))
np.write()
time.sleep(1.0)
# blank the pixels
np.fill((0, 0, 0))
np.write()

#DHT22 sensor
sensor = dht.DHT22(machine.Pin(__DHT_PIN))

while True:
    try:
        sensor.measure()
        humidity = sensor.humidity()
        #TODO: temperature = sensor.temperature()
        print('Humidity: {0:0.2f}'.format(humidity))
        # convert humidity to color
        red = (humidity / 100.0) * __MAX_BRIGHTNESS
        blue = ((100.0 - humidity) / 100.0) * __MAX_BRIGHTNESS
        np.fill((int(red), 0, int(blue)))
        np.write()
        time.sleep(1.0)
    except OSError:
        pass
Exemple #19
0
import network
import dht
import machine
import time
import urequests
import gc
#import micropython

pms = machine.UART(2)
dhs = dht.DHT22(machine.Pin(4))

CFG_BSSID = 'SRRU-WiFi'
CFG_BSSID_PASS = ''
START = time.ticks_ms()


def __init__():
    pms.init(9600, bits=8, parity=None, stop=1)
    gc.enable()


def do_connect():

    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)

    if wlan.isconnected():
        #print(wlan.ifconfig())
        return True

    wlan.connect(CFG_BSSID, CFG_BSSID_PASS)
import gc

from machine import Pin
from machine import SPI
from upy_rfm9x import RFM9x
from machine import I2C
import ssd1306
import time
import machine
import dht

d = dht.DHT22(machine.Pin(18))

TIMEOUT = 1.
DISPLAY = True
OLED_LINESKIP = 18
OLED_CURRENTLINE = 0

# i2c (and display) tests
i2c = I2C(-1, Pin(14), Pin(2))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
oled.fill(0)
oled.text("i2c works;", 0, OLED_CURRENTLINE)
oled.show()

# radio test
sck = Pin(25)
mosi = Pin(33)
miso = Pin(32)
cs = Pin(26, Pin.OUT)
#reset=Pin(13)
Exemple #21
0
from machine import  Pin
import dht
import time

_pin = Pin(12)
_dht = dht.DHT22(_pin)

for  i in range(10):
	_dht.measure()
	print("Temperature {}".format(_dht.temperature()))
	print("Humidity {}".format(_dht.humidity()))
	time.sleep_ms(500)