Commit fa7cf878 authored by zeroday's avatar zeroday
Browse files

Merge pull request #287 from nodemcu/dev

Dev merge to master
parents 6c8cace9 1652df50
HDC1000 = require("HDC1000")
sda = 1
scl = 2
drdyn = false
HDC1000.init(sda, scl, drdyn)
HDC1000.config() -- default values are used if called with no arguments. prototype is config(address, resolution, heater)
print(string.format("Temperature: %.2f °C\nHumidity: %.2f %%", HDC1000.getTemp(), HDC1000.getHumi()))
HDC1000 = nil
package.loaded["HDC1000"]=nil
\ No newline at end of file
-------------------------------------------------------
-- This library was written for the Texas Instruments
-- HDC1000 temperature and humidity sensor.
-- It should work for the HDC1008 too.
-- Written by Francesco Truzzi (francesco@truzzi.me)
-- Released under GNU GPL v2.0 license.
-------------------------------------------------------
-------------- NON-DEFAULT CONFIG VALUES --------------
------------- config() optional arguments -------------
-- HDC1000_HEAT_OFF 0x00 (heater)
-- HDC1000_TEMP_11BIT 0x40 (resolution)
-- HDC1000_HUMI_11BIT 0x01 (resolution)
-- HDC1000_HUMI_8BIT 0x20 (resolution)
-------------------------------------------------------
local modname = ...
local M = {}
_G[modname] = M
local id = 0
local i2c = i2c
local delay = 20000
local _drdyn_pin
local HDC1000_ADDR = 0x40
local HDC1000_TEMP = 0x00
local HDC1000_HUMI = 0x01
local HDC1000_CONFIG = 0x02
local HDC1000_HEAT_ON = 0x20
local HDC1000_TEMP_HUMI_14BIT = 0x00
-- reads 16bits from the sensor
local function read16()
i2c.start(id)
i2c.address(id, HDC1000_ADDR, i2c.RECEIVER)
data_temp = i2c.read(0, 2)
i2c.stop(id)
data = bit.lshift(string.byte(data_temp, 1, 1), 8) + string.byte(data_temp, 2, 2)
return data
end
-- sets the register to read next
local function setReadRegister(register)
i2c.start(id)
i2c.address(id, HDC1000_ADDR, i2c.TRANSMITTER)
i2c.write(id, register)
i2c.stop(id)
end
-- writes the 2 configuration bytes
local function writeConfig(config)
i2c.start(id)
i2c.address(id, HDC1000_ADDR, i2c.TRANSMITTER)
i2c.write(id, HDC1000_CONFIG, config, 0x00)
i2c.stop(id)
end
-- returns true if battery voltage is < 2.7V, false otherwise
function M.batteryDead()
setReadRegister(HDC1000_CONFIG)
return(bit.isset(read16(), 11))
end
-- initalize i2c
function M.init(sda, scl, drdyn_pin)
_drdyn_pin = drdyn_pin
i2c.setup(id, sda, scl, i2c.SLOW)
end
function M.config(addr, resolution, heater)
-- default values are set if the function is called with no arguments
HDC1000_ADDR = addr or HDC1000_ADDR
resolution = resolution or HDC1000_TEMP_HUMI_14BIT
heater = heater or HDC1000_HEAT_ON
writeConfig(bit.bor(resolution, heater))
end
-- outputs temperature in Celsius degrees
function M.getHumi()
setReadRegister(HDC1000_HUMI)
if(_drdyn_pin ~= false) then
gpio.mode(_drdyn_pin, gpio.INPUT)
while(gpio.read(_drdyn_pin)==1) do
end
else tmr.delay(delay) end
return(read16()/65535.0*100)
end
-- outputs humidity in %RH
function M.getTemp()
setReadRegister(HDC1000_TEMP)
if(_drdyn_pin ~= false) then
gpio.mode(_drdyn_pin, gpio.INPUT)
while(gpio.read(_drdyn_pin)==1) do
end
else tmr.delay(delay) end
return(read16()/65535.0*165-40)
end
return M
HDC1000 NodeMCU module
=======================
Here's my NodeMCU module for the TI HDC1000 temperature and humidity sensor. It should work with the HDC1008 too but I haven't tested it.
### Setup your sensor:
First, require it:
`HDC1000 = require("HDC1000")`
Then, initialize it:
`HDC1000.init(sda, scl, drdyn)`
If you don't want to use the DRDYn pin, set it to false: a 20ms delay will be automatically set after each read request.
`HDC1000.init(sda, scl, false)`
Configure it:
`HDC1000.config()`
Default options set the address to 0x40 and enable both temperature and humidity readings at 14-bit resolution, with the integrated heater on. You can change them by initializing your sensor like this:
`HDC1000.config(address, resolution, heater);`
"resolution" can be set to 14 bits for both temperature and humidity (0x00 - default) 11 bits for temperature (0x40), 11 bits for humidity (0x01), 8 bits for humidity (0x20)
"heater" can be set to ON (0x20 - default) or OFF (0x00)
### Read some values
You can read temperature and humidity by using the following commands:
`temperature = HDC1000.getTemp()` in Celsius degrees.
`humidity = HDC1000.getHumi()` in %
### Check your battery
The following code returns true if the battery voltage is <2.8V, false otherwise.
`isDead = HDC1000.batteryDead();`
Happy making! Also, check out my Breakout Board and Arduino library for this chip: http://b.truzzi.me/hdc1000-temperature-and-humidity-sensor-breakout-with-arduino-library/.
---
-- @description Expands the ESP8266's GPIO to 8 more I/O pins via I2C with the MCP23008 I/O Expander
-- MCP23008 Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/21919e.pdf
-- Tested on NodeMCU 0.9.5 build 20150213.
-- @date March 02, 2015
-- @author Miguel
-- GitHub: https://github.com/AllAboutEE
-- YouTube: https://www.youtube.com/user/AllAboutEE
-- Website: http://AllAboutEE.com
--------------------------------------------------------------------------------
local moduleName = ...
local M = {}
_G[moduleName] = M
-- Constant device address.
local MCP23008_ADDRESS = 0x20
-- Registers' address as defined in the MCP23008's datashseet
local MCP23008_IODIR = 0x00
local MCP23008_IPOL = 0x01
local MCP23008_GPINTEN = 0x02
local MCP23008_DEFVAL = 0x03
local MCP23008_INTCON = 0x04
local MCP23008_IOCON = 0x05
local MCP23008_GPPU = 0x06
local MCP23008_INTF = 0x07
local MCP23008_INTCAP = 0x08
local MCP23008_GPIO = 0x09
local MCP23008_OLAT = 0x0A
-- Default value for i2c communication
local id = 0
-- pin modes for I/O direction
M.INPUT = 1
M.OUTPUT = 0
-- pin states for I/O i.e. on/off
M.HIGH = 1
M.LOW = 0
-- Weak pull-up resistor state
M.ENABLE = 1
M.DISABLE = 0
---
-- @name write
-- @description Writes one byte to a register
-- @param registerAddress The register where we'll write data
-- @param data The data to write to the register
-- @return void
----------------------------------------------------------
local function write(registerAddress, data)
i2c.start(id)
i2c.address(id,MCP23008_ADDRESS,i2c.TRANSMITTER) -- send MCP's address and write bit
i2c.write(id,registerAddress)
i2c.write(id,data)
i2c.stop(id)
end
---
-- @name read
-- @description Reads the value of a regsiter
-- @param registerAddress The reigster address from which to read
-- @return The byte stored in the register
----------------------------------------------------------
local function read(registerAddress)
-- Tell the MCP which register you want to read from
i2c.start(id)
i2c.address(id,MCP23008_ADDRESS,i2c.TRANSMITTER) -- send MCP's address and write bit
i2c.write(id,registerAddress)
i2c.stop(id)
i2c.start(id)
-- Read the data form the register
i2c.address(id,MCP23008_ADDRESS,i2c.RECEIVER) -- send the MCP's address and read bit
local data = 0x00
data = i2c.read(id,1) -- we expect only one byte of data
i2c.stop(id)
return string.byte(data) -- i2c.read returns a string so we convert to it's int value
end
---
--! @name begin
--! @description Sets the MCP23008 device address's last three bits.
-- Note: The address is defined as binary 0100[A2][A1][A0] where
-- A2, A1, and A0 are defined by the connection of the pins,
-- e.g. if the pins are connected all to GND then the paramter address
-- will need to be 0x0.
-- @param address The 3 least significant bits (LSB) of the address
-- @param pinSDA The pin to use for SDA
-- @param pinSCL The pin to use for SCL
-- @param speed The speed of the I2C signal
-- @return void
---------------------------------------------------------------------------
function M.begin(address,pinSDA,pinSCL,speed)
MCP23008_ADDRESS = bit.bor(MCP23008_ADDRESS,address)
i2c.setup(id,pinSDA,pinSCL,speed)
end
---
-- @name writeGPIO
-- @description Writes a byte of data to the GPIO register
-- @param dataByte The byte of data to write
-- @return void
----------------------------------------------------------
function M.writeGPIO(dataByte)
write(MCP23008_GPIO,dataByte)
end
---
-- @name readGPIO
-- @description reads a byte of data from the GPIO register
-- @return One byte of data
----------------------------------------------------------
function M.readGPIO()
return read(MCP23008_GPIO)
end
---
-- @name writeIODIR
-- @description Writes one byte of data to the IODIR register
-- @param dataByte The byte of data to write
-- @return void
----------------------------------------------------------
function M.writeIODIR(dataByte)
write(MCP23008_IODIR,dataByte)
end
---
-- @name readIODIR
-- @description Reads a byte from the IODIR register
-- @return The byte of data in IODIR
----------------------------------------------------------
function M.readIODIR()
return read(MCP23008_IODIR)
end
---
-- @name writeGPPU The byte to write to the GPPU
-- @description Writes a byte of data to the
-- PULL-UP RESISTOR CONFIGURATION (GPPU)REGISTER
-- @param databyte the value to write to the GPPU register.
-- each bit in this byte is assigned to an individual GPIO pin
-- @return void
----------------------------------------------------------------
function M.writeGPPU(dataByte)
write(MCP23008_GPPU,dataByte)
end
---
-- @name readGPPU
-- @description Reads the GPPU (Pull-UP resistors register) byte
-- @return The GPPU byte i.e. state of all internal pull-up resistors
-------------------------------------------------------------------
function M.readGPPU()
return read(MCP23008_GPPU)
end
return M
[Downloads](https://github.com/nodemcu/nodemcu-firmware/releases/latest)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment