thethings.iO
Published © GPL3+

Knock Lock with the thethings.iO and Mindstorms NXT

We show you how to make a Knock Lock using a Mindstorms NXT and to monitor it using the Internet of Things platform thethings.iO

IntermediateFull instructions provided2 hours770
Knock Lock with the thethings.iO and Mindstorms NXT

Things used in this project

Hardware components

Mindstorms NXT Programming Brick / Kit
LEGO Mindstorms NXT Programming Brick / Kit
×1
Mindstorms NXT Motor
×1
Mindstorms NXT sound sensor
×1
Mindstorms NXT cable
×2
USB-A to B Cable
USB-A to B Cable
×1

Software apps and online services

thethings.iO
MATLAB
MATLAB

Story

Read more

Schematics

Connections for the Knock Lock

Here you can see how the sound sensor and the motor are connected to the brick. The USB cable connects the brick with the computer.

Code

Code for the Knock Lock using thethings.iO and Mindstorms NXT

MATLAB
Code for the Knock Lock. It connects to the Mindstorms NXT and waits for input at the sound sensor. If a correct knock sequence is detected it will open the door and register the date time and name of the door use with the platform thethings.iO. It also registers false attempts.
function door()
    
%% CODIGO
      
    COM_CloseNXT all;           % Close all NXT connection 
    h=COM_OpenNXT();            % Open NXT connection
    COM_SetDefaultNXT(h);       % Default value
    OpenSound(SENSOR_2, 'DB');  % Open Sound Sensor
   
    while(1)                                % Working loop
        disp('Ready!')
        NXT_PlayTone(800,500,h);            % Play tone to signal ready 
        while GetSound(SENSOR_2) < 1000     % Wait for knock
        end
        secuence = [];                      % Knock secuence array
        secuence = get_secuence();          % Get knock secuence
        secuence                            % Show knock secuence
        if numel(secuence) == 4                 % Only if 4 intervals
            result =check_secuence(secuence);   % secuence can be valid
                                                % and will be checked
        else
            NXT_PlayTone(2000,500,h);           % else it is rejected
            result = 'Wrong Pass!'
        end
        result                                  % Show result
        write_data(result);                     % Write result to thethings.IO
        if not(strcmp(result,'Wrong Pass!'))    % If secuence is valid
            open_door();                     % door is opened
        else 
            NXT_PlayTone(2000,500,h);
        end
        pause(2);                               % Wait 2 sec
    end
 
    %% FUNCTIONS
    
    % Function to read data from thethings.iO (not used)
    function read_data()
        url_read = 'https://api.thethings.io/v2/things/wJqZQR2aIiCb3iluClnkRs1N2hv_lk9_QknvQpnkR5U/resources/user?limit=10';
        webread(url_read)
    end

    % Function to write data to thethings.IO
    function write_data(result)
        options = weboptions('MediaType', 'application/json','ContentType','json');
        url_write = 'https://api.thethings.io/v2/things/wJqZQR2aIiCb3iluClnkRs1N2hv_lk9_QknvQpnkR5U';
        data1 = '{ "values": [ { "key": "user", "value": "';
        data2 = '" } ] }'; 
        data = strcat(data1,result,data2);
        webwrite(url_write,data,options);
    end

    % Function to register and return knock secuence intervals
    function secuence = get_secuence()
        secuence_aux = [];
        valid = 1;
        for i=1:4
            tstart = tic;
            pause(0.15);
            GetSound(SENSOR_2);
            while GetSound(SENSOR_2) < 1000 && toc(tstart) <= 3
            end
            if toc(tstart) < 3
                tstop = toc(tstart);
                secuence_aux = [secuence_aux, tstop];
            else 
                valid = 0
            end
            if valid == 0
                break
            end
            pause(0.15)
        end
        secuence = secuence_aux;
    end

    % Function that compares knock secuences to the validated ones and
    % returns result
    function result = check_secuence(secuence)
        if secuence(1) > 0.5 && secuence(2) > 0.5 && secuence(3) > 0.5 && secuence(4) > 0.5
            result = 'Peter';   % Knock secuence for Peter
        elseif secuence(1) < 0.5 && secuence(2) < 0.5 && secuence(3) < 0.5 && secuence(4) < 0.5
            result = 'Mary';    % Knock secuence for Mary
        elseif secuence(1) > 0.5 && secuence(2) < 0.5 && secuence(3) > 0.5 && secuence(4) < 0.5
            result = 'John';    % Knock secuence for John
        else
            result = 'Wrong Pass!'; % Not an accepted secuence
        end
    end

    % Function to open the door
    function open_door()
        NXT_PlayTone(500,500,h);
        pause(0.5);
        NXT_PlayTone(800,750,h);
        pause(0.75);
        NXT_PlayTone(1000,500,h);
        mA= NXTMotor('A');
        mA.Power=20;
        mA.TachoLimit= 90;
        mA.SendToNXT();
        pause(2);
        mA.Power=-20;
        mA.TachoLimit= 90;
        mA.SendToNXT();
    end        
end

Credits

thethings.iO

thethings.iO

6 projects • 18 followers
thethings.iO is the most simple enterprise IoT platform. We help businesses (and individuals) to connect your devices to the Internet.

Comments