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 :)


Tuesday, December 6, 2016

ESPEasy - P2 - Domoticz - RPi - MPDMv4 AC Dimmer setup example


UPDATE !! If you are using Domoticz under Windows, please take a look also at the PART3






Part 2 of the ESPEasy Series. This time we will go further and complete a full ESP8266 / Domoticz Dimmer setup on a RPi2 Board using the MPDMv4 Universal AC Dimmer Board.


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
- Raspberry PI 2 Board
- Domoticz
   Lates available bin on a Raspberry PI 2 Board install :  
     sudo curl -L install.domoticz.com | bash




Software 

 LUA Script for Domoticz MPDM4 Universal AC Dimmer implementation:

commandArray = {}
DomDevice = 'MPDMv4_Dimmer';
IP = '192.168.1.169';
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 step-by-step setup as done in the Youtube Video Tutorial above and all OK, you should end up with a working MPDMv4 AC Dimmer interface as below:



 In Part 3 you can find details about changes that need to be done for a Domoticz for Windows installation.


Happy Breadboarding!!

Saturday, December 3, 2016

ESPEasy Series - Part 1 - Firmware upload and MPDMv4 driver simple test









ESP Easy Series, Part 1: Firmware upload and MPDMv4 driver simple test


  The ESP Easy firmware can be used to turn the ESP module into an easy to use multifunction sensor device for Home Automation solutions, been easy to integrate it in setups based on Domoticz, OpenHUB, etc.
  The configuration process of the ESP Easy is entirely web based, so once you've got the firmware loaded, you don't need any other tool besides a common web browser.

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 !
- PCA9685 16CH PWM Extension board
- MPRSx8 Home Automation Board
- ESP Easy firmware
- Raspberry PI 2 Board
- Domoticz
   Lates available bin on a Raspberry PI 2 Board install :  
     sudo curl -L install.domoticz.com | bash


  First thing that I want to mention, as I found that a lot of people sometime forget about this simple one: You DAC/ADC/PWM, etc Input/Output is as good as your Power Supply/Voltage regulator!! If you have a crap power supply/regulator output with bad filtering & stuff, don't expect miracles happening!



You have at least 3 ways to drive a MPDMv4 AC Dimmer:

   1. ESP8266 PWM output:

      It is working OK as long as you don't have any blocking process running on the ESP8266 that can badly interrupt your PWM signal (1/2 second and more). With a bit of attention to some details and also for not to complicated lighting setup scenarios is working very nice.
    I will do also a more complicated setup example Youtube Video showing you the MPDMv4 AC Dimmer working OK driven directly by ESP8266 PWM pin, connected as a daughter board on the MPRSx8 Home Automation board. From Domoticz you can drive thru ESPEasy the MPRSx8 8ON/OFF AC Relays AND MPDMv4 AC Dimmer channel without any problems at all!

MPDMv4 AC Dimmer board has a first order filter on the VCNT input so can handle even noisy PWM and decent level of duty cycle skipping/derating.



  2. Dedicated PWM IC.

     I have used with a lot of success the PCA9685, working very smooth and has no problem related with ESP8266 workload process, etc. You can find more about a PCA9685 Board here. Probably the best solution for multiple AC Dimmer boards (upto 16 per each PCA9685 Extension Board).



 3. Digital driver using a dedicated DAC IC.

     VCNT input voltage driven from a MCP4726 12 bit DAC output is working very smooth and has no problem related with ESP8266 workload process, etc, same as with PCA9685.
As been a 12 Bit DAC it's giving you very fine VCNT control. I have designed also a Dimmer board with integrated DAC and digital control, but that's another story.




In Part 2, we will talk about Domoticz setup and MPDMv4 AC Dimmer control interface.

Friday, November 25, 2016

ESP Basic - PCA9685-16Channel 12-bit PWM I²C-bus LED controller Driver - Part 1



PCA9685 Extension Board Module Available also on Tindie!





