Wednesday, January 9, 2019

How to convert html to pdf using Puppetter from dotnet Core


Puppetter is a nodejs library developed by google which calls chromium on the background to do certain tasks. One of this tasks can be convert an html document/url into a pdf.
Since we want to use this feature from dotnet core C# there are a few steps that we must follow.

First we need the nutget package for NodeServices
Install-Package Microsoft.AspNetCore.NodeServices

Then we need to enable this NodeServices on our Startup.cs


Install required node packages

npm init
npm install puppetter
npm install fs-extra
npm install uuid

Create a puppetter.js file on your solution with this contents.
Now everything is set, only step required is to call the module from C#.

Friday, August 5, 2016

Internet de las cosas en arroz y habichuelas

Que es el Internet de las cosas?
Segun Google, “Un propuesto desarollo en el cual objetos cotidianos tienen conectividad en una red
permitiendo asi recibir y enviar data”

Cuando se dice objetos cotidianos, que son?
Telefonos, Autos, bicicletas, televisores, neveras, muebles, ropa. Absolutamente todo lo que usamos a
diario.

Y eso de conectividad? El Internet como el celular?
Sí, mas no tan solo eso. Una red puede ser por cable o por wireless. Pública o privada. Puedo tener una
red de comunicación de las ventanas de la casa con el aire acondicionado y no necesariamente esta
información estar saliendo de mi casa. Esto seria una red privada. Publica es que sale de mi casa/oficina al mundo exterior independientemente si solo yo puedo verla. Ahí definitivamente la vía de comunicación
seria el Internet.

Eso de que los equipos se conectan, como así?
Un ejemplo simple. Imagine que el aire acondicionado tenga comunicación con la puerta y las ventanas
de la habitación y pueda alertar que la ventana esta abierta y dar una alerta o cerrarla automáticamente.
El que la secadora sepa que acabo de encender el aire acondicionado y espere a que el consumo inicial (o
pico) del aire baje y entonces se active. Así puedo ahorrar luz y ser una carga menor para sistema.

Es esto nuevo?
No. La idea de inter-conectividad lleva mucho tiempo. Según Wikipedia. En el 1982 una máquina de
refresco en el Carnegie Mellon University se convirtió en el primer equipo en tener conectividad al
Internet informando los niveles de inventario.

Y que se espera con esto del internet de las cosas?
Los expertos (Gartner. Estima) dicen que equipos catalogados como Internet of Things
va a crecer a 26 billones de equipos para el 2020, y esto es sin incluir tabletas, teléfonos y
computadoras, hablamos de cafeteras, ropa, mascotas y/o personas. Estos números varían un poco
dependiendo la fuente pero todos están de acuerdo que es algo grande.
Económicamente estamos hablando de un mercado en los trillones de dolares. Estiman casi 2 trillones
de dolares en valor añadido.

Que industrias están mucho en esto?
Esto afecta a todas las industrias de una manera u otra. Desde manufactura hasta consumo o
agricultura.
Por lo que veo, todo apunta a que el crecimiento mayor está en la industria de la Salud.
Piensa en un dispositivo que una persona pueda hasta dormir con el. Que detecte algo raro y pueda
preveer que esta persona va a sufrir un ataque cardíaco. Que automáticamente llame a emergencias
medicas y vengan a buscarte antes que te de el ataque cardíaco. La persona como quiera va a sufrir del ataque, pero le pasaría bajo supervisión medica. De esta manera reduciendo los riesgos de daños. Estamos hablando de cosas que pueden prolongar nuestra vida útil e independencia así como mejorar nuestra calidad de vida. Reducir costos en tratamientos y pólizas también.

Es bueno o malo?
Si lo que buscamos es detenerlo pensando si es bueno o malo. Ya eso pasó. Esto no tiene vuelta atrás nos guste o no. Esto algo que está cambiando el mundo y si lo vamos a usar para el bien o mal depende de nosotros.

Los profecionales tienen la responsabilidad de crear adelantos para bien. La ciudadanía en general decide si acepta o no ciertas tecnologias. Por lo cual debemos abrirnos al diálogo y proveer espacio a cada cual que exponga su punto de vista. Claro está antes de abrir esa boca linda que tenemos, está en nuestro sano juicio el educarnos para así hablar con sentido.



Saturday, July 30, 2016

Are other developers better than me?

It's not uncommon for a developer to question about his/her skills. And compare to other. Specially against those great developer that do products used by millions of people. Well the ego is common among not so good developers however good developers always think there is other that knows more. You can check yourself in the category that may better suits you.

It is true, there is always someone that knows more than you. Other that can code faster and cleaner. Other that can design software like da Vinci did those amazing inventions. All might be true. 

However nobody is perfect and that is nobody.

That said we do always need to work on our skills as there is always room to improve. Unicorns do not exist and neither perfect developers. Success is not synonym of perfection.
In conclusion, keep working on your skills and do not underestimate your abilities. Nor the capacity to learn new things. But remember to keep the ego under control.

Thursday, April 28, 2016

BSidesPR 2016 Led Matrix break down

So you got a BSides PR 2016 badge? Pretty cool ahh. If you are wondering how this led matrix works, well lets look at it.
 The badge led matrix is pretty simple. It consist of two shift registers that control rows and columns. Rows and Columns are represented by an eight bit number. Refer to picture bellow to show how bits are arranged in relation to the badge matrix.
When sending data from the micro controller to the badge. We first send row bits then send column bits. Data is pushed from one shift register into the other via the overflow bit. In order to turn on a led or multiple led the right combination of bits in row and column should be achieved. A 0 value on a row and a 1 value on a column will make a led to turn on.
This is an example of several leds turning on. We are using a set of functions provided in the firmware example available on github.
This example is sending a 0x01 value to the rows ( 00000001 ) and a 0x80 to the columns ( 10000000 ). This cause the columns bits 0 - 6 to be 0 and Column bit 7 be 1. Also to have Row bits 1-7 = 0 and Row bit 1 = 0. The yellow cells with ON represent what leds shold be turned on.
To ease the sending of data a function ledMatrix.write is used. This function receives two parameters. The first parameter being the byte for the rows and the second parameter the byte for the columns. In this example the function is called ledMatrix.write( 0x01, 0x80 )

This is a simple way of turning on several leds on the badge. Repeat same thing several times with different combination and an animation can be represented with leds.

For the very curious hackers, communication to the led matrix is via SPI and the STM32F0 supports DMA. So a clever hacker can use DMA to send data to the led matrix and free up some clock ticks from the CPU. As of this post, the example on github is not using DMA just simple serial communication.


Sunday, February 21, 2016

BSidesPR 2016 Badge programming tools


Now lets talk about how to code for the BSidesPR 2016 badge. If you do not know what this is all about refer to last post about an overview of the board here.

If you do not want to read, what you need?
  1. Keil (for windows)
    1. ARM Compiler, compatible with a lot of ARM MCUs
    2. Supports debug (to debug requires an external programmer debugger ST-Link)
  2. STM32CubeMX
    1. Code generator, helps creating Keil project and configuring MCU pins
    2. Compatible with Windows and Linux
  3. System Workbench for STM32 ( Linux)
    1. ARM Compiler compatible with Linux
    2. Free but need to register
    3. I downloaded but have not tested
  4. stm32flash to download binaries to board in linux
    1. clone and build tool from https://github.com/aguegu/stm32flash
    2. Use with -s to download
    3. ./stm32flash -s /dev/ttyUSB0 (should recognize device)

The badge has a STM3230F4P6 micro controller. It has a Arm Cortex M0 cpu, so standard coding tools used for arm chips should work with this board. I am far from an expert here and I was able to create some simple codes and download to it.
This chip is cross platform, but by the time I write this I haven't played with it in other than windows so that is what this post is all about.

I have used Keil to program for this badge from Windows it can be freely downloaded. For what I know Keil runs only on windows. It is basically an IDE like visual studio to code for Arm micro controllers. There are other compilers that should be compatible with the micro controller but as of this post I have not tested them.

You can do all with Keil alone, however I like to get a little help setting up the project so I use STM32CubeMX. The STM32CubeMX is a code generator, it help you get started on creating a project and configuring different ports of the STM32 chip as input, output or any other feature available. It is a wizard like interaction and it can produce a project file that you can open with Keil and compile.

Badge hardware circuit diagram (Please note that the prototypes use 5VDC regulators not 3.3V)

To use Serial data communication from code, it is required to configure the corresponding pins to be TX and RX. Also to use the LED matrix corresponding pins should be configured as well. Here is a simple configuration on how to just make it work, configured via STM32CubeMX.


LED matrix can be used bu configuring PA7, PA5 as SPI and should provide a better performance that just using them as standard outputs, however this way also works.





Saturday, February 20, 2016

BSidesPR 2016 Badge prototype

Last year (2015) BSides PR badge was cool but time pass and we need to keep on improving. This year badge is little different. As of when this post is published we only have a prototype, final version can and will change a bit. However key aspects of the board will stay the same. Design files for the board can be found at github.

Lets divide the badge in three areas. The brain, the communication and peripherals.

The Brain:
This badge carries some cool stuff. We went with a STM32F030F4P6 micro controller. This is an ARM® 32 bit Cortext M0 micro controller made by ST and consists of 16Kb of flash memory and it is clocked at 48Mhz. Information and manual can be found at manufacturer web site. It has 1 UART (in use to communicate with pc), 1 I2C and 1 SPI port. SPI port is used for shift registers.



Communication:
The badge can comunicate with the pc via USB. For this it uses a CH340G usb to serial chip. I have been using this chip for so long that I do not remember if linux needs any special driver. But for windows it indeed needs a driver. The chip works with Linux, Windows and MAC after is corresponding driver installation.
For drivers

    • Chinese official web site link
    • Some guy blogpost (has something for MAC, but I have never tried it)
    • Or just go to google and search for CH340G driver


Peripherals:
This baby is packing a few things.

  1. LED matrix
    • 8x8 LED matrix that is controlled from two 595 shift registers
    • One shift register is used for the rows and one for the columns
  2. Buttons
    • In the front it consists of four push buttons
    • The prototype do no have any of these buttons wired up to the microcontroller. Instead pins for the buttons are exposed at JP4 port. Pins are labeled Left, Right, Up and Down.
    • On the back there are two more buttons. One for programming and the other for reset.
  3. I/O exposed pins
    • To the left of the microcontroller a few pins are exposed. Please note the one that says 3.3V it is actually 5V. There pins PA0, PA1, PA2, PA3, PA6 and PB1 are available to be used. Easily can jump wire one of those pins to the buttons to interact with them.
    • On the right (beneath PROTOTYPE) is a ST-LINK port. It can be used for debugging with an external programmer. This is not required for normal programming but if you'll like to see a break point in your code this is the way to go and an external programmer is required. An external programmer can be a cheap ST-Link V2 programmer/debugger and are cheap on ebay.