Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1.1 pull branch #1

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MQTT Logging of events & access
-Added MQTT logging for events
-Added MQTT logging for access
-Added GUI Switch under MQTT to enable/disable it
-Activate MQTT logging disables wirte to ESP8266 log file
  • Loading branch information
marelab committed Jun 10, 2019
commit d51ac43d0360ec50dcf1d2281af38a070f0bce04
7 changes: 7 additions & 0 deletions src/config.esp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ bool ICACHE_FLASH_ATTR loadConfiguration()
mqttTopic = strdup(mqttTopicString.c_str());

interval = mqtt["syncrate"];

if (mqtt["mqttlog"]==1)
mqttEvents = true;
else
mqttEvents = false;



mqttClient.setServer(mhs, mport);
mqttClient.setCredentials(muser, mpas);
Expand Down
32 changes: 24 additions & 8 deletions src/log.esp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
void extern mqtt_publish_event(JsonObject *root);

void ICACHE_FLASH_ATTR writeEvent(String type, String src, String desc, String data) {
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
Expand All @@ -6,10 +8,17 @@ void ICACHE_FLASH_ATTR writeEvent(String type, String src, String desc, String d
root["desc"] = desc;
root["data"] = data;
root["time"] = now();
File eventlog = SPIFFS.open("/eventlog.json", "a");
root.printTo(eventlog);
eventlog.print("\n");
eventlog.close();
if ((mqttEvents) && (mqttenabled==1)){ // log to MQTT
root["cmd"] = "event";
mqtt_publish_event(&root);
}
else // log to file
{
File eventlog = SPIFFS.open("/eventlog.json", "a");
root.printTo(eventlog);
eventlog.print("\n");
eventlog.close();
}
}

void ICACHE_FLASH_ATTR writeLatest(String uid, String username, int acctype) {
Expand All @@ -19,10 +28,17 @@ void ICACHE_FLASH_ATTR writeLatest(String uid, String username, int acctype) {
root["username"] = username;
root["acctype"] = acctype;
root["timestamp"] = now();
File latestlog = SPIFFS.open("/latestlog.json", "a");
root.printTo(latestlog);
latestlog.print("\n");
latestlog.close();
if ((mqttEvents) && (mqttenabled==1)){ // log to MQTT
//root["cmd"] = "access";
//mqtt_publish_event(&root);
}
else // log to file
{
File latestlog = SPIFFS.open("/latestlog.json", "a");
root.printTo(latestlog);
latestlog.print("\n");
latestlog.close();
}
}

void ICACHE_FLASH_ATTR sendEventLog(int page) {
Expand Down
5 changes: 4 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define VERSION "1.1.0"
#define VERSION "1.2.0"

#include "Arduino.h"
#include <ESP8266WiFi.h>
Expand Down Expand Up @@ -143,6 +143,9 @@ unsigned long nextbeat = 0;
// Add to html mqtt to control the sync
unsigned long interval = 1800; // 30 min

// log events to mqtt
bool mqttEvents = true;


#include "log.esp"
#include "mqtt.esp"
Expand Down
13 changes: 13 additions & 0 deletions src/mqtt.esp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ void mqtt_publish_access(time_t accesstime, String const &isknown, String const

DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
if (mqttEvents){ // log to MQTT
root["cmd"] = "log";
}
root["type"] = "access";
root["time"] = accesstime;
root["isKnown"] = isknown;
Expand All @@ -110,6 +113,16 @@ void mqtt_publish_access(time_t accesstime, String const &isknown, String const
}
}

void mqtt_publish_event(JsonObject *root) {
if (mqttClient.connected()) {
String stopic (mqttTopic);
stopic = stopic + "/send";
String mqttBuffer;
root->printTo(mqttBuffer);
mqttClient.publish(stopic.c_str(), 0, false, mqttBuffer.c_str());
}
}

void onMqttPublish(uint16_t packetId) {
writeEvent("INFO", "mqtt", "MQTT publish acknowledged", String(packetId));
}
Expand Down
16 changes: 8 additions & 8 deletions src/webh/esprfid.htm.gz.h

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions src/webh/esprfid.js.gz.h

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/webh/index.html.gz.h

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/websrc/esprfid.htm
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,18 @@ <h5>Please wait while fetching data...<span id="loadpages"></span></h5>
<span class="col-xs-9">
<input class="form-control input-sm" placeholder="Sync rate" value="" style="display:inline;max-width:185px" id="syncrate" type="text">
</span>
<br>
<div class="row form-group">
<label class="col-xs-3">MQTT logging<i style="margin-left: 10px;" class="glyphicon glyphicon-info-sign" aria-hidden="true" data-toggle="popover" data-trigger="hover" data-placement="right" data-content="Enable MQTT logging disables logging on device"></i></label>
<div class="col-xs-9">
<form>
<label class="radio-inline">
<input type="radio" value="1" name="mqttlog">Enabled</label>
<label class="radio-inline">
<input type="radio" value="0" name="mqttlog" checked>Disabled</label>
</form>
</div>
</div>
<br>
<br>
<div class="col-xs-9 col-md-8">
Expand Down
9 changes: 3 additions & 6 deletions src/websrc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>esp-rfid</title>
<title>esp-door</title>
<!-- Bootstrap core CSS -->
<link href="css/required.css" rel="stylesheet">
</head>
Expand All @@ -22,7 +22,7 @@
</div>
<div class="sidebar-header">
<br>
<h1><i class="glyphicon glyphicon-tags" aria-hidden="true"></i>esp-rfid</h1>
<h1><i class="glyphicon glyphicon-tags" aria-hidden="true"></i>esp-door</h1>
</div>
<ul class="list-unstyled components">
<li class="active">
Expand Down Expand Up @@ -75,10 +75,7 @@ <h1><i class="glyphicon glyphicon-tags" aria-hidden="true"></i>esp-rfid</h1>
</ul>
<ul class="list-unstyled CTAs">
<li>
<a href="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/esprfid/esp-rfid" class="download">Project's GitHub Page</a>
</li>
<li>
<a href="https://salt.bountysource.com/checkout/amount?team=esp-rfid" class="download" aria-hidden="true" data-toggle="popover" data-trigger="hover" data-placement="top" data-content="Nothing says better thank you than a donation."><i class="glyphicon glyphicon-euro"></i>Donate</a>
<a href="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/marelab/esp-rfid" class="download">Project's GitHub Page</a>
</li>
<li>
<a href="#" class="article" onclick="logout();">Logout</a>
Expand Down
7 changes: 6 additions & 1 deletion src/websrc/js/esprfid.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ var config = {
"topic": "",
"user": "",
"pswd": "",
"syncrate": 1800
"syncrate": 180,
"mqttlog": 0
},
"ntp": {
"server": "pool.ntp.org",
Expand Down Expand Up @@ -231,6 +232,10 @@ function savemqtt() {
config.mqtt.user = document.getElementById("mqttuser").value;
config.mqtt.pswd = document.getElementById("mqttpwd").value;
config.mqtt.syncrate = document.getElementById("syncrate").value;
config.mqtt.mqttlog = 0;
if (parseInt($("input[name=\"mqttlog\"]:checked").val()) === 1) {
config.mqtt.mqttlog = 1;
}
uncommited();
}

Expand Down