ESP Basic Tutorials Series: 

 PCA9685-16Channel 12-bit PWM I²C-bus LED controller Driver - Part 1

  If you are looking for a very simple to use 16 Channel LED controller or just for a nice 16 Channel, high resolution PWM driver then this one is for you!

   The PCA9685 is an I²C-bus controlled 16-channel LED controller optimized for Red/Green/Blue/Amber (RGBA) color backlighting applications.



 What we will need:

    Connection with the ESP8266 nEXT EVO Board is very easy, as PCA9685 Module connector is fully compatible with the nEXT Bus connector.


 
Driver implementation

   As been a I2C compatible device you need to have a standard I2C Bus Initialisation function as usual and also to know the I2C address of the device.  
  For a more detailed hardware overview go to PCA9685 General Description Article


 Software: 

1. Main program:

 i2c.setup(4,5) 'choose your I2C bus pins

dev_addr = 64  '0x40 - I2C Address for the PCA9685

mode1 = 0    '0x00 location for Mode1 register address
mode2 = 1    '0x01 location for Mode2 reigster address
led0 = 6     '0x06 location for start of LED0 registers
rst = 1      '0x01 reset device
rval = 0     'read register value

led_on = 4096
led_off = 0

ledN = 0

dim led_stat(8)
led_stat(0)=0
led_stat(1)=0
led_stat(2)=0
led_stat(3)=0
led_stat(4)=0
led_stat(5)=0
led_stat(6)=0
led_stat(7)=0


stat = "Init.."

'reset device     write_reg(mode1, rst)
reg = mode1
data = rst
gosub [WRITE_REG]

'check if proper initialised
gosub [READ_REG]
'wprint rval
if rval == "1" then
   status = true
   stat = "PCA9685 Init OK"
else
   status = false
   stat = "PCA9685 Init Failure!"
end if

wprint " <b>PCA9685 - 16Channel 12-bit PWM I2C bus LED controller<br>driver demo</b><br><i>by tech@esp8266-projects.com</i><br><br><br>"
wprint "Initialisation Status: "
textbox stat

'set for auto-increment
let data = 160
gosub [WRITE_REG]

'Choose below the desired output mode!
'Direct LED connection
'set to output mode INVRT = 1 OUTDRV = 0
reg = mode2
data = 16
gosub [WRITE_REG]


'External N-type driver
'write_reg(mode2, 0x04)   -- set to output mode INVRT = 0 OUTDRV = 1

'External P-type driver
'write_reg(mode2, 0x14) --set to output mode INVRT = 1 OUTDRV = 1
 
'choose LED_N - ON
wprint " <br>Input LED number : "
textbox ledN
wprint "<br> <br>"
button " Set LED ON ",[SetON]
button " Set LED OFF ",[SetOFF]
wprint "<br> <br> <br>"
button "LED 0",[LED0]
button "LED 1",[LED1]
button "LED 2",[LED2]
button "LED 3",[LED3]
button "LED 4",[LED4]
button "LED 5",[LED5]
button "LED 6",[LED6]
button "LED 7",[LED7]
wprint "<br> <br><br>"
button "LOOP",[loop]
wprint "<br> <br><br> <br>"
button "EXIT",[Exit]
wait


2. LOW Level subroutines


2.1 WRITE Register "reg"

[WRITE_REG]
i2c.begin(dev_addr)
i2c.write(reg)
i2c.write(data)
i2c.end()
return

2.2 READ Register "reg"


[READ_REG]
i2c.begin(dev_addr)
i2c.write(reg)
i2c.end()
i2c.begin(dev_addr)
i2c.requestfrom(dev_addr,1) 'start a transaction to read 1 byte
rval = i2c.read()           'read the byte
i2c.end()
return


2.3 WRITE LED_N Register
 

[WRITE_LED]
i2c.begin(dev_addr)
i2c.write(led0+4*ledN)
ah = led_on >> 8
al = led_on and 255
i2c.write(al)
i2c.write(ah)

ah = led_off >> 8
al = led_off and 255
i2c.write(al)
i2c.write(ah)
i2c.end()
return


 3. BUTTON Subroutines

[LED0]
ledN = 0
gosub [LED_SW]
wait

[LED1]
ledN = 1
gosub [LED_SW]
wait

[LED2]
ledN = 2
gosub [LED_SW]
wait

[LED3]
ledN = 3
gosub [LED_SW]
wait

