Last active
August 29, 2015 14:25
-
-
Save KarthiPnsmy/f2aa259edadf571db2c7 to your computer and use it in GitHub Desktop.
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 "tentacle-build.h" | |
#include <SoftwareSerial.h> | |
#define server "tentacle.octoblu.com" | |
#define port 80 | |
static const char uuid[] = "b04c9213-79db-44a4-a3bf-3838e9df3274"; | |
static const char token[] = "4cc7e83ec5003e657e6234a5b1cb78ad5bb91035"; | |
YunClient conn; | |
TentacleArduino tentacle; | |
Pseudopod pseudopod(conn, conn, tentacle); | |
SoftwareSerial rfid_reader(10, 11); // RX, TX | |
int count = 0; | |
void setup() { | |
rfid_reader.begin(9600); // Initialise Serial Communication with the RFID reader | |
Serial.begin(9600); | |
Serial.println(F("The Day of the Tentacle has begun!")); | |
Bridge.begin(); | |
connectToServer(); | |
} | |
void loop() { | |
if (!conn.connected()) { | |
conn.stop(); | |
connectToServer(); | |
} | |
readData(); | |
if (pseudopod.shouldBroadcastPins() ) { | |
delay(pseudopod.getBroadcastInterval()); | |
Serial.println(F("Sending pins")); | |
pseudopod.sendConfiguredPins(); | |
} | |
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 | |
{ | |
char input = rfid_reader.read(); // Read 1 Byte of data and store it in a character variable | |
Serial.print(input); // Print the Byte | |
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 | |
} | |
// Print Tag Length | |
Serial.println(); | |
Serial.print("Tag Length : "); | |
Serial.print(count); | |
Serial.println(" Bytes"); | |
//post RFID data to server once it reads RFID tag | |
postData(); | |
} | |
} | |
void readData() { | |
while (conn.available()) { | |
Serial.println(F("Received message")); | |
Serial.flush(); | |
if (pseudopod.readMessage() == TentacleMessageTopic_action) { | |
pseudopod.sendPins(); | |
} | |
} | |
} | |
void connectToServer() { | |
Serial.println(F("Connecting to the server.")); | |
Serial.flush(); | |
while (!conn.connect(server, port)) { | |
Serial.println(F("Can't connect to the server.")); | |
Serial.flush(); | |
conn.stop(); | |
} | |
size_t authSize = pseudopod.authenticate(uuid, token); | |
Serial.print(authSize); | |
Serial.println(F(" bytes written for authentication")); | |
} | |
unsigned long time = 0; | |
int val; | |
int val2; | |
String payload; | |
char serverurl[] = "meshblu.octoblu.com"; | |
void postData() | |
{ | |
val = 10238; | |
val2 = "110077E937B8"; | |
//using hardcoded values for time being | |
payload = "{\"tsDiff\": " + val + ", \"rfidTag\": " + String(val2) + "}"; | |
//UUID you want to send this message to. * to broadcast to all listening | |
String target = "*"; | |
//******************************************************* | |
unsigned long t1 = millis(); | |
// construct a HTTP PUT request | |
String PostData = "{\"devices\": \"" + target + "\", \"payload\" : " + payload + "}"; | |
String PostData3 = "{\"devices\": \"b04c9213-79db-44a4-a3bf-3838e9df3274\", \"payload\": {\"pins\": [{ \"action\" : \"digitalRead\", \"number\": 10}, { \"action\" : \"digitalWrite\", \"number\": 13, \"value\": 1}] }, \"customData\" : \"You can put whatever you want here\"}"; | |
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); | |
unsigned long t2 = millis(); | |
time = t2 - t1; | |
Serial.print("Total time spend (we will send this to serverurl next time): "); | |
Serial.println(time); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment