How to safely store Arduino secrets

Posted on Wed 16 December 2020 in Development • Tagged with arduino, secrets, secret, security, password, passwords, storage, howto, safe, hide, secure

Best practices to store Arduino secrets safely


Continue reading

Using a light sensor with BBC micro:bit and MicroPython

Posted on Mon 08 February 2016 in Development • Tagged with bbc, breadboard, embedded, microbit, micropython, howto, python

A light sensor is a small component with a particular characteristic: it is basically a resistor and its resistance decreases if the light is more intense. To use it with micro:bit we need to use one of the analogic ports. To build this circuit you will need a breadboard, 3 jumper wires, a 10k resistance and possibly a Kitronik breadboard kit.

The project

I wanted to realise a simple project where, depending on the light intensity captured by the light sensor, the micro:bit shows an image of the Sun if the light is intense and an image of the Moon if the light is less intense.

Here is the complete circuit scheme:

microbit_breadboard_schema_light

"Image Copyright © Kitronik"

and here is a picture of the finished project I created:

microbit_breadboard_example_2

The source code I needed is available here:

and as a demo I realised this small video:


Using BBC MicroBit accelerometer with Python

Posted on Tue 26 January 2016 in Development • Tagged with bbc, microbit, Python, howto

In these days I'm having a bit of fun with BBC MicroBit board and I'm learning how to use the different sensors available. The latest one I wanted to try was the accelerometer. The board can "sense" if you are moving it in any of the 3 dimensional axes: X, Y, Z. According to the documentation there are four methods available that can be used to get these values: microbit.accelerometer.get_values() will return you a tuple with all the 3 values, while  microbit.accelerometer.get_x()microbit.accelerometer.get_y()microbit.accelerometer.get_z() will give you the single values.

The documentation on the official website doesn't explain much and for example I didn't even know what was the range of the values I can get back from these methods (by the way it's between -1024 and 1024), so I decided to play with the code directly and write a very simple example. The small example I wrote, shows a smile on the board display if you keep it straight and shows a sad face if you bend it.

This is the result:

and this is all the needed code of the application:

In the next days I will try to play with more sensors and to publish other examples.


How to write a custom Django Middleware

Posted on Sun 23 August 2015 in Development • Tagged with Django, HowTo, middleware, Python, tutorial

To understand how a Django Middleware works we need to remember that the basic architecture of Django is composed by a request and a response. A middleware is something that stays in the middle. Let's give a look to the next diagram, taken from official Django documentation:

middleware

Important things to know

There are four important things to know about middlewares:

  • You need to write a class that just inherit from object
  • The order where you place your middleware in settings.py is important: middlewares are processed from top to bottom during a request and from bottom to top during a response.
  • You don't need to implement all the available methods of a middleware. For example you can just implement process_request and process_template_response
  • If you implement process_request and you decide to return an HttpResponse, all the other middlewares, views etc... will be ignored and only your response will be returned

Writing a middleware

In my example I wanted to implement a feature that saves the time when a request is made and the time when a request has been processed, then calculates the time delta and exposes this value in the context so that is accessible from our templates. How to implement a similar feature using a middleware? Here is my example:

from datetime import datetime


class BenchmarkMiddleware(object):
    def process_request(self, request):
        request._request_time = datetime.now()

    def process_template_response(self, request, response):
        response_time = request._request_time - datetime.now()
        response.context_data['response_time'] = abs(response_time)
        return response

Please don't care about how I calculated the time. I'm aware that there are better ways to do it, but I just wanted to keep it simple and show how to implement a simple middleware.

If you want to see a complete example of a project that includes and uses this middleware, here you can find the complete source code: https://github.com/andreagrandi/benchmark-middleware-example

References