[LED4]
ledN = 4
gosub [LED_SW]
wait

[LED5]
ledN = 5
gosub [LED_SW]
wait

[LED6]
ledN = 6
gosub [LED_SW]
wait

[LED7]
ledN = 7
gosub [LED_SW]
wait



4. LED_N Switch subroutine



[LED_SW]
if led_stat(ledN) = 0 then
   led_stat(ledN) = 1
   led_on = 4096
   led_off = 0
   gosub [WRITE_LED]
else
   led_stat(ledN) = 0
   led_on = 0
   led_off = 4096
   gosub [WRITE_LED]
end if
return


 

5. ON / OFF Subroutines

          [SetON]
led_on = 4096
led_off = 0
gosub [WRITE_LED]
wait

[SetOFF]
led_on = 0
led_off = 4096
gosub [WRITE_LED]
wait


 
6. FOR loop demo
[loop]
For x = 0 to 8
ledN = x
gosub [LED_SW]
Next x
For x = 8 to 0 step -1
ledN = x
gosub [LED_SW]
Next x
wait



7. EXIT Program subroutine

[Exit]
end


 


In the ESP Basic Web editor interface Type & Save your program as "PCA9685.bas" and Run it.  
If all OK the result should look as below:
  



Monday, November 14, 2016

Mailbag - PCF8591 I2C 8 bit ADC/DAC Driver - Part 2








ESP Basic seriesI2C driver example for the PCF8591 I2C 8-bit A/D and D/A converter - PART2

For a deeper description of the PCF8591 and simple DAC driver example please go to PART 1



 What we will need:


    Connection with the ESP8266 nEXT EVO Board is very easy, as PCF8591 Module connector is fully compatible with the nEXT Bus connector.


Driver implementation
As been a I2C compatible device you need to have a standard I2C Bus Initialisation function as usual and also to know the I2C address of the device.  
For a detailed PCF 8591 Control Byte description go to PART1.


Software: 

1. Main program: 
 
let address = 72   'PCF8591 I2C Address
i2c.setup(4,5)       'choose your I2C bus pins

cls
let dac = 0
let dac_v = 0
let adc0_1 = 0
let adc0_v = 0
let v_cal = 0.0128

adc0_1 = "ADC stopped"

wprint " <b>PCF8591 8-bit A/D and D/A converter driver demo</b><br><i>by tech@esp8266-projects.com</i><br><br> "

wprint " Input DAC value"
textbox dac

button " Set Value ",[SetDAC]

wprint " <br>DAC Output (V)"
textbox dac_v

wprint "<br>"

wprint "<br><br>ADC0 Read Value:"
textbox adc0_1

wprint "<br>ADC0 Read (Volt):"
textbox adc0_v
wprint "<br><br>"
 
button " Start ADC_0 ",[ADC0]
button " Stop ADC", [Exit ADC]

wprint "<br><br>"
button " Stop Program", [Exit]

wait


2. Set DAC subroutine: 

 [SetDAC]
dac_v = dac * v_cal

i2c.begin(address)
i2c.write(64)  'DAC Enable
delay 5
i2c.write(dac)
i2c.end()
wait


3. Enable ADC subroutine:

[ADC0]
timer 1000, [readADC]
wait

 

4. Read ADC subroutine:
 
[readADC]
ch = 64
i2c.begin(address)               'start another transaction
i2c.write(ch)                         'point to the ADC_0 - keep DAC ON
delay 5
i2c.end()                                'end write transaction

i2c.begin(address)                 'start another transaction
i2c.requestfrom(address,2)    'start a transaction to read 2 bytes
delay 5
adc0_0 = i2c.read()              'read the 1st byte
adc0_1 = i2c.read()              'read the 2nd byte
i2c.end()                                'end read transaction
adc0_v = v_cal + adc0_1 * v_cal
wait

 

5. EXIT ADC subroutine:
 
[Exit ADC]
timer 0
adc0_1 = "ADC stopped"
wait



 

6. Exit Program button subroutine:
 
[Exit]
timer 0
end

 

In the ESP Basic Web editor interface Type & Save your program as "PCF8591.bas" and Run it.  

If all OK the result should look as below:



Friday, November 11, 2016

