Created
October 19, 2016 17:08
-
-
Save scottjenson/31e8f82cc163d34ffac176023bf1f18d to your computer and use it in GitHub Desktop.
RFDuino arduino sketch to reply to a web bluetooth request
This file contains 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
/* | |
CandyMachine_rfduino.ino | |
Arduino sketch that turns the vending machine into an Eddystone-URL beacon | |
and also interfaces with the attached relay shield in order to dispense candy | |
*/ | |
#include <SPI.h> | |
#include <EddystoneBeacon.h> // from BLEPeripheral by Sandeep Mistry | |
int DISPENSE_ITEM_RELAY_PIN = 1; | |
int EDDYSTONE_BEACON_REQ = 6; | |
int EDDYSTONE_BEACON_RDY = 7; | |
int EDDYSTONE_BEACON_RST = 4; | |
int DISPENSE_ITEM_DURATION = 800; | |
// Create the eddystone beacon | |
EddystoneBeacon eddystoneBeacon = EddystoneBeacon(EDDYSTONE_BEACON_REQ, EDDYSTONE_BEACON_RDY, EDDYSTONE_BEACON_RST); | |
// Set up the gatt service dispense item characteristic | |
BLECharCharacteristic dispenseItemCharacteristic = BLECharCharacteristic("0000feaa00002000800000805f9b34fb", BLERead | BLEWrite); | |
void setup() { | |
initRelay(); | |
initBeacon(); | |
} | |
// Initialize the relay that will dispense candy | |
void initRelay() { | |
pinMode(DISPENSE_ITEM_RELAY_PIN, OUTPUT); | |
digitalWrite(DISPENSE_ITEM_RELAY_PIN, HIGH); | |
} | |
// Initialize the Eddystone beacon and start broadcasting | |
void initBeacon() { | |
//Serial.begin(115200); // only init when | |
eddystoneBeacon.setConnectable(true); | |
eddystoneBeacon.setLocalName("CANDY BOT 9000"); | |
eddystoneBeacon.addAttribute(dispenseItemCharacteristic); | |
eddystoneBeacon.setEventHandler(BLEConnected, onBleConnected); | |
eddystoneBeacon.setEventHandler(BLEDisconnected, onBleDisconnected); | |
dispenseItemCharacteristic.setEventHandler(BLEWritten, onBleWritten); | |
eddystoneBeacon.begin(-18, "http://tiny.cc/cb9000"); | |
} | |
// Fires when a connection is made to the gatt server | |
void onBleConnected(BLECentral& central) { | |
Serial.print(F("Connected event, central: ")); | |
Serial.println(central.address()); | |
} | |
// Fires when the gatt server connection is broken | |
void onBleDisconnected(BLECentral& central) { | |
Serial.print(F("Disconnected event, central: ")); | |
Serial.println(central.address()); | |
} | |
// Fires when the gatt server dispense item characteristic has been successfully written to | |
void onBleWritten(BLECentral& central, BLECharacteristic& characteristic) { | |
Serial.print(F("Characteristic event, writen: ")); | |
if (dispenseItemCharacteristic.value()) { | |
digitalWrite(DISPENSE_ITEM_RELAY_PIN, LOW); | |
delay(DISPENSE_ITEM_DURATION); | |
Serial.print(F("stop item dispense")); | |
digitalWrite(DISPENSE_ITEM_RELAY_PIN, HIGH); | |
} | |
} | |
// Run the main loop | |
void loop() { | |
eddystoneBeacon.loop(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment