-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ESP32: Add AM2320 support #3202
Closed
thornley-david
wants to merge
1
commit into
nodemcu:dev-esp32
from
thornley-david:feature-am2320-port
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,139 @@ | ||
/* | ||
* app/modules/am2320.c | ||
* | ||
* Copyright (c) 2016 Henk Vergonet <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation; either version 2 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
*/ | ||
#include "module.h" | ||
#include "lauxlib.h" | ||
#include "platform.h" | ||
#include "lwip/udp.h" | ||
#include <errno.h> | ||
|
||
static const uint32_t am2320_i2c_id = 0; | ||
static const uint8_t am2320_i2c_addr = 0xb8 >> 1; | ||
|
||
static uint16_t crc16(uint8_t *ptr, unsigned int len) | ||
{ | ||
uint16_t crc =0xFFFF; | ||
uint8_t i; | ||
|
||
while(len--) { | ||
crc ^= *ptr++; | ||
for(i=0;i<8;i++) { | ||
if(crc & 0x01) { | ||
crc >>= 1; | ||
crc ^= 0xA001; | ||
} else { | ||
crc >>= 1; | ||
} | ||
} | ||
} | ||
return crc; | ||
} | ||
|
||
/* make sure buf has lenght len+2 in order to accommodate for extra bytes */ | ||
static int _read(uint32_t id, void *buf, uint8_t len, uint8_t off) | ||
{ | ||
int i; | ||
uint8_t *b = (uint8_t *)buf; | ||
uint16_t crc; | ||
|
||
// step 1: Wake sensor | ||
platform_i2c_send_start(id); | ||
platform_i2c_send_address(id, am2320_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER, 1); | ||
ets_delay_us(800); | ||
platform_i2c_send_stop(id); | ||
|
||
// step 2: Send read command | ||
platform_i2c_send_start(id); | ||
platform_i2c_send_address(id, am2320_i2c_addr, PLATFORM_I2C_DIRECTION_TRANSMITTER, 1); | ||
platform_i2c_send_byte(id, 0x03, 1); | ||
platform_i2c_send_byte(id, off, 1); | ||
platform_i2c_send_byte(id, len, 1); | ||
platform_i2c_send_stop(id); | ||
|
||
// step 3: Read the data | ||
ets_delay_us(1500); | ||
platform_i2c_send_start(id); | ||
platform_i2c_send_address(id, am2320_i2c_addr, PLATFORM_I2C_DIRECTION_RECEIVER, 1); | ||
ets_delay_us(30); | ||
for(i=0; i<len+2; i++) | ||
b[i] = platform_i2c_recv_byte(id,1); | ||
crc = platform_i2c_recv_byte(id,1); | ||
crc |= platform_i2c_recv_byte(id,0) << 8; | ||
platform_i2c_send_stop(id); | ||
|
||
if(b[0] != 0x3 || b[1] != len) | ||
return -EIO; | ||
if(crc != crc16(b,len+2)) | ||
return -EIO; | ||
|
||
return 0; | ||
} | ||
|
||
static int am2320_setup(lua_State* L) | ||
{ | ||
int ret; | ||
struct { | ||
uint8_t cmd; | ||
uint8_t len; | ||
uint16_t model; | ||
uint8_t version; | ||
uint32_t id; | ||
} nfo; | ||
|
||
ets_delay_us(1500); // give some time to settle things down | ||
ret = _read(am2320_i2c_id, &nfo, sizeof(nfo)-2, 0x08); | ||
if(ret) | ||
return luaL_error(L, "transmission error"); | ||
|
||
lua_pushinteger(L, ntohs(nfo.model)); | ||
lua_pushinteger(L, nfo.version); | ||
lua_pushinteger(L, ntohl(nfo.id)); | ||
return 3; | ||
} | ||
|
||
static int am2320_read(lua_State* L) | ||
{ | ||
int ret; | ||
struct { | ||
uint8_t cmd; | ||
uint8_t len; | ||
uint16_t rh; | ||
uint16_t temp; | ||
} nfo; | ||
|
||
ret = _read(am2320_i2c_id, &nfo, sizeof(nfo)-2, 0x00); | ||
if(ret) | ||
return luaL_error(L, "transmission error"); | ||
|
||
ret = ntohs(nfo.temp); | ||
if(ret & 0x8000) | ||
ret = -(ret & 0x7fff); | ||
|
||
lua_pushinteger(L, ntohs(nfo.rh)); | ||
lua_pushinteger(L, ret); | ||
return 2; | ||
} | ||
|
||
LROT_BEGIN(am2320) | ||
LROT_FUNCENTRY( read, am2320_read ) | ||
LROT_FUNCENTRY( setup, am2320_setup ) | ||
LROT_END(am2320, NULL, 0) | ||
|
||
|
||
NODEMCU_MODULE(AM2320, "am2320", am2320, NULL); |
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,42 @@ | ||
# AM2320 Module | ||
| Since | Origin / Contributor | Maintainer | Source | | ||
| :----- | :-------------------- | :---------- | :------ | | ||
| 2016-02-14 | [Henk Vergonet](https://github.com/hvegh) | [Henk Vergonet](https://github.com/hvegh) | [am2320.c](../../components/modules/am2320.c)| | ||
|
||
This module provides access to the [AM2320](https://akizukidenshi.com/download/ds/aosong/AM2320.pdf) humidity and temperature sensor, using the i2c interface. | ||
|
||
## am2320.read() | ||
Samples the sensor and returns the relative humidity in % and temperature in celsius, as an integer multiplied with 10. | ||
|
||
#### Syntax | ||
`am2320.read()` | ||
|
||
#### Returns | ||
- `relative humidity` percentage multiplied with 10 (integer) | ||
- `temperature` in celcius multiplied with 10 (integer) | ||
|
||
#### Example | ||
```lua | ||
sda, scl = 1, 2 | ||
i2c.setup(0, sda, scl, i2c.SLOW) -- call i2c.setup() only once | ||
am2320.setup() | ||
rh, t = am2320.read() | ||
print(string.format("RH: %s%%", rh / 10)) | ||
print(string.format("Temperature: %s degrees C", t / 10)) | ||
``` | ||
|
||
## am2320.setup() | ||
Initializes the module. Returns model, version, serial but is seams these where all zero on my model. | ||
|
||
#### Syntax | ||
`model, version, serial = am2320.setup()` | ||
|
||
#### Parameters | ||
None | ||
|
||
#### Returns | ||
- `model` 16 bits number of model | ||
- `version` 8 bits version number | ||
- `serial` 32 bits serial number | ||
|
||
Note: I have only observed values of 0 for all of these, maybe other sensors return more sensible readings. |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case you rebase this will have to become
NODEMCU_CMODULE_AM2320
.