Skip to content

Instantly share code, notes, and snippets.

@redaphid
Created August 5, 2015 07:02
Show Gist options
  • Save redaphid/698c14633502625714e4 to your computer and use it in GitHub Desktop.
Save redaphid/698c14633502625714e4 to your computer and use it in GitHub Desktop.
Arduino Yun Meshblu REST + Tentacle example
#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
static const char uuid[] = "b04c9213-79db-44a4-a3bf-3838e9df3274";
static const char token[] = "4cc7e83ec5003e657e6234a5b1cb78ad5bb91035";
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();
}
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 tentacleServer once it reads RFID tag
postData();
}
}
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;
String payload;
void postData()
{
while(!meshbluConn.connect(meshbluServer, port)) {
delay(200);
}
//using hardcoded values for time being
payload = "{\"tsDiff\": 5, \"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:
meshbluConn.println("POST /messages HTTP/1.1");
meshbluConn.println("Host: meshblu.octoblu.com");
// meshbluConn.print(tentacleServer);
meshbluConn.println("User-Agent: chrome");
meshbluConn.print("skynet_auth_uuid: ");
meshbluConn.println(uuid);
meshbluConn.print("skynet_auth_token: ");
meshbluConn.println(token);
meshbluConn.println("Content-Type: application/json");
meshbluConn.println("Connection: close");
meshbluConn.print("Content-Length: ");
meshbluConn.println(PostData.length());
meshbluConn.println();
meshbluConn.println(PostData);
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("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