Mailbag - PCF8591 I2C 8 bit ADC/DAC Driver - Part 1








ESP Basic series -  I2C driver example for the PCF8591 I2C 8-bit A/D and D/A converter.


General view:



   The PCF8591 is a single-chip, single-supply low-power 8-bit CMOS data acquisition
device with four analog inputs, one analog output and a serial I2C-bus interface. Three
address pins A0, A1 and A2 are used for programming the hardware address, allowing
the use of up to eight devices connected to the I2C-bus without additional hardware.
Address, control and data to and from the device are transferred serially via the two-line
bidirectional I2C-bus.
 

  The functions of the device include analog input multiplexing, on-chip track and hold
function, 8-bit analog-to-digital conversion and an 8-bit digital-to-analog conversion. The
maximum conversion rate is given by the maximum speed of the I2C-bus.



Features:

  • Single power supply
  • Operating supply voltage 2.5 V to 6.0 V
  • Low standby current
  • Serial input and output via I2C-bus
  • I2C address selection by 3 hardware address pins
  • Max sampling rate given by I2C-bus speed
  • 4 analog inputs configurable as single ended or differential inputs
  • Auto-incremented channel selection
  • Analog voltage range from VSS to VDD
  • On-chip track and hold circuit
  • 8-bit successive approximation A/D conversion
  • Multiplying DAC with one analog output.

For more details please take a look at the PCF8591 Datasheet


What we will need:

    Connection with the ESP8266 nEXT EVO Board is very easy, as PCF8591 Module connector is fully compatible with the nEXT Bus connector.



Driver implementation

As been a I2C compatible device you need to have a standard I2C Bus Initialisation function as usual and also to know the I2C address of the device.

Control Byte is the second byte sent to a PCF8591 device and is stored in its control register and is required to control the device function :


 Things to take care about: 

 The upper nibble of the control register is used for enabling the analog output, and for programming the analog inputs as single-ended or differential inputs. 
  The lower nibble selects one of the analog input channels defined by the upper nibble .

  If the auto-increment flag is set, the channel number is incremented automatically after each A/D conversion. If the auto-increment mode is desired in applications where the internal oscillator is used,
the analog output enable flag must be set in the control byte (bit 6). This allows the
internal oscillator to run continuously, by this means preventing conversion errors
resulting from oscillator start-up delay. 


  The analog output enable flag can be reset at other times to reduce quiescent power consumption.
 

   The selection of a non-existing input channel results in the highest available channel number being allocated. Therefore, if the auto-increment flag is set, the next selected channel is always channel 0. 

   The most significant bits of both nibbles are reserved for possible future functions and must be set to logic 0. After a Power-On Reset (POR) condition, all bits of the control register are reset to logic 0. 
   The D/A converter and the oscillator are disabled for power saving. 
   
  The analog output is switched to a high-impedance state.
 

Software: 

1. Main program: 
 
let address = 72 'PCF8591 I2C Address
i2c.setup(4,5) 'choose your I2C bus pins

cls
let dac = 0
let dac_v = 0
let v_cal = 0.0128

wprint " <b>PCF8591 - 8-bit A/D and D/A converter driver <br>DAC demo</b><br><i>by tech@esp8266-projects.com</i><br><br> "

wprint " Input DAC value"
textbox dac

button " Set Value ",[SetDAC]

wprint " <br>DAC Output (V)"
textbox dac_v

wprint "<br><br>"
button " Stop Program", [Exit]

wait




2. Set DAC subroutine: 

 
[SetDAC]
dac_v = dac * v_cal
i2c.begin(address)
i2c.write(64) 'Enable DAC
delay 5
i2c.write(dac)
i2c.end()
wait


3. EXIT program button:

[Exit]
timer 0
end


In the ESP Basic Web editor interface Type & Save your program as "PCF8591.bas" and Run it.  

 
If all OK the result should look as below:




If you want your program to start automatically at reboot/power ON then just Save it as "default.bas" and also from Settings Tab enable the "Run default.bas at startup".
  



Be aware that at start-up/reboot, it is a delay before your program will start automatically.



In  the next part about PCF8591 we will talk about the ADC Driver implementation.





