martedì 10 settembre 2013

Accoppiamento WiFly con lettore Rfid



Per lo shield WiFly e' stata effettuata la patch descritta qui


Per la connessione del lettore Rfid si usano i seguenti pin

VCC : 3.3V
GND :
MOSI: Pin 11 / ICSP-4
MISO: Pin 12 / ICSP-1
SCK: Pin 13 / ISCP-3
SS: Pin 10
RST: Pin 9

da notare che vista la presenza dello shield il PIN 3.3V risulta coperto per cui e' stato necessario mettere una patch piegando il piedino in modo da poter appoggiare lo shield. In realta' si potrebbe rubare la corrente saldando un contatto direttamente sullo shield

Lo sketch inizializza il lettore Rfid e la WiFly. Successivamente nel loop attende che venga passato il tag Rfid, lo legge ed invia il codice allo script in Php sul server
---------------------------------------------------
#include <WiFlyHQ.h>
#include <SPI.h>
#include <RFID.h>
#include <SoftwareSerial.h>


//WiFi
SoftwareSerial wifiSerial(2,3);
WiFly wifly;
const char mySSID[] = "TIM_PN51T_C0D1";
const char myPassword[] = "50276337";
const char site[] = "m.msn.unifi.it";

//RFid
#define SS_PIN 10
#define RST_PIN 9
RFID rfid(SS_PIN, RST_PIN); 
int serNum0;
int serNum1;
int serNum2;
int serNum3;
int serNum4;

void terminal();

void setup()
{
    char buf[32];
    
    SPI.begin(); 
    rfid.init();

    Serial.begin(9600);
    pinMode(pushButton, INPUT);

    Serial.println("Starting");
    Serial.print("Free memory: ");
    Serial.println(wifly.getFreeMemory(),DEC);

    wifiSerial.begin(9600);
    if (!wifly.begin(&wifiSerial, &Serial)) {
        Serial.println("Failed to start wifly");
terminal();
    }

    /* Join wifi network if not already associated */
    if (!wifly.isAssociated()) {
/* Setup the WiFly to connect to a wifi network */
Serial.println("Joining network");
wifly.setSSID(mySSID);
wifly.setPassphrase(myPassword);
wifly.enableDHCP();

if (wifly.join()) {
   Serial.println("Joined wifi network");
} else {
   Serial.println("Failed to join wifi network");
   terminal();
}
    } else {
        Serial.println("Already joined network");
    }

    //terminal();

    Serial.print("MAC: ");
    Serial.println(wifly.getMAC(buf, sizeof(buf)));
    Serial.print("IP: ");
    Serial.println(wifly.getIP(buf, sizeof(buf)));
    Serial.print("Netmask: ");
    Serial.println(wifly.getNetmask(buf, sizeof(buf)));
    Serial.print("Gateway: ");
    Serial.println(wifly.getGateway(buf, sizeof(buf)));

    wifly.setDeviceID("Wifly-WebClient");
    Serial.print("DeviceID: ");
    Serial.println(wifly.getDeviceID(buf, sizeof(buf)));


}

void loop()
{
    if (wifly.available() > 0) {
char ch = wifly.read();
Serial.write(ch);
if (ch == '\n') {
   /* add a carriage return */ 
   Serial.write('\r');
}
    }

    if (Serial.available() > 0) {
wifly.write(Serial.read());
    }


 if (rfid.isCard()) {
        if (rfid.readCardSerial()) {
              if (wifly.isConnected()) {
                  Serial.println("Old connection active. Closing");
                  wifly.close();
                  }

              if (wifly.open(site, 80)) {
                  Serial.print("Connected to ");
                  Serial.println(site);
                  } else {
                  Serial.println("Failed to connect");
                }
                serNum0 = rfid.serNum[0];
                serNum1 = rfid.serNum[1];
                serNum2 = rfid.serNum[2];
                serNum3 = rfid.serNum[3];
                serNum4 = rfid.serNum[4];
                String Id = String(rfid.serNum[0],HEX)+String(rfid.serNum[1],HEX)+String(rfid.serNum[2],HEX)+String(rfid.serNum[3],HEX)+ String(rfid.serNum[4],HEX);
                Serial.println(Id);
                String params = "{}";
                String paramsLength = String(params.length());
                /* Send the request */
                
                wifly.println("POST /luca/examples/rfid_server2.php?rfid="+Id+"&msg=definitivo HTTP/1.1"); // paste your number here
                wifly.println("Host: m.msn.unifi.it:80");
                wifly.print("Content-Length: ");
                wifly.println(paramsLength);
                wifly.println("User-Agent: lifegraph/0.0.1");
                wifly.println();
                wifly.println(params);
             } 
        
    rfid.halt();
    }
}

/* Connect the WiFly serial to the serial monitor. */
void terminal()
{
    while (1) {
if (wifly.available() > 0) {
   Serial.write(wifly.read());
}


if (Serial.available() > 0) {
   wifly.write(Serial.read());
}
    }
}

-----------------------------------------------------------




Script Php

-----------------------------------------------------------
require_once '../src/facebook.php';
//Luca
if ($_GET["rfid"] == "f479abb492")
    {
    $accessToken = "CAAJkNq2LttoBAJywtmNSbVHqFPubOjYmQ2yyUG4kpibD75BHid9J7xsvif8osSOc4ewgK4aYerrLZBsZCQM5wykRTYVLDkCJXlXYl3gi8WFRQ8U4p4y4ZAXYsdamz6RVdRJZAe4vZA80mWBayuG0qc4IAj2TMDmQXKiv1OrKp8CwKjD6xxxxxxxxxxxxxxxxx";
    print "Luca<br>";
    }
//Gabriele
if ($_GET["rfid"] == "0800364CD7")
    {
    $accessToken = "CAAJkNq2LttoBACxBnb2QjtvbedXaRNwWDAniJsIE59ZBZBjDgsT1FtTmBIVxZAmS4MbbEXY8Y9kduFxfBmQhEgflGxdvXjRG1CAMHICGlHGaCXfP81VlLRySw2m8lzatt074jHsDKYlQjwZAHI725LLSMsAjjDDty3GDOrso0361xLCDzxxxxxxxxxxxxxxxxxxxxxxxxxx";
    print "Gabriele";
    }

$appid = '6731359560xxxxxx';
$appsecret = 'dde6cfb07dbb769c7efe94xxxxxxxxx';
$msg = $_GET["msg"];
$title = 'Dove sono ??';
$uri = 'http://debiaonoldcomputers.blogspot.com/';
$desc = 'Sono ad Arcetri';
$pic = 'http://www.dst.unifi.it/themes/Boozook/images/testa.gif';
$action_name = 'Link';
$action_link = 'http://www.geo.unifi.it/';

$facebook = new Facebook(array(
 'appId' => $appid,
 'secret' => $appsecret,
 'cookie' => false,
 ));

 $user = $facebook->getUser();


$attachment = array(
'access_token' => $accessToken,
'message' => $msg,
'name' => $title,
'link' => $uri,
'description' => $desc,
'picture'=>$pic,
'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
);
$status = $facebook->api("/me/feed", "post", $attachment);
print "Ok";
?> 


Nessun commento:

Posta un commento

Arduino logger low power

Alla fine ci sono riuscito La arduino MKR zero e' ormai da un mese che e' rimasta accesa sul tavolo...o meglio si attiva ogni minuto...