Skip to content

Commit

Permalink
now using queue if Wi-Fi or target web unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
gojimmypi committed Jun 19, 2020
1 parent 85a87c2 commit 09a6361
Showing 1 changed file with 196 additions and 17 deletions.
213 changes: 196 additions & 17 deletions RFID_Logger.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ SOFTWARE.
// see: https://github.com/miguelbalboa/rfid
//
#include "GlobalDefine.h"

#include <vector>
#include <stdlib.h>
#include <SPI.h>
#include <MFRC522.h>
#ifdef ARDUINO_ARCH_ESP8266
Expand Down Expand Up @@ -75,6 +76,7 @@ byte nuidPICC[4];

void WebServerConnect() {
while (!client.connect(SECRET_APP_HOST, APP_HTTPS_PORT)) {
// TODO what if the web server is down? we shouldn't wait here forever...
Serial.println(F("client.connect failed; check firewall on receiver"));
Serial.print(F("IP address="));
Serial.println(WiFi.localIP());
Expand All @@ -97,14 +99,28 @@ bool WiFiConnected() {
return (WiFi.status() == WL_CONNECTED);
}

typedef struct ItemUID {
String UID = "";
// String MAC = "";
String IP = "";
String MSG = "";
bool sent = false;
};

int SaveUID(String thisUID, String thisMessage) {

if (thisUID) {
if (!WiFiConnected()) {
wifiConnect(50);
}
std::vector<ItemUID> QueueOfUID;
ulong SendAttempts = 0;

int SendQueue() {
// WiFiStart occurs in SaveUID()
if (QueueOfUID.empty()) {
Serial.println(F("QueueOfUID is empty!"));
return 0;
}

SendAttempts++;

if (WiFiConnected()) {
WebServerConnect();

if (!client.connected()) {
Expand All @@ -113,14 +129,88 @@ int SaveUID(String thisUID, String thisMessage) {
Serial.println(WiFi.localIP());
return 2;
}
Serial.println(F("Saving UID"));
String url = String(SECRET_APP_PATH) + F("?UID=") + thisUID + F("&MAC=") + wifiMacAddress() + F("&MSG=") + thisMessage; // reminder that IIS will return a 302 (moved) for default.aspx that points to default :/
String thisRequest = HTML_RequestText(url);
String thisMovedRequestURL = "";
HTML_SendRequestFollowMove(&client, thisRequest, thisMovedRequestURL);
client.flush();
delay(100);
client.stop();

Serial.println(F(""));
Serial.print("Items to check: ");
Serial.println(QueueOfUID.size());

bool HasUnsentItems = false;
int thisResult = -1;
for (uint i = 0; i < QueueOfUID.size(); i++)
{
if (QueueOfUID[i].sent) {
Serial.println(F("Item already sent"));
}
else {
Serial.println(F("Saving UID"));
String url = String(SECRET_APP_PATH)
+ F("?UID=") + QueueOfUID[i].UID
+ F("&MAC=") + wifiMacAddress()
+ F("&MSG=") + QueueOfUID[i].MSG; // reminder that IIS will return a 302 (moved) for default.aspx that points to default :/
String thisRequest = HTML_RequestText(url);
String thisMovedRequestURL = "";
thisResult = HTML_SendRequestFollowMove(&client, thisRequest, thisMovedRequestURL);
if (thisResult == 0) {
// TODO check if successful, for now, assume it was
// HasUnsentItems = true // if it fails we have an unsent item
Serial.println(F("Item successfully sent!"));
QueueOfUID[i].sent = true;
}
else {
Serial.println(F("Item NOT sent!"));
HasUnsentItems = true;
}


client.flush();
delay(100);
client.stop();
}
if (HasUnsentItems) {
// if we can't send one, we probably can't send any others, so give up
Serial.println(F("Giving up..."));
break;
}
}

if (HasUnsentItems) {
Serial.println(F("There are still unsent items!"));
}
else {
Serial.println(F("Clearing unsent item list..."));
QueueOfUID.clear();
Serial.println(F("Cleared unsent item list!"));
SendAttempts = 0;
}
}
else {
Serial.print(F("Exiting, WiFi not connected; item not saved; "));
Serial.print(String(QueueOfUID.size()));
Serial.println(F(" Items queued"));
return 3;
}

}

int SaveUID(String thisUID, String thisMessage) {
if (thisUID) {
ItemUID newItem;
newItem.UID = thisUID;
// newItem.MAC = wifiMacAddress();
newItem.MSG = thisMessage;
newItem.sent = false;
QueueOfUID.push_back(newItem);

Serial.println(F("Queue Size: "));
Serial.print(QueueOfUID.size());

// we'll only try to turn on WiFi when we know we have an item to send
if (!WiFiConnected()) {
WiFiStart(IS_EAP);
}

// it's unlikely that we'll be able to connect yet, but let's try:
SendQueue();
return 0;
}
else {
Expand Down Expand Up @@ -225,8 +315,82 @@ bool IsCardReady() {
return true;
}

void loop() {
// these counters are only general approximations, not accurate time.
int minutes_uptime = 0;
int hours_uptime = 0;
int days_uptime = 0;
int this_ms_counter = 0;
int save_result = 0;

// wait [ms_count] milliseconds; attempt to send data every minute
void wait_ms(int ms_count) {
this_ms_counter++;
unsigned long CodeStart = millis();
bool SentQueue = false; // we'll keep track of whether we attempted a slow operation, like talking to the web server

if (this_ms_counter >= (60000 / ms_count) ) {
minutes_uptime++;
this_ms_counter = 0;
Serial.println("Uptime: " + String(days_uptime) + " days; "
+ String(hours_uptime) + " hours; "
+ String(minutes_uptime) + " minutes. "
+ "Queue Size: " + String(QueueOfUID.size()));

// code that runs every minute

// once per minute, we'll need to try to connect again if the queue has items to be sent
if (QueueOfUID.empty()) {

}
else {
// TODO for low power operations, we probably don't want to try this every minute
Serial.print(F("Send attempt: "));
Serial.println(SendAttempts);
SendQueue();
SentQueue = true;
}
}

// keep track of hours
if (minutes_uptime >= 60) {
hours_uptime++;
minutes_uptime = 0;

// any hourly code would go here
}

// keep track of days
if (hours_uptime >= 24) {
days_uptime++;
hours_uptime = 0;

// any daily code would go here
}

// CodeDuration is typically 0, as the above will take less than a millisecond, unless data sent to web server
unsigned long CodeDuration = millis() - CodeStart;

if (SentQueue) {
Serial.print(F("Code Duration: "));
Serial.println(CodeDuration);
}
if ((CodeDuration >= 0) && (CodeDuration < ms_count)) {
int thisDelay = ms_count - CodeDuration;
if (SentQueue || thisDelay > -1) {
TIMER_DEBUG_PRINT(F("waiting: "));
TIMER_DEBUG_PRINTLN(thisDelay);
}
delay(thisDelay);
}
else {
// no delay, we already spend more time in code than the delay!
Serial.print(F("No delay! Duration="));
Serial.println(CodeDuration);
this_ms_counter = this_ms_counter + CodeDuration;
}
}

void loop() {
// check to see if we have a card
if (IsCardReady()) {

Expand All @@ -238,7 +402,13 @@ void loop() {
String detectedUID = UID_Hex(rfid.uid.uidByte, rfid.uid.size);
Serial.println(detectedUID);

SaveUID(detectedUID, F("Detected"));
save_result = SaveUID(detectedUID, F("Detected"));
if (save_result == 0) {
Serial.println(F("Data queued. Awaiting next card..."));
}
else {
Serial.println(F("Error! Data NOT queued. Awaiting next card..."));
}

// Halt PICC
rfid.PICC_HaltA();
Expand All @@ -247,8 +417,17 @@ void loop() {
rfid.PCD_StopCrypto1();
}
else {
delay(100);
wait_ms(100); // data sent while waiting, too
}

// check to see of we have any items still queued to send
if (QueueOfUID.empty()) {
if (WiFiConnected()) {
Serial.println(F("WiFi.disconnect"));
WiFi.disconnect();
}
}
else {
}
}

0 comments on commit 09a6361

Please sign in to comment.