FUNCTIONAL_CAR_DEMO.INO-3aa.../FUNCTIONAL_CAR_DEMO.INO
2018-03-10 17:33:14 +01:00

180 lines
No EOL
6.1 KiB
C++
Raw Permalink Blame History

// This #include statement was automatically added by the Particle IDE.
#include <ParticleSoftSerial.h>
/*****************************************************************************
* ParticleSoftSerial library (PSS_SimpleTest.ino)
* Copyright (c) 2016 Free Software Foundation. All right reserved.
* Written by Andreas Rothenw<6E>nder (aka ScruffR)
*
* This sample shows sends data from Serial1 to ParticleSoftSerial(D2/D3)
*
* Prerequisites:
* import SparkIntervalTimer library (by Paul Kourany)
* wire Serial1 TX to D2
* Serial1 RX to D3 (for sending ParticleSoftSerial to Serial1)
*
* Due to relatively low interrupt priorities used, baudrates greater 31250
* may be prone to data corruption, depending on over all system load.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
#include <ParticleSoftSerial.h>
#include "Particle.h"
#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
void setup()
{
pinMode(OpenRelay, OUTPUT);
pinMode(CloseRelay, OUTPUT);
pinMode(StatusLED, OUTPUT);
Particle.function("Open",OpenRelayCmd);
Particle.function("Close",CloseRelayCmd);
Particle.function("StatusLED",StatusLEDFlash);
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(cardread, HEX);
bool success;
success = Particle.publish("RFIDident", String(cardread, HEX), 0, PUBLIC);
if (!success) {
Serial.println("failedtopublish"); // get here if event publish did not work
}
}
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") {
digitalWrite(OpenRelay,HIGH);
delay(400);
digitalWrite(OpenRelay,LOW);
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") {
digitalWrite(CloseRelay,HIGH);
delay(400);
digitalWrite(CloseRelay,LOW);
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") {
digitalWrite(StatusLED,HIGH);
delay(1000);
digitalWrite(StatusLED,LOW);
return 1;
}
else {
return -1;
}
}