Go Down

Topic: Arduino Due and USBhost Passcode (Read 2074 times) previous topic - next topic

andi968

I have successfully installed the USBhost library and uploaded the sketch below to my Due.
Which enables me to connect a USB numeric keypad to the native port on the Due, and read the OEM value of the keystrokes in serial monitor.
What I am hoping to do next is utilise a four digit passcode for locking and unlocking a door, none of the example sketches for passwords appear to support USBhost instead only supporting keypad library.
Can someone please point me in the right direction to a foundation sketch I can build upon.
Thanks in advance.
Code: [Select]
#include <KeyboardController.h>
 
USBHost usb;
 
// Attach Keyboard controller to USB
KeyboardController keyboard(usb);
 
void setup() {
  Serial.begin(9600);
}
 
void keyPressed() {
  Serial.print("OEM key: ");
  Serial.println(keyboard.getOemKey());
}
 
void loop() {
  usb.Task();
}

xl97

what you need to do is set up a variable to 'collect' the keypad presses...  (as well as limiter or event, signifying that you then need to compare the entered 'password' to correctly stored/saved 'password' to see if they match.)

right now... you are doing nothing.

* outputting the 'key press' to the serial monitor, which does nothing for you.





andi968

#2
Mar 27, 2017, 08:42 pm Last Edit: Mar 27, 2017, 08:53 pm by andi968
Adding to the basic, I have now produced the following.
The only problem is with the USBHost library, one key press enters all four,if i press "1" it produces "1111"

Code: [Select]
#include <KeyboardController.h>
#include <Password.h>
 
 
USBHost usb;
Password password = Password( "1111" );
// Attach Keyboard controller to USB
KeyboardController keyboard(usb);
 
void setup() {
  Serial.begin(9600);
}
 
void keyPressed() {
  Serial.print(keyboard.getKey());
  password.append(keyboard.getKey());
Serial.flush();
  Serial.print(keyboard.getKey());
  password.append(keyboard.getKey());
Serial.flush();
  Serial.print(keyboard.getKey());
  password.append(keyboard.getKey());
  Serial.flush();
  Serial.println(keyboard.getKey());
  password.append(keyboard.getKey());
  Serial.println( password.evaluate() ? "true" : "false" );
  password.reset();
}
 
void loop() {
  usb.Task();
 
}

ard_newbie

#3
Mar 28, 2017, 06:47 am Last Edit: Mar 28, 2017, 06:27 pm by ard_newbie
You could change your code like the one below (not tested though):

Code: [Select]

#include <KeyboardController.h>
String MyPassword = "1234\n";
String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

USBHost usb;

// Attach Keyboard to native USB socket
KeyboardController keyboard(usb);

void setup() {
  Serial.begin(250000);
  Serial.println(" Enter Password  ");
  
}

void keyPressed() {
  char inChar = keyboard.getOemKey();
  //Serial.print("OEM key: ");
  //Serial.println(keyboard.getOemKey());
  inputString += inChar;
  if (inChar == '\n') {  // If carriage return
    stringComplete = true;
  }
}

void loop() {
  usb.Task();
  
  if (stringComplete) {
    Serial.print(" You entered this Password: ");
    Serial.println(inputString);
    if (inputString == MyPassword) {
      // clear the string:
      inputString = "";

      Serial.println(" Password OK ---> Unlock the door ");
      // TODO : Set a PIO to open the door
    }
    else {
      Serial.println(" Password not OK ---> door remains closed ");
      inputString = "";
      
    }
    stringComplete = false;
    Serial.println(" Enter Password  ");
  }

}



For Keypad Library :

http://playground.arduino.cc/Code/Keypad#Download


andi968

Thank you for your help, this is exactly what I am wanting to do.
However as you can see for some reason, even whilst the password == password the sketch says it is wrong.
Can anyone see from the code what the problem is, I cant see the wood for the trees. :-)

ard_newbie


The correct MyPassword should obviously be "1234\n"  to take care of the carriage return !!

andi968

Thank you for your help, the enter key reports back "19" however when I enter this in the sketch it turns from blue text to black and after it uploads does not work.

andi968

A big thank you to ard_newbie.
A quick amendment of the following lines and everything now works as desired.

Code: [Select]
String MyPassword = "1234";
and
Code: [Select]
if (inChar == '\') {  // If carriage return
    stringComplete = true;
  }


The sketch now works fine with a normal USB keyboard, unfortunately the library doesn't want to work with a numeric USB key pad.

Does anyone know of an alernative library that includes numeric USB key pads?

andi968

#8
Mar 28, 2017, 10:40 pm Last Edit: Mar 28, 2017, 10:51 pm by andi968
I have never attempted messing with a library before, but think that because the numeric usb keypad outputs a enum KeyboardModifiers that is not mapped as below, I may be correct in thinking merging them into the library would solve my problem.
I have tried to simply merge the two together without success, how would I add these to the mapping ?

Actual Key pressed          GetOemKey response
1                                         = 89,
2                                         = 90,
3                                         = 91,
4                                         = 92,
5                                         = 93,
6                                         = 94,
7                                         = 95,
8                                         = 96,
9                                         = 97,
0                                         = 98

Code: [Select]
/*
 Copyright (c) 2012 Arduino LLC.  All right reserved.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 See the GNU Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef KEYBOARD_CONTROLLER_H
#define KEYBOARD_CONTROLLER_H

#include <hidboot.h>

enum KeyboardModifiers {
  LeftCtrl = 1,
  LeftShift = 2,
  Alt = 4,
  LeftCmd = 8,
  RightCtrl = 16,
  RightShift = 32,
  AltGr = 64,
  RightCmd = 128
 
};

class KeyboardController : public KeyboardReportParser {
public:
  KeyboardController(USBHost &usb) : hostKeyboard(&usb), key(0), keyOem(0), modifiers(0) {
    hostKeyboard.SetReportParser(0, this);
  };

  uint8_t getKey()       { return key; };
  uint8_t getModifiers() { return modifiers; };
  uint8_t getOemKey()    { return keyOem; };

protected:
  virtual void OnKeyDown(uint8_t mod, uint8_t key);
  virtual void OnKeyUp(uint8_t mod, uint8_t key);

private:
  HIDBoot<HID_PROTOCOL_KEYBOARD> hostKeyboard;
  uint8_t key, keyOem, modifiers;
};

#endif

Go Up