Hello,
I know it might be a C/C++ standard question but I am working on a firmware that will help me monitor the assistance of personal in a remote location. I am using examples that I have found in the forum to implement my solution.
I am using a SparkFun RFID 125kHz card reader and a Photon. The RFID reader communicates with my Photon using the serial port and when its online its working really good. No problems at all. When its offline, I would like to read as much as 100 (As many as possible honestly) RFID cards and store this IDs within the Photon, every card ID is 12 characters long followed by ‘\r’’\n’. The problem I’m having is that whenever I am offline instead of sending the RFID tags that I am storing its sending “null”. I believe my photon is not really storing anything and I am not sure which datatype would work better to store this tags so later whenever it connects to WiFi again it can send them.
I also get a SOS and a hard fault. Not sure why.
/* Libraries */
#include "Particle.h"
/* Begining WiFi communication status variables */
#define disconnectDelay 15000 // After 3 seconds of no WiFi, disconnect
#define checkInterval 45000 //Every 30 seconds it attempts to connect to WiFi
unsigned int lastAttempt, lastDisconnect;
bool connected = true;
SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
/* End WiFi communication status variables */
/* Beginning Serial communication variables */
const unsigned long SEND_INTERVAL_MS = 2000;
const size_t READ_BUF_SIZE = 64;
void processBuffer();
int counter = 0;
unsigned long lastSend = 0;
char readBuf[READ_BUF_SIZE];
size_t readBufOffset = 0;
/* End Serial communication variables */
/* System variables */
int OutputLED1 = D7;
retained char offlineBuffer[20][16];
String auxString = "";
retained int offlineBufferPos = 0;
retained int whileOffline = 0;
retained int whileOnline = 0;
void setup() {
Serial1.begin(9600);
pinMode(OutputLED1, OUTPUT);
Particle.connect();
}
void checkConnection(){
if (Particle.connected() == false && millis() - lastDisconnect >= disconnectDelay){
//statusControl(1);
lastDisconnect = millis();
WiFi.off();
connected = false;
}
if (millis() - lastAttempt >= checkInterval){
//statusControl(3);
WiFi.on();
Particle.connect();
lastAttempt = millis();
}
if (millis() - lastAttempt >= disconnectDelay){
//statusControl(0);
Particle.connect();
connected = Particle.connected();
}
}
void statusControl(int rfidState){
/*
This will control the output LED and flashing varies depending on buffer status
0 - Connected
1 - Not connected
2 - Not connected and buffer at 25%
3 - Not connected and buffer at 50%
4 - Not connected and buffer at 75%
5 - Not connected and buffer at 100%
*/
switch (rfidState){
case 0:
digitalWrite(OutputLED1, LOW);
delay(50);
break;
case 1:
digitalWrite(OutputLED1, HIGH);
delay(50);
digitalWrite(OutputLED1, LOW);
delay(450);
digitalWrite(OutputLED1, HIGH);
delay(50);
digitalWrite(OutputLED1, LOW);
delay(450);
break;
case 2:
digitalWrite(OutputLED1, HIGH);
delay(450);
digitalWrite(OutputLED1, LOW);
delay(50);
digitalWrite(OutputLED1, HIGH);
delay(450);
digitalWrite(OutputLED1, LOW);
delay(50);
break;
default:
digitalWrite(OutputLED1, HIGH);
delay(250);
digitalWrite(OutputLED1, LOW);
delay(250);
break;
}
}
void loop() {
checkConnection();
if (connected == false){ // This code will run while the Photon is disconnected from Cloud and WiFi
//statusControl(1);
while(Serial1.available()) {
if (readBufOffset < READ_BUF_SIZE) {
char c = Serial1.read();
if(c != '\n'){
if((c != '') && (c != '')){
readBuf[readBufOffset++] = c;// Add character to buffer
offlineBuffer[offlineBufferPos][readBufOffset++] = c;
}
}
else
{
// End of line character found, process line
readBuf[readBufOffset] = '\n';
offlineBuffer[offlineBufferPos][readBufOffset] = '\n';
readBufOffset = 0;
offlineBufferPos++;
}
}
else
{
Particle.publish("readBuf overflow, emptying buffer");
readBufOffset = 0;
offlineBufferPos = 30405;;
}
}
}
else
{
//statusControl(0);
// Read data from serial
while(Serial1.available()) {
if (readBufOffset < READ_BUF_SIZE) {
char c = Serial1.read();
if(c != '\n'){
if((c != '') && (c != '')){
readBuf[readBufOffset++] = c;// Add character to buffer
}
}
else {
// End of line character found, process line
readBuf[readBufOffset] = '\n';
processBuffer();
readBufOffset = 0;
}
}
else {
Particle.publish("readBuf overflow, emptying buffer");
readBufOffset = 0;
}
if(offlineBufferPos > 0){
for(int i = 0; i < offlineBufferPos; i++){
Particle.publish("offlineRFID:", String(offlineBuffer[i]), PRIVATE);
statusControl(2);
}
offlineBufferPos = 0;
}
}
}
}
void processBuffer() {
Particle.publish("P_RFIDAssistance:", String(readBuf), PRIVATE);
Particle.publish("offlinePos:", String(offlineBufferPos));
}