commit c46cef36b4575c68bb6a6019f2dd1d0e6854a998 Author: James Devine Date: Sun Mar 25 18:53:22 2018 +0200 diff --git a/Open_Auto.ino b/Open_Auto.ino new file mode 100644 index 0000000..a83a9e2 --- /dev/null +++ b/Open_Auto.ino @@ -0,0 +1,282 @@ +// This #include statement was automatically added by the Particle IDE. +#include +#include "Particle.h" + +//This is licensed under GPL V3. + + +#define RECEIVER SoftSer +#define PROTOCOL SERIAL_8N1 + +const uint32_t baud = 9600; + +//#if (SYSTEM_VERSION >= 0x00060000) +// SerialLogHandler logHandler; +//#endif + +#define PSS_RX D3 // RX must be interrupt enabled (on Photon/Electron D0/A5 are not) +#define PSS_TX C5 //this pin isn't something we use, I couldn't find a null pin +ParticleSoftSerial SoftSer(PSS_RX, PSS_TX); +char CARDcurrent[4]; + +// First, let's create our "shorthand" for the pins +// Same as in the Blink an LED example: +// led1 is D0, led2 is D7 + +int OpenRelay = D4; +int CloseRelay = D5; +int StatusLED = D7; +// Last time, we only needed to declare pins in the setup function. +// This time, we are also going to register our Particle function + +bool DoorsOpenBool = false; +//boolean to track the door status; actually we'll only use it as a toggle. + +String ReservationsTable; +String ValidGPSPosition; +String MasterRFIDCard = "983553fe"; + +class ReservationCmd { + String argument; +public: + void extractValues(String); + String UserIDStr (void) { + return argument.substring(argument.indexOf("#UserID=") + 8, argument.indexOf("#StartTime=")); + } + String StartTimeStr (void) { + return argument.substring(argument.indexOf("#StartTime=") + 14, argument.indexOf("#EndTime=")); + } + String EndTimeStr (void) { + return argument.substring(argument.indexOf("#EndTime=") + 14, argument.indexOf("#EndTime=")); + } +}; +// +void ReservationCmd::extractValues (String stringPassed){ + argument = stringPassed; +} + +void DoorOpenFn() +{ + digitalWrite(OpenRelay,HIGH); + delay(400); + digitalWrite(OpenRelay,LOW); + DoorsOpenBool = true; +} + +void DoorCloseFn() +{ + digitalWrite(CloseRelay,HIGH); + delay(400); + digitalWrite(CloseRelay,LOW); + DoorsOpenBool = false; +} + +void StatusLEDFn() +{ + digitalWrite(StatusLED,HIGH); + delay(1000); + digitalWrite(StatusLED,LOW); +} + + +void setup() +{ + pinMode(OpenRelay, OUTPUT); + pinMode(CloseRelay, OUTPUT); + pinMode(StatusLED, OUTPUT); + + Particle.function("Open", OpenRelayCmd); + Particle.function("Close", CloseRelayCmd); + Particle.function("StatusLED", StatusLEDFlash); + Particle.function("PubPosn", SendPosition); + Particle.function("MkResvn", ReserveString); + Particle.function("CanResvn", CancelString); + Particle.function("HomeBox", HomeCoords); + Particle.function("PubResvn", ShowReservations); + + Particle.variable("CarResvn", ReservationsTable); + Particle.variable("CarPos", ValidGPSPosition); + + digitalWrite(OpenRelay, LOW); + digitalWrite(CloseRelay, LOW); + digitalWrite(StatusLED, LOW); + + Serial.begin(); + Serial.printlnf("ready for data"); + RECEIVER.begin(baud, PROTOCOL); // but SoftSerial can ;-) +} + +void loop() +{ + +int counter = 0; +unsigned long cardread = 0; +String cardpublish; + +while (RECEIVER.available()) + { + for (int cardident = 0; cardident < 5; cardident++) { + counter = (RECEIVER.read()); + if (cardident > 0) Serial.print(int(counter), HEX); + CARDcurrent[cardident-1] = counter; + Serial.print(" "); + } + Serial.println(""); + cardread=int(CARDcurrent[0]); + cardread=cardread << 8; + cardread=cardread+ int(CARDcurrent[1]); + cardread=cardread << 8; + cardread=cardread+ int(CARDcurrent[2]); + cardread=cardread << 8; + cardread=cardread+ int(CARDcurrent[3]); + Serial.println("card raw"); + Serial.println(cardread, HEX); + cardpublish = String(cardread, HEX); + Serial.println("card string"); + Serial.println(cardpublish); + bool success; + success = Particle.publish("RFIDident", String(cardread, HEX), 0, PUBLIC); + if (!success) { + Serial.println("failedtopublish"); // get here if event publish did not work + } + if (String(cardread, HEX)==MasterRFIDCard) { + Serial.println("match"); + StatusLEDFn(); + if (DoorsOpenBool) { + DoorCloseFn(); + Serial.println("closing"); + } + else + { + DoorOpenFn(); + Serial.println("Opening"); + } + } + + } + RECEIVER.flush(); + + if (Particle.connected()) { + Serial.println("Connected!"); + } + delay(1000); +} + + + +int OpenRelayCmd(String command) { + /* Particle.functions always take a string as an argument and return an integer. + Since we can pass a string, it means that we can give the program commands on how the function should be used. + In this case, telling the function "on" will turn the LED on and telling it "off" will turn the LED off. + Then, the function returns a value to us to let us know what happened. + In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off, + and -1 if we received a totally bogus command that didn't do anything to the LEDs. + */ + + if (command=="on") { + DoorOpenFn(); + return 1; + } + else { + return -1; + } +} + + + +int CloseRelayCmd(String command) { + /* Particle.functions always take a string as an argument and return an integer. + Since we can pass a string, it means that we can give the program commands on how the function should be used. + In this case, telling the function "on" will turn the LED on and telling it "off" will turn the LED off. + Then, the function returns a value to us to let us know what happened. + In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off, + and -1 if we received a totally bogus command that didn't do anything to the LEDs. + */ + + if (command=="on") { + DoorCloseFn(); + return 1; + } + else { + return -1; + } +} + +int StatusLEDFlash(String command) { + /* Particle.functions always take a string as an argument and return an integer. + Since we can pass a string, it means that we can give the program commands on how the function should be used. + In this case, telling the function "on" will turn the LED on and telling it "off" will turn the LED off. + Then, the function returns a value to us to let us know what happened. + In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off, + and -1 if we received a totally bogus command that didn't do anything to the LEDs. + */ + + if (command=="on") { + StatusLEDFn(); + return 1; + } + else { + return -1; + } + +} + +int SendPosition(String command){ + if (command=="on") { + digitalWrite(StatusLED,HIGH); + delay(1000); + digitalWrite(StatusLED,LOW); + return 1; + } + else { + return -1; + } +} + +int ReserveString(String command){ + if (command=="on") { + digitalWrite(StatusLED,HIGH); + delay(1000); + digitalWrite(StatusLED,LOW); + return 1; + } + else { + return -1; + } +} + +int CancelString(String command){ + if (command=="on") { + digitalWrite(StatusLED,HIGH); + delay(1000); + digitalWrite(StatusLED,LOW); + return 1; + } + else { + return -1; + } +} + +int HomeCoords(String command){ + if (command=="on") { + digitalWrite(StatusLED,HIGH); + delay(1000); + digitalWrite(StatusLED,LOW); + return 1; + } + else { + return -1; + } +} + +int ShowReservations(String command){ + if (command=="on") { + digitalWrite(StatusLED,HIGH); + delay(1000); + digitalWrite(StatusLED,LOW); + return 1; + } + else { + return -1; + } +}