Reading and writing data into a BBC Micro:bit without a single line of code

BBC Micro:bit

The BBC Micro:bit is a microcontroller board that, as far as I know, was built it thinking on young people for help them to learn coding.

If you are not aware (Like me) this board will pass you away easily thinking that is limited and is only for basic stuff.

The reality is the absolute opposite: This board has so many sensors, leds, buttons and pins to use that, having the same set on an Arduino will require a full box of parts.

Of course, I am not saying this is BETTER. Just saying this is really GOOD!

Anyway, marketing aside, I have had this sitting on my desk for almost a year without even trying to understand its power. Last night suddenly something happened: I look at it, it look at me and challenged me: I challenge you to write and read data from me without writing a single line of code! – It said… ejem! Sorry… I too earlier and I have not had my coffee yet.

Nevertheless, something close to that happened. I started wondering how we could send and receive data from/to the board without using the traditional processes: Coding something in C. Reading the serial output with Python. Etc.

Well, clearly we will need to use a communication channel and the serial port is, perhaps, the simpler. In the other hand, remember, “no coding” is the premise. So, what do we have in the operating system that could help us? We will discuss this later, but first, the board.

I lied a little bit: We do need some code! But, not in the traditional way. For the micro:bit we can use blocks to create a basic sketch that opens the serial for us.

Because this is intended for young people, using this building blocks makes the code creation super easy. And the most important: trivial.

With the previous code then, we are setting the serial ready, expecting data from the serial port and using the front led matrix to write the incoming message and writing into the serial the values from the internal board compass with a refresh rate of 500ms.

So, how we read and write data? We need to know which port the board is using. For this we could go to our devices and search there OR, type some command lines. Clearly, we will type command lines.

Opening a command console and typing MODE will list the connections

On the previous image we can see the device is connected to the COM4. One thing that we need to notice are the properties. Depending how much control do we have over the code deployed into the board, we could decide the bauds speed and other properties. By default 9600 bauds will not work or, at least, the data transferred will be “broken” (Let use this name). We will need to set the this property to 115200 bauds.

Using mode COM4 baud=115200 parity=n data=8 we set the expected configurations for this board

Now we have all set and ready to write something into the board. Another command will help us on this.

echo HELLO THERE > COM4

echo” is used to print into the console if we used without any other property. On the console, the arrow head (>) means direction. For this case, echo will print the text INTO something. That something is the COM4. Technically we could print to any other connection such as printer or a file.

Now we wrote into the board. How we read from it? Easy!

type COM4

There are a couple of ways to read from the port, the easiest is using type command. This will open a connection with the board over the designated port and listen anything that is sent by the board.

So, I have not completely lied :). Without writing a line of code and only using what the OS provides we were able to read and write from a board like this one.

[Event] All about C# 8

Next May 15th I will be talking about all the new elements, syntax changes and more on C# 8. This talk will be streamed on YouTube thanks to CatchIt community.

Of course, this stream will be in Spanish, so be prepare to hear me in my native language.

If you want to participate, you need to subscribe on the following link: https://www.eventbrite.com.ar/e/matias-iacono-c-8-paso-a-paso-tickets-103566626568

See you there!

Handling a simple led light in Arduino with Python 3

This is not the most complex implementation with Arduino: Flashing a led light! Actually, is one of the “101” level ones, those that are used to start with and \makes you very happy one you complete it.

But, what happen when you want to achieve the same without using C/C++ directly deployed into the board? What happen when you are not familiar with that particular language and want to use the one that you know? Well, there are some boards that support LUA, micro Python and other languages to be “deployed” (Consider the double quotes) directly into the board but, this do not solve our problem. Clearly we do not want to go and buy a different board if we already have one in the first place.

So, if we do not want to buy a new board, and let say that we know Python, we have a solution at hand.

First lets assemble the Arduino and the led that we want to use:

As you can see (And as I said) this is a very simple project. Basically we will need the Arduino, a led light, a resistor and two wires. These wires will be connected to the Arduino’s ground pin, and the other one from any of the digital pins (In this schematic, pin 5) to the other led light leg.