Saturday, November 5, 2016

WIFI Dev Board for Home Automation - Part 2



This is Part 2 of the Wifi Dev Board for Home Automation series. For general presentation and hardware description please take a look also at Part 1 of the series


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







What we will need:



Software implementation:

As this time we will talk about the Software side we will design a very simple driver for our board that will include also a interactive Web command interface for the MPRSx8 Home Automation Dev Board Relays.

To keep things simple, we will just add in our Web Interface 8 ON/OFF Buttons and one general OFF one, from where we can turn ON/OFF all the Relays switches. 


1. Main Program
cls                       ' clear interface
let address = 56  'PCF8574 I2C Address

i2c.setup(4,5)      'choose your I2C bus pins

i2c.begin(address)
ss = 0 xor 255      'XOR - Bit masking for the desired I/O pins
i2c.write(ss)
i2c.end()
button "x1", [5]    ' Button for Relay 1
button "x2", [6]
button "x3", [7]
button "x4", [8]
button "x5", [1]
button "x6", [2]
button "x7", [3]
button "x8", [4]    ' Button for Relay 8
button "OFF", [9] ' General OFF for all the Relays
wait


2. General OFF for all the Relays subroutine
[9]
i2c.begin(address)
ss = 0 xor 255 'XOR - Bit masking for the desired I/O pins
i2c.write(ss)
i2c.end()
wait


3. Subroutines for each Button
[1]
i2c.begin(address)
ss = ss xor 1   'XOR - Bit masking for the desired I/O pins
i2c.write(ss)
i2c.end()
wait

[2]
i2c.begin(address)
ss = ss xor 2
i2c.write(ss)
i2c.end()
wait

[3]
i2c.begin(address)
ss = ss xor 4
i2c.write(ss)
i2c.end()
wait

[4]
i2c.begin(address)
ss = ss xor 8
i2c.write(ss)
i2c.end()
wait

[5]
i2c.begin(address)
ss = ss xor 16
i2c.write(ss)
i2c.end()
wait

[6]
i2c.begin(address)
ss = ss xor 32
i2c.write(ss)
i2c.end()
wait

[7]
i2c.begin(address)
ss = ss xor 64
i2c.write(ss)
i2c.end()
wait

[8]
i2c.begin(address)
ss = ss xor 128
i2c.write(ss)
i2c.end()
wait


In the Web editor interface Save your program as "test_MPRSx8_1.bas" and Run it.



If all OK the result should look as below:




If you want your program to start automatically at reboot/power ON then just Save it as "default.bas" and also from Settings Tab enable the "Run default.bas at startup".



Be aware that at start-up/reboot, it is a delay before your program will start automatically.




WIFI MAINS 8x Relay Board for Home Automation and more


Available also on Tindie store: https://www.tindie.com/stores/nEXT_EVO1/








Features:
  • Compact, standard size: 16x5 cm! (half Eurocard height)
  • Universal AC MAINS input (240VAC/50Hz (EU) or 110V/60Hz (US)
  • Proper MOV and high quality ceramic FUSE MAINS input protection.
  • 8 x MAINS rated 15A Relays
  • 8 x independent swithing Power Channels 
  • Power Channels can be software configured for mutually exclusive usage
  • Recommended load upto 10A each channel
  • LED signaling Panel for each Power Channel Status 
  • Integrated switchmode MAINS PSU
  • Separate high efficient switchmode PSU for command & control circuit
  • Can work also from a external +12V power supply just not install the MAINS PSU
  • Safe operation design
  • I2C / nEXT Bus compatible - you can add your own extension modules - light sensors, gas, motion, etc
  • Can be stacked upto 8 boards ( 1 Master and 7 slaves) for a total of 64 Power Channels!!
  • Compatible with any I2C compatible MCU: Arduino, ARM, PIC, ATMEGA, ESP8266, ESP32, etc
  • ESP8266 friendly design: can add your ESP8266 module directly onboard for a full WIFI MAINS Power switchboard solution
  • Full access to ESP8266 pins thru 2 headers gives you even more flexibility in developing your projects!
  • Full programming header  (Tx, Rx, GPIO0 and Reset)

Command and control side - pinout details




 Schematics:


MAINS input power supply



3.3V power supply for the command and control section



Relays driver


In Part 2 we will continue with the software side, how to connect, program and use the MPRSx8 Relay board.






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, October 27, 2016

ESP Basic Tutorial - Beep Button


ESP Basic tutorial: How to "beep" your Button in the Web interface using a bit of Javascript :)

