Created
August 11, 2015 18:47
-
-
Save sqrtofsaturn/0dd889efa7bbb7fe3bf4 to your computer and use it in GitHub Desktop.
Working 2 connection yun
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 tentacleServer "tentacle.octoblu.com" | |
#define meshbluServer "meshblu.octoblu.com" | |
#define port 80 | |
#define uuid "d82c5f58-fc66-4ea6-a896-9302e5604570" | |
#define token "3ce16d718b5a0c2988a50671e6d89be5a6b43fdc" | |
YunClient tentacleConn; | |
YunClient meshbluConn; | |
TentacleArduino tentacle; | |
Pseudopod pseudopod(tentacleConn, tentacleConn, 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 (!tentacleConn.connected()) { | |
tentacleConn.stop(); | |
connectToServer(); | |
} | |
readData(); | |
if (pseudopod.shouldBroadcastPins() ) { | |
delay(pseudopod.getBroadcastInterval()); | |
Serial.println(F("Sending pins")); | |
pseudopod.sendConfiguredPins(); | |
} | |
postData(); | |
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(F("Tag Length : ")); | |
Serial.print(count); | |
Serial.println(F(" Bytes")); | |
//post RFID data to tentacleServer once it reads RFID tag | |
} | |
} | |
void readData() { | |
while (tentacleConn.available()) { | |
Serial.println(F("Received message")); | |
Serial.flush(); | |
if (pseudopod.readMessage() == TentacleMessageTopic_action) { | |
pseudopod.sendPins(); | |
} | |
} | |
} | |
void connectToServer() { | |
Serial.println(F("Connecting to the tentacleServer.")); | |
Serial.flush(); | |
while (!tentacleConn.connect(tentacleServer, port)) { | |
Serial.println(F("Can't connect to the tentacleServer.")); | |
Serial.flush(); | |
tentacleConn.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; | |
void postData() | |
{ | |
while(!meshbluConn.connect(meshbluServer, port)) { | |
delay(200); | |
} | |
//******************************************************* | |
unsigned long t1 = millis(); | |
meshbluConn.println(F("POST /messages HTTP/1.1")); | |
meshbluConn.println(F("Host: meshblu.octoblu.com")); | |
// meshbluConn.print(tentacleServer); | |
meshbluConn.print(F("skynet_auth_uuid: ")); | |
meshbluConn.println(uuid); | |
meshbluConn.print(F("skynet_auth_token: ")); | |
meshbluConn.println(token); | |
meshbluConn.println(F("Content-Type: application/json")); | |
meshbluConn.println(F("Connection: close")); | |
meshbluConn.print(F("Content-Length: ")); | |
meshbluConn.println(48); | |
meshbluConn.println(); | |
meshbluConn.println(F("{\"devices\": \"*\", \"payload\": { \"x\": 5, \"y\": 7}}")); | |
meshbluConn.println(); | |
// this is required delay, to allow server response | |
delay(300); | |
// disconnect from server | |
meshbluConn.stop(); | |
// add delay to prevent connect again too fast | |
//delay(300); | |
unsigned long t2 = millis(); | |
time = t2 - t1; | |
Serial.print(F("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