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/y8QADBwMWgQAZG/ILNAARQ4GLTcDeIIIhxGOBAuD7hOfBB3/94gcJ3w+o5/5eIAIAAAVwWgQAVQ2ORaIQwEMAJiDg95G4nQL7mQVWI6GwRcfsZAcsKkJvxgxEjzFUgfHoSQ9Qq7KNwqHwuB13MA4a1q/DmBrHgPcmjiGoh//EwC5nGPEmS4RcfkVKOhJf+WOgoxJclFz3kgn//dBA+ya1GhurNn8zb//9NNutNuhz31f////9vt///z+IdAEAAAK4LQIAKobHItEIYCGAExBwe8jcToF9zIKrEdDYIuP2MgOWFSE34wYiR5iqQPj0JIeoVdlG4VD4XA67mAcNa1fhzA1jwHuTRxDUQ//iYBczjHiTJcIuPyKlHQkv/LHQUYkuSi57yQT//uggfZNajQ3Vmz+ Zt//+mm3Wm3Q576v////+32///5/EOgAAADVghQAAAAA//uQZAUAB1WI0PZugAAAAAoQwAAAEk3nRd2qAAAAACiDgAAAAAAABCqEEQRLCgwpBGMlJkIz8jKhGvj4k6jzRnqasNKIeoh5gI7BJaC1A1AoNBjJgbyApVS4IDlZgDU5WUAxEKDNmmALHzZp0Fkz1FMTmGFl1FMEyodIavcCAUHDWrKAIA4aa2oCgILEBupZgHvAhEBcZ6joQBxS76AgccrFlczBvKLC0QI2cBoCFvfTDAo7eoOQInqDPBtvrDEZBNYN5xwNwxQRfw8ZQ5wQVLvO8OYU+mHvFLlDh05Mdg7BT6YrRPpCBznMB2r//xKJjyyOh+cImr2/4doscwD6neZjuZR4AgAABYAAAABy1xcdQtxYBYYZdifkUDgzzXaXn98Z0oi9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GVGYjSUUKNpuJUQoOIAyDvEyG8S5yfK6dhZc0Tx1KI/gviKL6qvvFs1+bWtaz58uUNnryq6kt5RzOCkPWlVqVX2a/EEBUdU1KrXLf40GoiiFXK///qpoiDXrOgqDR38JB0bw7SoL+ZB9o1RCkQjQ2CBYZKd/+VJxZRRZlqSkKiws0WFxUyCwsKiMy7hUVFhIaCrNQsKkTIsLivwKKigsj8XYlwt/WKi2N4d//uQRCSAAjURNIHpMZBGYiaQPSYyAAABLAAAAAAAACWAAAAApUF/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



Monday, October 3, 2016

Smart Mon Ext Board - Voltage, Current, Power monitor - P1



UPDATE !! UPDATE !!

Available also on Tindie Store !








And the story behind:

Do you remember the BLMS (Battery Live Monitor System) Project?

Today we will start a new Battery Live Monitor System (BLMS) series related with the SmartMon v.2.7Ex Board.

   SmartMonv2.7Ex board has been born as the next generation of BLMS, bringing more flexibility and a lot of extra functionalities than the previous one.

   If you want to quickly add to your project Voltage, Current and Power monitoring capabilities, plus, as a bonus, a proper hardware RTC with integrated Alarm and Timer functions, this is the extension board that you was looking for.

  The modular design will let you to easy connect it and use it with any MCU you want as long as it has I2C bus capabilities  and is working in the 3-5.5V voltage range. That includes all the popular existing platforms like Arduino, PIC, ARM, Atmega, ESP8266 and many more!



Because of the ammount of the information, the related main Videos and Articles are splitted in 4 Parts, as follow:

Part1 - General view
Part2 - Real Time Clock driver example
Part3 - Power Monitor software example
Part4 - Real World example with Thinkspeak data upload



Part 1 - GENERAL VIEW


FEATURES:

  • TI INA219 integrated high-side current shunt and power monitor
  • High precision, high quality shunt resistor from BOURNS: very low Inductance (10nH) and Temperature Coeficient ( ±20 ppm/°C max.)  
  • Measured Bus Current interval: 0-10A (standard) / 0-20A (optional) 
  • High range Measured Bus Voltages from 0 to 26 V
  • Bus voltage is measured directly on the load side of the shunt resistor
  • SmartMon v.2.Ex Power supply voltage range - 3.3 to 5.5V
  • Standard I2C bus communication ( nEXT Bus connector)
  • PCF8563 RTC - real-time clock and calendar optimized for low power consumption
  • RTC Alarm and timer functions with MOSFET driven Interrupt output
  • On-board regulator option (can be populated and used it when you want to use the same main BUS Voltage input also as a power supply for the SmartMon Board)



Schematics:


High-side Voltage measurement, current shunt and power monitor


 
RTC


Main Bus Voltage Regulator - optional



PCB: 

SmartMon v2.7Ex - Bottom


SmartMon v2.7Ex - TOP



























 Next time in Part2 we will continue exploring the SmartMon v2.7Ex Board and also entering on the software side, talking about the  Real Time Clock usage, Driver example, etc.