Take the code from below and save it in "beep.js" file on your PC

<code>
function beep() {
  (new
    Audio(
    "data:audio/wav;base64,//uQRAAAAWMSLwUIYAAsYkXgoQwAEaYLWfkWgAI0wWs/ItAAAGDgYtAgAyN+QWaAAihwMWm4G8QQRDiMcCBcH3Cc+CDv/7xA4Tvh9Rz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jURNIHpMZBGYiaQPSYyAAABLAAAAAAAACWAAAAApUF/Mg+0aohSIRobBAsMlO//Kk4soosy1JSFRYWaLC4qZBYWFRGZdwqKiwkNBVmoWFSJkWFxX4FFRQWR+LsS4W/rFRb//////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////VEFHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAU291bmRib3kuZGUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjAwNGh0dHA6Ly93d3cuc291bmRib3kuZGUAAAAAAAAAACU="
    )).play();
}
</code>

Upload "beep.js" file on your ESP8266

Type,  Save as "sound.bas" & RUN the code from below on your ESP8266 :

<code>
 cls
javascript beep.js
wprint |<BUTTON onClick="beep();">Beep!</BUTTON>|
wait
</code>

Press the Beep! button. Does it beep or not? :)

--------------------------------------------------------------------------------------------------------------------------

!! Update !!

SECOND OPTION

If you don't want to embed also the sound data and maybe want to load it from an external server then this can be done as below:

Code to be typed saved on your PC as "playsound.js":

<code>
function PlaySound() {
  var sound = document.getElementById("audio");
  sound.play()
}
</code>

Upload "playsound.js" file on your ESP8266.


Type,  Save as "sound2.bas" & RUN the code from below on your ESP8266 :

 <code>
 cls
wprint "Let's try a Beep!<br><br>"
wprint |<audio id="audio" src="http://www.soundjay.com/button/beep-06.wav" autostart="false" ></audio>|
javascript playsound.js
wprint |<a onclick="PlaySound()"> Play</a>|
wait
</code>

Click on play "Play". Does it beep or not? :)

Friday, October 14, 2016

Smart Mon Ext Board - RTC clock driver example - P2



Part 2 of the SmartMon v2.7ex series.


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



    This time we are exploring the Real Time Clock  implementation and we will see how easy or complicated is to set & program the onboard RTC Clock.







   And the story behind:

    For a complete monitoring/ data logging experience, a nice to have feature is a proper RTC clock. Having this in mind, for the new version of the SmartMon I choose to add it on-board for a very simple and easy integration in your related Voltage, Current, Power projects.

   The choosen one is the PCF8563 from NXP, a very nice and easy to program CMOS Real-Time Clock (RTC) and Calendar optimized for low power consumption. A programmable clock output, interrupt output and voltage-low detector are also provided. All addresses and data are transferred serially via a two-line bidirectional I2C-bus with a maximum bus speed of 400 kbit/s.


Features: 

     •  Provides year, month, day, weekday, hours, minutes, and seconds based on a

        32.768 kHz quartz crystal
     •  Century flag

     •  Clock operating voltage: 1.0 V to 5.5 V at room temperature

     •  Low backup current; typical 0.25uA at Vdd =3.0V and Tamb=25C


     •   400 kHz two-wire I2C-bus interface (at VDD= 1.8 V to 5.5 V)

     •   Programmable clock output for peripheral devices (32.768 kHz, 1.024 kHz, 32 Hz, and 1Hz)

     •   Alarm and timer functions - separate Alarm triggered MOSFET output with separate voltage   
                     input/supply included onboard, can direct drive upto 2A external devices, 3/5/12V 
                     Relays, interrupts, etc

     •   Integrated oscillator capacitor

     •   Internal Power-On Reset (POR)

     •   I2C-bus slave address: read A3h and write A2h

     •   Open-drain interrupt pin




