Created
August 25, 2015 07:43
-
-
Save KarthiPnsmy/7dd741f2e0b7f75704aa to your computer and use it in GitHub Desktop.
RFID Reader + Octoblu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Bridge.h> | |
#include <YunClient.h> | |
#include <SoftwareSerial.h> | |
//static const char uuid[] = "ef462118-69f4-4ed9-ba12-20ce4f8ee8f9"; | |
//static const char token[] = "6631d217cfc81157752dfc151820bb04debaab20"; | |
static const char uuid[] = "b04c9213-79db-44a4-a3bf-3838e9df3274"; | |
static const char token[] = "4cc7e83ec5003e657e6234a5b1cb78ad5bb91035"; | |
SoftwareSerial rfid_reader(10, 11); // RX, TX | |
int count = 0; // A variable to count the length of the Tag DATA | |
char input[11] = ""; | |
#define mesh_server "meshblu.octoblu.com" | |
#define tentacle_server "tentacle.octoblu.com" | |
#define port 80 | |
int send = 0; | |
YunClient conn; | |
int val; | |
String val2; | |
String payload; | |
char serverurl[] = "meshblu.octoblu.com"; | |
long lastRequest = 0; | |
long now = 0; | |
String tag; | |
void setup() { | |
rfid_reader.begin(9600); | |
Serial.begin(9600); | |
Bridge.begin(); | |
Serial.println("setup."); | |
connectToMeshServer(); | |
} | |
void loop() { | |
connectToMeshServer(); | |
if (rfid_reader.available()) // Check if there is Incoming Data in the RFID Reader Serial Buffer. | |
{ | |
count = 0; // Reset count to zero | |
while (rfid_reader.available()) // Keep reading Byte by Byte from the Buffer till the RFID Reader Buffer is empty | |
{ | |
input[count] = rfid_reader.read(); | |
Serial.print(input[count]); // Print the Byte | |
Serial.print("\t"); | |
String tag(input); | |
Serial.print(tag); | |
Serial.print("\n"); | |
count++; // Increment the Byte count after every Byte Read | |
delay(5); // A small delay - Removing this might make the Program run faster and not respond properly as data from the reader could be slow | |
val2 = tag; | |
send += 1; | |
} | |
// Print Tag Length | |
Serial.println(); | |
Serial.print("Tag Length : "); | |
Serial.print(count); | |
val = count; | |
Serial.println(" Bytes"); | |
postData(); | |
/* | |
if (count == 57) | |
{ | |
//disconnect from tentacle | |
conn.stop(); | |
connectToMeshServer(); | |
postData(); | |
count = 0; | |
} | |
*/ | |
} | |
delay(3000); | |
} | |
void connectToMeshServer() { | |
//Serial.println(F("Connecting to the server.")); | |
Serial.flush(); | |
while (!conn.connect(mesh_server, port)) { | |
Serial.println(F("Can't connect to the server.")); | |
Serial.flush(); | |
conn.stop(); | |
} | |
} | |
void postData() | |
{ | |
// val = 111; | |
// val2 = "hungama"; | |
now = millis(); | |
long tsDiff = 0; | |
if (lastRequest > 0) { | |
tsDiff = now - lastRequest; | |
} | |
Serial.println("now"); | |
Serial.println(now); | |
//Serial.println("lastRequest"); | |
//Serial.println(lastRequest); | |
//Serial.println("tsDiff"); | |
//Serial.println(tsDiff); | |
payload = "{\"tsDiff\": " + String(tsDiff) + ", \"rfidTag\": " + "\"" + val2 + "\"" + "}"; | |
//UUID you want to send this message to. * to broadcast to all listening | |
String target = "*"; | |
//******************************************************* | |
// construct a HTTP PUT request | |
String PostData = "{\"devices\": \"" + target + "\", \"payload\" : " + payload + "}"; | |
Serial.println(PostData); | |
// if there's a successful connection: | |
// send the HTTP PUT request: | |
conn.println("POST /messages HTTP/1.1"); | |
conn.println("Host: meshblu.octoblu.com"); | |
// conn.print(server); | |
conn.println("User-Agent: chrome"); | |
conn.print("skynet_auth_uuid: "); | |
conn.println(uuid); | |
conn.print("skynet_auth_token: "); | |
conn.println(token); | |
conn.println("Content-Type: application/json"); | |
conn.println("Connection: close"); | |
conn.print("Content-Length: "); | |
conn.println(PostData.length()); | |
conn.println(); | |
conn.println(PostData); | |
conn.println(); | |
// this is required delay, to allow server response | |
delay(300); | |
// disconnect from server | |
// client.close(); | |
// add delay to prevent connect again too fast | |
//delay(300); | |
lastRequest = now; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment