Thursday, March 16, 2017

Smart Mon Ext Board - Software example - P3

This is Part 3 of the SmartMon Extension Board series.

  SmartMon v2.7ex Board  is a extension board fully compatible with Arduino, ESP8266, ARM, PIC & other available MCu's out there.  As long as your MCU/Dev Board has I2C Bus capabilities you are good to go!
It is probably one of the easiest ways to create your own Voltage/Current and Power consumption monitoring system for your Projects, devices, batteries, power supplies and many more!


UPDATE !! SmartMon v2.7Ex is available also on Tindie Store !






Previous Articles:


What we will need:


Software implementation

1.  INIT Data
id = 0
sda = 2
scl = 1
devaddr = 0x40   -- A0,A1 = GND

voltage = 0
current = 0
power = 0
peng = 0
eng = 0

2. I2C  Init function 
function init_i2c()
  i2c.setup(id, sda, scl, i2c.SLOW)
end

3. INA219 Reset function
function reset()
  write_reg(0x00, 0xFFFF)
end

4. Read register function
 --read from reg_addr content of dev_addr
function read_reg_str(reg_addr)
  i2c.start(id)
  i2c.address(id, devaddr, i2c.TRANSMITTER)
  i2c.write(id,reg_addr)
  i2c.stop(id)
  tmr.delay(1)
  i2c.start(id)
  i2c.address(id, devaddr, i2c.RECEIVER)
  c=i2c.read(id, 16) -- read 16bit val
  i2c.stop(id)
  return c
end

5. Read register function - 16bit--returns 16 bit int
function read_reg_int(reg_addr)
  i2c.start(id)
  i2c.address(id, devaddr, i2c.TRANSMITTER)
  i2c.write(id,reg_addr)
  i2c.stop(id)
  tmr.delay(1)
  i2c.start(id)
  i2c.address(id, devaddr, i2c.RECEIVER)
  local c = i2c.read(id, 16) -- read 16bit val
  i2c.stop(id)
  --convert to 16 bit int
  local val = bit.lshift(string.byte(c, 1), 8)
  local val2 = bit.bor(val, string.byte(c, 2))
  return val2
end

6. Write register function
function write_reg(reg_addr, reg_val)
  print("writing reg:" .. reg_addr .. ", reg_val:" .. reg_val)
  i2c.start(id)
  i2c.address(id, devaddr, i2c.TRANSMITTER)
  local bw = i2c.write(id, reg_addr)
  local bw2 = i2c.write(id, bit.rshift(reg_val, 8))
  local bw3 = i2c.write(id, bit.band(reg_val, 0xFF))
  i2c.stop(id)
end

7. Calibration / settings function
function setCAL_reg()
  maxVoltage = 32
  maxCurrentmA = 10000
  write_reg(0x05,3950) --CALIBRATE FOR YOUR rshunt & stuff
  local config = 15391
  write_reg(0x00, config)
end

8. Read Current values (mA)
 function getCurrent_mA()
  local valueInt = read_reg_int(0x04)
  return valueInt
end

9. Read Bus Voltage (V)
 function getBusVoltage_V()
  local valueInt = read_reg_int(0x02)
  -- Shift to the right 3 to drop CNVR and OVF and multiply by LSB = 4
  local val2 = bit.rshift(valueInt, 3)
  local val2 = val2 * 4
  return val2 * 0.001
end

10. Read Shunt resistor voltage drop (mV)
function getShuntVoltage_mV()
  -- Gets the raw shunt voltage (16-bit signed integer, so +-32767)
  local valueInt = read_reg_int(0x01)
  return valueInt * 0.01
end

11. Read Bus Power (W) 
function getBusPowerWatts()
  local valueInt = read_reg_int(0x03)
  return valueInt*20*0.001
end

12. Print read values 
 function print_values()
  volt = getBusVoltage_V()
  print("\nVoltage  :  " .. volt.." V")
  current = getCurrent_mA()/1000
  power = getBusPowerWatts()
  if (current > 65 ) then
         print("ERR 00.23")
         current = 0
         power = 0
  end
  eng = eng + power/60
  print("Current  : " .. string.format("%6.3f",current) .." A")
  print("Power    :  " .. power .." W\n")
  print("Energy   : " .. string.format("%6.3f",eng) .." Wh\n")
end

MAIN Program
---- INA216 Module TEST
init_i2c()
setCAL_reg()
print_values()

If you want to read and update values every minute add function from below:

tmr.alarm( 1, 60000, 1, function()
     print_values()
     sendDataTh()
end)
You can stop your running timer anytime with:
tmr.stop(1)




That's all for today, next time we will more further and we will go thru a full SmartMon Battery Monitor System + Thinkspeak data upload example.
 


Creative Commons License All schematics, boards, software and articles released by ESP8266-Projects.com are licensed under a Creative Commons Attribution-NonCommercial 4.0 International License









Thursday, March 9, 2017

MPRSx8 - P4 - Domoticz configuration



MPRSx8 Board is available also on Tindie store: https://www.tindie.com/stores/nEXT_EVO1/

For MPRSx8 general presentation and hardware description please take a look also at Part 1 of the MPRSx8 series:http://www.esp8266-projects.com/2016/11/wifi-mains-8x-relay-board-for-home.html



Part 4 of the MPRSx8 + ESPEasy Tutorial - After having all the bits & pieces in place, ESPEasy Formware properly uploaded and configured, now is time for the Domoticz setup and testing the Control interface!






What we will need:
         - CP2102 version
         - Another even smaller size version
         - extra pinout version
         - FT232 Version - Full pinout. Looks nice but because of the FTDI horror stories I would probably stay away from it.


Thank you all for watching and see you next time! If you like my Articles and Tutorials please subscribe!

Happy breadboarding,
TJ.