Schematic:


SmartMon v2.7Ex - RTC module schematic

 For more details about the RTC, please see PCF8563 Datasheet and the related PCF8563 RTC clock driver Article



What we will need:

    Connection with the ESP8266 nEXT EVO Board is very easy, as Analog Extension Board - AN1 connector is fully compatible with the nEXTBus connector:

SmartMon v2.7Ex Board

    This time we are talking here about ESP8266/LUA driver but also Arduino implementation will follow.

    If you use another ESP8266, or Arduino, ARM, PIC, whatever MCU you use there days for your projects, then just be sure that you are connecting the I2C lines (SDA/SCL) on the allocated pins for your setup.


Driver implementation


    As PCF8563 has a I2C compatible compatible interface, driver building it following more or less the same  process  as before for I2C devices.

   For more details about the RTC, please take a deeper look at the  PCF8563 Datasheet and the related PCF8563 RTC clock driver Article.



1. Data conversion functions:

  1.1 Decimal to BCD:


        function decToBcd(val)
             local d = string.format("%d",tonumber(val / 10))
             local d1 = tonumber(d*10)
             local d2 = val - d1
            return tonumber(d*16+d2)
         end

  

1.2  BCD to Decimal:

      function bcdToDec(val)
           local hl=bit.rshift(val, 4)
           local hh=bit.band(val,0xf)
          local hr = string.format("%d%d", hl, hh)
          return string.format("%d%d", hl, hh)
     end


 
2. Init I2C bus/interface:

        address = 0x51, -- A2, A1, A0 = 0
        id = 0


        init = function (self, sda, scl)
               self.id = 0
              i2c.setup(self.id, sda, scl, i2c.SLOW)
       end

 

3. ReadTime function:

   readTime = function (self)
       wkd = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }
       i2c.start(self.id)
       i2c.address(self.id, self.address, i2c.TRANSMITTER)
       i2c.write(self.id, 0x02)
       i2c.stop(self.id)
       i2c.start(self.id)
       i2c.address(self.id, self.address, i2c.RECEIVER)
       c=i2c.read(self.id, 7)
       i2c.stop(self.id)
       return  bcdToDec(bit.band(string.byte(c,1),0x7f)),
               bcdToDec(bit.band(string.byte(c,2),0x7f)),
               bcdToDec(bit.band(string.byte(c,3),0x3f)),
               bcdToDec(bit.band(string.byte(c,4),0x3f)),
               wkd[tonumber(bcdToDec(bit.band(string.byte(c,5),0x7)))],
               bcdToDec(bit.band(string.byte(c,6),0x1f)),
               bcdToDec(string.byte(c,7))
   end



4. SetTime function:

   setTime = function (self, second, minute, hour, day, date, month, year)
       i2c.start(self.id)
       i2c.address(self.id, self.address, i2c.TRANSMITTER)
       i2c.write(self.id, 0x02)
       i2c.write(self.id, decToBcd(second))
       i2c.write(self.id, decToBcd(minute))
       i2c.write(self.id, decToBcd(hour))
       i2c.write(self.id, decToBcd(day))
       i2c.write(self.id, decToBcd(date))
       i2c.write(self.id, decToBcd(month))
       i2c.write(self.id, decToBcd(year))
       i2c.stop(self.id)
   end



For testing,  pack it together and save the code on ESP as 'pcf8563.lua', restart ESP and run:

-- Set Initial Time and Date
require('pcf8563')                                -- call for new created PCF8563 Module Driver
sda, scl = 2, 1                                      --  declare your I2C interface PIN's
pcf8563:init(sda, scl)                          
-- initialize I2C Bus

 pcf8563:setTime(0,34,13,12,4,3,15)   -- setTime(s,min,hour,day,weekday,month, year)
-- get Time and Date
require('pcf8563')
sda, scl = 2, 1
pcf8563:init(sda, scl)


s, m, h, d, dt, mn, y = pcf8563:readTime()        --ReadTime function call
=string.format("%s - %s/%s/20%s",dt, d, mn, y)
=string.format(" %s:%s:%s", h, m, s)



Next Article in series can be found here Part 3: INA219 Driver example and software implementation


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