Monday, January 9, 2017

ESPBasic driver example for AT24C32 - I2C EEPROM


Do you remember the widely used DS3231 RTC Module

As you might rememeber we have identified onboard an EEPROM chip, a 32k AT24C32 one and have written some driver examples for LUA 

I was looking a few days ago for a I2C EEPROM to do some tests with ESPBasic and the results...can be seen below :)

1. MAIN Program
cls
i2c.setup(4,5)                    'choose your I2C bus pins

let eeprom_address = 0    'EEPROM memory start address
i2c_address = 87              ' 0x57 - AT24C32 I2C address
let i = 0
stw = "87654321"            'string that we want to save in EEPROM

button "i2c Read",[I2C_read]
button "i2c Write",[I2C_Write]
wait

2. EEPROM READ Function





[I2C_read]
  stwl = len(stw)
  print "Reading from I2C address " & i2c_address
  adrh = eeprom_address >> 8
  adrl = eeprom_address and 255

  i2c.begin(i2c_address)
  i2c.write(adrh)
  i2c.write(adrl)
  i2c.end()

  i2c.begin(i2c_address)
  i2c.requestfrom(i2c_address,stwl)
  tr = i2c.available()
  print "Available bytes :" & tr
  do
      i2c_read = i2c.read()
      readt = i2c_read-48
      print  "Read value : " & readt
      i = i + 1
  loop while i < tr
  i = 0
  i2c.end()
  print "Read complete"
wait


3. EEPPROM WRITE function

[I2C_Write]
 print "Writing to I2C address " & i2c_address

 stwl = len(stw)
 print "String : " & stw
 print "String Lenght : " & stwl

 adrh = eeprom_address >> 8
 adrl = eeprom_address and 255

 i2c.begin(i2c_address)
 i2c.write(adrh)
 i2c.write(adrl)

 i = 1
 do
     stru = mid(stw,i,1)
     print  "Value to Write: " & asc(stru)
     i2c.write(asc(stru))
     i = i + 1
 loop while i < stwl+1
 i = 0
 i2c.end()
 print "Write complete"
wait

Of course, this is just a proof of concept driver example for the I2C 24C32 EEPROM but I think might help you with a good start :)



Happy breadboarding,
TJ.

Thursday, December 8, 2016

ESPEasy - P3 - Domoticz - Windows - MPDMv4 AC Dimmer setup example



In this Part3 of the ESPEasy series we are talking about the changes that need to be done for a Windows 7 Domoticz installation.


You can find more details about ESP Easy firmware HERE, on the ESP Easy WIKI page



What we will need:

- ESP8266 NextEVO Board
- MPDMv4 Universal AC Dimmer - available also on Tindie Store !
- ESP Easy firmware
- Domoticz - Windows version
- CURL for Windows
- PC with Windows 7 OS installed



The install process is pretty straight forward so I will not insist to much on it:

1. Install Domoticz
2. Install CURL
3. Save LUA script in Domoticz \scripts\lua folder
4. Check that you don't have any Firewall/AV/Whatever program blocking the Network IN/OUT traffic for the needed programs and Ports !

For the used version of CURL for Windows installed  I was forced to change a bit the sintax, nothing fancy, just see below:
 
   runcommand = "curl http://" .. IP .. "/control?cmd=PWM,"  ..PIN.. "," .. CalcValue .. " ";



Software 

Updated  LUA Script for Domoticz for WINDOWS MPDM4 Universal AC Dimmer implementation:

commandArray = {}
DomDevice = 'MPDMv4_Dimmer';
IP = '192.168.2.106';
PIN = "5";
if devicechanged[DomDevice] then
   if(devicechanged[DomDevice]=='Off') then
     print ("OFF dimm = "..uservariables['dimm']);
     CalcValue = 890;
   else if(devicechanged[DomDevice]=='On') then
        DomValue = uservariables['dimm'];
        print ("ON dimm = "..uservariables['dimm']);
        CalcValue = DomValue;
      else
         print("Other");
         DomValue = otherdevices_svalues[DomDevice];
         CalcValue = 900-(DomValue*9);
         commandArray['Variable:dimm'] = tostring(CalcValue);
         print ("dimm Level = "..uservariables['dimm']);
   end
   end
   runcommand = "curl http://" .. IP .. "/control?cmd=PWM,"  ..PIN.. "," .. CalcValue .. " ";
   os.execute(runcommand);
print("PWM calculated value= "..CalcValue);
end
return commandArray

You need to save it in your Domoticz folder under "/scripts/lua/" subfolder as "script_device_MPDMv4_Dimmer.lua"


If followed the step-by-step setup from above and all OK, you should end up with a working MPDMv4 AC Dimmer interface as before under RaspberryPI :)