SmartArduino / WiFiMCU

WiFiCMU-v0.9.8-pre release
https://github.com/SmartArduino/WiFiMCU
123 stars 65 forks source link

I2C Module #5

Open sn2k opened 8 years ago

sn2k commented 8 years ago

The Module I2C module have a bug: i2c.write is ok (PCF8574) c=i2c.read(id,1) the mcu restart i have tried with SHT21, BMP180, TSL2561 , PCF8574 all FAIL

Please can you repair this? And have your examples to this I2C Devices? Thanks a lot

SmartArduino commented 8 years ago

There is a example here: https://github.com/SmartArduino/WiFiMCU/blob/master/Document/demos/11%20i2c/oled.lua The oled1306 can be controled via I2C.

We are sorry, the I2C module has not been well tested at present.

sn2k commented 8 years ago

The oled.lua example uses only output to i2c But Input I2C is not working.

Triade1 commented 8 years ago

Hello, the i2c module works ok.

Read bh1750 lux metter:

local id = 0 -- i2c device ID local addr = 0x23 -- default i2c address local init = false local lux local i2c = i2c

function init(sda, scl,address) i2c.setup(id,sda,scl,i2c.SLOW) if (address ~= nil ) then addr = address end i2c.start(id) i2c.address(id, addr, 'w') i2c.write(id, 0x01) --power-on i2c.stop(id) i2c.start(id) i2c.address(id, addr, 'w') i2c.write(id, 0x10) -- set mode i2c.stop(id) tmr.delayms(250) -- delay for 250 msec init = true end

local function read_data() i2c.start(id) i2c.address(id, addr, 'r') recv = i2c.read(id, 2) -- read two bytes i2c.stop(id) return recv end

function readBH1750() if (not init) then M.init() end
data = read_data() lux = (256 * string.byte(data,1) + string.byte(data,2))/1.2 return lux end

sda_pin = 11 scl_pin = 10 local bh1750_addr = 0x23

init(sda_pin, scl_pin, bh1750_addr) local value = readBH1750()

if value == nil then lux = '--.-' else lux = string.format("%.1f", value) end

print(lux)

Triade1 commented 8 years ago

Read HTU21 / SHT21 temp / humidity sensor:

local id = 0 -- i2c device ID local addr = 0x40 -- default i2c address local init = false local reg_readtemp = 0xf3 -- Trigger Temperature Measurement, No Hold Master local reg_readhumid = 0xf5 -- Trigger Humidity Measurement, No Hold Master local reg_reset = 0xfe local temperature local humidity local i2c = i2c

function init(sda, scl,address) i2c.setup(id,sda,scl,i2c.SLOW) i2c.start(id) i2c.address(id, address,'w') --i2c.address(id, address, i2c.TRANSMITTER) i2c.write(id, reg_reset) i2c.stop(id) tmr.delayms(150); init = true end

local function read_data(reg_addr) i2c.start(id) i2c.address(id, htu21d_addr, 'w') i2c.write(id, reg_addr) i2c.stop(id) tmr.delayms(50) -- delay for 50msec i2c.start(id) i2c.address(id, htu21d_addr, 'r') local recv = i2c.read(id, 3) -- read three bytes --print(string.byte(recv,1)) --print(string.byte(recv,2)) i2c.stop(id) local value = (256 * string.byte(recv,1)) + bit.band(string.byte(recv,2), 0xfc) return value end

function readTemperature() if (not init) then M.init() end
local T = read_data(reg_readtemp) if T == nil then return nil end return T end

function readHumidity() if (not init) then M.init() end local RH = read_data(reg_readhumid) if RH == nil then return nil end return RH end

sda_pin = 11 scl_pin = 10 htu21d_addr = 0x40

init(sda_pin, scl_pin, htu21d_addr)

local t = readTemperature() print(t) local temp = (-46.85 + (175.72 * t)/65536) print(temp)

local h = readHumidity() print(h) local hum = (-6 + (125.0 * h)/65536) print(hum)

sn2k commented 8 years ago

I have write the SHT21 in a module, thats work. But when i want with a timer to get value the mcu reboots.

SDA_PIN = 11 -- sda pin, GPIO2 SCL_PIN = 10 -- scl pin, GPIO0

-- temperature and humidy local te,hu

sht21 = require("sht21_module")

local function get_data() sht21.init(SDA_PIN, SCL_PIN) sht21.read() te = sht21.getTemperature() hu = sht21.getHumidy()

-- temperature in degrees Celsius print("gTemperature: "..te)

-- humidy print("gHumidy: "..hu) end

get_data() -- WORK get_data() -- WORK get_data() -- WORK

local function tmr_cb() get_data() -- FAIL >>>>>>>>>>>>>>[ end

tmr.start(0,2000,tmr_cb)

function exit() -- release module sht21 = nil package.loaded["sht21"]=nil end

Sorry a can't upload the module file

Triade1 commented 8 years ago

I have the same issue with timers...

Solution:

function setTimer0() timer0 = true end

function setTimer1() timer1 = true end

function setTimer2() timer2 = true end

-- INIT

timer0 = false timer1 = false timer2 = false

tmr.start(0,1000,setTimer0) tmr.start(1,1000,setTimer1) tmr.start(2,1000,setTimer2)

while( true ) do if (timer0) then timer0 = false readTemp() end

if (timer1) then timer1 = false readLux() end

if (timer2) then timer2 = false readGas() end

tmr.delayms(1000) end

sn2k commented 8 years ago

I2C is still buggy i have trobbels with TSL 2561 2xTSL works with WifiMcu 4x not Working. But i have the Same tested with NodeMCU all works fine. I have capture the SCL and SDA Line i2c_nodemcu_wifimcu

Please check the I2C Module

sn2k commented 8 years ago

Project Wifimcu Dead?

Karim1975 commented 7 years ago

Hi all, I have a problem with I2C module. When i send I2C.adress, the function I2C.adress return nil, never 1. Function: i2c.address(id, dev_addr, 'w') with id=0, dev_adress=slave adress, 'w': want to write. Morever i see the schem of the WIFIMCU board, and i see that the push pull resistor is 10K in SDL and SCL Line. That's correct value ? Thanks for your help.