-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
222 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# SPDX-FileCopyrightText: 2016 Mike Causer <https://github.com/mcauser> | ||
# SPDX-License-Identifier: MIT | ||
|
||
""" | ||
MicroPython Aosong AM2320 I2C driver | ||
https://github.com/mcauser/micropython-am2320 | ||
Prints the temperature and humidity | ||
""" | ||
|
||
from machine import I2C, Pin | ||
import am2320 | ||
|
||
i2c = I2C(0) | ||
sensor = am2320.AM2320(i2c) | ||
|
||
if sensor.check(): | ||
print(f"AM2320 found at I2C address {am2320.I2C_ADDRESS:#x}") | ||
|
||
sensor.measure() | ||
print(f"Temperature: {sensor.temperature()} C") | ||
print(f"Humidity: {sensor.humidity()} RH") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# SPDX-FileCopyrightText: 2016 Mike Causer <https://github.com/mcauser> | ||
# SPDX-License-Identifier: MIT | ||
|
||
""" | ||
MicroPython Aosong AM2320 I2C driver | ||
https://github.com/mcauser/micropython-am2320 | ||
Prints the temperature and humidity every 4 sec | ||
""" | ||
|
||
from time import sleep_ms | ||
from machine import I2C, Pin | ||
import am2320 | ||
|
||
i2c = I2C(0) | ||
sensor = am2320.AM2320(i2c) | ||
|
||
if sensor.check(): | ||
print(f"AM2320 found at I2C address {am2320.I2C_ADDRESS:#x}") | ||
|
||
while True: | ||
sensor.measure() | ||
print(f"Temperature: {sensor.temperature()} C, Humidity: {sensor.humidity()} RH") | ||
sleep_ms(4000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"urls": [ | ||
["am2320/examples/basic_measurement.py", "github:mcauser/micropython-am2320/examples/basic_measurement.py"], | ||
["am2320/examples/continuous_measurement.py", "github:mcauser/micropython-am2320/examples/continuous_measurement.py"] | ||
], | ||
"deps": [], | ||
"version": "1.1.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"urls": [ | ||
["am2320/__init__.py", "github:mcauser/micropython-am2320/src/am2320.py"] | ||
], | ||
"deps": [], | ||
"version": "1.1.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# SPDX-FileCopyrightText: 2016 Mike Causer <https://github.com/mcauser> | ||
# SPDX-License-Identifier: MIT | ||
|
||
""" | ||
MicroPython Aosong AM2320 I2C driver | ||
https://github.com/mcauser/micropython-am2320 | ||
""" | ||
|
||
from time import sleep_ms | ||
from micropython import const | ||
|
||
__version__ = "1.1.0" | ||
|
||
I2C_ADDRESS = const(0x5C) # fixed I2C address | ||
|
||
|
||
class AM2320: | ||
def __init__(self, i2c): | ||
self._i2c = i2c | ||
self._buf = bytearray(8) | ||
|
||
def check(self): | ||
self._wake() | ||
if self._i2c.scan().count(I2C_ADDRESS) == 0: | ||
raise OSError(f"AM2320 not found at I2C address {I2C_ADDRESS:#x}") | ||
return True | ||
|
||
def measure(self): | ||
buf = self._buf | ||
# wake sensor | ||
self._wake() | ||
# read 4 registers starting at offset 0x00 | ||
self._i2c.writeto(I2C_ADDRESS, b"\x03\x00\x04") | ||
# wait at least 1.5ms | ||
sleep_ms(2) | ||
# read data | ||
self._i2c.readfrom_mem_into(I2C_ADDRESS, 0, buf) | ||
crc = buf[6] | (buf[7] << 8) | ||
if crc != self._crc16(buf[:-2]): | ||
raise ValueError("Checksum error") | ||
|
||
def temperature(self): | ||
t = ((self._buf[4] & 0x7F) << 8 | self._buf[5]) * 0.1 | ||
if self._buf[4] & 0x80: | ||
t = -t | ||
return t | ||
|
||
def humidity(self): | ||
return (self._buf[2] << 8 | self._buf[3]) * 0.1 | ||
|
||
def _wake(self): | ||
try: | ||
self._i2c.writeto(I2C_ADDRESS, b"") | ||
except OSError: | ||
pass | ||
sleep_ms(10) | ||
|
||
def _crc16(self, buf): | ||
crc = 0xFFFF | ||
for c in buf: | ||
crc ^= c | ||
for _ in range(8): | ||
if crc & 0x01: | ||
crc >>= 1 | ||
crc ^= 0xA001 | ||
else: | ||
crc >>= 1 | ||
return crc |