Beispiel #1
0
def main(args):
    sensor = am2320.AM2320()

    print("argumentos", args[1:])
    (t, h) = sensor.leerTemHume()
    print("Temperature: ", t, "\nHumidity", h)
    sensor.close()
def main():
    width  = 128 # SSD1306 horizontal resolution
    height = 32   # SSD1306 vertical resolution

    i2c_dev = I2C(0,scl=Pin(1),sda=Pin(0),freq=200000)  # start I2C on I2C1 (GPIO 26/27)
    i2c_addr = [hex(ii) for ii in i2c_dev.scan()] # get I2C address in hex format

    sensor = am2320.AM2320(i2c_dev)

    oled = SSD1306_I2C(width, height, i2c_dev) # oled controller

    tempbuffer = TemperatureBuffer (width)

    text_x = 0 # start point for text in x-dir
    text_y = 0 # start point for text in y-dir
    text_y_space = 13 # space between text in y-dir

    max_temp = 30
    min_temp = 0

    graph_start_y = 22

    # This value used by temp_to_yoffset - put outside of function so only need to calculate once
    # Scale is temperature range divided by number pixels
    scale =  (height -1 - graph_start_y) / (max_temp - min_temp)

    while (1):
        sensor.measure()
        temperature = sensor.temperature()
        humidity = sensor.humidity()
        print("Temp: {}C RH {}%".format(temperature, humidity))
        # Check that the value is in a valid range (skip errornous readings)
        if (temperature > max_temp or humidity > 100) :
            print (" skipping erroroneous value")
            continue
        tempbuffer.addValue(temperature)

        oled.fill(0) # clear the OLED
        
        # Create text display
        text_1 = "Temp: {:.1f}C".format(sensor.temperature())
        text_2 = "RH: {:.1f}%".format(sensor.humidity())
        oled.text(text_1,text_x,text_y) # Show temperature
        oled.text(text_2,text_x,text_y+text_y_space) # Show Humidity

        # Create line graph across bottom of screen
        # Most recent value at right of screen (could reverse if preferred)
        x_pos = 0
        values = tempbuffer.getValues()
        for temp_value in values:
            # Convert temperature value into y position (high value = graph_start_y, low value = height of screen)
            y_pos = graph_start_y + int((max_temp - temp_value) * scale)
            # Draw a pixel for each value
            oled.pixel(x_pos, y_pos, 1)
            x_pos += 1

        oled.show() # output to OLED display
        
        utime.sleep(1)
Beispiel #3
0
 def start(self):
     try:
         self.__i2c = I2C(scl=Pin(self.__scl), sda=Pin(self.__sda))
         self.__am2320 = am2320.AM2320(self.__i2c)
         if self.debug == True:
             print("AM2320 is gestart")
         Sensor.teller += 1
         return (0, 1)
     except Exception as E:
         if self.debug == True:
             print("fout bij opstart van AM2320")
             print(E)
         return (-1, -1)
Beispiel #4
0
def readSensor():
    sensorOn = machine.Pin('P11', mode=machine.Pin.OUT)
    sensorOn(1)

    i2c = I2C(0, I2C.MASTER)
    i2c = I2C(0, pins=('P10', 'P9'))

    import am2320
    am = am2320.AM2320(i2c)

    while True:
        try:
            temp = am.temperature
            hum = am.relative_humidity
            break
        except Exception as e:
            # These sensors are a bit flakey, its ok if the readings fail
            pass

    sensorOn.value(0)

    return ([temp, hum])
Beispiel #5
0
#!/usr/bin/python
import am2320
import datetime as dt

hum_sensor = am2320.AM2320(1)
_, humidity = hum_sensor.readSensor()

with open("/home/pi/balloonS/sensor_logs/hum_log", mode="a+") as hum_log:
    time_stamp = dt.datetime.now().strftime('[%Y-%m-%d %H:%M:%S]')
    hum_log.write(time_stamp + " h=" + str(humidity) + "%\n")
Beispiel #6
0
from microbit import *
import am2320

i2c.init(sda=pin15, scl=pin13)
sensor = am2320.AM2320(i2c)

tempReadings = []
currTemp = 0


def tempCompare():
    """Compares the temperature readings of the MicroBit and AM232"""
    am2320_temp = 0
    microBit_temp = 0

    sensor.measure()
    am2320 = sensor.temperature()
    microBit_temp = temperature()

    if am2320_temp > microBit_temp:
        display.scroll("AM2320 reads a higher temperature")
    if am2320_temp < microBit_temp:
        display.scroll("MicroBit reads a higher temperature")
    else:
        display.scroll("The sensors read the same temperature")


def showSavedTemps():
    """Displays recorded temperatures"""
    for i in tempReadings:
        display.scroll(i + "   ")
Beispiel #7
0
def read_hum():
    ''' Read humidity from am2320 sensor. '''
    hum_sensor = am2320.AM2320()
    _, humidity = hum_sensor.readSensor()
    return humidity
Beispiel #8
0
# -*- coding: utf-8 -*-
"""
   程式說明請參閱18-22頁
   publish am2320's temperature & humidity to Adafruit IO
"""

import machine
import ubinascii
import time, gc
import am2320
from machine import I2C, Pin
from umqtt.simple import MQTTClient

i2c = I2C(scl=Pin(5), sda=Pin(4))
d = am2320.AM2320(i2c)

config = {
    'broker': 'io.adafruit.com',
    'user': '******',
    'key': '你的AIO KEY',
    'id': 'room/' + ubinascii.hexlify(machine.unique_id()).decode(),
    'topic_temp': b'poushen/feeds/temperature',
    'topic_humi': b'poushen/feeds/humidity'
}


def publish(topic, data):
    client = MQTTClient(client_id=config['id'],
                        server=config['broker'],
                        user=config['user'],
                        password=config['key'])
Beispiel #9
0
import am2320
import machine
import network
from config import NETWORK_PASSWORD, NETWORK_SSID

try:
    import uasyncio as asyncio
except ImportError:
    import upip

    upip.install("uasyncio")
    import uasyncio as asyncio

I2C = machine.I2C(scl=machine.Pin(14), sda=machine.Pin(2))
SENSOR = am2320.AM2320(I2C)


async def get_data():
    try_counter = 0
    temp = None
    hum = None
    while try_counter <= 5 and not temp and not hum:
        try:
            SENSOR.measure()
            temp = SENSOR.temperature()
            hum = SENSOR.humidity()
        except BaseException as e:
            print("problem with sensor: {}".format(e))
            try_counter += 1
            print("try counter: {}".format(try_counter))
    return "temperature {}\nhumidity {}".format(temp, hum)
import time, ustruct
from machine import I2C, Pin, HSPI

# Nokia 5110
import upcd8544, framebuf

# Temp sensor
import am2320

# Initialise I2C for temp sensor
i2c = I2C(scl=Pin(0), sda=Pin(2), freq=20000)

i2c.scan()
# should see sensor at [92]

dht = am2320.AM2320(i2c)

# Initialise HSPI for display
spi = HSPI(baudrate=80000000, polarity=0, phase=0)
RST = Pin(4)
CE = Pin(5)
DC = Pin(12)
BL = Pin(16)
lcd = upcd8544.PCD8544(spi, RST, CE, DC, BL)

# Initialise framebuffer for display
width = 84
height = 48
pages = height // 8
buffer = bytearray(pages * width)
framebuf = framebuf.FrameBuffer1(buffer, width, height)