venerdì 9 settembre 2016

Raspberry Pi - NFC door opener


As shown in the previous tutorial, using the MFRC522-python library (https://github.com/mxgxw/MFRC522-python.git) and the data read example script, I'm able to read the UniqueID (UID) of my NFC card.

On GPIO17 (= pin 11) I have the 220v relay signal that open the electric door lock. Therefore, editing the read example script, if an allowed UID is read than the door will be opened!

I add also a log file to control if somebody try to force the system
import RPi.GPIO as GPIO
import MFRC522
import signal
import time
import logging

# Logging
logging.basicConfig(filename='/var/www/nfc.log',
                            filemode='a',
                            format='%(asctime)s %(levelname)s %(message)s',
                            datefmt='%d/%m/%Y %H:%M:%S',
                            level=logging.DEBUG)
logging.info("script initialized")

# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()

# 220v relay - electric door opener
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(11, GPIO.OUT) ## Setup GPIO17 -> pin 11 to OUT
GPIO.output(11,1)

# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while true:
    
    # Scan for cards    
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

    # If a card is found
    if status == MIFAREReader.MI_OK:
    
          # Get the UID of the card
          (status,uid) = MIFAREReader.MFRC522_Anticoll()

          # If we have the UID, continue
          if status == MIFAREReader.MI_OK:

          # My card UID
myUID = [141,113,243,56, 231]

# Check if authenticated
          if uid == myUID:
                  # open the door
                 GPIO.output(11,0)
                 time.sleep(1)
                 GPIO.output(11,1)
                  # save to log file
                  logging.info('Authenticated uid = %s' % uid)
else:
                 logging.error('Authentication failed uid = %s' % uid)

RFID-RC522 on Raspberry PI (Python)

Enable SPI interface

Edit /boot/config.txt and add the following lines
device_tree_param=spi=on
dtoverlay=spi-bcm2708
Reboot  the device.

Install Python header files

sudo apt-get install python-dev

Install SPI libraries

git clone https://github.com/lthiery/SPI-Py.git
cd SPI-Py
sudo python setup.py install

Test RFID-RC522

Use the following schema to link the RFID-RC522 to the GPIO
Name Pin # Pin name
SDA 24 GPIO8
SCK 23 GPIO11
MOSI 19 GPIO10
MISO 21 GPIO9
IRQ None None
GND Any Any Ground
RST 22 GPIO25
3.3V 1 3V3

Download the RFID-RC522 interface and test it
git clone https://github.com/mxgxw/MFRC522-python.git
cd MFRC522-python
sudo python Read.py
Touching the reader with a card, the output must be
Welcome to the MFRC522 data read example
Press Ctrl-C to stop.
Card detected
Card read UID: 141,113,243,56
Size: 8
Sector 8 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

mercoledì 31 agosto 2016

Install Tomcat8 on Raspberry Pi

Install tomcat from apt-get
sudo apt-get install tomcat8
Check if the installation is successful by accessing the raspberry from a web browser and port 8080 (in my case http://192.168.1.200:8080/)

The following page is returned

It works !

If you're seeing this page via a web browser, it means you've setup Tomcat successfully. Congratulations!
This is the default Tomcat home page. It can be found on the local filesystem at: /var/lib/tomcat8/webapps/ROOT/index.html
Tomcat8 veterans might be pleased to learn that this system instance of Tomcat is installed with CATALINA_HOME in /usr/share/tomcat8 and CATALINA_BASE in /var/lib/tomcat8, following the rules from /usr/share/doc/tomcat8-common/RUNNING.txt.gz.
You might consider installing the following packages, if you haven't already done so:
tomcat8-docs: This package installs a web application that allows to browse the Tomcat 8 documentation locally. Once installed, you can access it by clicking here.
tomcat8-examples: This package installs a web application that allows to access the Tomcat 8 Servlet and JSP examples. Once installed, you can access it by clicking here.
tomcat8-admin: This package installs two web applications that can help managing this Tomcat instance. Once installed, you can access the manager webapp and the host-manager webapp.
NOTE: For security reasons, using the manager webapp is restricted to users with role "manager-gui". The host-manager webapp is restricted to users with role "admin-gui". Users are defined in /etc/tomcat8/tomcat-users.xml.

giovedì 31 luglio 2014

RaspberryPi Webcam Server

Plugin a USB Webcam

Install the webcam server
sudo apt-get install motion
Edit the configuration file /etc/motion/motion.conf
daemon on
output_normal off
ffmpeg_cap_new off
webcam_localhost off
Edit /etc/default/motion to enable daemon service
start_motion_daemon = yes
Start motion
sudo service motion start 
Now you can open the streaming at raspberry address on 8081 port (http://xx.xx.xx.xx:8081)
ATTENTION! Try different browsers since it doesn't work on my laptop's Chrome

HTML EMBEDDED

You can embed the streaming on your custom page adding the following line
<img src="http://xx.xx.xx.xx:8081" />
where xx.xx.xx.xx is the raspi address. (127.0.0.1 doesn't work - I don't know why!)

RaspberryPi and Arduino - PHP Serial Communication

ARDUINO SIDE

Connect Arduino to Raspberry through USB.

Load on Arduino this simple sketch that sends back everything on serial port.
byte input;
void setup() {
        Serial.begin(9600);
}
void loop() {
        if (Serial.available()) {
                input = Serial.read();
                Serial.write((char)input);
        }
}
Test the Arduino connection using screen. Probably Arduino interface is /dev/ttyACM0
sudo screen /dev/ttyACM0 9600

RASPBERRY SIDE

To use serial port within PHP I suggest this library PhpSerial.php

Load on the WebServer this php page
<?php
  include 'include/PhpSerial.php';
  $serial = new phpSerial;
  $serial->deviceSet("/dev/ttyACM0");
  $serial->confBaudRate(9600);
  $serial->confParity("none");
  $serial->confCharacterLength(8);
  $serial->confStopBits(1);
  $serial->confFlowControl("none");
  $serial->deviceOpen();
  $serial->sendMessage("Hello World!");
  $read = $serial->readPort();
  echo $read;
  $serial->deviceClose();
?>
That's all!

mercoledì 30 luglio 2014

PHP and MySQL on RaspberryPi

PHP

Install PHP and apache library.
sudo apt-get install php5 libapache2-mod-php5

MySQL 

Install MySQL server, client and php library.
sudo apt-get install mysql-server mysql-client php5-mysql
It will ask you a root password during the installation wizard. Remember it!

MySQL admin page

To easy manage the DB download "phpMyAdmin" from its site and extract it in /var/www/admin folder.

Restart the raspi.

Now you can control the DB from http://your.address/admin with MySQL root user and password. 


giovedì 15 maggio 2014

Control Raspberry Pi GPIO from web

WiringPi

The best way to control the GPIO interface is to use WiringPi library. See https://projects.drogon.net/raspberry-pi/wiringpi/ for installation instructions and some basic examples.

In this case I connect a LED to the GPIO 17 (pin 0 for WiringPi)
So the command to turn on the led is
gpio mode 0 out
gpio write 0 1

CGI script

Default Apache script directory  is /usr/lib/cgi-bin. Save here the scripts

ledOn.cgi
#!/bin/bash

#ledOn.cgi
#Turn on led on GPIO 17

gpio mode 0 out
gpio write 0 1
echo -e "Content-type: text/html\n\n"
echo "<h1>LED ON</h1>"


ledOff.cgi
#!/bin/bash
#ledOff.cgi
#Turn off led on GPIO 17
gpio mode 0 out
gpio write 0 0
echo -e "Content-type: text/html\n\n"
echo "<h1>LED OFF</h1>"

The echo commands are used to print HTML text on the browser.

IMPORTANT! Don't forget to make files executable:
sudo chmod +x /usr/lib/cgi-bin/ledOn.cgi
sudo chmod +x /usr/lib/cgi-bin/ledOff.cgi

Web Interface

This is the web page, with a javascript that run CGI scripts
<html>
<head>
<script>
function ledOn()
{
document.location="cgi-bin/ledOn.cgi";}
function ledOff()
{
document.location="cgi-bin/ledOff.cgi";}
</script>
</head>
<body>
<h1>GREEN LED</h1>
<button onclick="ledOn()">ON</button>
<button onclick="ledOff()">OFF</button>
</body></html>

The Result