Now, we need a way to communicate our language (Python in this case) with the board. For this we will need to install “Firmata” into the board. This will allow us to send and receive messages from the board using the serial port. To do this, using the Arduino IDE, search for the sample code included with the tool and run it to be installed into the board.

Having this in place the only missing piece is our Python code. Due we have Firmata into the board, we need the same communication component in our Python code. Finally, we will be able to handle the board as if we were writing code directly into it.

import pyfirmata2

DELAY = 1
PORT = pyfirmata2.Arduino.AUTODETECT

board = pyfirmata2.Arduino(PORT)

while True:
    board.digital[5].write(1)
    board.pass_time(DELAY)
    board.digital[5].write(0)
    board.pass_time(DELAY)

This code is searching for the board. Connecting to it. Sending a signal to the pin 5 for increasing the voltage (Turning on the led). Waiting 1 second. Sending a signal to decrease the voltage (Turning off the led). Waiting for another second. Repeating the process forever!

With this simple code we will see that led flashing!

NetCore Conf

The NetCore Conference has passed. And 29 sessions with more that 5000 viewers are the final results. Not bad at all!

My lecture was streamed a couple of hours ago and, because this is Internet, will be on YouTube if you missed it or if you want to see it again.

As I mentioned in a previous post, I was about to talk about performance on .Net Core with C# but in particular, showing two examples of how performance is affected when, for the first scenario, the overuse of some libraries (On special cases) could cause more harm than benefits. And for the second one, how our coding style could affect the performance forcing the compiler to add more unnecessary instructions to the IL code.

In any case, if you want to take a look to the lecture:

Event – NetCore Conf

This weekend I will be giving a lecture as part of the NetCoreConf 2020 event.

Due the current situation around the world, this event will be “virtual” meaning that all sessions will be recorded and streamed during the event. But, thanks to this, people from different parts of the world will be presenting different topics.

In my case, I will be talking about code optimization, remembering some old-but-good algorithms to improve performance and raising flags on bad overuse of some .Net libraries and how these could impact our final product, even if we do not notice that.

Of course, this event is mostly in Spanish but will be transmitted using YouTube as platform which gives some very good tools to autotranslate videos to any language.

If you want to know more about the event, click here: https://netcoreconf.com/virtual.html

Quarantine? No worries, read these books

Yes, I know. Everybody is recommending books, so, why I will not do the same, right? right…?

Well, yes, I will recommend some books but not new ones, very old ones. I am totally convinced that one of the today’s developers lack is knowing from where our practice come from. There are a lot of new developers arriving and stumbling with many new concepts but, sometimes, those concepts and ideas are not necessary new, or were around for a wile making everybody wondering how to solve it.

Let talk about agility and Scrum. For the new comers that could be the standard paradigm of project management and they will not know anything about waterfall or any other sort of software management attempt.

What happen when you just face these concepts but don’t know the reason behind it? Why we decided to change? What happened before and the necessity for that change? How you will know, if don’t know the past, that thing the facilitator (self-called guru, scrum master or coach) is valid and is not just trying to manipulate you if what they are telling to you sound fresh and original but, actually, is not?

The answer is: THE OLD BOOKS (No religion related)

So, here is my recommendation for three books and a paper that, if you haven’t read it yet, could change your mind drastically.

What is Blazor, the (not new) tech set from Microsoft

Well, yes, Blazor is not necessary new. It has some time around but it was officially released as stable version some months ago.

In any case, Blazor is, as the title of the post says, a set of new tools (Other quite old, or already existent such as SignalR), all together and simplifying the work that we should do to achieve the same in other platforms or, even, using the same tools separately.

But, what is Blazor after all? It is an approach to work with WebSocket and, in the other hand, create Web applications using WebAssembly.

The good point, as Microsoft usually get us used to, is that all this significant amount of technologies are absolutely transparent for us, the developers, and we don’t need to care too much about the instrumentation. Our focus is just functionality.

The bad part is, perhaps, that is too transparent and, compared with other proposals such as Rust, feels like we lose some control.

In any case, here is a video from my channel showing Blazor using WebAssembly and, also, SignalR (WebSockets).